Skip to content

Commit f89744f

Browse files
committed
add various wrappers for gpu code generation
1 parent 22a499f commit f89744f

File tree

5 files changed

+136
-2
lines changed

5 files changed

+136
-2
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::ops::Deref;
33
use std::{iter, ptr};
44

55
pub(crate) mod autodiff;
6+
pub(crate) mod gpu_offload;
67

78
use libc::{c_char, c_uint, size_t};
89
use rustc_abi as abi;
@@ -117,6 +118,70 @@ impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
117118
}
118119
bx
119120
}
121+
122+
pub(crate) fn my_alloca2(&mut self, ty: &'ll Type, align: Align, name: &str) -> &'ll Value {
123+
let val = unsafe {
124+
let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED);
125+
llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
126+
// Cast to default addrspace if necessary
127+
llvm::LLVMBuildPointerCast(self.llbuilder, alloca, self.cx.type_ptr(), UNNAMED)
128+
};
129+
if name != "" {
130+
let name = std::ffi::CString::new(name).unwrap();
131+
llvm::set_value_name(val, &name.as_bytes());
132+
}
133+
val
134+
}
135+
136+
pub(crate) fn inbounds_gep(
137+
&mut self,
138+
ty: &'ll Type,
139+
ptr: &'ll Value,
140+
indices: &[&'ll Value],
141+
) -> &'ll Value {
142+
unsafe {
143+
llvm::LLVMBuildGEPWithNoWrapFlags(
144+
self.llbuilder,
145+
ty,
146+
ptr,
147+
indices.as_ptr(),
148+
indices.len() as c_uint,
149+
UNNAMED,
150+
GEPNoWrapFlags::InBounds,
151+
)
152+
}
153+
}
154+
155+
pub(crate) fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
156+
debug!("Store {:?} -> {:?}", val, ptr);
157+
assert_eq!(self.cx.type_kind(self.cx.val_ty(ptr)), TypeKind::Pointer);
158+
unsafe {
159+
let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
160+
llvm::LLVMSetAlignment(store, align.bytes() as c_uint);
161+
store
162+
}
163+
}
164+
165+
pub(crate) fn load(&mut self, ty: &'ll Type, ptr: &'ll Value, align: Align) -> &'ll Value {
166+
unsafe {
167+
let load = llvm::LLVMBuildLoad2(self.llbuilder, ty, ptr, UNNAMED);
168+
llvm::LLVMSetAlignment(load, align.bytes() as c_uint);
169+
load
170+
}
171+
}
172+
173+
fn memset(&mut self, ptr: &'ll Value, fill_byte: &'ll Value, size: &'ll Value, align: Align) {
174+
unsafe {
175+
llvm::LLVMRustBuildMemSet(
176+
self.llbuilder,
177+
ptr,
178+
align.bytes() as c_uint,
179+
fill_byte,
180+
size,
181+
false,
182+
);
183+
}
184+
}
120185
}
121186

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

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub(crate) unsafe fn create_module<'ll>(
210210

211211
// Ensure the data-layout values hardcoded remain the defaults.
212212
{
213-
let tm = crate::back::write::create_informational_target_machine(tcx.sess, false);
213+
let tm = crate::back::write::create_informational_target_machine(sess, false);
214214
unsafe {
215215
llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm.raw());
216216
}
@@ -683,6 +683,22 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
683683
unsafe { llvm::LLVMConstInt(ty, val, llvm::False) }
684684
}
685685

686+
pub(crate) fn get_const_i64(&self, n: u64) -> &'ll Value {
687+
self.get_const_int(self.type_i64(), n)
688+
}
689+
690+
pub(crate) fn get_const_i32(&self, n: u64) -> &'ll Value {
691+
self.get_const_int(self.type_i32(), n)
692+
}
693+
694+
pub(crate) fn get_const_i16(&self, n: u64) -> &'ll Value {
695+
self.get_const_int(self.type_i16(), n)
696+
}
697+
698+
pub(crate) fn get_const_i8(&self, n: u64) -> &'ll Value {
699+
self.get_const_int(self.type_i8(), n)
700+
}
701+
686702
pub(crate) fn get_function(&self, name: &str) -> Option<&'ll Value> {
687703
let name = SmallCStr::new(name);
688704
unsafe { llvm::LLVMGetNamedFunction((**self).borrow().llmod, name.as_ptr()) }

compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use libc::{c_char, c_uint};
55

66
use super::MetadataKindId;
77
use super::ffi::{AttributeKind, BasicBlock, Metadata, Module, Type, Value};
8-
use crate::llvm::Bool;
8+
use crate::llvm::{Bool, Builder};
99

1010
#[link(name = "llvm-wrapper", kind = "static")]
1111
unsafe extern "C" {
@@ -32,6 +32,14 @@ unsafe extern "C" {
3232
index: c_uint,
3333
kind: AttributeKind,
3434
);
35+
pub(crate) fn LLVMRustPositionBefore<'a>(B: &'a Builder<'_>, I: &'a Value);
36+
pub(crate) fn LLVMRustPositionAfter<'a>(B: &'a Builder<'_>, I: &'a Value);
37+
pub(crate) fn LLVMRustGetFunctionCall(
38+
F: &Value,
39+
name: *const c_char,
40+
NameLen: libc::size_t,
41+
) -> Option<&Value>;
42+
3543
}
3644

3745
unsafe extern "C" {

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,11 @@ unsafe extern "C" {
11381138
Count: c_uint,
11391139
Packed: Bool,
11401140
) -> &'a Value;
1141+
pub(crate) fn LLVMConstNamedStruct<'a>(
1142+
StructTy: &'a Type,
1143+
ConstantVals: *const &'a Value,
1144+
Count: c_uint,
1145+
) -> &'a Value;
11411146
pub(crate) fn LLVMConstVector(ScalarConstantVals: *const &Value, Size: c_uint) -> &Value;
11421147

11431148
// Constant expressions
@@ -1217,6 +1222,8 @@ unsafe extern "C" {
12171222
) -> &'a BasicBlock;
12181223

12191224
// Operations on instructions
1225+
pub(crate) fn LLVMGetInstructionParent(Inst: &Value) -> &BasicBlock;
1226+
pub(crate) fn LLVMGetCalledValue(CallInst: &Value) -> Option<&Value>;
12201227
pub(crate) fn LLVMIsAInstruction(Val: &Value) -> Option<&Value>;
12211228
pub(crate) fn LLVMGetFirstBasicBlock(Fn: &Value) -> &BasicBlock;
12221229
pub(crate) fn LLVMGetOperand(Val: &Value, Index: c_uint) -> Option<&Value>;
@@ -2562,6 +2569,7 @@ unsafe extern "C" {
25622569

25632570
pub(crate) fn LLVMRustSetDataLayoutFromTargetMachine<'a>(M: &'a Module, TM: &'a TargetMachine);
25642571

2572+
pub(crate) fn LLVMRustPositionBuilderPastAllocas<'a>(B: &Builder<'a>, Fn: &'a Value);
25652573
pub(crate) fn LLVMRustPositionBuilderAtStart<'a>(B: &Builder<'a>, BB: &'a BasicBlock);
25662574

25672575
pub(crate) fn LLVMRustSetModulePICLevel(M: &Module);

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,12 +1591,49 @@ extern "C" LLVMValueRef LLVMRustBuildMemSet(LLVMBuilderRef B, LLVMValueRef Dst,
15911591
MaybeAlign(DstAlign), IsVolatile));
15921592
}
15931593

1594+
extern "C" void LLVMRustPositionBuilderPastAllocas(LLVMBuilderRef B,
1595+
LLVMValueRef Fn) {
1596+
Function *F = unwrap<Function>(Fn);
1597+
unwrap(B)->SetInsertPointPastAllocas(F);
1598+
}
15941599
extern "C" void LLVMRustPositionBuilderAtStart(LLVMBuilderRef B,
15951600
LLVMBasicBlockRef BB) {
15961601
auto Point = unwrap(BB)->getFirstInsertionPt();
15971602
unwrap(B)->SetInsertPoint(unwrap(BB), Point);
15981603
}
15991604

1605+
extern "C" void LLVMRustPositionBefore(LLVMBuilderRef B, LLVMValueRef Instr) {
1606+
if (auto I = dyn_cast<Instruction>(unwrap<Value>(Instr))) {
1607+
unwrap(B)->SetInsertPoint(I);
1608+
}
1609+
}
1610+
1611+
extern "C" void LLVMRustPositionAfter(LLVMBuilderRef B, LLVMValueRef Instr) {
1612+
if (auto I = dyn_cast<Instruction>(unwrap<Value>(Instr))) {
1613+
auto J = I->getNextNonDebugInstruction();
1614+
unwrap(B)->SetInsertPoint(J);
1615+
}
1616+
}
1617+
1618+
extern "C" LLVMValueRef
1619+
LLVMRustGetFunctionCall(LLVMValueRef Fn, const char *Name, size_t NameLen) {
1620+
auto targetName = StringRef(Name, NameLen);
1621+
Function *F = unwrap<Function>(Fn);
1622+
for (auto &BB : *F) {
1623+
for (auto &I : BB) {
1624+
if (auto *callInst = llvm::dyn_cast<llvm::CallBase>(&I)) {
1625+
const llvm::Function *calledFunc = callInst->getCalledFunction();
1626+
if (calledFunc && calledFunc->getName() == targetName) {
1627+
// Found a call to the target function
1628+
return wrap(callInst);
1629+
}
1630+
}
1631+
}
1632+
}
1633+
1634+
return nullptr;
1635+
}
1636+
16001637
extern "C" bool LLVMRustConstIntGetZExtValue(LLVMValueRef CV, uint64_t *value) {
16011638
auto C = unwrap<llvm::ConstantInt>(CV);
16021639
if (C->getBitWidth() > 64)

0 commit comments

Comments
 (0)