Skip to content

Commit c532810

Browse files
committed
fixup! Enable LICM for ops with read side effects in scf.for wrapped by a guard
1 parent 0e596fd commit c532810

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

mlir/lib/Interfaces/SideEffectInterfaces.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,22 @@ bool mlir::wouldOpBeTriviallyDead(Operation *op) {
308308

309309
bool mlir::hasOnlyReadEffect(Operation *op) {
310310
if (auto memEffects = dyn_cast<MemoryEffectOpInterface>(op)) {
311-
return memEffects.onlyHasEffect<MemoryEffects::Read>();
311+
if (!op->hasTrait<OpTrait::HasRecursiveMemoryEffects>())
312+
return memEffects.onlyHasEffect<MemoryEffects::Read>();
313+
} else if (!op->hasTrait<OpTrait::HasRecursiveMemoryEffects>()) {
314+
// Otherwise, if the op does not implement the memory effect interface and
315+
// it does not have recursive side effects, then it cannot be known that the
316+
// op is moveable.
317+
return false;
312318
}
313-
return false;
319+
320+
// Recurse into the regions and ensure that all nested ops are memory effect
321+
// free.
322+
for (Region &region : op->getRegions())
323+
for (Operation &op : region.getOps())
324+
if (!hasOnlyReadEffect(&op))
325+
return false;
326+
return true;
314327
}
315328

316329
bool mlir::isMemoryEffectFree(Operation *op) {

mlir/lib/Transforms/Utils/LoopInvariantCodeMotionUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ size_t mlir::moveLoopInvariantCode(
139139
!canBeHoisted(op, definedOutside))
140140
continue;
141141
// Can only hoist pure ops (side-effect free) when there is an op with
142-
// write side effects in the loop.
142+
// write and/or unknown side effects in the loop.
143143
if (!loopSideEffectFreeOrHasOnlyReadSideEffect && !isMemoryEffectFree(op))
144144
continue;
145145

0 commit comments

Comments
 (0)