Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addr11 #887

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions enzyme/BCLoad/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ ExternalProject_Add(openblas
set_target_properties(openblas PROPERTIES EXCLUDE_FROM_ALL TRUE)

ExternalProject_Add(fblas
GIT_REPOSITORY https://github.com/UCSantaCruzComputationalGenomicsLab/clapack
GIT_TAG 8bac8d5cd7aa8506b11cdb2cfa2ce8a2e03048f3
GIT_REPOSITORY https://github.com/EnzymeAD/clapack
GIT_TAG 42603bf49ac2b27a7c00c53f92fa648748476510
PREFIX fblas
BUILD_IN_SOURCE 1
INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/openblas/install
Expand Down
28 changes: 23 additions & 5 deletions enzyme/Enzyme/CacheUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,10 +795,10 @@ AllocaInst *CacheUtility::createCacheForScope(LimitContext ctx, Type *T,
auto P = B.CreatePHI(i64, 1);

CallInst *malloccall;
allocType = cast<PointerType>(CreateAllocation(B, types.back(), P,
Value *A = CreateAllocation(B, types.back(), P,
"tmpfortypecalc",
&malloccall, nullptr)
->getType());
&malloccall, nullptr);
allocType = cast<PointerType>(A->getType());
malloctypes.push_back(cast<PointerType>(malloccall->getType()));
SmallVector<Instruction *, 2> toErase;
for (auto &I : *BB)
Expand Down Expand Up @@ -908,6 +908,7 @@ AllocaInst *CacheUtility::createCacheForScope(LimitContext ctx, Type *T,
}
scopeInstructions[alloc].push_back(ZeroInst);
}

storealloc = allocationBuilder.CreateStore(firstallocation, storeInto);

scopeAllocs[alloc].push_back(malloccall);
Expand Down Expand Up @@ -1016,11 +1017,21 @@ AllocaInst *CacheUtility::createCacheForScope(LimitContext ctx, Type *T,
#else
cast<LoadInst>(storeInto)->setAlignment(alignSize);
#endif
storeInto = v.CreateGEP(storeInto->getType()->getPointerElementType(),
storeInto, idx);
#else
storeInto = v.CreateLoad(storeInto);
cast<LoadInst>(storeInto)->setAlignment(alignSize);
#endif

Value *storeIntoN = GetAddressableFromAllocation(v, storeInto);
if (storeInto != storeIntoN) {
scopeInstructions[alloc].push_back(cast<Instruction>(storeIntoN));
}
storeInto = storeIntoN;

#if LLVM_VERSION_MAJOR > 7
storeInto = v.CreateGEP(storeInto->getType()->getPointerElementType(),
storeInto, idx);
#else
storeInto = v.CreateGEP(storeInto, idx);
#endif
cast<GetElementPtrInst>(storeInto)->setIsInBounds(true);
Expand Down Expand Up @@ -1557,6 +1568,13 @@ Value *CacheUtility::getCachePointer(bool inForwardPass, IRBuilder<> &BuilderM,
#else
cast<LoadInst>(next)->setAlignment(align);
#endif

Value *nextN = GetAddressableFromAllocation(BuilderM, next);
if (next != nextN) {
scopeInstructions[cast<AllocaInst>(cache)].push_back(
cast<Instruction>(nextN));
}
next = nextN;

const auto &containedloops = sublimits[i].second;

Expand Down
1 change: 1 addition & 0 deletions enzyme/Enzyme/EnzymeLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2525,6 +2525,7 @@ const AugmentedReturn &EnzymeLogic::CreateAugmentedPrimal(
tapeMemory = CreateAllocation(
ib, tapeType, ConstantInt::get(i64, 1), "tapemem", &malloccall,
EnzymeZeroCache ? &zero : nullptr, /*isDefault*/ true);
tapeMemory = GetAddressableFromAllocation(ib, tapeMemory);
memory = malloccall;
} else {
memory = ConstantPointerNull::get(
Expand Down
2 changes: 2 additions & 0 deletions enzyme/Enzyme/FunctionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ UpgradeAllocasToMallocs(Function *NewF, DerivativeMode mode,
MDNode::get(CI->getContext(), {}));
}

rep = GetAddressableFromAllocation(B, rep);

auto PT0 = cast<PointerType>(rep->getType());
auto PT1 = cast<PointerType>(AI->getType());
if (PT0->getAddressSpace() != PT1->getAddressSpace()) {
Expand Down
58 changes: 32 additions & 26 deletions enzyme/Enzyme/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ llvm::SmallVector<llvm::Instruction *, 2> PostCacheStore(llvm::StoreInst *SI,
return res;
}

Value *GetAddressableFromAllocation(IRBuilder <>&B, Value* gVal) {
auto PT = cast<PointerType>(gVal->getType());
if (PT->getAddressSpace() == 10)
return B.CreateAddrSpaceCast(gVal, PointerType::get(PT->getPointerElementType(), 11));
return gVal;
}

llvm::PointerType *getDefaultAnonymousTapeType(llvm::LLVMContext &C) {
if (EnzymeDefaultTapeType)
return cast<PointerType>(unwrap(EnzymeDefaultTapeType(wrap(&C))));
Expand Down Expand Up @@ -164,28 +171,46 @@ Function *getOrInsertExponentialAllocator(Module &M, Function *newFunc,

Value *args[] = {B.CreatePointerCast(ptr, allocType), next};
gVal = B.CreateCall(reallocF, args);
if (ZeroInit) {
Value *zeroSize = B.CreateSub(next, prevSize);

Value *margs[] = {
#if LLVM_VERSION_MAJOR > 7
B.CreateInBoundsGEP(gVal->getType()->getPointerElementType(), gVal,
prevSize),
#else
B.CreateInBoundsGEP(gVal, prevSize),
#endif
ConstantInt::get(Type::getInt8Ty(M.getContext()), 0),
zeroSize,
ConstantInt::getFalse(M.getContext())
};
Type *tys[] = {margs[0]->getType(), margs[2]->getType()};
auto memsetF = Intrinsic::getDeclaration(&M, Intrinsic::memset, tys);
B.CreateCall(memsetF, margs);
}
} else {
Value *tsize = ConstantInt::get(
next->getType(),
newFunc->getParent()->getDataLayout().getTypeAllocSizeInBits(RT) / 8);
auto elSize = B.CreateUDiv(next, tsize, "", /*isExact*/ true);
Instruction *SubZero = nullptr;
gVal = CreateAllocation(B, RT, elSize, "", nullptr, &SubZero);
Value *tmp = GetAddressableFromAllocation(B, gVal);

gVal = B.CreatePointerCast(
gVal, PointerType::get(
Type::getInt8Ty(gVal->getContext()),
cast<PointerType>(gVal->getType())->getAddressSpace()));
auto pVal = B.CreatePointerCast(ptr, gVal->getType());
tmp = B.CreatePointerCast(
tmp, PointerType::get(
Type::getInt8Ty(tmp->getContext()),
cast<PointerType>(tmp->getType())->getAddressSpace()));
auto pVal = B.CreatePointerCast(ptr, tmp->getType());

Value *margs[] = {gVal, pVal, prevSize,
Value *margs[] = {tmp, pVal, prevSize,
ConstantInt::getFalse(M.getContext())};
Type *tys[] = {margs[0]->getType(), margs[1]->getType(),
margs[2]->getType()};
auto memsetF = Intrinsic::getDeclaration(&M, Intrinsic::memcpy, tys);
B.CreateCall(memsetF, margs);
if (SubZero) {
ZeroInit = false;
IRBuilder<> BB(SubZero);
Value *zeroSize = BB.CreateSub(next, prevSize);
Value *tmp = SubZero->getOperand(0);
Expand All @@ -200,25 +225,6 @@ Function *getOrInsertExponentialAllocator(Module &M, Function *newFunc,
SubZero->setOperand(2, zeroSize);
}
}

if (ZeroInit) {
Value *zeroSize = B.CreateSub(next, prevSize);

Value *margs[] = {
#if LLVM_VERSION_MAJOR > 7
B.CreateInBoundsGEP(gVal->getType()->getPointerElementType(), gVal,
prevSize),
#else
B.CreateInBoundsGEP(gVal, prevSize),
#endif
ConstantInt::get(Type::getInt8Ty(M.getContext()), 0),
zeroSize,
ConstantInt::getFalse(M.getContext())
};
Type *tys[] = {margs[0]->getType(), margs[2]->getType()};
auto memsetF = Intrinsic::getDeclaration(&M, Intrinsic::memset, tys);
B.CreateCall(memsetF, margs);
}
gVal = B.CreatePointerCast(gVal, ptr->getType());

B.CreateBr(ok);
Expand Down
2 changes: 2 additions & 0 deletions enzyme/Enzyme/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ llvm::Value *CreateAllocation(llvm::IRBuilder<> &B, llvm::Type *T,
bool isDefault = false);
llvm::CallInst *CreateDealloc(llvm::IRBuilder<> &B, llvm::Value *ToFree);

llvm::Value *GetAddressableFromAllocation(llvm::IRBuilder <>&B, llvm::Value* gVal);

llvm::Value *CreateReAllocation(llvm::IRBuilder<> &B, llvm::Value *prev,
llvm::Type *T, llvm::Value *OuterCount,
llvm::Value *InnerCount, llvm::Twine Name = "",
Expand Down