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

MLIR: Recursive readonly #2107

Merged
merged 5 commits into from
Oct 6, 2024
Merged
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
32 changes: 31 additions & 1 deletion enzyme/Enzyme/MLIR/Analysis/ActivityAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,36 @@ static Operation *getFunctionFromCall(CallOpInterface iface) {

constexpr bool EnzymePrintActivity = false;

static bool isReadOnly(Operation *op) {
bool hasRecursiveEffects = op->hasTrait<OpTrait::HasRecursiveMemoryEffects>();
if (hasRecursiveEffects) {
for (Region &region : op->getRegions()) {
for (auto &block : region) {
for (auto &nestedOp : block)
if (!isReadOnly(&nestedOp))
return false;
}
}
return true;
}

// If the op has memory effects, try to characterize them to see if the op
// is trivially dead here.
if (auto effectInterface = dyn_cast<MemoryEffectOpInterface>(op)) {
// Check to see if this op either has no effects, or only allocates/reads
// memory.
SmallVector<MemoryEffects::EffectInstance, 1> effects;
effectInterface.getEffects(effects);
if (!llvm::all_of(effects, [op](const MemoryEffects::EffectInstance &it) {
return isa<MemoryEffects::Read>(it.getEffect());
})) {
return false;
}
return true;
}
return false;
}

/// Is the use of value val as an argument of call CI known to be inactive
/// This tool can only be used when in DOWN mode
bool mlir::enzyme::ActivityAnalyzer::isFunctionArgumentConstant(
Expand Down Expand Up @@ -653,7 +683,7 @@ bool mlir::enzyme::ActivityAnalyzer::isConstantOperation(MTypeResults const &TR,
// doesn't write to any memory
bool noActiveWrite = false;

if (isa<MemoryEffectOpInterface>(I) && !hasEffect<MemoryEffects::Write>(I))
if (isReadOnly(I))
noActiveWrite = true;
else if (auto CI = dyn_cast<CallOpInterface>(I)) {
// if (AA.onlyReadsMemory(CI)) {
Expand Down
Loading