diff options
author | Matthias Springer <me@m-sp.org> | 2023-10-04 08:35:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-04 08:35:40 +0200 |
commit | 8823e961f6a41854668d2dc1a1fc787cfa85ca43 (patch) | |
tree | 417945d9dbf0d9f70ed0f6e5b5498a76bf2d5ce0 /mlir/tools | |
parent | 3b34c117db175ae6a1404f7c07857c4aa6fc1ae3 (diff) | |
download | llvm-8823e961f6a41854668d2dc1a1fc787cfa85ca43.zip llvm-8823e961f6a41854668d2dc1a1fc787cfa85ca43.tar.gz llvm-8823e961f6a41854668d2dc1a1fc787cfa85ca43.tar.bz2 |
[mlir][ODS] Change `get...Mutable` to return `OpOperand &` for single operands (#66519)
The TableGen code generator now generates C++ code that returns a single
`OpOperand &` for `get...Mutable` of operands that are not variadic and
not optional. `OpOperand::set`/`assign` can be used to set a value (same
as `MutableOperandRange::assign`). This is safer than
`MutableOperandRange` because only single values (and no longer
`ValueRange`) can be assigned.
E.g.:
```
// Assignment of multiple values to non-variadic operand.
// Before: Compiles, but produces invalid op.
// After: Compilation error.
extractSliceOp.getSourceMutable().assign({v1, v2});
```
Diffstat (limited to 'mlir/tools')
-rw-r--r-- | mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp index 5f6a4e3..7029c0e 100644 --- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp +++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp @@ -2071,14 +2071,26 @@ void OpEmitter::genNamedOperandSetters() { continue; std::string name = op.getGetterName(operand.name); - auto *m = opClass.addMethod(operand.isVariadicOfVariadic() - ? "::mlir::MutableOperandRangeRange" - : "::mlir::MutableOperandRange", - name + "Mutable"); + StringRef returnType; + if (operand.isVariadicOfVariadic()) { + returnType = "::mlir::MutableOperandRangeRange"; + } else if (operand.isVariableLength()) { + returnType = "::mlir::MutableOperandRange"; + } else { + returnType = "::mlir::OpOperand &"; + } + auto *m = opClass.addMethod(returnType, name + "Mutable"); ERROR_IF_PRUNED(m, name, op); auto &body = m->body(); - body << " auto range = getODSOperandIndexAndLength(" << i << ");\n" - << " auto mutableRange = " + body << " auto range = getODSOperandIndexAndLength(" << i << ");\n"; + + if (!operand.isVariadicOfVariadic() && !operand.isVariableLength()) { + // In case of a single operand, return a single OpOperand. + body << " return getOperation()->getOpOperand(range.first);\n"; + continue; + } + + body << " auto mutableRange = " "::mlir::MutableOperandRange(getOperation(), " "range.first, range.second"; if (attrSizedOperands) { |