I am writing a custom MLIR dialect, and am failing to get parsing to work. I have no attributes in my dialect, and it seems to be failing to parse a builtin attribute, despite my use of registerAllDialects()
before parsing.
This is my parsing code:
int main(int argc, char **argv) {
mlir::registerAllPasses();
mlir::registerMLIRContextCLOptions();
mlir::registerPassManagerCLOptions();
cl::ParseCommandLineOptions(argc, argv, "bonsai compiler\n");
mlir::DialectRegistry registry;
registry.insert<bonsai::bonsaiDialect>();
registerAllDialects(registry);
// Load the input and output files.
std::string errorMessage;
auto inputFile = mlir::openInputFile(inputFilename.getValue(), &errorMessage);
if (!inputFile) {
llvm::errs() << errorMessage << "\n";
return 1;
}
auto sourceMgr = std::make_shared<llvm::SourceMgr>();
sourceMgr->AddNewSourceBuffer(std::move(inputFile), mlir::SMLoc());
// Actually parse the input file. We're not going to do any
// multithreading of the compilation right now.
mlir::MLIRContext ctx(registry, mlir::MLIRContext::Threading::DISABLED);
mlir::FallbackAsmResourceMap fallbackResourceMap;
mlir::ParserConfig parseConfig(&ctx, /*verifyAfterParse=*/true,
&fallbackResourceMap);
mlir::OwningOpRef<mlir::Operation *> op =
mlir::parseSourceFile(sourceMgr, parseConfig);
if (!op) {
llvm::errs() << "Error parsing input program.\n";
return 1;
}
return 0;
}
This is my sample file:
// RUN: bonsai-opt %s | bonsai-opt | FileCheck %s
module {
// CHECK-LABEL: func @bonsai_types(%arg0: !bonsai.vector<"10">)
func.func @bonsai_types(%arg0: !bonsai.vector<10 x i32>) {
return
}
}
And this is the produced error:
ajroot@computer mlir-temp % ./build/bin/bonsai ./examples/dummy.mlir
LLVM ERROR: can't create Attribute 'mlir::TypeAttr' because storage uniquer isn't initialized: the dialect was likely not loaded, or the attribute wasn't added with addAttributes<...>() in the Dialect::initialize() method.
zsh: abort ./build/bin/bonsai ./examples/dummy.mlir
Here are my type definitions:
class bonsai_Type<string name, string typeMnemonic, list<Trait> traits = []>
: TypeDef<bonsai_Dialect, name, traits> {
let mnemonic = typeMnemonic;
}
def VectorType : bonsai_Type<"Vector", "vector"> {
let summary = "Vector type with arbitrary scalar types";
let description = [{
Vector types have a constant-sized lane count.
}];
/// Here we defined a single parameter for the type, which is the lane count
let parameters = (ins "uint32_t":$lanes, "::mlir::Type":$type);
let assemblyFormat = "`<` $lanes`x`$type `>`";
}
Any help would be greatly appreciated!