Skip to content

Commit 72ab072

Browse files
committed
Fix nvvm_internal attribute usage. add example now compiles and runs!
`register_attr` was removed in favor of `register_tool`. This updates how the nvvm attributes are applied in our proc_macros and how the nvvm backend parses them. Also removes redundant applications of no_std, as cuda_builder applies it to all crates built on the cuda target via rustc flags. The add example crate now compiles and runs the geneated ptx! :D
1 parent ad7d0d3 commit 72ab072

File tree

8 files changed

+24
-42
lines changed

8 files changed

+24
-42
lines changed

crates/cuda_builder/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ fn invoke_rustc(builder: &CudaBuilder) -> Result<PathBuf, CudaBuilderError> {
379379
let mut rustflags = vec![
380380
format!("-Zcodegen-backend={}", rustc_codegen_nvvm.display()),
381381
"-Zcrate-attr=feature(register_tool)".into(),
382-
"-Zcrate-attr=register_tool(rust_cuda)".into(),
382+
"-Zcrate-attr=register_tool(nvvm_internal)".into(),
383383
"-Zcrate-attr=no_std".into(),
384384
"-Zsaturating_float_casts=false".into(),
385385
];
@@ -440,8 +440,7 @@ fn invoke_rustc(builder: &CudaBuilder) -> Result<PathBuf, CudaBuilderError> {
440440
"--lib",
441441
"--message-format=json-render-diagnostics",
442442
"-Zbuild-std=core,alloc",
443-
"--target",
444-
"nvptx64-nvidia-cuda",
443+
"--target=nvptx64-nvidia-cuda",
445444
]);
446445

447446
cargo.args(&builder.build_args);

crates/cuda_std/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//!
66
//! This library will build on non-nvptx targets or targets not using the nvvm backend. However, it will not
77
//! be usable, and it will throw linker errors if you attempt to use most of the functions in the library.
8-
//! However, [`kernel`] automatically cfg-gates the function annotated for `nvptx64` or `nvptx`, therefore,
8+
//! However, [`kernel`] automatically cfg-gates the function annotated for `nvptx64`, therefore,
99
//! no "actual" functions from this crate should be used when compiling for a non-nvptx target.
1010
//!
1111
//! This crate cannot be used with the llvm ptx backend either, it heavily relies on external functions implicitly
@@ -24,9 +24,7 @@
2424
#![allow(internal_features)]
2525
#![cfg_attr(
2626
target_os = "cuda",
27-
no_std,
2827
feature(alloc_error_handler, asm_experimental_arch, link_llvm_intrinsics),
29-
register_attr(nvvm_internal)
3028
)]
3129

3230
extern crate alloc;

crates/cuda_std/src/shared.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ macro_rules! shared_array {
6868
/// Gets a pointer to the dynamic shared memory that was allocated by the caller of the kernel. The
6969
/// data is left uninitialized.
7070
///
71-
/// **Calling this function multiple times will yield the same pointer**.
71+
/// **Calling this function multiple times will yield the same pointer**.
7272
#[gpu_only]
7373
pub fn dynamic_shared_mem<T>() -> *mut T {
7474
// it is unclear whether an alignment of 16 is actually required for correctness, however,
@@ -77,7 +77,7 @@ pub fn dynamic_shared_mem<T>() -> *mut T {
7777
extern "C" {
7878
// need to use nvvm_internal and not address_space because address_space only parses
7979
// static definitions, not extern static definitions.
80-
#[nvvm_internal(addrspace(3))]
80+
#[nvvm_internal::addrspace(3)]
8181
#[allow(improper_ctypes)]
8282
// mangle it a bit to make sure nobody makes the same thing
8383
#[link_name = "_Zcuda_std_dyn_shared"]

crates/cuda_std_macros/src/lib.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn kernel(input: proc_macro::TokenStream, item: proc_macro::TokenStream) ->
2727
let mut item = parse_macro_input!(item as ItemFn);
2828
let no_mangle = parse_quote!(#[no_mangle]);
2929
item.attrs.push(no_mangle);
30-
let internal = parse_quote!(#[cfg_attr(target_arch="nvptx64", nvvm_internal(kernel(#input)))]);
30+
let internal = parse_quote!(#[cfg_attr(target_arch="nvptx64", nvvm_internal::kernel(#input))]);
3131
item.attrs.push(internal);
3232

3333
// used to guarantee some things about how params are passed in the codegen.
@@ -155,12 +155,7 @@ pub fn gpu_only(_attr: proc_macro::TokenStream, item: proc_macro::TokenStream) -
155155
} = syn::parse_macro_input!(item as syn::ItemFn);
156156

157157
let mut cloned_attrs = attrs.clone();
158-
cloned_attrs.retain(|a| {
159-
!a.path()
160-
.get_ident()
161-
.map(|x| *x == "nvvm_internal")
162-
.unwrap_or_default()
163-
});
158+
cloned_attrs.retain(|a| a.path().segments[0].ident != "nvvm");
164159

165160
let fn_name = sig.ident.clone();
166161

@@ -203,7 +198,7 @@ pub fn externally_visible(
203198
"#[externally_visible] function should also be #[no_mangle]"
204199
);
205200

206-
let new_attr = parse_quote!(#[cfg_attr(target_os = "cuda", nvvm_internal(used))]);
201+
let new_attr = parse_quote!(#[cfg_attr(target_os = "cuda", nvvm_internal::used)]);
207202
func.attrs.push(new_attr);
208203

209204
func.into_token_stream().into()
@@ -231,7 +226,7 @@ pub fn address_space(attr: proc_macro::TokenStream, item: proc_macro::TokenStrea
231226
};
232227

233228
let new_attr =
234-
parse_quote!(#[cfg_attr(target_os = "cuda", nvvm_internal(addrspace(#addrspace_num)))]);
229+
parse_quote!(#[cfg_attr(target_os = "cuda", nvvm_internal::addrspace(#addrspace_num))]);
235230
global.attrs.push(new_attr);
236231

237232
global.into_token_stream().into()

crates/rustc_codegen_nvvm/src/attributes.rs

+13-21
Original file line numberDiff line numberDiff line change
@@ -116,31 +116,23 @@ impl NvvmAttributes {
116116
let mut nvvm_attrs = Self::default();
117117

118118
for attr in attrs {
119-
if attr.has_name(cx.symbols.nvvm_internal) {
119+
if attr.path_matches(&[cx.symbols.nvvm_internal, cx.symbols.kernel]) {
120+
nvvm_attrs.kernel = true;
121+
} else if attr.path_matches(&[cx.symbols.nvvm_internal, sym::used]) {
122+
nvvm_attrs.used = true;
123+
} else if attr.path_matches(&[cx.symbols.nvvm_internal, cx.symbols.addrspace]) {
120124
let args = attr.meta_item_list().unwrap_or_default();
121-
if let Some(arg) = args.first() {
122-
if arg.has_name(cx.symbols.kernel) {
123-
nvvm_attrs.kernel = true;
124-
}
125-
if arg.has_name(sym::used) {
126-
nvvm_attrs.used = true;
127-
}
128-
if arg.has_name(cx.symbols.addrspace) {
129-
let args = arg.meta_item_list().unwrap_or_default();
130-
if let Some(MetaItemInner::Lit(MetaItemLit {
131-
kind: LitKind::Int(val, _),
132-
..
133-
})) = args.first()
134-
{
135-
nvvm_attrs.addrspace = Some(val.get() as u8);
136-
} else {
137-
panic!();
138-
}
139-
}
125+
if let Some(MetaItemInner::Lit(MetaItemLit {
126+
kind: LitKind::Int(val, _),
127+
..
128+
})) = args.first()
129+
{
130+
nvvm_attrs.addrspace = Some(val.get() as u8);
131+
} else {
132+
panic!();
140133
}
141134
}
142135
}
143-
144136
nvvm_attrs
145137
}
146138
}

crates/rustc_codegen_nvvm/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
257257
impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
258258
/// Computes the address space for a static.
259259
pub fn static_addrspace(&self, instance: Instance<'tcx>) -> AddressSpace {
260-
let ty = instance.ty(self.tcx, ty::TypingEnv::fully_monomorphized());
260+
let ty = instance.ty(self.tcx, self.typing_env());
261261
let is_mutable = self.tcx().is_mutable_static(instance.def_id());
262262
let attrs = self.tcx.get_attrs_unchecked(instance.def_id()); // TODO: replace with get_attrs
263263
let nvvm_attrs = NvvmAttributes::parse(self, attrs);

crates/rustc_codegen_nvvm/src/mono_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'ll, 'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
9090
attributes::from_fn_attrs(self, lldecl, instance);
9191

9292
let def_id = instance.def_id();
93-
let attrs = self.tcx.get_attrs_unchecked(def_id); // FIXME(jorge): Replace with get_attrs
93+
let attrs = self.tcx.get_attrs_unchecked(def_id); // TODO: Replace with get_attrs
9494
let nvvm_attrs = NvvmAttributes::parse(self, attrs);
9595

9696
unsafe {

examples/cuda/gpu/add_gpu/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![cfg_attr(target_os = "cuda", no_std, register_attr(nvvm_internal))]
2-
31
use cuda_std::prelude::*;
42

53
#[kernel]

0 commit comments

Comments
 (0)