Skip to content

Commit 2413d2e

Browse files
committed
build under older LLVM's, handle rust mangling
1 parent 6e6d240 commit 2413d2e

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

enzyme/Enzyme/GradientUtils.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ llvm::cl::opt<bool>
127127
llvm::cl::opt<bool>
128128
EnzymePrintDiffUse("enzyme-print-diffuse", cl::init(false), cl::Hidden,
129129
cl::desc("Print differential use analysis"));
130+
131+
llvm::cl::opt<std::string>
132+
EnzymeRustDeallocName("rust-dealloc-name", cl::init(""), cl::Hidden,
133+
cl::desc("Name of Rust deallocation function"));
130134
}
131135

132136
SmallVector<unsigned int, 9> MD_ToCopy = {
@@ -9473,7 +9477,8 @@ llvm::CallInst *freeKnownAllocation(llvm::IRBuilder<> &builder,
94739477
llvm::CallInst *orig,
94749478
GradientUtils *gutils) {
94759479
assert(isAllocationFunction(allocationfn, TLI));
9476-
9480+
9481+
#if LLVM_VERSION_MAJOR >= 17
94779482
std::string demangledName = llvm::demangle(allocationfn);
94789483
if (demangledName == "__rustc::__rust_alloc" || demangledName == "__rustc::__rust_alloc_zeroed" ) {
94799484
Type *VoidTy = Type::getVoidTy(tofree->getContext());
@@ -9482,10 +9487,26 @@ llvm::CallInst *freeKnownAllocation(llvm::IRBuilder<> &builder,
94829487
Type *inTys[3] = {IntPtrTy, RustSz, RustSz};
94839488

94849489
auto FT = FunctionType::get(VoidTy, inTys, false);
9490+
if (EnzymeRustDeallocName == "") {
9491+
// If we aren't given the deallocator name, try to find it ourselves.
9492+
// Except of no-std code, we are almost guaranteed to find one.
9493+
for (auto &F : *builder.GetInsertBlock()->getParent()->getParent()) {
9494+
auto demangledName = llvm::demangle(F.getName());
9495+
if (demangledName == "__rustc::__rust_dealloc") {
9496+
EnzymeRustDeallocName = F.getName();
9497+
break;
9498+
}
9499+
}
9500+
if (EnzymeRustDeallocName == "") {
9501+
// If we still can't find it, use the raw __rust_dealloc as a fallback.
9502+
EnzymeRustDeallocName = "__rust_dealloc";
9503+
// FIXME: Make this a hard error once we pass the right name from rustc.
9504+
}
9505+
}
94859506
Value *freevalue = builder.GetInsertBlock()
94869507
->getParent()
94879508
->getParent()
9488-
->getOrInsertFunction("__rust_dealloc", FT)
9509+
->getOrInsertFunction(EnzymeRustDeallocName, FT)
94899510
.getCallee();
94909511
Value *vals[3];
94919512
vals[0] = builder.CreatePointerCast(tofree, IntPtrTy);
@@ -9509,6 +9530,7 @@ llvm::CallInst *freeKnownAllocation(llvm::IRBuilder<> &builder,
95099530
builder.Insert(freecall);
95109531
return freecall;
95119532
}
9533+
#endif
95129534
if (allocationfn == "julia.gc_alloc_obj" ||
95139535
allocationfn == "jl_gc_alloc_typed" ||
95149536
allocationfn == "ijl_gc_alloc_typed" ||

enzyme/Enzyme/LibraryFuncs.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern llvm::StringMap<
4242
std::function<llvm::CallInst *(llvm::IRBuilder<> &, llvm::Value *)>>
4343
shadowErasers;
4444

45+
4546
/// Return whether a given function is a known C/C++ memory allocation function
4647
/// For updating below one should read MemoryBuiltins.cpp, TargetLibraryInfo.cpp
4748
static inline bool isAllocationFunction(const llvm::StringRef name,
@@ -56,9 +57,11 @@ static inline bool isAllocationFunction(const llvm::StringRef name,
5657
return true;
5758
if (name == "__size_returning_new_experiment")
5859
return true;
60+
#if LLVM_VERSION_MAJOR >= 17
5961
std::string demangledName = llvm::demangle(name.str());
6062
if (demangledName == "__rustc::__rust_alloc" || demangledName == "__rustc::__rust_alloc_zeroed")
6163
return true;
64+
#endif
6265
if (name == "julia.gc_alloc_obj" || name == "jl_gc_alloc_typed" ||
6366
name == "ijl_gc_alloc_typed")
6467
return true;
@@ -210,8 +213,12 @@ static inline void zeroKnownAllocation(llvm::IRBuilder<> &bb,
210213
assert(isAllocationFunction(funcName, TLI));
211214

212215
// Don't re-zero an already-zero buffer
216+
#if LLVM_VERSION_MAJOR >= 17
213217
std::string demangledName = llvm::demangle(funcName.str());
214-
if (funcName == "calloc" || demangledName == "__rustc::__rust_alloc_zeroed")
218+
if (demangledName == "__rustc::__rust_alloc_zeroed")
219+
return;
220+
#endif
221+
if (funcName == "calloc")
215222
return;
216223

217224
Value *allocSize = argValues[0];

0 commit comments

Comments
 (0)