Skip to content

Commit cb2df2f

Browse files
cxy-1993chenxunyu
authored andcommitted
refactor(*): Include more precise side effects
This patch adds more precise side effects to the current ops with memory effects, allowing us to determine which OpOperand/OpResult/BlockArgument the operation reads or writes, rather than just recording the reading and writing of values. This allows for convenient use of precise side effects to achieve analysis and optimization. Related discussions: https://discourse.llvm.org/t/rfc-add-operandindex-to-sideeffect-instance/79243
1 parent 9f44d5d commit cb2df2f

File tree

39 files changed

+645
-322
lines changed

39 files changed

+645
-322
lines changed

flang/lib/Optimizer/Dialect/FIROps.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,9 +1060,9 @@ void fir::BoxRankOp::getEffects(
10601060
llvm::SmallVectorImpl<
10611061
mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
10621062
&effects) {
1063-
mlir::Value inputBox = getBox();
1064-
if (fir::isBoxAddress(inputBox.getType()))
1065-
effects.emplace_back(mlir::MemoryEffects::Read::get(), inputBox,
1063+
mlir::OpOperand &inputBox = getBoxMutable();
1064+
if (fir::isBoxAddress(inputBox.get().getType()))
1065+
effects.emplace_back(mlir::MemoryEffects::Read::get(), &inputBox,
10661066
mlir::SideEffects::DefaultResource::get());
10671067
}
10681068

@@ -2903,9 +2903,9 @@ void fir::ReboxAssumedRankOp::getEffects(
29032903
llvm::SmallVectorImpl<
29042904
mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
29052905
&effects) {
2906-
mlir::Value inputBox = getBox();
2907-
if (fir::isBoxAddress(inputBox.getType()))
2908-
effects.emplace_back(mlir::MemoryEffects::Read::get(), inputBox,
2906+
mlir::OpOperand &inputBox = getBoxMutable();
2907+
if (fir::isBoxAddress(inputBox.get().getType()))
2908+
effects.emplace_back(mlir::MemoryEffects::Read::get(), &inputBox,
29092909
mlir::SideEffects::DefaultResource::get());
29102910
}
29112911

flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ getIntrinsicEffects(mlir::Operation *self,
4545
"hlfir intrinsic ops only produce 1 result");
4646
if (mlir::isa<hlfir::ExprType>(self->getResult(0).getType()))
4747
effects.emplace_back(mlir::MemoryEffects::Allocate::get(),
48-
self->getResult(0),
48+
self->getOpResult(0),
4949
mlir::SideEffects::DefaultResource::get());
5050

5151
// read effect if we read from a pointer or refference type
@@ -59,10 +59,10 @@ getIntrinsicEffects(mlir::Operation *self,
5959
// } to {
6060
// hlfir.yield %0#0 : !fir.box<!fir.array<?x?xf32>>
6161
// }
62-
for (mlir::Value operand : self->getOperands()) {
63-
mlir::Type opTy = operand.getType();
62+
for (mlir::OpOperand &operand : self->getOpOperands()) {
63+
mlir::Type opTy = operand.get().getType();
6464
if (fir::isa_ref_type(opTy) || fir::isa_box_type(opTy))
65-
effects.emplace_back(mlir::MemoryEffects::Read::get(), operand,
65+
effects.emplace_back(mlir::MemoryEffects::Read::get(), &operand,
6666
mlir::SideEffects::DefaultResource::get());
6767
}
6868
}

flang/lib/Optimizer/HLFIR/Transforms/ScheduleOrderedAssignments.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,14 @@ static void gatherMemoryEffects(
225225

226226
/// Return the entity yielded by a region, or a null value if the region
227227
/// is not terminated by a yield.
228-
static mlir::Value getYieldedEntity(mlir::Region &region) {
228+
static mlir::OpOperand *getYieldedEntity(mlir::Region &region) {
229229
if (region.empty() || region.back().empty())
230230
return nullptr;
231231
if (auto yield = mlir::dyn_cast<hlfir::YieldOp>(region.back().back()))
232-
return yield.getEntity();
232+
return &yield.getEntityMutable();
233233
if (auto elementalAddr =
234234
mlir::dyn_cast<hlfir::ElementalAddrOp>(region.back().back()))
235-
return elementalAddr.getYieldOp().getEntity();
235+
return &elementalAddr.getYieldOp().getEntityMutable();
236236
return nullptr;
237237
}
238238

@@ -244,7 +244,7 @@ static void gatherAssignEffects(
244244
hlfir::RegionAssignOp regionAssign,
245245
bool userDefAssignmentMayOnlyWriteToAssignedVariable,
246246
llvm::SmallVectorImpl<mlir::MemoryEffects::EffectInstance> &assignEffects) {
247-
mlir::Value assignedVar = getYieldedEntity(regionAssign.getLhsRegion());
247+
mlir::OpOperand *assignedVar = getYieldedEntity(regionAssign.getLhsRegion());
248248
assert(assignedVar && "lhs cannot be an empty region");
249249
assignEffects.emplace_back(mlir::MemoryEffects::Write::get(), assignedVar);
250250

@@ -389,8 +389,8 @@ void Scheduler::saveEvaluationIfConflict(mlir::Region &yieldRegion,
389389
// with a finalizer, or a user defined assignment where the LHS is
390390
// intent(inout)).
391391
if (yieldIsImplicitRead) {
392-
mlir::Value entity = getYieldedEntity(yieldRegion);
393-
if (entity && hlfir::isFortranVariableType(entity.getType()))
392+
mlir::OpOperand *entity = getYieldedEntity(yieldRegion);
393+
if (entity && hlfir::isFortranVariableType(entity->get().getType()))
394394
effects.emplace_back(mlir::MemoryEffects::Read::get(), entity);
395395
}
396396
if (!leafRegionsMayOnlyRead && anyWrite(effects)) {
@@ -600,9 +600,9 @@ hlfir::buildEvaluationSchedule(hlfir::OrderedAssignmentTreeOpInterface root,
600600
}
601601

602602
mlir::Value hlfir::SaveEntity::getSavedValue() {
603-
mlir::Value saved = getYieldedEntity(*yieldRegion);
603+
mlir::OpOperand *saved = getYieldedEntity(*yieldRegion);
604604
assert(saved && "SaveEntity must contain region terminated by YieldOp");
605-
return saved;
605+
return saved->get();
606606
}
607607

608608
//===----------------------------------------------------------------------===//

mlir/examples/transform/Ch2/lib/MyExtension.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void mlir::transform::ChangeCallTargetOp::getEffects(
129129
// Indicate that the `call` handle is only read by this operation because the
130130
// associated operation is not erased but rather modified in-place, so the
131131
// reference to it remains valid.
132-
onlyReadsHandle(getCall(), effects);
132+
onlyReadsHandle(getCallMutable(), effects);
133133

134134
// Indicate that the payload is modified by this operation.
135135
modifiesPayload(effects);

mlir/examples/transform/Ch3/lib/MyExtension.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void mlir::transform::ChangeCallTargetOp::getEffects(
139139
// Indicate that the `call` handle is only read by this operation because the
140140
// associated operation is not erased but rather modified in-place, so the
141141
// reference to it remains valid.
142-
onlyReadsHandle(getCall(), effects);
142+
onlyReadsHandle(getCallMutable(), effects);
143143

144144
// Indicate that the payload is modified by this operation.
145145
modifiesPayload(effects);

mlir/examples/transform/Ch4/lib/MyExtension.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,8 @@ mlir::transform::HasOperandSatisfyingOp::apply(
160160
void mlir::transform::HasOperandSatisfyingOp::getEffects(
161161
llvm::SmallVectorImpl<mlir::MemoryEffects::EffectInstance> &effects) {
162162
onlyReadsPayload(effects);
163-
onlyReadsHandle(getOp(), effects);
164-
producesHandle(getPosition(), effects);
165-
producesHandle(getResults(), effects);
163+
onlyReadsHandle(getOpMutable(), effects);
164+
producesHandle(getOperation()->getOpResults(), effects);
166165
}
167166

168167
// Verify well-formedness of the operation and emit diagnostics if it is

mlir/include/mlir/Dialect/Affine/IR/AffineOps.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ class AffineDmaStartOp
107107

108108
/// Returns the source MemRefType for this DMA operation.
109109
Value getSrcMemRef() { return getOperand(getSrcMemRefOperandIndex()); }
110+
OpOperand &getSrcMemRefMutable() {
111+
return getOperation()->getOpOperand(getSrcMemRefOperandIndex());
112+
}
110113
MemRefType getSrcMemRefType() {
111114
return cast<MemRefType>(getSrcMemRef().getType());
112115
}
@@ -117,7 +120,8 @@ class AffineDmaStartOp
117120
/// Returns the affine map used to access the source memref.
118121
AffineMap getSrcMap() { return getSrcMapAttr().getValue(); }
119122
AffineMapAttr getSrcMapAttr() {
120-
return cast<AffineMapAttr>(*(*this)->getInherentAttr(getSrcMapAttrStrName()));
123+
return cast<AffineMapAttr>(
124+
*(*this)->getInherentAttr(getSrcMapAttrStrName()));
121125
}
122126

123127
/// Returns the source memref affine map indices for this DMA operation.
@@ -139,6 +143,9 @@ class AffineDmaStartOp
139143

140144
/// Returns the destination MemRefType for this DMA operation.
141145
Value getDstMemRef() { return getOperand(getDstMemRefOperandIndex()); }
146+
OpOperand &getDstMemRefMutable() {
147+
return getOperation()->getOpOperand(getDstMemRefOperandIndex());
148+
}
142149
MemRefType getDstMemRefType() {
143150
return cast<MemRefType>(getDstMemRef().getType());
144151
}
@@ -156,7 +163,8 @@ class AffineDmaStartOp
156163
/// Returns the affine map used to access the destination memref.
157164
AffineMap getDstMap() { return getDstMapAttr().getValue(); }
158165
AffineMapAttr getDstMapAttr() {
159-
return cast<AffineMapAttr>(*(*this)->getInherentAttr(getDstMapAttrStrName()));
166+
return cast<AffineMapAttr>(
167+
*(*this)->getInherentAttr(getDstMapAttrStrName()));
160168
}
161169

162170
/// Returns the destination memref indices for this DMA operation.
@@ -173,6 +181,9 @@ class AffineDmaStartOp
173181

174182
/// Returns the Tag MemRef for this DMA operation.
175183
Value getTagMemRef() { return getOperand(getTagMemRefOperandIndex()); }
184+
OpOperand &getTagMemRefMutable() {
185+
return getOperation()->getOpOperand(getTagMemRefOperandIndex());
186+
}
176187
MemRefType getTagMemRefType() {
177188
return cast<MemRefType>(getTagMemRef().getType());
178189
}
@@ -185,7 +196,8 @@ class AffineDmaStartOp
185196
/// Returns the affine map used to access the tag memref.
186197
AffineMap getTagMap() { return getTagMapAttr().getValue(); }
187198
AffineMapAttr getTagMapAttr() {
188-
return cast<AffineMapAttr>(*(*this)->getInherentAttr(getTagMapAttrStrName()));
199+
return cast<AffineMapAttr>(
200+
*(*this)->getInherentAttr(getTagMapAttrStrName()));
189201
}
190202

191203
/// Returns the tag memref indices for this DMA operation.
@@ -300,14 +312,16 @@ class AffineDmaWaitOp
300312

301313
/// Returns the Tag MemRef associated with the DMA operation being waited on.
302314
Value getTagMemRef() { return getOperand(0); }
315+
OpOperand &getTagMemRefMutable() { return getOperation()->getOpOperand(0); }
303316
MemRefType getTagMemRefType() {
304317
return cast<MemRefType>(getTagMemRef().getType());
305318
}
306319

307320
/// Returns the affine map used to access the tag memref.
308321
AffineMap getTagMap() { return getTagMapAttr().getValue(); }
309322
AffineMapAttr getTagMapAttr() {
310-
return cast<AffineMapAttr>(*(*this)->getInherentAttr(getTagMapAttrStrName()));
323+
return cast<AffineMapAttr>(
324+
*(*this)->getInherentAttr(getTagMapAttrStrName()));
311325
}
312326

313327
/// Returns the tag memref index for this DMA operation.

mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ def MemRef_DmaStartOp : MemRef_Op<"dma_start"> {
706706
let extraClassDeclaration = [{
707707
// Returns the source MemRefType for this DMA operation.
708708
Value getSrcMemRef() { return getOperand(0); }
709+
OpOperand &getSrcMemRefMutable() { return getOperation()->getOpOperand(0); }
709710
// Returns the rank (number of indices) of the source MemRefType.
710711
unsigned getSrcMemRefRank() {
711712
return ::llvm::cast<MemRefType>(getSrcMemRef().getType()).getRank();
@@ -718,6 +719,7 @@ def MemRef_DmaStartOp : MemRef_Op<"dma_start"> {
718719

719720
// Returns the destination MemRefType for this DMA operations.
720721
Value getDstMemRef() { return getOperand(1 + getSrcMemRefRank()); }
722+
OpOperand &getDstMemRefMutable() { return getOperation()->getOpOperand(1 + getSrcMemRefRank()); }
721723
// Returns the rank (number of indices) of the destination MemRefType.
722724
unsigned getDstMemRefRank() {
723725
return ::llvm::cast<MemRefType>(getDstMemRef().getType()).getRank();
@@ -745,6 +747,9 @@ def MemRef_DmaStartOp : MemRef_Op<"dma_start"> {
745747
Value getTagMemRef() {
746748
return getOperand(1 + getSrcMemRefRank() + 1 + getDstMemRefRank() + 1);
747749
}
750+
OpOperand &getTagMemRefMutable() {
751+
return getOperation()->getOpOperand(1 + getSrcMemRefRank() + 1 + getDstMemRefRank() + 1);
752+
}
748753

749754
// Returns the rank (number of indices) of the tag MemRefType.
750755
unsigned getTagMemRefRank() {
@@ -801,11 +806,11 @@ def MemRef_DmaStartOp : MemRef_Op<"dma_start"> {
801806
void getEffects(
802807
SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>> &
803808
effects) {
804-
effects.emplace_back(MemoryEffects::Read::get(), getSrcMemRef(),
809+
effects.emplace_back(MemoryEffects::Read::get(), &getSrcMemRefMutable(),
805810
SideEffects::DefaultResource::get());
806-
effects.emplace_back(MemoryEffects::Write::get(), getDstMemRef(),
811+
effects.emplace_back(MemoryEffects::Write::get(), &getDstMemRefMutable(),
807812
SideEffects::DefaultResource::get());
808-
effects.emplace_back(MemoryEffects::Read::get(), getTagMemRef(),
813+
effects.emplace_back(MemoryEffects::Read::get(), &getTagMemRefMutable(),
809814
SideEffects::DefaultResource::get());
810815
}
811816
}];
@@ -852,7 +857,7 @@ def MemRef_DmaWaitOp : MemRef_Op<"dma_wait"> {
852857
void getEffects(
853858
SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>> &
854859
effects) {
855-
effects.emplace_back(MemoryEffects::Read::get(), getTagMemRef(),
860+
effects.emplace_back(MemoryEffects::Read::get(), &getTagMemRefMutable(),
856861
SideEffects::DefaultResource::get());
857862
}
858863
}];

mlir/include/mlir/Dialect/Transform/Interfaces/MatchInterfaces.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ class AtMostOneOpMatcherOpTrait
102102
}
103103

104104
void getEffects(SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
105-
onlyReadsHandle(this->getOperation()->getOperands(), effects);
106-
producesHandle(this->getOperation()->getResults(), effects);
105+
onlyReadsHandle(this->getOperation()->getOpOperands(), effects);
106+
producesHandle(this->getOperation()->getOpResults(), effects);
107107
onlyReadsPayload(effects);
108108
}
109109
};
@@ -163,8 +163,8 @@ class SingleValueMatcherOpTrait
163163
}
164164

165165
void getEffects(SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
166-
onlyReadsHandle(this->getOperation()->getOperands(), effects);
167-
producesHandle(this->getOperation()->getResults(), effects);
166+
onlyReadsHandle(this->getOperation()->getOpOperands(), effects);
167+
producesHandle(this->getOperation()->getOpResults(), effects);
168168
onlyReadsPayload(effects);
169169
}
170170
};

mlir/include/mlir/Dialect/Transform/Interfaces/TransformInterfaces.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,11 +1261,13 @@ struct PayloadIRResource
12611261
/// - consumes = Read + Free,
12621262
/// - produces = Allocate + Write,
12631263
/// - onlyReads = Read.
1264-
void consumesHandle(ValueRange handles,
1264+
void consumesHandle(MutableArrayRef<OpOperand> handles,
12651265
SmallVectorImpl<MemoryEffects::EffectInstance> &effects);
1266-
void producesHandle(ValueRange handles,
1266+
void producesHandle(ResultRange handles,
12671267
SmallVectorImpl<MemoryEffects::EffectInstance> &effects);
1268-
void onlyReadsHandle(ValueRange handles,
1268+
void producesHandle(MutableArrayRef<BlockArgument> handles,
1269+
SmallVectorImpl<MemoryEffects::EffectInstance> &effects);
1270+
void onlyReadsHandle(MutableArrayRef<OpOperand> handles,
12691271
SmallVectorImpl<MemoryEffects::EffectInstance> &effects);
12701272

12711273
/// Checks whether the transform op consumes the given handle.
@@ -1296,8 +1298,8 @@ class FunctionalStyleTransformOpTrait
12961298
/// the results by allocating and writing it and reads/writes the payload IR
12971299
/// in the process.
12981300
void getEffects(SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
1299-
consumesHandle(this->getOperation()->getOperands(), effects);
1300-
producesHandle(this->getOperation()->getResults(), effects);
1301+
consumesHandle(this->getOperation()->getOpOperands(), effects);
1302+
producesHandle(this->getOperation()->getOpResults(), effects);
13011303
modifiesPayload(effects);
13021304
}
13031305

@@ -1322,8 +1324,8 @@ class NavigationTransformOpTrait
13221324
/// This op produces handles to the Payload IR without consuming the original
13231325
/// handles and without modifying the IR itself.
13241326
void getEffects(SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
1325-
onlyReadsHandle(this->getOperation()->getOperands(), effects);
1326-
producesHandle(this->getOperation()->getResults(), effects);
1327+
onlyReadsHandle(this->getOperation()->getOpOperands(), effects);
1328+
producesHandle(this->getOperation()->getOpResults(), effects);
13271329
if (llvm::any_of(this->getOperation()->getOperandTypes(), [](Type t) {
13281330
return isa<TransformHandleTypeInterface,
13291331
TransformValueHandleTypeInterface>(t);

0 commit comments

Comments
 (0)