Skip to content

Commit 80c8b4e

Browse files
j2kuncopybara-github
authored andcommitted
Fix support for scf.for in helpers to set/find attrs
This change updates the attribute set/get helpers so that they can operate on RegionBranchOpInterface (provided the same op also implements OperandAndResultAttrInterface). This is only hard for region block arguments, and it required a rather unclear method to map from a region's block argument to the corresponding OpOperand of the parent op, in order to figure out which of the parent op's operands are forwarded to the block argument via control flow. PiperOrigin-RevId: 885723146
1 parent 41d77b9 commit 80c8b4e

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

lib/Utils/AttributeUtils.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,23 @@
1111
#include "mlir/include/mlir/IR/BuiltinAttributes.h" // from @llvm-project
1212
#include "mlir/include/mlir/IR/Value.h" // from @llvm-project
1313
#include "mlir/include/mlir/IR/Visitors.h" // from @llvm-project
14+
#include "mlir/include/mlir/Interfaces/ControlFlowInterfaces.h" // from @llvm-project
1415
#include "mlir/include/mlir/Interfaces/FunctionInterfaces.h" // from @llvm-project
1516
#include "mlir/include/mlir/Support/LLVM.h" // from @llvm-project
1617

1718
namespace mlir {
1819
namespace heir {
1920

21+
// https://discourse.llvm.org/t/regionbranchopinterface-agnostically-connect-a-region-block-argument-to-parent-opoperand/90199
22+
static RegionBranchInverseSuccessorMapping invertRegionBranchSuccessorMapping(
23+
const RegionBranchSuccessorMapping& operandToInputs) {
24+
RegionBranchInverseSuccessorMapping inputToOperands;
25+
for (const auto& [operand, inputs] : operandToInputs) {
26+
for (Value input : inputs) inputToOperands[input].push_back(operand);
27+
}
28+
return inputToOperands;
29+
}
30+
2031
int getOperandNumber(Operation* op, Value value) {
2132
for (OpOperand& operand : op->getOpOperands()) {
2233
if (operand.get() == value) return operand.getOperandNumber();
@@ -49,6 +60,23 @@ Attribute findAttributeForBlockArgument(BlockArgument blockArg,
4960
if (operandNumber == -1) return nullptr;
5061
return argAttrInterface.getOperandAttr(operandNumber, attrName);
5162
})
63+
.Case<RegionBranchOpInterface>(
64+
[&](RegionBranchOpInterface op) -> Attribute {
65+
auto argAttrInterface =
66+
dyn_cast<OperandAndResultAttrInterface>(op.getOperation());
67+
if (!argAttrInterface) return nullptr;
68+
69+
RegionBranchSuccessorMapping mapping;
70+
op.getSuccessorOperandInputMapping(mapping,
71+
RegionBranchPoint::parent());
72+
auto inverseMapping = invertRegionBranchSuccessorMapping(mapping);
73+
if (inverseMapping.count(blockArg)) {
74+
return argAttrInterface.getOperandAttr(
75+
inverseMapping.lookup(blockArg)[0]->getOperandNumber(),
76+
attrName);
77+
}
78+
return nullptr;
79+
})
5280
.Case<OperandAndResultAttrInterface>([&](auto op) -> Attribute {
5381
return op.getOperandAttr(blockArg.getArgNumber(), attrName);
5482
})
@@ -141,6 +169,21 @@ void setAttributeForBlockArgument(BlockArgument blockArg, StringRef attrName,
141169
if (operandNumber == -1) return;
142170
return argAttrInterface.setOperandAttr(operandNumber, attrName, attr);
143171
})
172+
.Case<RegionBranchOpInterface>([&](RegionBranchOpInterface op) {
173+
auto argAttrInterface =
174+
dyn_cast<OperandAndResultAttrInterface>(op.getOperation());
175+
if (!argAttrInterface) return;
176+
177+
RegionBranchSuccessorMapping mapping;
178+
op.getSuccessorOperandInputMapping(mapping,
179+
RegionBranchPoint::parent());
180+
auto inverseMapping = invertRegionBranchSuccessorMapping(mapping);
181+
if (inverseMapping.count(blockArg)) {
182+
return argAttrInterface.setOperandAttr(
183+
inverseMapping.lookup(blockArg)[0]->getOperandNumber(), attrName,
184+
attr);
185+
}
186+
})
144187
.Case<OperandAndResultAttrInterface>([&](auto op) {
145188
return op.setOperandAttr(blockArg.getArgNumber(), attrName, attr);
146189
});

lib/Utils/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ cc_library(
6161
"@heir//lib/Dialect:HEIRInterfaces",
6262
"@llvm-project//llvm:Support",
6363
"@llvm-project//mlir:AffineDialect",
64+
"@llvm-project//mlir:ControlFlowInterfaces",
6465
"@llvm-project//mlir:FuncDialect",
6566
"@llvm-project//mlir:FunctionInterfaces",
6667
"@llvm-project//mlir:IR",

0 commit comments

Comments
 (0)