Skip to content

Commit c478fbe

Browse files
committed
wip
1 parent 2351ae8 commit c478fbe

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::llvm::AttributePlace::Function;
66
use crate::llvm::{self, Linkage, Visibility, build_string};
77
use crate::{LlvmCodegenBackend, ModuleLlvm, SimpleCx, attributes};
88
use rustc_codegen_ssa::back::write::{CodegenContext, FatLtoInput};
9+
use std::ffi::c_uint;
910

1011
use llvm::Linkage::*;
1112
use rustc_abi::Align;
@@ -23,7 +24,86 @@ fn create_struct_ty<'ll>(
2324
entry_struct
2425
}
2526
}
26-
//const DATA_NULL: &[u8] = c"111111";
27+
28+
//weak_odr hidden local_unnamed_addr addrspace(1) constant i32 0
29+
pub(crate) fn gen_asdf<'ll>(cgcx: &CodegenContext<LlvmCodegenBackend>, old_cx: &SimpleCx<'ll>) {
30+
let llcx = unsafe { llvm::LLVMRustContextCreate(false) };
31+
let module_name = CString::new("offload.wrapper.module").unwrap();
32+
let llmod = unsafe { llvm::LLVMModuleCreateWithNameInContext(module_name.as_ptr(), llcx) };
33+
let cx = SimpleCx::new(llmod, llcx, cgcx.pointer_size);
34+
let initializer = cx.get_const_i32(0);
35+
let llglobal = add_unnamed_global_in_addrspace(
36+
&cx,
37+
"__omp_rtl_debug_kind",
38+
initializer,
39+
WeakODRLinkage,
40+
1,
41+
);
42+
let llglobal = add_unnamed_global_in_addrspace(
43+
&cx,
44+
"__omp_rtl_assume_teams_oversubscription",
45+
initializer,
46+
WeakODRLinkage,
47+
1,
48+
);
49+
let llglobal = add_unnamed_global_in_addrspace(
50+
&cx,
51+
"__omp_rtl_assume_threads_oversubscription",
52+
initializer,
53+
WeakODRLinkage,
54+
1,
55+
);
56+
let llglobal = add_unnamed_global_in_addrspace(
57+
&cx,
58+
"__omp_rtl_assume_no_thread_state",
59+
initializer,
60+
WeakODRLinkage,
61+
1,
62+
);
63+
let llglobal = add_unnamed_global_in_addrspace(
64+
&cx,
65+
"__oclc_ABI_version",
66+
cx.get_const_i32(500),
67+
WeakODRLinkage,
68+
4,
69+
);
70+
unsafe {
71+
llvm::LLVMPrintModuleToFile(
72+
llmod,
73+
CString::new("rustmagic-openmp-amdgcn-amd-amdhsa-gfx90a.ll").unwrap().as_ptr(),
74+
std::ptr::null_mut(),
75+
);
76+
77+
// Clean up
78+
llvm::LLVMDisposeModule(llmod);
79+
llvm::LLVMContextDispose(llcx);
80+
}
81+
// TODO: addressspace 1 or 4
82+
}
83+
// source_filename = "mem.cpp"
84+
// GPU: target datalayout = "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128-p9:192:256:256:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9"
85+
// CPU: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
86+
// target triple = "amdgcn-amd-amdhsa"
87+
//
88+
// @__omp_rtl_debug_kind = weak_odr hidden local_unnamed_addr addrspace(1) constant i32 0
89+
// @__omp_rtl_assume_teams_oversubscription = weak_odr hidden local_unnamed_addr addrspace(1) constant i32 0
90+
// @__omp_rtl_assume_threads_oversubscription = weak_odr hidden local_unnamed_addr addrspace(1) constant i32 0
91+
// @__omp_rtl_assume_no_thread_state = weak_odr hidden local_unnamed_addr addrspace(1) constant i32 0
92+
// @__omp_rtl_assume_no_nested_parallelism = weak_odr hidden local_unnamed_addr addrspace(1) constant i32 0
93+
// @__oclc_ABI_version = weak_odr hidden local_unnamed_addr addrspace(4) constant i32 500
94+
//
95+
// !llvm.module.flags = !{!0, !1, !2, !3, !4}
96+
// !opencl.ocl.version = !{!5}
97+
// !llvm.ident = !{!6, !7}
98+
//
99+
// !0 = !{i32 1, !"amdhsa_code_object_version", i32 500}
100+
// !1 = !{i32 1, !"wchar_size", i32 4}
101+
// !2 = !{i32 7, !"openmp", i32 51}
102+
// !3 = !{i32 7, !"openmp-device", i32 51}
103+
// !4 = !{i32 8, !"PIC Level", i32 2}
104+
// !5 = !{i32 2, i32 0}
105+
// !6 = !{!"clang version 20.1.5-rust-1.89.0-nightly (https://github.com/rust-lang/llvm-project.git c1118fdbb3024157df7f4cfe765f2b0b4339e8a2)"}
106+
// !7 = !{!"AMD clang version 19.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.4.0 25133 c7fe45cf4b819c5991fe208aaa96edf142730f1d)"}
27107

28108
// We don't copy types from other functions because we generate a new module and context.
29109
// Bringing in types from other contexts would likely cause issues.
@@ -171,6 +251,7 @@ pub(crate) fn handle_gpu_code<'ll>(
171251
dbg!("gen_call_handling");
172252
gen_call_handling(&cx, &kernels, at_one, begin, update, end, fn_ty, &o_types);
173253
gen_image_wrapper_module(&cgcx, &cx);
254+
gen_asdf(&cgcx, &cx);
174255
} else {
175256
dbg!("no marker found");
176257
}
@@ -312,6 +393,18 @@ fn add_priv_unnamed_arr<'ll>(cx: &SimpleCx<'ll>, name: &str, vals: &[u64]) -> &'
312393
add_unnamed_global(cx, name, initializer, PrivateLinkage)
313394
}
314395

396+
fn add_unnamed_global_in_addrspace<'ll>(
397+
cx: &SimpleCx<'ll>,
398+
name: &str,
399+
initializer: &'ll llvm::Value,
400+
l: Linkage,
401+
addrspace: u32,
402+
) -> &'ll llvm::Value {
403+
let llglobal = add_global_in_addrspace(cx, name, initializer, l, addrspace);
404+
unsafe { llvm::LLVMSetUnnamedAddress(llglobal, llvm::UnnamedAddr::Global) };
405+
llglobal
406+
}
407+
315408
fn add_unnamed_global<'ll>(
316409
cx: &SimpleCx<'ll>,
317410
name: &str,
@@ -323,6 +416,26 @@ fn add_unnamed_global<'ll>(
323416
llglobal
324417
}
325418

419+
fn add_global_in_addrspace<'ll>(
420+
cx: &SimpleCx<'ll>,
421+
name: &str,
422+
initializer: &'ll llvm::Value,
423+
l: Linkage,
424+
addrspace: u32,
425+
) -> &'ll llvm::Value {
426+
let c_name = CString::new(name).unwrap();
427+
let llglobal: &'ll llvm::Value = llvm::add_global_in_addrspace(
428+
cx.llmod,
429+
cx.val_ty(initializer),
430+
&c_name,
431+
addrspace as c_uint,
432+
);
433+
llvm::set_global_constant(llglobal, true);
434+
llvm::set_linkage(llglobal, l);
435+
llvm::set_initializer(llglobal, initializer);
436+
llglobal
437+
}
438+
326439
fn add_global<'ll>(
327440
cx: &SimpleCx<'ll>,
328441
name: &str,

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,12 @@ unsafe extern "C" {
11851185
// Operations on global variables
11861186
pub(crate) fn LLVMIsAGlobalVariable(GlobalVar: &Value) -> Option<&Value>;
11871187
pub(crate) fn LLVMAddGlobal<'a>(M: &'a Module, Ty: &'a Type, Name: *const c_char) -> &'a Value;
1188+
pub(crate) fn LLVMAddGlobalInAddressSpace<'a>(
1189+
M: &'a Module,
1190+
Ty: &'a Type,
1191+
Name: *const c_char,
1192+
addrspace: c_uint,
1193+
) -> &'a Value;
11881194
pub(crate) fn LLVMGetNamedGlobal(M: &Module, Name: *const c_char) -> Option<&Value>;
11891195
pub(crate) fn LLVMGetFirstGlobal(M: &Module) -> Option<&Value>;
11901196
pub(crate) fn LLVMGetNextGlobal(GlobalVar: &Value) -> Option<&Value>;

compiler/rustc_codegen_llvm/src/llvm/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ pub(crate) fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name_cstr: &CStr)
252252
unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) }
253253
}
254254

255+
pub(crate) fn add_global_in_addrspace<'a>(
256+
llmod: &'a Module,
257+
ty: &'a Type,
258+
name_cstr: &CStr,
259+
addrspace: c_uint,
260+
) -> &'a Value {
261+
unsafe { LLVMAddGlobalInAddressSpace(llmod, ty, name_cstr.as_ptr(), addrspace) }
262+
}
263+
255264
pub(crate) fn set_initializer(llglobal: &Value, constant_val: &Value) {
256265
unsafe {
257266
LLVMSetInitializer(llglobal, constant_val);

0 commit comments

Comments
 (0)