Understanding how mlir DialectInterface are loaded

i am trying to write a mlir driver that from a custom language lowers to mlir, then to llvm and then compile it, without serializing it on disk.

i am able to emit mlir code that looks correct to me

module @example {
  llvm.func @"example2"() {
    llvm.return loc(#loc2)
  } loc(#loc1)
} loc(#loc0)
#loc0 = loc("examples/declaration.rl":0:0)
#loc1 = loc("examples/declaration.rl":2:0)

when i pipe this into mlir-opt or mlir-translate, it lowers it to llvmir correctly. If lower it to llvmir from within the process that created the mlir module, it fails with a fairly cryptic message.

error: cannot be converted to LLVM IR: missing `LLVMTranslationDialectInterface` registration for dialect for op: llvm.return

furthermore, if the llvm.return is removed, and the llvm.func is left as a declaration, that works correctly too.

i understand that this should not happen, and i guess i need to somehow load the LLVMTranslationDialectInterface, but grepping around the codebase to see what other people where doing did not solved the issue, nor explicitly linking into the project more mlir libraries that looked related.

what i am doing at the moment to lower the mlir module is

	mlir::MLIRContext context;
	mlir::DialectRegistry Registry;
	Registry.insert<mlir::BuiltinDialect, mlir::LLVM::LLVMDialect>();

	auto mlirModule = lowerSomeCustomAst(...);

	LLVMContext LLVMcontext;
	auto Module = mlir::translateModuleToLLVMIR(
			lowerer.getModule(0), LLVMcontext, inputFileName);

this snippet fails within translateModuleToLLVMIR.

the return operation is created as follow

		auto block = declaration->addEntryBlock();
		mlir::OpBuilder builder(block, block->begin());
		mlir::ValueRange range({});
		builder.create<mlir::LLVM::ReturnOp>(mlir::UnknownLoc::get(context), range);

I guess all i need to do is to register the llvm dialect interface, but i can’t understand how that would be performed.

Thank you in advance.

You can use this function to register the translation: MLIR: mlir Namespace Reference