Skip to content

Commit beea833

Browse files
committed
tool: versionize rust_gpu::spirv to rust_gpu::spirv_v0_9
1 parent f4adaf6 commit beea833

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

crates/rustc_codegen_spirv/src/attr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ pub(crate) fn provide(providers: &mut Providers) {
518518
// FIXME(eddyb) find something nicer for the error type.
519519
type ParseAttrError = (Span, String);
520520

521+
#[allow(clippy::get_first)]
521522
fn parse_attrs_for_checking<'a>(
522523
sym: &'a Symbols,
523524
attrs: &'a [Attribute],
@@ -530,12 +531,10 @@ fn parse_attrs_for_checking<'a>(
530531
Attribute::Unparsed(item) => {
531532
// #[...]
532533
let s = &item.path.segments;
533-
if let Some(rust_gpu) = s.get(0)
534-
&& rust_gpu.name == sym.rust_gpu
535-
{
534+
if let Some(rust_gpu) = s.get(0) && rust_gpu.name == sym.rust_gpu {
536535
// #[rust_gpu ...]
537536
match s.get(1) {
538-
Some(command) if command.name == sym.spirv => {
537+
Some(command) if command.name == sym.spirv_attr_with_version => {
539538
// #[rust_gpu::spirv ...]
540539
if let Some(args) = attr.meta_item_list() {
541540
// #[rust_gpu::spirv(...)]
@@ -551,10 +550,11 @@ fn parse_attrs_for_checking<'a>(
551550
}
552551
_ => {
553552
// #[rust_gpu::...] but not a know version
553+
let spirv = sym.spirv_attr_with_version.as_str();
554554
Err((
555555
attr.span(),
556-
"unknown `rust_gpu` attribute, expected `rust_gpu::spirv`"
557-
.to_string(),
556+
format!("unknown `rust_gpu` attribute, expected `rust_gpu::{spirv}`. \
557+
Do the versions of `spirv-std` and `rustc_codegen_spirv` match?"),
558558
))
559559
}
560560
}

crates/rustc_codegen_spirv/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ mod custom_decorations;
133133
mod custom_insts;
134134
mod link;
135135
mod linker;
136+
#[path = "../../spirv_attr_version.rs"]
137+
mod spirv_attr_version;
136138
mod spirv_type;
137139
mod spirv_type_constraints;
138140
mod symbols;

crates/rustc_codegen_spirv/src/symbols.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::attr::{IntrinsicType, SpirvAttribute};
22
use crate::builder::libm_intrinsics;
3+
use crate::spirv_attr_version::spirv_attr_with_version;
34
use rspirv::spirv::{BuiltIn, ExecutionMode, ExecutionModel, StorageClass};
45
use rustc_data_structures::fx::FxHashMap;
56
use rustc_span::symbol::Symbol;
@@ -13,7 +14,7 @@ use std::rc::Rc;
1314
pub struct Symbols {
1415
pub discriminant: Symbol,
1516
pub rust_gpu: Symbol,
16-
pub spirv: Symbol,
17+
pub spirv_attr_with_version: Symbol,
1718
pub libm: Symbol,
1819
pub entry_point_name: Symbol,
1920
pub spv_khr_vulkan_memory_model: Symbol,
@@ -404,7 +405,7 @@ impl Symbols {
404405
Self {
405406
discriminant: Symbol::intern("discriminant"),
406407
rust_gpu: Symbol::intern("rust_gpu"),
407-
spirv: Symbol::intern("spirv"),
408+
spirv_attr_with_version: Symbol::intern(&spirv_attr_with_version()),
408409
libm: Symbol::intern("libm"),
409410
entry_point_name: Symbol::intern("entry_point_name"),
410411
spv_khr_vulkan_memory_model: Symbol::intern("SPV_KHR_vulkan_memory_model"),

crates/spirv-std/macros/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,16 @@
7272
#![doc = include_str!("../README.md")]
7373

7474
mod image;
75+
#[path = "../../../spirv_attr_version.rs"]
76+
mod spirv_attr_version;
7577

7678
use proc_macro::TokenStream;
7779
use proc_macro2::{Delimiter, Group, Span, TokenTree};
7880

7981
use syn::{ImplItemFn, visit_mut::VisitMut};
8082

81-
use quote::{ToTokens, TokenStreamExt, quote};
83+
use crate::spirv_attr_version::spirv_attr_with_version;
84+
use quote::{ToTokens, TokenStreamExt, format_ident, quote};
8285
use std::fmt::Write;
8386

8487
/// A macro for creating SPIR-V `OpTypeImage` types. Always produces a
@@ -144,11 +147,12 @@ pub fn Image(item: TokenStream) -> TokenStream {
144147
/// `#[cfg_attr(target_arch="spirv", rust_gpu::spirv(..))]`.
145148
#[proc_macro_attribute]
146149
pub fn spirv(attr: TokenStream, item: TokenStream) -> TokenStream {
150+
let spirv = format_ident!("{}", &spirv_attr_with_version());
147151
let mut tokens: Vec<TokenTree> = Vec::new();
148152

149153
// prepend with #[rust_gpu::spirv(..)]
150154
let attr: proc_macro2::TokenStream = attr.into();
151-
tokens.extend(quote! { #[cfg_attr(target_arch="spirv", rust_gpu::spirv(#attr))] });
155+
tokens.extend(quote! { #[cfg_attr(target_arch="spirv", rust_gpu::#spirv(#attr))] });
152156

153157
let item: proc_macro2::TokenStream = item.into();
154158
for tt in item {
@@ -167,9 +171,13 @@ pub fn spirv(attr: TokenStream, item: TokenStream) -> TokenStream {
167171
{
168172
// group matches [spirv ...]
169173
// group stream doesn't include the brackets
170-
let inner = group.stream();
174+
let inner = group
175+
.stream()
176+
.into_iter()
177+
.skip(1)
178+
.collect::<proc_macro2::TokenStream>();
171179
group_tokens.extend(
172-
quote! { [cfg_attr(target_arch="spirv", rust_gpu::#inner)] },
180+
quote! { [cfg_attr(target_arch="spirv", rust_gpu::#spirv #inner)] },
173181
);
174182
}
175183
_ => group_tokens.append(tt),

crates/spirv_attr_version.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! This is placed outside any crate, and included by both `spirv_std` and `rustc_codegen_spirv`.
2+
//! I could have made a new crate, shared between the two, but decided against having even more small crates for sharing
3+
//! types. Instead, you get this single small file to specify the versioned spirv attribute.
4+
//!
5+
//! This also ensures that the macros below take the *exact* version of the two crates above, and not some dependency
6+
//! that both of them depend on.
7+
8+
/// The spirv attribute with version tag
9+
///
10+
/// ```ignore
11+
/// # we don't know the namespace of our function
12+
/// let spirv = spirv_attr_with_version();
13+
/// let attr = format!("#[rust_gpu::{spirv}(vertex)]");
14+
/// // version here may be out-of-date
15+
/// assert_eq!("#[rust_gpu::spirv_v0_9(vertex)]", attr);
16+
/// ```
17+
pub fn spirv_attr_with_version() -> String {
18+
let major: u32 = env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap();
19+
let minor: u32 = env!("CARGO_PKG_VERSION_MINOR").parse().unwrap();
20+
format!("spirv_v{major}_{minor}")
21+
}

0 commit comments

Comments
 (0)