Skip to content

Commit 3dc0c3b

Browse files
committed
various wrappers
1 parent 22a499f commit 3dc0c3b

File tree

6 files changed

+154
-2
lines changed

6 files changed

+154
-2
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

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

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

compiler/rustc_codegen_llvm/src/common.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
119119
r
120120
}
121121
}
122+
123+
pub(crate) fn const_null(&self, t: &'ll Type) -> &'ll Value {
124+
unsafe { llvm::LLVMConstNull(t) }
125+
}
122126
}
123127

124128
impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
@@ -373,6 +377,11 @@ pub(crate) fn bytes_in_context<'ll>(llcx: &'ll llvm::Context, bytes: &[u8]) -> &
373377
}
374378
}
375379

380+
pub(crate) fn named_struct<'ll>(ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
381+
let len = c_uint::try_from(elts.len()).expect("LLVMConstStructInContext elements len overflow");
382+
unsafe { llvm::LLVMConstNamedStruct(ty, elts.as_ptr(), len) }
383+
}
384+
376385
fn struct_in_context<'ll>(
377386
llcx: &'ll llvm::Context,
378387
elts: &[&'ll Value],

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,13 +1004,22 @@ unsafe extern "C" {
10041004
SLen: c_uint,
10051005
) -> MetadataKindId;
10061006

1007-
// Create modules.
1007+
// Create, print, and destroy modules.
1008+
pub(crate) fn LLVMPrintModuleToFile(
1009+
M: &Module,
1010+
Name: *const c_char,
1011+
Error_message: *mut c_char,
1012+
);
1013+
pub(crate) fn LLVMDisposeModule(M: &Module);
10081014
pub(crate) fn LLVMModuleCreateWithNameInContext(
10091015
ModuleID: *const c_char,
10101016
C: &Context,
10111017
) -> &Module;
10121018
pub(crate) fn LLVMCloneModule(M: &Module) -> &Module;
10131019

1020+
pub(crate) fn LLVMGetTarget(M: &Module) -> *const c_char;
1021+
pub(crate) fn LLVMSetTarget(M: &Module, Name: *const c_char);
1022+
10141023
/// Data layout. See Module::getDataLayout.
10151024
pub(crate) fn LLVMGetDataLayoutStr(M: &Module) -> *const c_char;
10161025
pub(crate) fn LLVMSetDataLayout(M: &Module, Triple: *const c_char);
@@ -1138,6 +1147,11 @@ unsafe extern "C" {
11381147
Count: c_uint,
11391148
Packed: Bool,
11401149
) -> &'a Value;
1150+
pub(crate) fn LLVMConstNamedStruct<'a>(
1151+
StructTy: &'a Type,
1152+
ConstantVals: *const &'a Value,
1153+
Count: c_uint,
1154+
) -> &'a Value;
11411155
pub(crate) fn LLVMConstVector(ScalarConstantVals: *const &Value, Size: c_uint) -> &Value;
11421156

11431157
// Constant expressions
@@ -1217,6 +1231,8 @@ unsafe extern "C" {
12171231
) -> &'a BasicBlock;
12181232

12191233
// Operations on instructions
1234+
pub(crate) fn LLVMGetInstructionParent(Inst: &Value) -> &BasicBlock;
1235+
pub(crate) fn LLVMGetCalledValue(CallInst: &Value) -> Option<&Value>;
12201236
pub(crate) fn LLVMIsAInstruction(Val: &Value) -> Option<&Value>;
12211237
pub(crate) fn LLVMGetFirstBasicBlock(Fn: &Value) -> &BasicBlock;
12221238
pub(crate) fn LLVMGetOperand(Val: &Value, Index: c_uint) -> Option<&Value>;
@@ -2562,6 +2578,7 @@ unsafe extern "C" {
25622578

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

2581+
pub(crate) fn LLVMRustPositionBuilderPastAllocas<'a>(B: &Builder<'a>, Fn: &'a Value);
25652582
pub(crate) fn LLVMRustPositionBuilderAtStart<'a>(B: &Builder<'a>, BB: &'a BasicBlock);
25662583

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

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,12 +1591,50 @@ 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+
llvm::errs() << "Found call: " << *callInst << "\n";
1629+
return wrap(callInst);
1630+
}
1631+
}
1632+
}
1633+
}
1634+
1635+
return nullptr;
1636+
}
1637+
16001638
extern "C" bool LLVMRustConstIntGetZExtValue(LLVMValueRef CV, uint64_t *value) {
16011639
auto C = unwrap<llvm::ConstantInt>(CV);
16021640
if (C->getBitWidth() > 64)

0 commit comments

Comments
 (0)