@@ -903,12 +903,9 @@ class GradientUtils {
903
903
if (!getContext (blk, idx)) {
904
904
break ;
905
905
}
906
+ llvm::errs () << " adding to contexts: " << idx.header ->getName () << " starting ctx=" << ctx->getName () << " \n " ;
906
907
contexts.emplace_back (idx);
907
- if (idx.parent ) {
908
- blk = idx.parent ->getHeader ();
909
- } else {
910
- blk = nullptr ;
911
- }
908
+ blk = idx.preheader ;
912
909
}
913
910
914
911
std::vector<BasicBlock*> allocationPreheaders (contexts.size (), nullptr );
@@ -964,14 +961,16 @@ class GradientUtils {
964
961
size = allocationBuilder.CreateNUWMul (size, limits[i]);
965
962
}
966
963
964
+ llvm::errs () << " considering ctx " << ctx->getName () << " alph=" << allocationPreheaders[i]->getName () << " ctxheader=" << contexts[i].header ->getName () << " \n " ;
967
965
if (contexts[i].dynamic ) {
966
+ llvm::errs () << " starting outermost ph at " << allocationPreheaders[i]->getName () << " |ctx=" << ctx->getName () <<" \n " ;
968
967
sublimits.push_back (std::make_pair (size, lims));
969
968
size = nullptr ;
970
- break ;
971
969
}
972
970
}
973
971
974
972
if (size != nullptr ) {
973
+ llvm::errs () << " starting final outermost ph at " << allocationPreheaders[contexts.size ()-1 ]->getName ()<<" |ctx=" << ctx->getName () << " \n " ;
975
974
sublimits.push_back (std::make_pair (size, lims));
976
975
}
977
976
return sublimits;
@@ -994,13 +993,21 @@ class GradientUtils {
994
993
IRBuilder<> entryBuilder (inversionAllocs);
995
994
entryBuilder.setFastMathFlags (getFast ());
996
995
AllocaInst* alloc = entryBuilder.CreateAlloca (types.back (), nullptr , name+" _cache" );
996
+ llvm::errs () << " alloc: " << *alloc << " \n " ;
997
997
998
998
Type *BPTy = Type::getInt8PtrTy (ctx->getContext ());
999
999
auto realloc = newFunc->getParent ()->getOrInsertFunction (" realloc" , BPTy, BPTy, Type::getInt64Ty (ctx->getContext ()));
1000
1000
1001
1001
Value* storeInto = alloc;
1002
+ ValueToValueMapTy antimap;
1003
+
1002
1004
for (int i=sublimits.size ()-1 ; i>=0 ; i--) {
1003
1005
const auto & containedloops = sublimits[i].second ;
1006
+ for (auto riter = containedloops.rbegin (), rend = containedloops.rend (); riter != rend; riter++) {
1007
+ const auto & idx = riter->first ;
1008
+ antimap[idx.var ] = idx.antivar ;
1009
+ }
1010
+
1004
1011
Value* size = sublimits[i].first ;
1005
1012
Type* myType = types[i];
1006
1013
@@ -1027,7 +1034,9 @@ class GradientUtils {
1027
1034
// allocationBuilder.GetInsertBlock()->getInstList().push_back(cast<Instruction>(allocation));
1028
1035
// cast<Instruction>(firstallocation)->moveBefore(allocationBuilder.GetInsertBlock()->getTerminator());
1029
1036
// mallocs.push_back(firstallocation);
1030
- } else {
1037
+ } else {
1038
+ llvm::errs () << " storeInto: " << *storeInto << " \n " ;
1039
+ llvm::errs () << " myType: " << *myType << " \n " ;
1031
1040
allocationBuilder.CreateStore (ConstantPointerNull::get (PointerType::getUnqual (myType)), storeInto);
1032
1041
1033
1042
IRBuilder <> build (containedloops.back ().first .header ->getFirstNonPHI ());
@@ -1060,7 +1069,7 @@ class GradientUtils {
1060
1069
tbuild.SetInsertPoint (tbuild.GetInsertBlock ()->getFirstNonPHI ());
1061
1070
}
1062
1071
1063
- auto ci = cast<CallInst>(CallInst::CreateFree (tbuild.CreatePointerCast (tbuild.CreateLoad (storeInto), Type::getInt8PtrTy (ctx->getContext ())), tbuild.GetInsertBlock ()));
1072
+ auto ci = cast<CallInst>(CallInst::CreateFree (tbuild.CreatePointerCast (tbuild.CreateLoad (unwrapM ( storeInto, tbuild, antimap, /* lookup */ false ) ), Type::getInt8PtrTy (ctx->getContext ())), tbuild.GetInsertBlock ()));
1064
1073
ci->addAttribute (AttributeList::FirstArgIndex, Attribute::NonNull);
1065
1074
if (ci->getParent ()==nullptr ) {
1066
1075
tbuild.Insert (ci);
@@ -1072,9 +1081,9 @@ class GradientUtils {
1072
1081
IRBuilder <>v (&sublimits[i-1 ].second .back ().first .preheader ->back ());
1073
1082
// TODO
1074
1083
if (!sublimits[i].second .back ().first .dynamic ) {
1075
- storeInto = v.CreateLoad (v. CreateGEP (v.CreateLoad (storeInto), sublimits[i].second .back ().first .var ) );
1084
+ storeInto = v.CreateGEP (v.CreateLoad (storeInto), sublimits[i].second .back ().first .var );
1076
1085
} else {
1077
- storeInto = v.CreateLoad (v. CreateGEP (v.CreateLoad (storeInto), sublimits[i].second .back ().first .var ) );
1086
+ storeInto = v.CreateGEP (v.CreateLoad (storeInto), sublimits[i].second .back ().first .var );
1078
1087
}
1079
1088
}
1080
1089
}
0 commit comments