-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[MLIR][SideEffects] Added 'Init' Memory Effect which defines an Idempotent MemWrite effect and modified LICM pass #153281
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
34913fe
8097e75
78a55f1
0438627
872d60e
0c23f0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
#include "mlir/Interfaces/SideEffectInterfaces.h" | ||
|
||
#include "mlir/IR/SymbolTable.h" | ||
#include "llvm/ADT/SmallPtrSet.h" | ||
#include <unordered_set> | ||
#include <utility> | ||
|
||
using namespace mlir; | ||
|
@@ -25,7 +27,7 @@ using namespace mlir; | |
//===----------------------------------------------------------------------===// | ||
|
||
bool MemoryEffects::Effect::classof(const SideEffects::Effect *effect) { | ||
return isa<Allocate, Free, Read, Write>(effect); | ||
return isa<Allocate, Free, Read, Write, Init>(effect); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
|
@@ -130,6 +132,7 @@ template bool mlir::hasSingleEffect<MemoryEffects::Allocate>(Operation *); | |
template bool mlir::hasSingleEffect<MemoryEffects::Free>(Operation *); | ||
template bool mlir::hasSingleEffect<MemoryEffects::Read>(Operation *); | ||
template bool mlir::hasSingleEffect<MemoryEffects::Write>(Operation *); | ||
template bool mlir::hasSingleEffect<MemoryEffects::Init>(Operation *); | ||
|
||
template <typename EffectTy> | ||
bool mlir::hasSingleEffect(Operation *op, Value value) { | ||
|
@@ -159,6 +162,8 @@ template bool mlir::hasSingleEffect<MemoryEffects::Read>(Operation *, | |
Value value); | ||
template bool mlir::hasSingleEffect<MemoryEffects::Write>(Operation *, | ||
Value value); | ||
template bool mlir::hasSingleEffect<MemoryEffects::Init>(Operation *, | ||
Value value); | ||
|
||
template <typename ValueTy, typename EffectTy> | ||
bool mlir::hasSingleEffect(Operation *op, ValueTy value) { | ||
|
@@ -193,13 +198,18 @@ template bool | |
mlir::hasSingleEffect<OpOperand *, MemoryEffects::Write>(Operation *, | ||
OpOperand *); | ||
template bool | ||
mlir::hasSingleEffect<OpOperand *, MemoryEffects::Init>(Operation *, | ||
OpOperand *); | ||
template bool | ||
mlir::hasSingleEffect<OpResult, MemoryEffects::Allocate>(Operation *, OpResult); | ||
template bool mlir::hasSingleEffect<OpResult, MemoryEffects::Free>(Operation *, | ||
OpResult); | ||
template bool mlir::hasSingleEffect<OpResult, MemoryEffects::Read>(Operation *, | ||
OpResult); | ||
template bool mlir::hasSingleEffect<OpResult, MemoryEffects::Write>(Operation *, | ||
OpResult); | ||
template bool mlir::hasSingleEffect<OpResult, MemoryEffects::Init>(Operation *, | ||
OpResult); | ||
template bool | ||
mlir::hasSingleEffect<BlockArgument, MemoryEffects::Allocate>(Operation *, | ||
BlockArgument); | ||
|
@@ -212,6 +222,9 @@ mlir::hasSingleEffect<BlockArgument, MemoryEffects::Read>(Operation *, | |
template bool | ||
mlir::hasSingleEffect<BlockArgument, MemoryEffects::Write>(Operation *, | ||
BlockArgument); | ||
template bool | ||
mlir::hasSingleEffect<BlockArgument, MemoryEffects::Init>(Operation *, | ||
BlockArgument); | ||
|
||
template <typename... EffectTys> | ||
bool mlir::hasEffect(Operation *op) { | ||
|
@@ -228,6 +241,7 @@ template bool mlir::hasEffect<MemoryEffects::Allocate>(Operation *); | |
template bool mlir::hasEffect<MemoryEffects::Free>(Operation *); | ||
template bool mlir::hasEffect<MemoryEffects::Read>(Operation *); | ||
template bool mlir::hasEffect<MemoryEffects::Write>(Operation *); | ||
template bool mlir::hasEffect<MemoryEffects::Init>(Operation *); | ||
template bool | ||
mlir::hasEffect<MemoryEffects::Write, MemoryEffects::Free>(Operation *); | ||
|
||
|
@@ -249,6 +263,7 @@ template bool mlir::hasEffect<MemoryEffects::Allocate>(Operation *, | |
template bool mlir::hasEffect<MemoryEffects::Free>(Operation *, Value value); | ||
template bool mlir::hasEffect<MemoryEffects::Read>(Operation *, Value value); | ||
template bool mlir::hasEffect<MemoryEffects::Write>(Operation *, Value value); | ||
template bool mlir::hasEffect<MemoryEffects::Init>(Operation *, Value value); | ||
template bool | ||
mlir::hasEffect<MemoryEffects::Write, MemoryEffects::Free>(Operation *, | ||
Value value); | ||
|
@@ -274,6 +289,8 @@ template bool mlir::hasEffect<OpOperand *, MemoryEffects::Read>(Operation *, | |
OpOperand *); | ||
template bool mlir::hasEffect<OpOperand *, MemoryEffects::Write>(Operation *, | ||
OpOperand *); | ||
template bool mlir::hasEffect<OpOperand *, MemoryEffects::Init>(Operation *, | ||
OpOperand *); | ||
template bool | ||
mlir::hasEffect<OpOperand *, MemoryEffects::Write, MemoryEffects::Free>( | ||
Operation *, OpOperand *); | ||
|
@@ -286,6 +303,8 @@ template bool mlir::hasEffect<OpResult, MemoryEffects::Read>(Operation *, | |
OpResult); | ||
template bool mlir::hasEffect<OpResult, MemoryEffects::Write>(Operation *, | ||
OpResult); | ||
template bool mlir::hasEffect<OpResult, MemoryEffects::Init>(Operation *, | ||
OpResult); | ||
template bool | ||
mlir::hasEffect<OpResult, MemoryEffects::Write, MemoryEffects::Free>( | ||
Operation *, OpResult); | ||
|
@@ -301,6 +320,8 @@ template bool | |
mlir::hasEffect<BlockArgument, MemoryEffects::Write>(Operation *, | ||
BlockArgument); | ||
template bool | ||
mlir::hasEffect<BlockArgument, MemoryEffects::Init>(Operation *, BlockArgument); | ||
template bool | ||
mlir::hasEffect<BlockArgument, MemoryEffects::Write, MemoryEffects::Free>( | ||
Operation *, BlockArgument); | ||
|
||
|
@@ -312,14 +333,20 @@ bool mlir::wouldOpBeTriviallyDead(Operation *op) { | |
return wouldOpBeTriviallyDeadImpl(op); | ||
} | ||
|
||
bool mlir::isMemoryEffectMovable(Operation *op) { | ||
return (isMemoryEffectFree(op) || isMemoryInitMovable(op)); | ||
} | ||
|
||
bool mlir::isMemoryEffectFree(Operation *op) { | ||
if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) { | ||
if (!memInterface.hasNoEffect()) | ||
if (!memInterface.hasNoEffect()) { | ||
return false; | ||
} | ||
// If the op does not have recursive side effects, then it is memory effect | ||
// free. | ||
if (!op->hasTrait<OpTrait::HasRecursiveMemoryEffects>()) | ||
if (!op->hasTrait<OpTrait::HasRecursiveMemoryEffects>()) { | ||
return true; | ||
} | ||
} else if (!op->hasTrait<OpTrait::HasRecursiveMemoryEffects>()) { | ||
// Otherwise, if the op does not implement the memory effect interface and | ||
// it does not have recursive side effects, then it cannot be known that the | ||
|
@@ -329,13 +356,99 @@ bool mlir::isMemoryEffectFree(Operation *op) { | |
|
||
// Recurse into the regions and ensure that all nested ops are memory effect | ||
// free. | ||
for (Region ®ion : op->getRegions()) | ||
for (Operation &op : region.getOps()) | ||
if (!isMemoryEffectFree(&op)) | ||
for (Region ®ion : op->getRegions()) { | ||
for (Operation &op : region.getOps()) { | ||
if (!isMemoryEffectFree(&op)) { | ||
return false; | ||
} | ||
} | ||
} | ||
mbagherbeikTT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return true; | ||
} | ||
|
||
bool mlir::isMemoryInitMovable(Operation *op) { | ||
if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) { | ||
mbagherbeikTT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// gather all effects on op | ||
llvm::SmallVector<MemoryEffects::EffectInstance> effects; | ||
memInterface.getEffects(effects); | ||
|
||
// op has interface but no effects, be conservative | ||
if (effects.empty()) { | ||
return false; | ||
} | ||
|
||
std::unordered_map<std::string, int> resources; | ||
mbagherbeikTT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// ensure op only has Init effects and gather unique | ||
// resource names | ||
for (const MemoryEffects::EffectInstance &effect : effects) { | ||
if (!isa<MemoryEffects::Init>(effect.getEffect())) { | ||
return false; | ||
} | ||
|
||
std::string name = effect.getResource()->getName().str(); | ||
resources.try_emplace(name, 0); | ||
mbagherbeikTT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
// op itself is good, need to check rest of its parent region | ||
Operation *parent = op->getParentOp(); | ||
|
||
for (Region ®ion : parent->getRegions()) { | ||
for (Operation &op_i : region.getOps()) { | ||
if (hasMemoryEffectInitConflict(&op_i, resources)) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
// op does not implement the memory effect op interface | ||
// meaning it doesn't have any memory init effects and | ||
// shouldn't be flagged as movable to be conservative | ||
return false; | ||
} | ||
|
||
bool mlir::hasMemoryEffectInitConflict( | ||
Operation *op, std::unordered_map<std::string, int> &resources) { | ||
if (auto memInterface = dyn_cast<MemoryEffectOpInterface>(op)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above: easy-return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about this one since there's a loop that comes after that if statement |
||
if (!memInterface.hasNoEffect()) { | ||
llvm::SmallVector<MemoryEffects::EffectInstance> effects; | ||
memInterface.getEffects(effects); | ||
|
||
// ensure op only has Init effects and gather unique | ||
// resource names | ||
for (const MemoryEffects::EffectInstance &effect : effects) { | ||
if (!isa<MemoryEffects::Init>(effect.getEffect())) { | ||
return true; | ||
} | ||
|
||
// only care about resources of the op that called | ||
// this recursive function for the first time | ||
std::string name = effect.getResource()->getName().str(); | ||
|
||
if (resources.find(name) != resources.end()) { | ||
if (++resources[name] > 1) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
// Recurse into the regions and ensure that nested ops don't | ||
// conflict with each others MemInits | ||
for (Region ®ion : op->getRegions()) { | ||
for (Operation &op : region.getOps()) { | ||
if (hasMemoryEffectInitConflict(&op, resources)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
// the returned vector may contain duplicate effects | ||
std::optional<llvm::SmallVector<MemoryEffects::EffectInstance>> | ||
mlir::getEffectsRecursively(Operation *rootOp) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,7 +110,7 @@ size_t mlir::moveLoopInvariantCode(LoopLikeOpInterface loopLike) { | |
return loopLike.isDefinedOutsideOfLoop(value); | ||
}, | ||
[&](Operation *op, Region *) { | ||
return isMemoryEffectFree(op) && isSpeculatable(op); | ||
return isMemoryEffectMovable(op) && isSpeculatable(op); | ||
|
||
}, | ||
[&](Operation *op, Region *) { loopLike.moveOutOfLoop(op); }); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How it is different than a write-only effect?
An op with write-effect on a resource should also be idempotent under the conditions that it takes "identical inputs" right?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey. Thanks for replying and going through the PR.I was trying to see if I can go through the CI tests with skip-precommit-approval (and failed) but your feedback is very helpful. I just finished writing the RFC for this and didn't mean to bother anyone with the code yet.
To answer you question: based on how the LICM pass uses memory effects, a 'MemWrite' effect on an op, even if it also has 'Idempotent', does not allow the op to be moved. Conceptually, MemWrite<someResource> + Idempotent implies the entire op is idempotent, whereas MemInit<someResource> only implies that the op has an idempotent write effect on someResource.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get it, I need examples for see the nuance. Maybe we can keep the discussion on the RFC.