Skip to content

Commit

Permalink
Do simple mem2reg for autodiff fn detection (#1431)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmoses authored Sep 18, 2023
1 parent 9a087e6 commit 4c6c0a6
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions enzyme/Enzyme/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2271,6 +2271,66 @@ Function *GetFunctionFromValue(Value *fn) {
}
}
}
if (auto LI = dyn_cast<LoadInst>(fn)) {
auto obj = getBaseObject(LI->getPointerOperand());
if (isa<AllocaInst>(obj)) {
std::set<std::pair<Instruction *, Value *>> done;
SmallVector<std::pair<Instruction *, Value *>, 1> todo;
Value *stored = nullptr;
bool legal = true;
for (auto U : obj->users()) {
if (auto I = dyn_cast<Instruction>(U))
todo.push_back(std::make_pair(I, obj));
else {
legal = false;
break;
}
}
while (legal && todo.size()) {
auto tup = todo.pop_back_val();
if (done.count(tup))
continue;
done.insert(tup);
auto cur = tup.first;
auto prev = tup.second;
if (auto SI = dyn_cast<StoreInst>(cur))
if (SI->getPointerOperand() == prev) {
if (stored == SI->getValueOperand())
continue;
else if (stored == nullptr) {
stored = SI->getValueOperand();
continue;
} else {
legal = false;
break;
}
}

if (isPointerArithmeticInst(cur, /*includephi*/ true)) {
for (auto U : cur->users()) {
if (auto I = dyn_cast<Instruction>(U))
todo.push_back(std::make_pair(I, cur));
else {
legal = false;
break;
}
}
continue;
}

if (!cur->mayWriteToMemory() && cur->getType()->isVoidTy())
continue;

legal = false;
break;
}

if (legal && stored) {
fn = stored;
continue;
}
}
}
break;
}

Expand Down

0 comments on commit 4c6c0a6

Please sign in to comment.