Skip to content

Commit a16fe1b

Browse files
authored
Zero initialize in expontential allocation (rust-lang#387)
1 parent eac6409 commit a16fe1b

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

enzyme/Enzyme/Utils.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,6 @@ Function *getOrInsertExponentialAllocator(Module &M, bool ZeroInit) {
538538
std::string name = "__enzyme_exponentialallocation";
539539
if (ZeroInit)
540540
name += "zero";
541-
assert(!ZeroInit && "Zero initialization within reallocation not handled");
542541
FunctionType *FT =
543542
FunctionType::get(Type::getInt8PtrTy(M.getContext()), types, false);
544543

@@ -596,6 +595,22 @@ Function *getOrInsertExponentialAllocator(Module &M, bool ZeroInit) {
596595
Value *args[] = {B.CreatePointerCast(ptr, BPTy), next};
597596
Value *gVal =
598597
B.CreatePointerCast(B.CreateCall(reallocF, args), ptr->getType());
598+
if (ZeroInit) {
599+
Value *prevSize = B.CreateSelect(
600+
B.CreateICmpEQ(size, ConstantInt::get(size->getType(), 1)),
601+
ConstantInt::get(next->getType(), 0),
602+
B.CreateLShr(next, ConstantInt::get(next->getType(), 1)));
603+
604+
Value *zeroSize = B.CreateSub(next, prevSize);
605+
606+
Value *margs[] = {
607+
B.CreateGEP(gVal, prevSize),
608+
ConstantInt::get(Type::getInt8Ty(args[0]->getContext()), 0), zeroSize,
609+
ConstantInt::getFalse(args[0]->getContext())};
610+
Type *tys[] = {margs[0]->getType(), margs[2]->getType()};
611+
auto memsetF = Intrinsic::getDeclaration(&M, Intrinsic::memset, tys);
612+
B.CreateCall(memsetF, margs);
613+
}
599614

600615
B.CreateBr(ok);
601616
B.SetInsertPoint(ok);

0 commit comments

Comments
 (0)