Skip to content
Merged
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
124 changes: 6 additions & 118 deletions src/enzyme_ad/jax/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,120 +350,6 @@ bool getEffectsAfter(Operation *op,
return !conservative;
}

bool isCaptured(Value v, Operation *potentialUser = nullptr,
bool *seenuse = nullptr) {
SmallVector<Value> todo = {v};
while (todo.size()) {
Value v = todo.pop_back_val();
for (auto u : v.getUsers()) {
if (seenuse && u == potentialUser)
*seenuse = true;
if (isa<memref::LoadOp, LLVM::LoadOp, affine::AffineLoadOp>(u))
continue;
// if (isa<polygeist::CacheLoad>(u)) continue
if (auto s = dyn_cast<memref::StoreOp>(u)) {
if (s.getValue() == v)
return true;
continue;
}
if (auto s = dyn_cast<affine::AffineStoreOp>(u)) {
if (s.getValue() == v)
return true;
continue;
}
if (auto s = dyn_cast<LLVM::StoreOp>(u)) {
if (s.getValue() == v)
return true;
continue;
}
if (auto sub = dyn_cast<LLVM::GEPOp>(u)) {
todo.push_back(sub);
}
if (auto sub = dyn_cast<LLVM::BitcastOp>(u)) {
todo.push_back(sub);
}
if (auto sub = dyn_cast<LLVM::AddrSpaceCastOp>(u)) {
todo.push_back(sub);
}
if (auto sub = dyn_cast<func::ReturnOp>(u)) {
continue;
}
if (auto sub = dyn_cast<LLVM::MemsetOp>(u)) {
continue;
}
if (auto sub = dyn_cast<LLVM::MemcpyOp>(u)) {
continue;
}
if (auto sub = dyn_cast<LLVM::MemmoveOp>(u)) {
continue;
}
if (auto sub = dyn_cast<memref::CastOp>(u)) {
todo.push_back(sub);
}
if (auto sub = dyn_cast<memref::DeallocOp>(u)) {
continue;
}
// if (auto sub = dyn_cast<polygeist::SubIndexOp>(u)) {
// todo.push_back(sub);
// }
if (auto sub = dyn_cast<enzymexla::Memref2PointerOp>(u)) {
todo.push_back(sub);
}
if (auto sub = dyn_cast<enzymexla::Pointer2MemrefOp>(u)) {
todo.push_back(sub);
}
if (auto cop = dyn_cast<LLVM::CallOp>(u)) {
if (auto callee = cop.getCallee()) {
if (getNonCapturingFunctions().count(callee->str()))
continue;
}
}
if (auto cop = dyn_cast<func::CallOp>(u)) {
if (getNonCapturingFunctions().count(cop.getCallee().str()))
continue;
}
return true;
}
}

return false;
}

Value getBase(Value v) {
while (true) {
// if (auto s = v.getDefiningOp<SubIndexOp>()) {
// v = s.getSource();
// continue;
// }
if (auto s = v.getDefiningOp<enzymexla::Memref2PointerOp>()) {
v = s.getSource();
continue;
}
if (auto s = v.getDefiningOp<enzymexla::Pointer2MemrefOp>()) {
v = s.getSource();
continue;
}
if (auto s = v.getDefiningOp<LLVM::GEPOp>()) {
v = s.getBase();
continue;
}
if (auto s = v.getDefiningOp<LLVM::BitcastOp>()) {
v = s.getArg();
continue;
}
if (auto s = v.getDefiningOp<LLVM::AddrSpaceCastOp>()) {
v = s.getArg();
continue;
}
if (auto s = v.getDefiningOp<memref::CastOp>()) {
v = s.getSource();
continue;
}
break;
}
return v;
}

bool isStackAlloca(Value v) {
return v.getDefiningOp<memref::AllocaOp>() ||
v.getDefiningOp<memref::AllocOp>() ||
Expand Down Expand Up @@ -505,9 +391,10 @@ bool mayWriteTo(Operation *op, Value val, bool ignoreBarrier) {
// Calls which do not use a derived pointer of a known alloca, which is not
// captured can not write to said memory.
if (auto callOp = dyn_cast<CallOpInterface>(op)) {
auto base = getBase(val);
auto base = enzyme::oputils::getBaseObject(val);
bool seenuse = false;
if (isStackAlloca(base) && !isCaptured(base, op, &seenuse) && !seenuse) {
if (isStackAlloca(base) &&
!enzyme::oputils::isCaptured(base, op, &seenuse) && !seenuse) {
return false;
}
}
Expand Down Expand Up @@ -1407,9 +1294,10 @@ bool mayReadFrom(Operation *op, Value val) {
return false;
}
if (auto callOp = dyn_cast<CallOpInterface>(op)) {
auto base = getBase(val);
auto base = enzyme::oputils::getBaseObject(val);
bool seenuse = false;
if (isStackAlloca(base) && !isCaptured(base, op, &seenuse) && !seenuse) {
if (isStackAlloca(base) &&
!enzyme::oputils::isCaptured(base, op, &seenuse) && !seenuse) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion workspace.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
JAX_COMMIT = "fbf6588d55c5662ccf98558015d5b2c4c4a7bf6f"
JAX_SHA256 = ""

ENZYME_COMMIT = "3cd66b79ce224b8dc35cfac0e37038ed186b2013"
ENZYME_COMMIT = "56b02e7e94265e7af38fe95e9fbf9818ef1d09c0"
ENZYME_SHA256 = ""

ML_TOOLCHAIN_COMMIT = "30ef4a9096f9490e8f198faa5ce5bbddd1b72fdb"
Expand Down
Loading