Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit a4eb34f

Browse files
authored
[mlir][tblgen] Fix bug when mixing props and InferTypes (#157367)
This patch fixes a bug occurring when properties are mixed with any of the InferType traits, causing tblgen to crash. A simple reproducer is: ``` def _TypeInferredPropOp : NS_Op<"type_inferred_prop_op_with_properties", [ AllTypesMatch<["value", "result"]> ]> { let arguments = (ins Property<"unsigned">:$prop, AnyType:$value); let results = (outs AnyType:$result); let hasCustomAssemblyFormat = 1; } ``` The issue occurs because of the call: ``` op.getArgToOperandOrAttribute(infer.getIndex()); ``` To understand better the issue, consider: ``` attrOrOperandMapping = [Operand0] arguments = [Prop0, Operand0] ``` In this case, `infer.getIndex()` will return `1` for `Operand0`, but `getArgToOperandOrAttribute` expects `0`, causing the discrepancy that causes the crash. The fix is to change `attrOrOperandMapping` to also include props.
1 parent e7e7185 commit a4eb34f

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3849,9 +3849,9 @@ void OpEmitter::genTypeInterfaceMethods() {
38493849
const InferredResultType &infer = op.getInferredResultType(i);
38503850
if (!infer.isArg())
38513851
continue;
3852-
Operator::OperandOrAttribute arg =
3853-
op.getArgToOperandOrAttribute(infer.getIndex());
3854-
if (arg.kind() == Operator::OperandOrAttribute::Kind::Operand) {
3852+
Operator::OperandAttrOrProp arg =
3853+
op.getArgToOperandAttrOrProp(infer.getIndex());
3854+
if (arg.kind() == Operator::OperandAttrOrProp::Kind::Operand) {
38553855
maxAccessedIndex =
38563856
std::max(maxAccessedIndex, arg.operandOrAttributeIndex());
38573857
}
@@ -3877,17 +3877,16 @@ void OpEmitter::genTypeInterfaceMethods() {
38773877
if (infer.isArg()) {
38783878
// If this is an operand, just index into operand list to access the
38793879
// type.
3880-
Operator::OperandOrAttribute arg =
3881-
op.getArgToOperandOrAttribute(infer.getIndex());
3882-
if (arg.kind() == Operator::OperandOrAttribute::Kind::Operand) {
3880+
Operator::OperandAttrOrProp arg =
3881+
op.getArgToOperandAttrOrProp(infer.getIndex());
3882+
if (arg.kind() == Operator::OperandAttrOrProp::Kind::Operand) {
38833883
typeStr = ("operands[" + Twine(arg.operandOrAttributeIndex()) +
38843884
"].getType()")
38853885
.str();
38863886

38873887
// If this is an attribute, index into the attribute dictionary.
3888-
} else {
3889-
auto *attr =
3890-
cast<NamedAttribute *>(op.getArg(arg.operandOrAttributeIndex()));
3888+
} else if (auto *attr = dyn_cast<NamedAttribute *>(
3889+
op.getArg(arg.operandOrAttributeIndex()))) {
38913890
body << " ::mlir::TypedAttr odsInferredTypeAttr" << inferredTypeIdx
38923891
<< " = ";
38933892
if (op.getDialect().usePropertiesForAttributes()) {
@@ -3907,6 +3906,9 @@ void OpEmitter::genTypeInterfaceMethods() {
39073906
typeStr =
39083907
("odsInferredTypeAttr" + Twine(inferredTypeIdx) + ".getType()")
39093908
.str();
3909+
} else {
3910+
llvm::PrintFatalError(&op.getDef(),
3911+
"Properties cannot be used for type inference");
39103912
}
39113913
} else if (std::optional<StringRef> builder =
39123914
op.getResult(infer.getResultIndex())

0 commit comments

Comments
 (0)