I am bit new to this,. so this question might have been answered. However I cannot find a concrete solution. So here it goes. I have the following code.
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
using namespace mlir;
int main() {
// Initialize the MLIR context.
MLIRContext context;
context.loadDialect<func::FuncDialect>();
Builder builder(&context);
// Create an MLIR module.
OwningOpRef<ModuleOp> module(ModuleOp::create(UnknownLoc::get(&context)));
// Create a function type.
auto funcType = builder.getFunctionType(std::nullopt, std::nullopt);
// Create an MLIR function inside the module.
func::FuncOp func1 = func::FuncOp::create(builder.getUnknownLoc(), "my_function", funcType);
func1.setPrivate();
// Add the function to the module.
module->push_back(func1);
// Print the MLIR module.
module->print(llvm::outs());
return 0;
}
Specifically, this line is important. You must load a dialect before constructing ops that belong to the dialect. If MLIR is built with assertions (doesn’t seem to be the case for you), an assertion will fire when the dialect has not been loaded before an op is constructed.
I have assertions enabled on an Ubuntu machine. On MAC I could not compile the code, and I just used the brew install llvm version. That version installed LLVM and MLIR but I doubt they had the assertions enabled.