[Mlir-commits] [mlir] [mlir][mlir-tblgen] Emit correct error message if method is pruned (PR #160334)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Tue Sep 23 09:09:40 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-tosa
Author: Justin Kim (JustinKim98)
<details>
<summary>Changes</summary>
This PR fixes issue #<!-- -->160227
### Changes
* Added verification for pruned methods for `emitCustomBuilder` and `emitCheckedCustomBuilder`
* Without this verification, `mlir-tblgen` with `--gen-attrdef-decls` would segmentation fault if custom builder is provided with its body, but if method is pruned out due to duplication with other builders.
* Removed redundant custom builder in `TosaTypesBase.td`
---
Full diff: https://github.com/llvm/llvm-project/pull/160334.diff
2 Files Affected:
- (modified) mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td (+1-2)
- (modified) mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp (+19)
``````````diff
diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td
index 553d69cc21d17..93ab120339d55 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td
@@ -282,8 +282,7 @@ def Tosa_Shape : Tosa_Type<"shape", "shape"> {
!tosa.shape<0>
```
}];
- let parameters = (ins "int" : $rank);
- let builders = [TypeBuilder<(ins "int" : $rank)>];
+ let parameters = (ins "int":$rank);
let assemblyFormat = "`<` $rank `>`";
let genVerifyDecl = 1;
diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index 3140f12c0b7e8..b27e4cdcd5b38 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -513,6 +513,17 @@ getCustomBuilderParams(std::initializer_list<MethodParameter> prefix,
return builderParams;
}
+static void errorIfPruned(size_t line, Method *m, const Twine &methodName,
+ const AttrOrTypeDef &def) {
+ if (m)
+ return;
+ PrintFatalError(def.getLoc(), "Unexpected overlap when generating `" +
+ methodName + "` for " + def.getName() +
+ " (from line " + Twine(line) + ")");
+}
+
+#define ERROR_IF_PRUNED(M, N, O) errorIfPruned(__LINE__, M, N, O)
+
void DefGen::emitCustomBuilder(const AttrOrTypeBuilder &builder) {
// Don't emit a body if there isn't one.
auto props = builder.getBody() ? Method::Static : Method::StaticDeclaration;
@@ -521,6 +532,10 @@ void DefGen::emitCustomBuilder(const AttrOrTypeBuilder &builder) {
returnType = *builderReturnType;
Method *m = defCls.addMethod(returnType, "get", props,
getCustomBuilderParams({}, builder));
+
+ // If method is pruned, report error and terminate.
+ ERROR_IF_PRUNED(m, "get", def);
+
if (!builder.getBody())
return;
@@ -552,6 +567,10 @@ void DefGen::emitCheckedCustomBuilder(const AttrOrTypeBuilder &builder) {
getCustomBuilderParams(
{{"::llvm::function_ref<::mlir::InFlightDiagnostic()>", "emitError"}},
builder));
+
+ // If method is pruned, report error and terminate.
+ ERROR_IF_PRUNED(m, "getChecked", def);
+
if (!builder.getBody())
return;
``````````
</details>
https://github.com/llvm/llvm-project/pull/160334
More information about the Mlir-commits
mailing list