Skip to content

Commit d418c11

Browse files
committed
update handling of rust (de)alloc fn to use demangled names
1 parent 4d82802 commit d418c11

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

enzyme/Enzyme/GradientUtils.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
#include "llvm/Support/ErrorHandling.h"
5959
#include "llvm/Support/TimeProfiler.h"
6060

61+
#include "llvm/Demangle/Demangle.h"
62+
6163
#if LLVM_VERSION_MAJOR >= 14
6264
#define addAttribute addAttributeAtIndex
6365
#define hasAttribute hasAttributeAtIndex
@@ -9472,7 +9474,8 @@ llvm::CallInst *freeKnownAllocation(llvm::IRBuilder<> &builder,
94729474
GradientUtils *gutils) {
94739475
assert(isAllocationFunction(allocationfn, TLI));
94749476

9475-
if (allocationfn == "__rust_alloc" || allocationfn == "__rust_alloc_zeroed") {
9477+
std::string demangledName = llvm::demangle(allocationfn);
9478+
if (demangledName == "__rustc::__rust_alloc" || demangledName == "__rustc::__rust_alloc_zeroed" ) {
94769479
Type *VoidTy = Type::getVoidTy(tofree->getContext());
94779480
Type *IntPtrTy = orig->getType();
94789481
Type *RustSz = orig->getArgOperand(0)->getType();

enzyme/Enzyme/LibraryFuncs.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <llvm/ADT/StringMap.h>
2727
#include <llvm/Analysis/AliasAnalysis.h>
2828
#include <llvm/Analysis/TargetLibraryInfo.h>
29+
#include "llvm/Demangle/Demangle.h"
2930
#include <llvm/IR/IRBuilder.h>
3031
#include <llvm/IR/InlineAsm.h>
3132
#include <llvm/IR/Instructions.h>
@@ -55,7 +56,8 @@ static inline bool isAllocationFunction(const llvm::StringRef name,
5556
return true;
5657
if (name == "__size_returning_new_experiment")
5758
return true;
58-
if (name == "__rust_alloc" || name == "__rust_alloc_zeroed")
59+
std::string demangledName = llvm::demangle(name.str());
60+
if (demangledName == "__rustc::__rust_alloc" || demangledName == "__rustc::__rust_alloc_zeroed")
5961
return true;
6062
if (name == "julia.gc_alloc_obj" || name == "jl_gc_alloc_typed" ||
6163
name == "ijl_gc_alloc_typed")
@@ -131,7 +133,8 @@ static inline bool isDeallocationFunction(const llvm::StringRef name,
131133
return true;
132134
if (name == "_mlir_memref_to_llvm_free")
133135
return true;
134-
if (name == "__rust_dealloc")
136+
std::string demangledName = llvm::demangle(name.str());
137+
if (demangledName == "__rustc::__rust_dealloc")
135138
return true;
136139
if (name == "swift_release")
137140
return true;
@@ -207,7 +210,8 @@ static inline void zeroKnownAllocation(llvm::IRBuilder<> &bb,
207210
assert(isAllocationFunction(funcName, TLI));
208211

209212
// Don't re-zero an already-zero buffer
210-
if (funcName == "calloc" || funcName == "__rust_alloc_zeroed")
213+
std::string demangledName = llvm::demangle(funcName.str());
214+
if (funcName == "calloc" || demangledName == "__rustc::__rust_alloc_zeroed")
211215
return;
212216

213217
Value *allocSize = argValues[0];

0 commit comments

Comments
 (0)