Skip to content

Commit

Permalink
attr: mark #[spirv(...)] attributes as used and remove `#[allow(unu…
Browse files Browse the repository at this point in the history
…sed_attributes)]`.
  • Loading branch information
eddyb committed Mar 2, 2021
1 parent 71254b4 commit 6571f45
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 75 deletions.
14 changes: 9 additions & 5 deletions crates/rustc_codegen_spirv/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,13 @@ impl CheckSpirvAttrVisitor<'_> {
let parse_attrs = |attrs| crate::symbols::parse_attrs_for_checking(&self.sym, attrs);

let target = target.into();
for parse_attr_result in parse_attrs(attrs) {
let (span, attr) = match parse_attr_result {
Ok(span_and_attr) => span_and_attr,
for (attr, parse_attr_result) in parse_attrs(attrs) {
// Make sure to mark the whole `#[spirv(...)]` attribute as used,
// to avoid warnings about unused attributes.
self.tcx.sess.mark_attr_used(attr);

let (span, parsed_attr) = match parse_attr_result {
Ok(span_and_parsed_attr) => span_and_parsed_attr,
Err((span, msg)) => {
self.tcx.sess.span_err(span, &msg);
continue;
Expand All @@ -100,7 +104,7 @@ impl CheckSpirvAttrVisitor<'_> {
/// Error newtype marker used below for readability.
struct Expected<T>(T);

let valid_target = match attr {
let valid_target = match parsed_attr {
SpirvAttribute::Builtin(_)
| SpirvAttribute::DescriptorSet(_)
| SpirvAttribute::Binding(_)
Expand All @@ -109,7 +113,7 @@ impl CheckSpirvAttrVisitor<'_> {
let parent_hir_id = self.tcx.hir().get_parent_node(hir_id);
let parent_is_entry_point =
parse_attrs(self.tcx.hir().attrs(parent_hir_id))
.filter_map(|r| r.ok())
.filter_map(|(_, r)| r.ok())
.any(|(_, attr)| matches!(attr, SpirvAttribute::Entry(_)));
if !parent_is_entry_point {
self.tcx.sess.span_err(
Expand Down
10 changes: 8 additions & 2 deletions crates/rustc_codegen_spirv/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ pub fn parse_attrs<'a, 'tcx>(
attrs: &'tcx [Attribute],
) -> impl Iterator<Item = SpirvAttribute> + Captures<'tcx> + 'a {
parse_attrs_for_checking(&cx.sym, attrs)
.filter_map(move |parse_attr_result| {
.filter_map(move |(_, parse_attr_result)| {
// NOTE(eddyb) `delay_span_bug` ensures that if attribute checking fails
// to see an attribute error, it will cause an ICE instead.
parse_attr_result
Expand All @@ -497,7 +497,12 @@ type ParseAttrError = (Span, String);
pub(crate) fn parse_attrs_for_checking<'a>(
sym: &'a Symbols,
attrs: &'a [Attribute],
) -> impl Iterator<Item = Result<(Span, SpirvAttribute), ParseAttrError>> + 'a {
) -> impl Iterator<
Item = (
&'a Attribute,
Result<(Span, SpirvAttribute), ParseAttrError>,
),
> + 'a {
attrs.iter().flat_map(move |attr| {
let is_spirv = match attr.kind {
AttrKind::Normal(ref item, _) => {
Expand Down Expand Up @@ -558,6 +563,7 @@ pub(crate) fn parse_attrs_for_checking<'a>(
};
Ok((span, parsed_attr))
}))
.map(move |parse_attr_result| (attr, parse_attr_result))
})
}

Expand Down
30 changes: 0 additions & 30 deletions crates/spirv-builder/src/test/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ impl<'a> Drop for SetEnvVar<'a> {
#[test]
fn hello_world() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
}
Expand All @@ -35,7 +34,6 @@ pub fn main() {
fn no_dce() {
let _var = SetEnvVar::new(&"NO_DCE", "1");
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn no_dce() {
}
Expand All @@ -49,7 +47,6 @@ fn add_two_ints() {
fn add_two_ints(x: u32, y: u32) -> u32 {
x + y
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
add_two_ints(2, 3);
Expand Down Expand Up @@ -80,7 +77,6 @@ fn asm() {
);
}
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
asm();
Expand Down Expand Up @@ -112,7 +108,6 @@ fn add_two_ints(x: u32, y: u32) -> u32 {
}
result
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
add_two_ints(2, 3);
Expand Down Expand Up @@ -162,7 +157,6 @@ fn asm_op_decorate() {
);
}
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
add_decorate();
Expand Down Expand Up @@ -208,7 +202,6 @@ fn asm() {
);
}
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
asm();
Expand All @@ -222,7 +215,6 @@ fn logical_and() {
fn f(x: bool, y: bool) -> bool {
x && y
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
f(false, true);
Expand All @@ -232,7 +224,6 @@ pub fn main() {
#[test]
fn panic() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
panic!("aaa");
Expand All @@ -247,7 +238,6 @@ fn int_div(x: usize) -> usize {
1 / x
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
int_div(0);
Expand All @@ -262,7 +252,6 @@ fn array_bounds_check(x: [u32; 4], i: usize) -> u32 {
x[i]
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
array_bounds_check([0, 1, 2, 3], 5);
Expand All @@ -282,7 +271,6 @@ pub struct ShaderConstants {
pub time: f32,
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main(constants: PushConstant<ShaderConstants>) {
let _constants = *constants;
Expand All @@ -298,15 +286,13 @@ fn push_constant_vulkan() {
val_vulkan(
r#"
#[derive(Copy, Clone)]
#[allow(unused_attributes)]
#[spirv(block)]
pub struct ShaderConstants {
pub width: u32,
pub height: u32,
pub time: f32,
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main(constants: PushConstant<ShaderConstants>) {
let _constants = *constants;
Expand All @@ -318,7 +304,6 @@ pub fn main(constants: PushConstant<ShaderConstants>) {
#[test]
fn infinite_loop() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
loop {}
Expand All @@ -330,7 +315,6 @@ fn unroll_loops() {
dis_fn(
// FIXME(eddyb) use `for _ in 0..10` here when that works.
r#"
#[allow(unused_attributes)]
#[spirv(unroll_loops)]
fn java_hash_ten_times(mut x: u32, y: u32) -> u32 {
let mut i = 0;
Expand All @@ -340,7 +324,6 @@ fn java_hash_ten_times(mut x: u32, y: u32) -> u32 {
}
x
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
java_hash_ten_times(7, 42);
Expand Down Expand Up @@ -390,7 +373,6 @@ OpFunctionEnd"#,
#[test]
fn signum() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main(i: Input<f32>, mut o: Output<f32>) {
*o = i.signum();
Expand All @@ -406,7 +388,6 @@ fn allocate_const_scalar_pointer() {
use core::ptr::Unique;
const POINTER: Unique<[u8;4]> = Unique::<[u8; 4]>::dangling();
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let _pointer = POINTER;
Expand All @@ -422,7 +403,6 @@ const VEC_LIKE: (Unique<usize>, usize, usize) = (Unique::<usize>::dangling(), 0,
pub fn assign_vec_like() {
let _vec_like = VEC_LIKE;
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {}"#);
}
Expand All @@ -433,7 +413,6 @@ fn allocate_null_pointer() {
use core::ptr::null;
const NULL_PTR: *const i32 = null();
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let _null_ptr = NULL_PTR;
Expand All @@ -452,15 +431,13 @@ pub fn create_uninit_and_write() {
let _maybei32 = unsafe { maybei32.assume_init() };
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {}"#);
}

#[test]
fn vector_extract_dynamic() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let vector = glam::Vec2::new(1.0, 2.0);
Expand All @@ -473,7 +450,6 @@ pub fn main() {
#[test]
fn vector_insert_dynamic() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let vector = glam::Vec2::new(1.0, 2.0);
Expand All @@ -487,7 +463,6 @@ pub fn main() {
#[test]
fn mat3_vec3_multiply() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main(input: Input<glam::Mat3>, mut output: Output<glam::Vec3>) {
let input = *input;
Expand Down Expand Up @@ -548,7 +523,6 @@ fn complex_image_sample_inst() {
result
}
}
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
sample_proj_lod(glam::Vec4::zero(), glam::Vec2::zero(), glam::Vec2::zero(), 0, 0);
Expand All @@ -573,7 +547,6 @@ OpFunctionEnd",
fn any() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let vector = glam::BVec2::new(true, false);
Expand All @@ -585,7 +558,6 @@ pub fn main() {
#[test]
fn all() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let vector = glam::BVec2::new(true, true);
Expand All @@ -597,7 +569,6 @@ pub fn main() {
#[test]
fn image_read() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main(image: UniformConstant<StorageImage2d>, mut output: Output<glam::Vec2>) {
let coords = image.read(glam::IVec2::new(0, 1));
Expand All @@ -609,7 +580,6 @@ pub fn main(image: UniformConstant<StorageImage2d>, mut output: Output<glam::Vec
#[test]
fn image_write() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main(input: Input<glam::Vec2>, image: UniformConstant<StorageImage2d>) {
let texels = *input;
Expand Down
Loading

0 comments on commit 6571f45

Please sign in to comment.