Skip to content

Commit a786d6b

Browse files
committed
imprv
1 parent 3a8e8a6 commit a786d6b

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -840,13 +840,13 @@ fn gen_call_handling<'ll>(cx: &'ll SimpleCx<'_>, kernels: &[&'ll llvm::Value], s
840840
return;
841841
};
842842
let kernel_call_bb = unsafe {llvm::LLVMGetInstructionParent(kernel_call)};
843-
let called = unsafe {llvm::LLVMGetCalledValue(kernel_call)};
843+
let called = unsafe {llvm::LLVMGetCalledValue(kernel_call).unwrap()};
844844
let mut builder = SBuilder::build(cx, kernel_call_bb);
845845

846846
let types = cx.func_params_types(cx.get_type_of_global(called));
847847
dbg!(&types);
848848
let num_args = types.len() as u64;
849-
let mut names: Vec<&llvm::Value> = Vec::with_capacity(num_args);
849+
let mut names: Vec<&llvm::Value> = Vec::with_capacity(num_args as usize);
850850

851851
// Step 0)
852852
unsafe{llvm::LLVMRustPositionBuilderPastAllocas(builder.llbuilder, main_fn)};
@@ -859,21 +859,37 @@ fn gen_call_handling<'ll>(cx: &'ll SimpleCx<'_>, kernels: &[&'ll llvm::Value], s
859859
let ty2 = cx.type_array(cx.type_i64(), num_args);
860860
let a4 = builder.my_alloca2(ty2, Align::EIGHT, ".offload_sizes");
861861
// Now we allocate once per function param, a copy to be passed to one of our maps.
862+
let mut vals = vec![];
863+
let mut geps = vec![];
864+
let i32_0 = cx.get_const_i32(0);
862865
for (index, in_ty) in types.iter().enumerate() {
863-
// Todo:
866+
// get function arg, store it into the alloca, and read it.
864867
let p = llvm::get_param(called, index as u32);
865868
let name = llvm::get_value_name(p);
866-
let arg_name = format!("{name}.addr");
867-
let alloca = unsafe {llvm::LLVMBuildAlloca(builder.llbuilder, in_ty, arg_name)};
868-
// get function arg, store it into the alloca, and read it.
869+
let name = str::from_utf8(name).unwrap();
870+
let arg_name = CString::new(format!("{name}.addr")).unwrap();
871+
let alloca = unsafe {llvm::LLVMBuildAlloca(builder.llbuilder, in_ty, arg_name.as_ptr())};
872+
builder.store(p, alloca, Align::EIGHT);
873+
let val = builder.load(in_ty, alloca, Align::EIGHT);
874+
let gep = builder.inbounds_gep(cx.type_f32(), val, &[i32_0]);
875+
vals.push(val);
876+
geps.push(gep);
869877
}
870878

871879

872880
// Step 1)
873881
unsafe {llvm::LLVMRustPositionBefore(builder.llbuilder, kernel_call)};
882+
for i in 0..num_args {
883+
let idx = cx.get_const_i32(i);
884+
let gep1 = builder.inbounds_gep(ty, a1, &[i32_0, idx]);
885+
builder.store(vals[i as usize], gep1, Align::EIGHT);
886+
let gep2 = builder.inbounds_gep(ty, a2, &[i32_0, idx]);
887+
builder.store(geps[i as usize], gep2, Align::EIGHT);
888+
let gep3 = builder.inbounds_gep(ty2, a4, &[i32_0, idx]);
889+
builder.store(cx.get_const_i64(1024), gep3, Align::EIGHT);
890+
}
874891

875892
// Step 2)
876-
let i32_0 = cx.get_const_i32(0);
877893
let gep1 = builder.inbounds_gep(ty, a1, &[i32_0, i32_0]);
878894
let gep2 = builder.inbounds_gep(ty, a2, &[i32_0, i32_0]);
879895
let gep3 = builder.inbounds_gep(ty2, a4, &[i32_0, i32_0]);

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,29 @@ impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
153153
}
154154
}
155155

156+
pub(crate) fn store(
157+
&mut self,
158+
val: &'ll Value,
159+
ptr: &'ll Value,
160+
align: Align,
161+
) -> &'ll Value {
162+
debug!("Store {:?} -> {:?}", val, ptr);
163+
assert_eq!(self.cx.type_kind(self.cx.val_ty(ptr)), TypeKind::Pointer);
164+
unsafe {
165+
let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
166+
llvm::LLVMSetAlignment(store, align.bytes() as c_uint);
167+
store
168+
}
169+
}
170+
171+
pub(crate) fn load(&mut self, ty: &'ll Type, ptr: &'ll Value, align: Align) -> &'ll Value {
172+
unsafe {
173+
let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
174+
llvm::LLVMSetAlignment(load, align.bytes() as c_uint);
175+
load
176+
}
177+
}
178+
156179
}
157180

158181
/// Empty string, to be used where LLVM expects an instruction name, indicating

0 commit comments

Comments
 (0)