Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #[spirv(invariant)] #541

Merged
merged 4 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ jobs:
run: cargo fetch --locked
- name: Rustfmt
run: cargo fmt --all -- --check
- name: Rustfmt tests
run: rustfmt --check tests/ui/**/*.rs
- name: Clippy
run: .github/workflows/clippy.sh

Expand Down
6 changes: 5 additions & 1 deletion crates/rustc_codegen_spirv/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub enum SpirvAttribute {
DescriptorSet(u32),
Binding(u32),
Flat,
Invariant,

// `fn`/closure attributes:
UnrollLoops,
Expand Down Expand Up @@ -122,6 +123,7 @@ pub struct AggregatedSpirvAttributes {
pub descriptor_set: Option<Spanned<u32>>,
pub binding: Option<Spanned<u32>>,
pub flat: Option<Spanned<()>>,
pub invariant: Option<Spanned<()>>,

// `fn`/closure attributes:
pub unroll_loops: Option<Spanned<()>>,
Expand Down Expand Up @@ -204,6 +206,7 @@ impl AggregatedSpirvAttributes {
),
Binding(value) => try_insert(&mut self.binding, value, span, "#[spirv(binding)]"),
Flat => try_insert(&mut self.flat, (), span, "#[spirv(flat)]"),
Invariant => try_insert(&mut self.invariant, (), span, "#[spirv(invariant)]"),
UnrollLoops => try_insert(&mut self.unroll_loops, (), span, "#[spirv(unroll_loops)]"),
}
}
Expand Down Expand Up @@ -285,7 +288,8 @@ impl CheckSpirvAttrVisitor<'_> {
| SpirvAttribute::Builtin(_)
| SpirvAttribute::DescriptorSet(_)
| SpirvAttribute::Binding(_)
| SpirvAttribute::Flat => match target {
| SpirvAttribute::Flat
| SpirvAttribute::Invariant => match target {
Target::Param => {
let parent_hir_id = self.tcx.hir().get_parent_node(hir_id);
let parent_is_entry_point =
Expand Down
10 changes: 10 additions & 0 deletions crates/rustc_codegen_spirv/src/codegen_cx/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ impl<'tcx> CodegenCx<'tcx> {
self.emit_global()
.decorate(variable, Decoration::Flat, std::iter::empty());
}
if let Some(invariant) = attrs.invariant {
self.emit_global()
.decorate(variable, Decoration::Invariant, std::iter::empty());
if storage_class != StorageClass::Output {
self.tcx.sess.span_err(
invariant.span,
"#[spirv(invariant)] is only valid on Output variables",
);
}
}

// FIXME(eddyb) check whether the storage class is compatible with the
// specific shader stage of this entry-point, and any decorations
Expand Down
1 change: 1 addition & 0 deletions crates/rustc_codegen_spirv/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ impl Symbols {
),
("block", SpirvAttribute::Block),
("flat", SpirvAttribute::Flat),
("invariant", SpirvAttribute::Invariant),
(
"sampled_image",
SpirvAttribute::IntrinsicType(IntrinsicType::SampledImage),
Expand Down
13 changes: 12 additions & 1 deletion docs/src/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,16 @@ Example:

```rust
#[spirv(fragment)]
fn main(#[spirv(flat)] obj: &u32) { }
fn main(#[spirv(flat)] obj: u32) { }
```

## Invariant

The invariant attribute corresponds to the invariant keyword in glsl. It can only be applied to output variables.

Example:

```rust
#[spirv(vertex)]
fn main(#[spirv(invariant)] var: &mut f32) { }
```
3 changes: 1 addition & 2 deletions tests/ui/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
use spirv_std as _;

#[spirv(fragment)]
pub fn main() {
}
pub fn main() {}
3 changes: 1 addition & 2 deletions tests/ui/lang/control_flow/for_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ use spirv_std as _;

#[spirv(fragment)]
pub fn main(i: i32) {
for _ in 0..i {
}
for _ in 0..i {}
}
6 changes: 2 additions & 4 deletions tests/ui/lang/control_flow/for_with_custom_range_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

// build-pass

use spirv_std::num_traits::Num;
use core::ops::Range;
use spirv_std::num_traits::Num;

struct RangeIter<T>(Range<T>);

Expand All @@ -26,7 +26,5 @@ impl<T: Num + Ord + Copy> Iterator for RangeIter<T> {

#[spirv(fragment)]
pub fn main(i: i32) {
for _ in RangeIter(0..i) {
}
for _ in RangeIter(0..i) {}
}

4 changes: 1 addition & 3 deletions tests/ui/lang/control_flow/if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ use spirv_std as _;

#[spirv(fragment)]
pub fn main(i: i32) {
if i > 0 {

}
if i > 0 {}
}
2 changes: 0 additions & 2 deletions tests/ui/lang/control_flow/if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use spirv_std as _;
#[spirv(fragment)]
pub fn main(i: i32) {
if i > 0 {

} else {

}
}
3 changes: 0 additions & 3 deletions tests/ui/lang/control_flow/if_else_if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use spirv_std as _;
#[spirv(fragment)]
pub fn main(i: i32) {
if i > 0 {

} else if i < 0 {

} else {

}
}
4 changes: 1 addition & 3 deletions tests/ui/lang/control_flow/if_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use spirv_std as _;
#[spirv(fragment)]
pub fn main(i: i32) {
if i > 0 {
if i < 10 {

}
if i < 10 {}
}
}
3 changes: 1 addition & 2 deletions tests/ui/lang/control_flow/if_while.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use spirv_std as _;
#[spirv(fragment)]
pub fn main(i: i32) {
if i == 0 {
while i < 10 {
}
while i < 10 {}
}
}
8 changes: 2 additions & 6 deletions tests/ui/lang/control_flow/ifx2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ use spirv_std as _;

#[spirv(fragment)]
pub fn main(i: i32) {
if i > 0 {

}
if i > 1 {

}
if i > 0 {}
if i > 1 {}
}
3 changes: 1 addition & 2 deletions tests/ui/lang/control_flow/while.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ use spirv_std as _;

#[spirv(fragment)]
pub fn main(i: i32) {
while i < 10 {
}
while i < 10 {}
}
3 changes: 1 addition & 2 deletions tests/ui/lang/control_flow/while_while.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use spirv_std as _;
#[spirv(fragment)]
pub fn main(i: i32) {
while i < 20 {
while i < 10 {
}
while i < 10 {}
}
}
4 changes: 3 additions & 1 deletion tests/ui/lang/core/mem/create_unitialized_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const MAYBEI32: MaybeUninit<&i32> = MaybeUninit::<&i32>::uninit();

pub fn create_uninit_and_write() {
let mut maybei32 = MAYBEI32;
unsafe { maybei32.as_mut_ptr().write(&0); }
unsafe {
maybei32.as_mut_ptr().write(&0);
}
let _maybei32 = unsafe { maybei32.assume_init() };
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lang/core/ptr/allocate_const_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use spirv_std as _;

use core::ptr::Unique;
const POINTER: Unique<[u8;4]> = Unique::<[u8; 4]>::dangling();
const POINTER: Unique<[u8; 4]> = Unique::<[u8; 4]>::dangling();

#[spirv(fragment)]
pub fn main() {
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/spirv-attr/invalid-storage-class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ fn _entry(
#[spirv(private)] _: (),
#[spirv(function)] _: (),
#[spirv(generic)] _: (),
) {}
) {
}
Loading