[Mlir-commits] [mlir] [MLIR] Forward generated OpTy::create arguments (PR #170012)
Jason Rice
llvmlistbot at llvm.org
Sat Nov 29 15:31:05 PST 2025
https://github.com/ricejasonf created https://github.com/llvm/llvm-project/pull/170012
The recent changes in the MLIR TableGen interface for generated OpTy::build functions involves a new OpTy::create function that is generated passing arguments without forwarding. This is problematic with arguments that are move only such as `std::unique_ptr`. My particular use case involves `std::unique_ptr<mlir::Region>` which is desirable as the `mlir::OperationState` object accepts calls to `addRegion(std::unique_ptr<mlir::Region>`.
In Discord, the use of `extraClassDeclarations` was suggested which I may go with regardless since I still have to define the builder function anyways, but perhaps you would consider this trivial change as it supports a broader class of argument types for this approach.
Consider the declaration in TableGen:
```
let builders = [
OpBuilder<(ins "::mlir::Value":$cdr,
"::mlir::ValueRange":$packs,
"std::unique_ptr<::mlir::Region>&&":$body)>
];
```
Which currently generates:
```cpp
ExpandPacksOp ExpandPacksOp::create(::mlir::OpBuilder &builder, ::mlir::Location location, ::mlir::Value cdr, ::mlir::ValueRange packs, std::unique_ptr<::mlir::Region>&&body) {
::mlir::OperationState __state__(location, getOperationName());
build(builder, __state__, cdr, packs, body);
auto __res__ = ::llvm::dyn_cast<ExpandPacksOp>(builder.create(__state__));
assert(__res__ && "builder didn't return the right type");
return __res__;
}
```
With this change it will generate:
```cpp
ExpandPacksOp ExpandPacksOp::create(::mlir::OpBuilder &builder, ::mlir::Location location, ::mlir::Value cdr, ::mlir::ValueRange packs, std::unique_ptr<::mlir::Region>&&body) {
::mlir::OperationState __state__(location, getOperationName());
build(builder, __state__, static_cast<decltype(cdr)>(cdr), static_cast<decltype(packs)>(packs), static_cast<decltype(body)>(body));
auto __res__ = ::llvm::dyn_cast<ExpandPacksOp>(builder.create(__state__));
assert(__res__ && "builder didn't return the right type");
return __res__;
}
```
Another option could be to make this function a template but then it would not be hidden in the generated translation unit. I don't know if that was the original intent. Thank you for your consideration.
>From e716f0349e759d6afc56c4a82e7feb711c0929e3 Mon Sep 17 00:00:00 2001
From: Jason Rice <ricejasonf at gmail.com>
Date: Sat, 29 Nov 2025 15:12:09 -0800
Subject: [PATCH] [MLIR] Forward generated OpTy::create arguments
---
mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index 3b10842f2a127..4f6c70e269fee 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -2641,7 +2641,14 @@ void OpEmitter::genInlineCreateBody(
std::string nonBuilderStateArgs = "";
if (!nonBuilderStateArgsList.empty()) {
llvm::raw_string_ostream nonBuilderStateArgsOS(nonBuilderStateArgs);
- interleaveComma(nonBuilderStateArgsList, nonBuilderStateArgsOS);
+ interleave(
+ nonBuilderStateArgsList,
+ [&](StringRef name) {
+ nonBuilderStateArgsOS << "static_cast<decltype(" << name << ")>("
+ << name << ')';
+ },
+ [&] { nonBuilderStateArgsOS << ", "; });
+
nonBuilderStateArgs = ", " + nonBuilderStateArgs;
}
if (cWithLoc)
More information about the Mlir-commits
mailing list