@@ -538,7 +538,6 @@ Function *getOrInsertExponentialAllocator(Module &M, bool ZeroInit) {
538
538
std::string name = " __enzyme_exponentialallocation" ;
539
539
if (ZeroInit)
540
540
name += " zero" ;
541
- assert (!ZeroInit && " Zero initialization within reallocation not handled" );
542
541
FunctionType *FT =
543
542
FunctionType::get (Type::getInt8PtrTy (M.getContext ()), types, false );
544
543
@@ -596,6 +595,22 @@ Function *getOrInsertExponentialAllocator(Module &M, bool ZeroInit) {
596
595
Value *args[] = {B.CreatePointerCast (ptr, BPTy), next};
597
596
Value *gVal =
598
597
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
+ }
599
614
600
615
B.CreateBr (ok);
601
616
B.SetInsertPoint (ok);
0 commit comments