Skip to content

Commit 51a03d9

Browse files
committed
generate additional entries
1 parent 55581c4 commit 51a03d9

File tree

2 files changed

+74
-40
lines changed

2 files changed

+74
-40
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ pub(crate) fn run_pass_manager(
681681
let ti64 = cx.type_i64();
682682
let ti32 = cx.type_i32();
683683
let ti16 = cx.type_i16();
684+
let ti16 = cx.type_i8();
684685
let tarr = cx.type_array(ti32, 3);
685686

686687
let entry_elements = vec![ti64, ti16, ti16, ti32, tptr, tptr, ti64, ti64, tptr];
@@ -699,47 +700,75 @@ pub(crate) fn run_pass_manager(
699700
if !cx.get_function(&format!("kernel_{num}")).is_some() {
700701
continue;
701702
}
702-
//for function in cx.get_functions() {
703-
//if !attributes::has_attr(function, Function, llvm::AttributeKind::OptimizeForSize) {
704-
// dbg!("skipping minsize fnc");
705-
// dbg!(&function);
706-
// // print fnc name
707-
// let enzyme_marker = "minsize";
708-
// if attributes::has_string_attr(function, enzyme_marker) {
709-
// dbg!("found minsize str");
710-
// }
711-
// continue;
712-
713-
let size_name = format!(".offload_sizes.{num}");
714-
let size_ty = cx.type_array(ti64, 4);
715-
//let size_val = vec![8i64,0,16,0];
716-
let c_val_8 = cx.get_const_i64(8);
717-
let c_val_0 = cx.get_const_i64(0);
718-
let c_val_16 = cx.get_const_i64(16);
719-
let size_val = vec![c_val_8, c_val_0, c_val_16, c_val_0];
720-
721-
//let val = cx.define_global(&size_name, size_ty).unwrap();
722-
//dbg!(&val);
723-
//let section_var = cx
724-
// .define_global(section_var_name, llvm_type)
725-
// .unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
726-
//llvm::set_section(section_var, c".debug_gdb_scripts");
727-
//llvm::set_initializer(section_var, cx.const_bytes(section_contents));
728-
//llvm::LLVMSetGlobalConstant(section_var, llvm::True);
729-
//llvm::set_linkage(section_var, llvm::Linkage::LinkOnceODRLinkage);
730-
//// This should make sure that the whole section is not larger than
731-
//// the string it contains. Otherwise we get a warning from GDB.
732-
//llvm::LLVMSetAlignment(section_var, 1);
733-
//llvm::set_initializer(val, cx.const_bytes(size_val.as_slice()));
734-
let initializer = cx.const_array(ti64, &size_val);
735-
let name = format!(".offload_sizes.{num}");
736-
let c_name = CString::new(name).unwrap();
737-
let array = llvm::add_global(cx.llmod, cx.val_ty(initializer), &c_name );
738-
llvm::set_global_constant(array, true);
739-
unsafe {llvm::LLVMSetUnnamedAddress(array, llvm::UnnamedAddr::Global)};
740-
llvm::set_linkage(array, llvm::Linkage::PrivateLinkage);
741-
llvm::set_initializer(array, initializer);
703+
704+
fn add_priv_unnamed_arr(cx: &SimpleCx<'_>, name: &str, vals: &[u64]) {
705+
let ti64 = cx.type_i64();
706+
let size_ty = cx.type_array(ti64, vals.len() as u64);
707+
let mut size_val = Vec::with_capacity(vals.len());
708+
for &val in vals {
709+
size_val.push(cx.get_const_i64(val));
710+
}
711+
let initializer = cx.const_array(ti64, &size_val);
712+
let c_name = CString::new(name).unwrap();
713+
let array = llvm::add_global(cx.llmod, cx.val_ty(initializer), &c_name );
714+
llvm::set_global_constant(array, true);
715+
unsafe {llvm::LLVMSetUnnamedAddress(array, llvm::UnnamedAddr::Global)};
716+
llvm::set_linkage(array, llvm::Linkage::PrivateLinkage);
717+
llvm::set_initializer(array, initializer);
718+
719+
}
720+
721+
// We add a pair of sizes and maptypes per offloadable function.
722+
// @.offload_maptypes = private unnamed_addr constant [4 x i64] [i64 800, i64 544, i64 547, i64 544]
723+
let array = add_priv_unnamed_arr(&cx, &format!(".offload_sizes.{num}"), &vec![8u64,0,16,0]);
724+
let maptypes = add_priv_unnamed_arr(&cx, &format!(".offload_maptypes.{num}"), &vec![800u64, 544, 547, 544]);
742725
dbg!(&array);
726+
// TODO: We should add another pair per call to offloadable functions
727+
// @.offload_sizes.5 = private unnamed_addr constant [2 x i64] [i64 16384, i64 16384]
728+
// @.offload_maptypes.6 = private unnamed_addr constant [2 x i64] [i64 1, i64 3]
729+
730+
// Next: For each function, generate these three entries. A weak constant,
731+
// the llvm.rodata entry name, and the omp_offloading_entries value
732+
// @.__omp_offloading_86fafab6_c40006a1__Z3fooPSt7complexIdES1_S0_m_l7.region_id = weak constant i8 0
733+
// @.offloading.entry_name = internal unnamed_addr constant [66 x i8] c"__omp_offloading_86fafab6_c40006a1__Z3fooPSt7complexIdES1_S0_m_l7\00", section ".llvm.rodata.offloading", align 1
734+
// @.offloading.entry.__omp_offloading_86fafab6_c40006a1__Z3fooPSt7complexIdES1_S0_m_l7 = weak constant %struct.__tgt_offload_entry { i64 0, i16 1, i16 1, i32 0, ptr @.__omp_offloading_86fafab6_c40006a1__Z3fooPSt7complexIdES1_S0_m_l7.region_id, ptr @.offloading.entry_name, i64 0, i64 0, ptr null }, section "omp_offloading_entries", align 1
735+
736+
let name = format!(".kernel_{num}.region_id");
737+
let name = CString::new(name).unwrap();
738+
let entry = llvm::add_global(cx.llmod, cx.type_i8(), &name);
739+
llvm::set_global_constant(entry, true);
740+
llvm::set_linkage(entry, llvm::Linkage::WeakAnyLinkage);
741+
llvm::set_initializer(entry, cx.get_const_i8(0));
742+
743+
// typedef struct {
744+
// uint64_t Reserved;
745+
// uint16_t Version;
746+
// uint16_t Kind;
747+
// uint32_t Flags;
748+
// void *Address;
749+
// char *SymbolName;
750+
// uint64_t Size;
751+
// uint64_t Data;
752+
// void *AuxAddr;
753+
// } __tgt_offload_entry;
754+
//
755+
// enum Flags {
756+
// OMP_REGISTER_REQUIRES = 0x10,
757+
// };
758+
//
759+
// typedef struct {
760+
// void *ImageStart;
761+
// void *ImageEnd;
762+
// __tgt_offload_entry *EntriesBegin;
763+
// __tgt_offload_entry *EntriesEnd;
764+
// } __tgt_device_image;
765+
//
766+
// typedef struct {
767+
// int32_t NumDeviceImages;
768+
// __tgt_device_image *DeviceImages;
769+
// __tgt_offload_entry *HostEntriesBegin;
770+
// __tgt_offload_entry *HostEntriesEnd;
771+
// } __tgt_bin_desc;
743772
// 1. @.offload_sizes.{num} = private unnamed_addr constant [4 x i64] [i64 8, i64 0, i64 16, i64 0]
744773
// 2. @.offload_maptypes
745774
// 3. @.__omp_offloading_<hash>_fnc_name_<hash> = weak constant i8 0

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,11 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
685685
unsafe { llvm::LLVMConstInt(ty, n, llvm::False) }
686686
}
687687

688+
pub(crate) fn get_const_i8(&self, n: u64) -> &'ll Value {
689+
let ty = unsafe { llvm::LLVMInt8TypeInContext(self.llcx()) };
690+
unsafe { llvm::LLVMConstInt(ty, n, llvm::False) }
691+
}
692+
688693
pub(crate) fn get_function(&self, name: &str) -> Option<&'ll Value> {
689694
let name = SmallCStr::new(name);
690695
unsafe { llvm::LLVMGetNamedFunction((**self).borrow().llmod, name.as_ptr()) }

0 commit comments

Comments
 (0)