Skip to content

Commit b3d9c38

Browse files
committed
Add string attr to apply extra ptr arg to offload kernels
1 parent 97a8e96 commit b3d9c38

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>,
@@ -396,6 +404,10 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
396404
to_add.push(llvm::CreateAttrString(cx.llcx, "no-builtins"));
397405
}
398406

407+
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::OFFLOAD_KERNEL) {
408+
to_add.push(llvm::CreateAttrString(cx.llcx, "offload-kernel"))
409+
}
410+
399411
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
400412
to_add.push(AttributeKind::Cold.create_attr(cx.llcx));
401413
}

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() {
@@ -705,11 +705,12 @@ pub(crate) unsafe fn llvm_optimize(
705705
SimpleCx::new(module.module_llvm.llmod(), module.module_llvm.llcx, cgcx.pointer_size);
706706
// For now we only support up to 10 kernels named kernel_0 ... kernel_9, a follow-up PR is
707707
// introducing a proper offload intrinsic to solve this limitation.
708-
for num in 0..9 {
709-
let name = format!("kernel_{num}");
710-
if let Some(kernel) = cx.get_function(&name) {
711-
handle_offload(&cx, kernel);
708+
for func in cx.get_functions() {
709+
let offload_kernel = "offload-kernel";
710+
if attributes::has_string_attr(func, offload_kernel) {
711+
handle_offload(&cx, func);
712712
}
713+
attributes::remove_string_attr_from_llfn(func, offload_kernel);
713714
}
714715
}
715716

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
@@ -1108,6 +1108,11 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
11081108
rustc_autodiff, Normal,
11091109
template!(Word, List: &[r#""...""#]), DuplicatesOk,
11101110
EncodeCrossCrate::Yes,
1111+
),
1112+
rustc_attr!(
1113+
rustc_offload_kernel, Normal,
1114+
template!(Word), DuplicatesOk,
1115+
EncodeCrossCrate::Yes,
11111116
),
11121117
// Traces that are left when `cfg` and `cfg_attr` attributes are expanded.
11131118
// 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
@@ -1960,6 +1960,7 @@ symbols! {
19601960
rustc_objc_class,
19611961
rustc_objc_selector,
19621962
rustc_object_lifetime_default,
1963+
rustc_offload_kernel,
19631964
rustc_on_unimplemented,
19641965
rustc_outlives,
19651966
rustc_paren_sugar,

0 commit comments

Comments
 (0)