Skip to content

Commit 4370d63

Browse files
committed
tool: versionize rust_gpu::spirv to rust_gpu::spirv_v0_9
1 parent 706d734 commit 4370d63

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

crates/rustc_codegen_spirv/src/attr.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,10 @@ fn parse_attrs_for_checking<'a>(
530530
Attribute::Unparsed(item) => {
531531
// #[...]
532532
let s = &item.path.segments;
533-
if let Some(rust_gpu) = s.get(0)
534-
&& rust_gpu.name == sym.rust_gpu
535-
{
533+
if let Some(rust_gpu) = s.get(0) && rust_gpu.name == sym.rust_gpu {
536534
// #[rust_gpu ...]
537535
match s.get(1) {
538-
Some(command) if command.name == sym.spirv => {
536+
Some(command) if command.name == sym.spirv_attr_with_version => {
539537
// #[rust_gpu::spirv ...]
540538
if let Some(args) = attr.meta_item_list() {
541539
// #[rust_gpu::spirv(...)]
@@ -551,10 +549,11 @@ fn parse_attrs_for_checking<'a>(
551549
}
552550
_ => {
553551
// #[rust_gpu::...] but not a know version
552+
let spirv = sym.spirv_attr_with_version.as_str();
554553
Err((
555554
attr.span(),
556-
"unknown `rust_gpu` attribute, expected `rust_gpu::spirv`"
557-
.to_string(),
555+
format!("unknown `rust_gpu` attribute, expected `rust_gpu::{spirv}`. \
556+
Do the versions of `spirv-std` and `rustc_codegen_spirv` match?"),
558557
))
559558
}
560559
}

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
@@ -143,11 +146,12 @@ pub fn Image(item: TokenStream) -> TokenStream {
143146
/// `#[cfg_attr(target_arch="spirv", rust_gpu::spirv(..))]`.
144147
#[proc_macro_attribute]
145148
pub fn spirv(attr: TokenStream, item: TokenStream) -> TokenStream {
149+
let spirv = format_ident!("{}", &spirv_attr_with_version());
146150
let mut tokens: Vec<TokenTree> = Vec::new();
147151

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

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

crates/spirv_attr_version.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
/// ```rust,norun
11+
/// let spirv = spirv_attr_with_version();
12+
/// let attr = format!("#[rust_gpu::{spirv}(vertex)]");
13+
/// // version here may be out-of-date
14+
/// assert_eq!("#[rust_gpu::spirv_v0_9(vertex)]", attr);
15+
/// ```
16+
pub fn spirv_attr_with_version() -> String {
17+
let major: u32 = env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap();
18+
let minor: u32 = env!("CARGO_PKG_VERSION_MINOR").parse().unwrap();
19+
format!("spirv_v{major}_{minor}")
20+
}

0 commit comments

Comments
 (0)