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

Generic memory opts #2151

Open
wants to merge 6 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
28 changes: 25 additions & 3 deletions enzyme/Enzyme/JLInstSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ bool notCapturedBefore(llvm::Value *V, Instruction *inst) {
if (isa<CmpInst>(UI)) {
continue;
}
if (isa<SelectInst>(UI) || isa<PHINode>(UI)) {
todo.push_back(UI);
continue;
}
if (isa<LoadInst>(UI)) {
todo.push_back(UI);
continue;
Expand Down Expand Up @@ -159,7 +163,22 @@ bool jlInstSimplify(llvm::Function &F, TargetLibraryInfo &TLI,

bool legal = false;
ICmpInst::Predicate pred;
if (auto cmp = dyn_cast<ICmpInst>(&I)) {
if (auto LI = dyn_cast<LoadInst>(&I)) {
size_t offset = 0;
auto obj = getBaseObject(LI->getPointerOperand(),
/*offsetAllowed=*/true, &offset);
if (auto CI = dyn_cast<CallBase>(obj)) {
if (getFuncNameFromCall(CI) == "jl_alloc_genericmemory") {
if (offset == 0) {
// Size
LI->replaceAllUsesWith(CI->getArgOperand(1));
} else if (offset ==
F.getParent()->getDataLayout().getIndexSize(0) * 1) {
// Underlying data pointer
}
}
}
} else if (auto cmp = dyn_cast<ICmpInst>(&I)) {
pred = cmp->getPredicate();
legal = true;
} else if (auto CI = dyn_cast<CallBase>(&I)) {
Expand Down Expand Up @@ -199,8 +218,11 @@ bool jlInstSimplify(llvm::Function &F, TargetLibraryInfo &TLI,
}
auto llhs = dyn_cast<LoadInst>(lhs);
auto lrhs = dyn_cast<LoadInst>(rhs);
if (llhs && lrhs && isa<PointerType>(llhs->getType()) &&
isa<PointerType>(lrhs->getType())) {
if (llhs && lrhs &&
(isa<PointerType>(llhs->getType()) ||
isa<IntegerType>(llhs->getType())) &&
(isa<PointerType>(lrhs->getType()) ||
isa<IntegerType>(lrhs->getType()))) {
auto lhsv =
getBaseObject(llhs->getOperand(0), /*offsetAllowed*/ false);
auto rhsv =
Expand Down
34 changes: 28 additions & 6 deletions enzyme/Enzyme/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1377,19 +1377,39 @@ static inline bool isPointerArithmeticInst(const llvm::Value *V,
}

static inline llvm::Value *getBaseObject(llvm::Value *V,
bool offsetAllowed = true) {
bool offsetAllowed = true,
size_t *offset = nullptr) {
if (offset)
*offset = 0;
while (true) {
if (auto CI = llvm::dyn_cast<llvm::CastInst>(V)) {
V = CI->getOperand(0);
continue;
} else if (auto CI = llvm::dyn_cast<llvm::GetElementPtrInst>(V)) {
if (offsetAllowed || CI->hasAllZeroIndices()) {
if (CI->hasAllZeroIndices()) {
V = CI->getOperand(0);
continue;
}
if (offsetAllowed) {
if (offset) {
llvm::APInt ai(8 * sizeof(size_t), 0);
if (CI->accumulateConstantOffset(
CI->getParent()->getParent()->getParent()->getDataLayout(),
ai)) {
*offset += ai.getLimitedValue();
} else {
break;
}
}
V = CI->getOperand(0);
continue;
}
} else if (auto II = llvm::dyn_cast<llvm::IntrinsicInst>(V);
II && isIntelSubscriptIntrinsic(*II)) {
if (offsetAllowed) {
if (offset) {
break;
}
V = II->getOperand(3);
continue;
}
Expand All @@ -1412,7 +1432,8 @@ static inline llvm::Value *getBaseObject(llvm::Value *V,
auto funcName = getFuncNameFromCall(Call);
auto AttrList = Call->getAttributes().getAttributes(
llvm::AttributeList::FunctionIndex);
if (AttrList.hasAttribute("enzyme_pointermath") && offsetAllowed) {
if (AttrList.hasAttribute("enzyme_pointermath") && offsetAllowed &&
!offset) {
size_t res = 0;
bool failed = AttrList.getAttribute("enzyme_pointermath")
.getValueAsString()
Expand Down Expand Up @@ -1448,7 +1469,8 @@ static inline llvm::Value *getBaseObject(llvm::Value *V,
if (auto fn = getFunctionFromCall(Call)) {
auto AttrList = fn->getAttributes().getAttributes(
llvm::AttributeList::FunctionIndex);
if (AttrList.hasAttribute("enzyme_pointermath") && offsetAllowed) {
if (AttrList.hasAttribute("enzyme_pointermath") && offsetAllowed &&
!offset) {
size_t res = 0;
bool failed = AttrList.getAttribute("enzyme_pointermath")
.getValueAsString()
Expand Down Expand Up @@ -1478,15 +1500,15 @@ static inline llvm::Value *getBaseObject(llvm::Value *V,
// because it should be in sync with CaptureTracking. Not using it may
// cause weird miscompilations where 2 aliasing pointers are assumed to
// noalias.
if (offsetAllowed)
if (offsetAllowed && !offset)
if (auto *RP =
llvm::getArgumentAliasingToReturnedPointer(Call, false)) {
V = RP;
continue;
}
}

if (offsetAllowed)
if (offsetAllowed && !offset)
if (auto I = llvm::dyn_cast<llvm::Instruction>(V)) {
#if LLVM_VERSION_MAJOR >= 12
auto V2 = llvm::getUnderlyingObject(I, 100);
Expand Down
Loading