Skip to content

Commit 7647459

Browse files
committed
build under older LLVM's, handle rust mangling
1 parent 6a5a28b commit 7647459

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

enzyme/Enzyme/GradientUtils.cpp

Lines changed: 23 additions & 1 deletion
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 = {
@@ -9474,6 +9478,7 @@ llvm::CallInst *freeKnownAllocation(llvm::IRBuilder<> &builder,
94749478
GradientUtils *gutils) {
94759479
assert(isAllocationFunction(allocationfn, TLI));
94769480

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+
// Rust's (de)alloc names aren't stable. We expect rustc to set them
9492+
// for us, but if it fails to do so we instead search for it here.
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 can't find it, use the raw __rust_dealloc as a fallback.
9502+
// FIXME: Make this a hard error once we pass the right name from rustc.
9503+
EnzymeRustDeallocName = "__rust_dealloc";
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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ static inline bool isAllocationFunction(const llvm::StringRef name,
5656
return true;
5757
if (name == "__size_returning_new_experiment")
5858
return true;
59+
#if LLVM_VERSION_MAJOR >= 17
5960
std::string demangledName = llvm::demangle(name.str());
6061
if (demangledName == "__rustc::__rust_alloc" || demangledName == "__rustc::__rust_alloc_zeroed")
6162
return true;
63+
#endif
6264
if (name == "julia.gc_alloc_obj" || name == "jl_gc_alloc_typed" ||
6365
name == "ijl_gc_alloc_typed")
6466
return true;
@@ -210,8 +212,12 @@ static inline void zeroKnownAllocation(llvm::IRBuilder<> &bb,
210212
assert(isAllocationFunction(funcName, TLI));
211213

212214
// Don't re-zero an already-zero buffer
215+
#if LLVM_VERSION_MAJOR >= 17
213216
std::string demangledName = llvm::demangle(funcName.str());
214-
if (funcName == "calloc" || demangledName == "__rustc::__rust_alloc_zeroed")
217+
if (demangledName == "__rustc::__rust_alloc_zeroed")
218+
return;
219+
#endif
220+
if (funcName == "calloc")
215221
return;
216222

217223
Value *allocSize = argValues[0];

0 commit comments

Comments
 (0)