Create a new FuncOp in MLIR

Hello.
Could you please help me in creating a new mlir::FuncOp and insert it in a ModuleOp.
The FuncOp needs to have a certain name and certain signature, and a certain body I need to create also.
Could you please help me with an example for this task.
Thank you very much,
Alex

llvm-project/Utils.cpp at main · llvm/llvm-project · GitHub shows you relevant code.

Details are:

  1. You need to set the insertion point to inside of the ModuleOp. This function does this by setting the insertion point just before another function
  2. You create a FunctionType and then create a FuncOp with that signature (and name).
  3. You can then create a Block inside of the FuncOp to put any code that you want.
1 Like

Hello. I receive the following error when I create a FuncOp without a body:
error: ‘builtin.func’ op symbol declaration cannot have public visibility
convert_sw_layers_to_Affine2.mlir:5:1: note: see current operation: “builtin.func”() ({
}) {sym_name = “Cos0”, type = (memref<1000xf16>, memref<1000xf16>) → ()} : () → ()

Does anybody know why this happens?
Thank you very much,
Alex

Hello. I got away from the above error (error: ‘builtin.func’ op symbol declaration cannot have public visibility) by adding the FuncOp a body and the FunctionType a return type.
So, I solved this problem.
Thank you,
Alex