-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[flang][NFC] Characterize allocation based on MemAlloc effect instead of pattern matching #166806
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
Changes from 14 commits
05e4d39
0295bd1
4e64702
d4ffa26
163cda5
538424c
3e21642
fb74ffd
7c380e1
99228e3
caa0cde
1382cee
42fced2
704be71
df464d8
a37b956
d807d66
b9b30f5
c47e2a2
a3158e2
45f03bb
ce3cbaf
a0ce784
ca2e56d
80be520
92e11da
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 |
|---|---|---|
|
|
@@ -27,6 +27,29 @@ using namespace mlir; | |
|
|
||
| #define DEBUG_TYPE "fir-alias-analysis" | ||
|
|
||
| static bool classifyAllocateFromEffects(mlir::Operation *op, | ||
|
||
| mlir::Value candidate, mlir::Value &v, | ||
| mlir::Operation *&defOp, | ||
| fir::AliasAnalysis::SourceKind &type) { | ||
| if (!op) | ||
| return false; | ||
| auto interface = llvm::dyn_cast<mlir::MemoryEffectOpInterface>(op); | ||
| if (!interface) | ||
| return false; | ||
| llvm::SmallVector<mlir::MemoryEffects::EffectInstance, 4> effects; | ||
| interface.getEffects(effects); | ||
| for (mlir::MemoryEffects::EffectInstance &e : effects) { | ||
| if (mlir::isa<mlir::MemoryEffects::Allocate>(e.getEffect()) && | ||
| e.getValue() && e.getValue() == candidate) { | ||
| v = candidate; | ||
| defOp = op; | ||
| type = fir::AliasAnalysis::SourceKind::Allocate; | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // AliasAnalysis: alias | ||
| //===----------------------------------------------------------------------===// | ||
|
|
@@ -535,6 +558,9 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v, | |
| mlir::Operation *instantiationPoint{nullptr}; | ||
| while (defOp && !breakFromLoop) { | ||
| ty = defOp->getResultTypes()[0]; | ||
| // Value-scoped allocation detection via effects. | ||
| if (classifyAllocateFromEffects(defOp, v, v, defOp, type)) | ||
| break; | ||
| llvm::TypeSwitch<Operation *>(defOp) | ||
| .Case<hlfir::AsExprOp>([&](auto op) { | ||
| v = op.getVar(); | ||
|
|
@@ -554,11 +580,6 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v, | |
| defOp = v.getDefiningOp(); | ||
| } | ||
| }) | ||
| .Case<fir::AllocaOp, fir::AllocMemOp>([&](auto op) { | ||
| // Unique memory allocation. | ||
| type = SourceKind::Allocate; | ||
| breakFromLoop = true; | ||
| }) | ||
| .Case<fir::ConvertOp>([&](auto op) { | ||
| // Skip ConvertOp's and track further through the operand. | ||
| v = op->getOperand(0); | ||
|
|
@@ -628,16 +649,17 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v, | |
| type = SourceKind::Global; | ||
| } else { | ||
| auto def = llvm::cast<mlir::Value>(boxSrc.origin.u); | ||
| // TODO: Add support to fir.allocmem | ||
| if (auto allocOp = def.template getDefiningOp<fir::AllocaOp>()) { | ||
| v = def; | ||
| defOp = v.getDefiningOp(); | ||
| type = SourceKind::Allocate; | ||
| } else if (isDummyArgument(def)) { | ||
| defOp = nullptr; | ||
| v = def; | ||
| } else { | ||
| type = SourceKind::Indirect; | ||
| bool classified = false; | ||
| if (auto defDefOp = def.getDefiningOp()) | ||
| classified = | ||
| classifyAllocateFromEffects(defDefOp, def, v, defOp, type); | ||
| if (!classified) { | ||
| if (isDummyArgument(def)) { | ||
| defOp = nullptr; | ||
| v = def; | ||
| } else { | ||
| type = SourceKind::Indirect; | ||
| } | ||
| } | ||
| } | ||
| breakFromLoop = true; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,6 +285,21 @@ llvm::LogicalResult fir::AllocaOp::verify() { | |
| return mlir::success(); | ||
| } | ||
|
|
||
| void fir::AllocaOp::getEffects( | ||
| llvm::SmallVectorImpl< | ||
| mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>> | ||
| &effects) { | ||
| // Value-scoped Allocate for AA. | ||
| effects.emplace_back( | ||
| mlir::MemoryEffects::Allocate::get(), | ||
| mlir::cast<mlir::OpResult>(getOperation()->getResult(0)), | ||
| mlir::SideEffects::AutomaticAllocationScopeResource::get()); | ||
| // Preserve previous behavior: op-scoped Allocate for passes relying on it. | ||
|
||
| effects.emplace_back( | ||
| mlir::MemoryEffects::Allocate::get(), | ||
| mlir::SideEffects::AutomaticAllocationScopeResource::get()); | ||
| } | ||
|
|
||
| bool fir::AllocaOp::ownsNestedAlloca(mlir::Operation *op) { | ||
| return op->hasTrait<mlir::OpTrait::IsIsolatedFromAbove>() || | ||
| op->hasTrait<mlir::OpTrait::AutomaticAllocationScope>() || | ||
|
|
@@ -384,6 +399,19 @@ llvm::LogicalResult fir::AllocMemOp::verify() { | |
| return mlir::success(); | ||
| } | ||
|
|
||
| void fir::AllocMemOp::getEffects( | ||
| llvm::SmallVectorImpl< | ||
| mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>> | ||
| &effects) { | ||
| // Value-scoped Allocate for AA. | ||
| effects.emplace_back(mlir::MemoryEffects::Allocate::get(), | ||
| mlir::cast<mlir::OpResult>(getOperation()->getResult(0)), | ||
| mlir::SideEffects::DefaultResource::get()); | ||
| // Preserve previous behavior: op-scoped Allocate for passes relying on it. | ||
| effects.emplace_back(mlir::MemoryEffects::Allocate::get(), | ||
| mlir::SideEffects::DefaultResource::get()); | ||
| } | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // ArrayCoorOp | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
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.
Just FYI: if you can remove code in
fir::AllocaOp::getEffectsas I suggest below, then you can declare theMemAlloceffect in the operation definition like this:llvm-project/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
Line 249 in 32ebf63
This way you won't need to change the cpp files.