[mlir][tblgen] Improving error messages

This differential improves two error conditions, by detecting them earlier and by providing better messages to help users understand what went wrong.

Reviewed By: jpienaar

Differential Revision: https://reviews.llvm.org/D128555
This commit is contained in:
wren romano 2022-06-27 16:28:45 -07:00
parent 72a23cef7e
commit 46394861a3
3 changed files with 43 additions and 1 deletions

View File

@ -81,7 +81,13 @@ AttrOrTypeDef::AttrOrTypeDef(const llvm::Record *def) : def(def) {
"'assemblyFormat' or 'hasCustomAssemblyFormat' can only be "
"used when 'mnemonic' is set");
}
// Assembly format requires accessors to be generated.
// Assembly format parser requires default builders to be generated.
if (hasDeclarativeFormat && skipDefaultBuilders()) {
PrintFatalError(
getLoc(),
"'assemblyFormat' requires 'skipDefaultBuilders' to be false");
}
// Assembly format printer requires accessors to be generated.
if (hasDeclarativeFormat && !genAccessors()) {
PrintFatalError(getLoc(),
"'assemblyFormat' requires 'genAccessors' to be true");

View File

@ -22,6 +22,13 @@ StringRef Builder::Parameter::getCppType() const {
if (const auto *stringInit = dyn_cast<llvm::StringInit>(def))
return stringInit->getValue();
const llvm::Record *record = cast<llvm::DefInit>(def)->getDef();
// Inlining the first part of `Record::getValueAsString` to give better
// error messages.
const llvm::RecordVal *type = record->getValue("type");
if (!type || !type->getValue()) {
llvm::PrintFatalError("Builder DAG arguments must be either strings or "
"defs which inherit from CArg");
}
return record->getValueAsString("type");
}

View File

@ -0,0 +1,29 @@
// RUN: not mlir-tblgen -gen-typedef-defs -I %S/../../include %s 2>&1 | FileCheck %s
include "mlir/IR/AttrTypeBase.td"
include "mlir/IR/OpBase.td"
def Test_Dialect : Dialect {
let name = "TestDialect";
let cppNamespace = "::test";
}
class InvalidType<string name> : TypeDef<Test_Dialect, name> {
let mnemonic = ?;
}
// This definition should not generate an error due to the use in `InvalidTypeA`
// CHECK-NOT: Record `TestParameter' does not have a field named `type'!
def TestParameter : TypeParameter<"int", "int parameter">;
// Test builder uses wrong record class.
def InvalidTypeA : InvalidType<"InvalidTypeA"> {
let parameters = (ins "int":$v0);
let builders = [
// CHECK: Builder DAG arguments must be either strings or defs which inherit from CArg
TypeBuilder<(ins TestParameter:$arg0), [{
return $_get($_ctxt, arg0);
}]>
];
}