Can't create Attribute 'mlir::TypeAttr' because storage uniquer isn't initialized

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!

I’ve also tried with a simplified version of mlir-opt’s main function:

int main(int argc, char **argv) {
  mlir::registerAllPasses();

  mlir::DialectRegistry registry;
  registerAllDialects(registry);
  registry.insert<bonsai::bonsaiDialect>();

  return mlir::asMainReturnCode(mlir::MlirOptMain(
      argc, argv, "MLIR modular optimizer driver\n", registry));
}

And still receive the error, with the following stack dump:

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.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: ./build/bin/bonsai ./examples/dummy.mlir
1.      MLIR Parser: custom op parser 'builtin.module'
2.      MLIR Parser: custom op parser 'func.func'
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  libbonsaiDialect.dylib   0x0000000110b778d0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  libbonsaiDialect.dylib   0x0000000110b7559c llvm::sys::RunSignalHandlers() + 112
2  libbonsaiDialect.dylib   0x0000000110b77f8c SignalHandler(int) + 360
3  libsystem_platform.dylib 0x000000018cc43584 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018cc12c20 pthread_kill + 288
5  libsystem_c.dylib        0x000000018cb1fa20 abort + 180
6  libbonsaiDialect.dylib   0x0000000110b087f0 llvm::report_fatal_error(llvm::Twine const&, bool) + 280
7  bonsai                   0x00000001062fa1cc llvm::StringRef llvm::getTypeName<mlir::TypeAttr>() + 0
8  bonsai                   0x00000001062e3908 mlir::TypeAttr::get(mlir::Type) + 136
9  bonsai                   0x00000001062c89f8 mlir::function_interface_impl::parseFunctionOp(mlir::OpAsmParser&, mlir::OperationState&, bool, mlir::StringAttr, llvm::function_ref<mlir::Type (mlir::Builder&, llvm::ArrayRef<mlir::Type>, llvm::ArrayRef<mlir::Type>, mlir::function_interface_impl::VariadicFlag, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>&)>, mlir::StringAttr, mlir::StringAttr) + 600
10 bonsai                   0x00000001010c37c4 mlir::func::FuncOp::parse(mlir::OpAsmParser&, mlir::OperationState&) + 112
11 bonsai                   0x00000001061c2f14 (anonymous namespace)::OperationParser::parseOperation() + 912
12 bonsai                   0x00000001061cf298 (anonymous namespace)::OperationParser::parseBlock(mlir::Block*&) + 368
13 bonsai                   0x00000001061ccb94 (anonymous namespace)::OperationParser::parseRegion(mlir::Region&, llvm::ArrayRef<mlir::OpAsmParser::Argument>, bool) + 864
14 bonsai                   0x00000001061cae8c (anonymous namespace)::CustomOpAsmParser::parseRegion(mlir::Region&, llvm::ArrayRef<mlir::OpAsmParser::Argument>, bool) + 28
15 libbonsaiDialect.dylib   0x0000000110a015e8 mlir::ModuleOp::parse(mlir::OpAsmParser&, mlir::OperationState&) + 432
16 bonsai                   0x00000001061c2f14 (anonymous namespace)::OperationParser::parseOperation() + 912
17 bonsai                   0x00000001061c1e1c mlir::parseAsmSourceFile(llvm::SourceMgr const&, mlir::Block*, mlir::ParserConfig const&, mlir::AsmParserState*, mlir::AsmParserCodeCompleteContext*) + 1012
18 bonsai                   0x000000010619282c mlir::parseSourceFile(std::__1::shared_ptr<llvm::SourceMgr> const&, mlir::Block*, mlir::ParserConfig const&, mlir::LocationAttr*) + 232
19 bonsai                   0x0000000103543a44 mlir::parseSourceFileForTool(std::__1::shared_ptr<llvm::SourceMgr> const&, mlir::ParserConfig const&, bool) + 96
20 bonsai                   0x00000001035432e8 performActions(llvm::raw_ostream&, std::__1::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) + 336
21 bonsai                   0x000000010354305c llvm::LogicalResult llvm::function_ref<llvm::LogicalResult (std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>::callback_fn<mlir::MlirOptMain(llvm::raw_ostream&, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&)::$_2>(long, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) + 460
22 bonsai                   0x0000000106373950 mlir::splitAndProcessBuffer(std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<llvm::LogicalResult (std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef) + 604
23 bonsai                   0x000000010353d070 mlir::MlirOptMain(llvm::raw_ostream&, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) + 220
24 bonsai                   0x000000010353d330 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) + 344
25 bonsai                   0x000000010353d5e0 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) + 152
26 bonsai                   0x00000001008763b0 main + 124
27 dyld                     0x000000018c88a0e0 start + 2360
zsh: abort      ./build/bin/bonsai ./examples/dummy.mlir

And it also fails (with the same error) on MLIR code completely devoid of my custom dialect:

module {
    func.func @bar() {
        %0 = arith.constant 1 : i32
        return
    }
}

Looking at your backtrace, I suspect that you have some libraries that a statically linked into both libbonsaiDialect.dylib and bonsai, thus creating some ODR violation.
Basically you have two copies of the builtin dialect in the program and one of them is loaded in the context, but you’re trying to create an attribute with the other copy, hence the mismatch.

Thank you for the explanation! I’m not quite sure how this could be happening though - my CMakeLists.txt are almost exactly the same as the standalone example, with the only changes (besides “standalone” → “bonsai”) being that I am linking MLIRExecutionEngine in building the “opt” tool:
lib/bonsai/CMakeLists.txt:

add_mlir_dialect_library(bonsaiDialect
    bonsaiDialect.cpp
    bonsaiOps.cpp
    bonsaiPasses.cpp
    bonsaiTypes.cpp

    ADDITIONAL_HEADER_DIRS
    ${PROJECT_SOURCE_DIR}/include/bonsai

    DEPENDS
    bonsaiIncGen

    LINK_LIBS PUBLIC
    MLIRIR
    MLIRInferTypeOpInterface
    MLIRFuncDialect
)

tools/bonsai-jit/CMakeLists.txt:

get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
set(LIBS
    ${dialect_libs}
    ${conversion_libs}
    MLIRArithDialect
    MLIROptLib
    MLIRExecutionEngine
    bonsaiDialect
)
add_llvm_executable(bonsai bonsai.cpp)

llvm_update_compile_flags(bonsai)
target_link_libraries(bonsai PRIVATE ${LIBS})
mlir_check_all_link_libraries(bonsai)

I’ve tried this both with the release/19.x branch and LLVM main, both produce the same bug.

A friend of mine was able to build and execute this project with the same exact build commands (and using LLVM main) on their Mac x86 machine. I am trying to build on my Mac M1. Is it possible that there is some machine-specific build difference that could be causing it?

Confirmed that this exact same error happens for a different M1 as well, which uses a different LLVM version + different cmake.

That is potentially linking a lot of things: you should look at the actual linker invocation to see what is linked-in here.

The only duplicates were ArithDialect and FuncDialect, removing those from linking does not remove this error. Those lines are directly copied from the standalone example, which does compile and execute on my machine