-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[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
Draft
mbagherbeikTT
wants to merge
6
commits into
llvm:main
Choose a base branch
from
mbagherbeikTT:mbagherbeikTT/mem_init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+158
−2
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
34913fe
Added 'Init' Memory Effect which defines an Idempotent MemWrite effec…
mbagherbeikTT 8097e75
Merge branch 'main' into mbagherbeikTT/mem_init
mbagherbeikTT 78a55f1
Merge branch 'main' into mbagherbeikTT/mem_init
mbagherbeikTT 0438627
fixed braces and early returns
mbagherbeikTT 872d60e
switched to DenseMap
mbagherbeikTT 0c23f0c
reordered shouldMoveOutofRegion condition checks for LICM
mbagherbeikTT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
return false; | ||
|
||
// If the op does not have recursive side effects, then it is memory effect | ||
// free. | ||
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 | ||
|
@@ -333,9 +360,83 @@ bool mlir::isMemoryEffectFree(Operation *op) { | |
for (Operation &op : region.getOps()) | ||
if (!isMemoryEffectFree(&op)) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
bool mlir::isMemoryInitMovable(Operation *op) { | ||
auto memInterface = dyn_cast<MemoryEffectOpInterface>(op); | ||
// 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 | ||
if (!memInterface) return false; | ||
|
||
// 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; | ||
|
||
|
||
DenseMap<TypeID, int> resourceCounts; | ||
|
||
// 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; | ||
|
||
resourceCounts.try_emplace(effect.getResource()->getResourceID(), 0); | ||
} | ||
|
||
// 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, resourceCounts)) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
bool mlir::hasMemoryEffectInitConflict( | ||
Operation *op, DenseMap<TypeID, int> &resourceCounts) { | ||
|
||
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 | ||
auto resourceID = effect.getResource()->getResourceID(); | ||
|
||
if (resourceCounts.contains(resourceID)) | ||
if (++resourceCounts[resourceID] > 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, resourceCounts)) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
// the returned vector may contain duplicate effects | ||
std::optional<llvm::SmallVector<MemoryEffects::EffectInstance>> | ||
mlir::getEffectsRecursively(Operation *rootOp) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.