Skip to content

Commit 3204c42

Browse files
committed
Add string attr to apply extra ptr arg to offload kernels
1 parent 0a1cd54 commit 3204c42

File tree

8 files changed

+47
-5
lines changed

8 files changed

+47
-5
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ pub(crate) fn apply_to_callsite(callsite: &Value, idx: AttributePlace, attrs: &[
3030
}
3131
}
3232

33+
pub(crate) fn has_string_attr(llfn: &Value, name: &str) -> bool {
34+
llvm::HasStringAttribute(llfn, name)
35+
}
36+
37+
pub(crate) fn remove_string_attr_from_llfn(llfn: &Value, name: &str) {
38+
llvm::RemoveStringAttrFromFn(llfn, name);
39+
}
40+
3341
/// Get LLVM attribute for the provided inline heuristic.
3442
pub(crate) fn inline_attr<'ll, 'tcx>(
3543
cx: &SimpleCx<'ll>,
@@ -408,6 +416,10 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
408416
to_add.push(llvm::CreateAttrString(cx.llcx, "no-builtins"));
409417
}
410418

419+
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::OFFLOAD_KERNEL) {
420+
to_add.push(llvm::CreateAttrString(cx.llcx, "offload-kernel"))
421+
}
422+
411423
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
412424
to_add.push(AttributeKind::Cold.create_attr(cx.llcx));
413425
}

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::errors::{
4343
use crate::llvm::diagnostic::OptimizationDiagnosticKind::*;
4444
use crate::llvm::{self, DiagnosticInfo};
4545
use crate::type_::llvm_type_ptr;
46-
use crate::{LlvmCodegenBackend, ModuleLlvm, SimpleCx, base, common, llvm_util};
46+
use crate::{LlvmCodegenBackend, ModuleLlvm, SimpleCx, attributes, base, common, llvm_util};
4747

4848
pub(crate) fn llvm_err<'a>(dcx: DiagCtxtHandle<'_>, err: LlvmError<'a>) -> ! {
4949
match llvm::last_error() {
@@ -706,11 +706,12 @@ pub(crate) unsafe fn llvm_optimize(
706706
SimpleCx::new(module.module_llvm.llmod(), module.module_llvm.llcx, cgcx.pointer_size);
707707
// For now we only support up to 10 kernels named kernel_0 ... kernel_9, a follow-up PR is
708708
// introducing a proper offload intrinsic to solve this limitation.
709-
for num in 0..9 {
710-
let name = format!("kernel_{num}");
711-
if let Some(kernel) = cx.get_function(&name) {
712-
handle_offload(&cx, kernel);
709+
for func in cx.get_functions() {
710+
let offload_kernel = "offload-kernel";
711+
if attributes::has_string_attr(func, offload_kernel) {
712+
handle_offload(&cx, func);
713713
}
714+
attributes::remove_string_attr_from_llfn(func, offload_kernel);
714715
}
715716
}
716717

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,16 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
791791
llvm::LLVMMDStringInContext2(self.llcx(), name.as_ptr() as *const c_char, name.len())
792792
}
793793
}
794+
795+
pub(crate) fn get_functions(&self) -> Vec<&'ll Value> {
796+
let mut functions = vec![];
797+
let mut func = unsafe { llvm::LLVMGetFirstFunction(self.llmod()) };
798+
while let Some(f) = func {
799+
functions.push(f);
800+
func = unsafe { llvm::LLVMGetNextFunction(f) }
801+
}
802+
functions
803+
}
794804
}
795805

796806
impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {

compiler/rustc_codegen_llvm/src/llvm/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ pub(crate) fn AddFunctionAttributes<'ll>(
4343
}
4444
}
4545

46+
pub(crate) fn HasStringAttribute<'ll>(llfn: &'ll Value, name: &str) -> bool {
47+
unsafe { LLVMRustHasFnAttribute(llfn, name.as_c_char_ptr(), name.len()) }
48+
}
49+
50+
pub(crate) fn RemoveStringAttrFromFn<'ll>(llfn: &'ll Value, name: &str) {
51+
unsafe { LLVMRustRemoveFnAttribute(llfn, name.as_c_char_ptr(), name.len()) }
52+
}
53+
4654
pub(crate) fn AddCallSiteAttributes<'ll>(
4755
callsite: &'ll Value,
4856
idx: AttributePlace,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ fn process_builtin_attrs(
334334
codegen_fn_attrs.patchable_function_entry =
335335
parse_patchable_function_entry(tcx, attr);
336336
}
337+
sym::rustc_offload_kernel => {
338+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::OFFLOAD_KERNEL
339+
}
337340
_ => {}
338341
}
339342
}

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,11 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
11001100
rustc_autodiff, Normal,
11011101
template!(Word, List: &[r#""...""#]), DuplicatesOk,
11021102
EncodeCrossCrate::Yes,
1103+
),
1104+
rustc_attr!(
1105+
rustc_offload_kernel, Normal,
1106+
template!(Word), DuplicatesOk,
1107+
EncodeCrossCrate::Yes,
11031108
),
11041109
// Traces that are left when `cfg` and `cfg_attr` attributes are expanded.
11051110
// The attributes are not gated, to avoid stability errors, but they cannot be used in stable

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ bitflags::bitflags! {
190190
const NO_BUILTINS = 1 << 15;
191191
/// Marks foreign items, to make `contains_extern_indicator` cheaper.
192192
const FOREIGN_ITEM = 1 << 16;
193+
/// `#[rustc_offload_kernel]`: indicates that this is an offload kernel, an extra ptr arg will be added.
194+
const OFFLOAD_KERNEL = 1 << 17;
193195
}
194196
}
195197
rustc_data_structures::external_bitflags_debug! { CodegenFnAttrFlags }

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,7 @@ symbols! {
19661966
rustc_objc_class,
19671967
rustc_objc_selector,
19681968
rustc_object_lifetime_default,
1969+
rustc_offload_kernel,
19691970
rustc_on_unimplemented,
19701971
rustc_outlives,
19711972
rustc_paren_sugar,

0 commit comments

Comments
 (0)