Hello everyone,I attempted to create a subfunction in the MLIR file through OpBuilder and call between the main function and the subfunction by creating CallOp. However, the MLIR Parser(mlir::parseSourceFile) parsed the file and found that it cannot recognize the subfunctions in the mian function. Do I need to create a function declaration before the main function and how to create it? I hope there are relevant examples for reference.
Code
There is no parsing bug here, this is a verification error and you should try to understand it and fix it.
In this case, this hints at symbol visibility, which is documented here: Symbols and Symbol Tables - MLIR
If you look at the various examples in the tests in the repo, you may figure that the external declarations must be private:
thanks,i agree with your point of view.
I spent some time learning about Symbols and SymbolTable(DenseMap),know that ModuleOp is a SymbolTableOp that has a trait named SymbolTable,but funcOp does not,If you want to use callOp to call a symbol, you must use func.func to define a symbol first that needs to be inserted into the SymbolTable of MoudleOp.So I insert a new subfunction before the main func to insert the symbol into the SymbolTable of moduleOp.
Unfortunately, there were still errors.
error:‘func.call’ op ‘mysubfunc’ does not reference a valid function
I think that the definition of the insert function in file’Opdefinition.h’ is different from that of the insert function in SymbolTable.I used the following function.(end->front)
/// Insert the operation into the back of the body.
template <typename OpT = ConcreteType>
enable_if_single_region<OpT> push_back(Operation *op) {
insert(Block::iterator(getBody()->end()), op);
}
I have successfully completed inserting subfunction before the main function, but parse is completed in other file. Below is the output IR for completing pass。
I successfully compiled mlir project through clang14 debug version
Compiled successfully, but there were some issues with the link. I don’t seem to understand why
tools/mlir/examples/toy/Ch2/CMakeFiles/toyc-ch2.dir/toyc.cpp.o: In function mlir::detail::TypeIDResolver<mlir::func::FuncDialect, void>::resolveTypeID()': /public/home/xxx/llvm-project main/build/tools/mlir/include/mlir/Dialect/Func/IR/FuncOpsDialect.h.inc:32: undefined reference to mlir::detail::TypeIDResolver<mlir::func::FuncDialect, void>::id’
tools/mlir/examples/toy/Ch2/CMakeFiles/toyc-ch2.dir/toyc.cpp.o: In function operator()': /public/home/xxx/llvm-project-main/mlir/include/mlir/IR/MLIRContext.h:100: undefined reference to mlir::func::FuncDialect::FuncDialect(mlir::MLIRContext*)‘’
Thanks!
I solved this problem by adding MLIRFuncDilalect in Cmake. I understand that ParseSourceFile is parsed based on mlir text format, so it has been validated here(Toy-ch2). The issue I mentioned earlier was maybe due to the issue of using pybind to transfer data from the Python to the C++. I will continue to understand the issue here. Thank you for your helpful answer.
Excuse me, I have redesigned the test case and compared two different MLIR files. The MLIR modified based on toy ch2 can pass through the ParserSource file, but the other one is not. I understand that Parser is parsed based on MLIR files. I hope you can help me find out where the file does not comply with MLIR rules, resulting in invalid function calls. Thank you a lot.
Base on toy ch2:
There is not parser issue here, this is an IR verification failure.
It says: error: 'func.call' op 'sunfunc' does not reference a valid function ; which means it looks for a @sunfunc function in the current module but can’t find it (you just use a textual search you may figure out why quickly )
Haha, it’s because I manually modified the text, but this is not the main validation issue. I have already corrected the character error in the previous answer, which is indeed a low-level error. However, after the modification is completed, it still cannot be passed. Are there any other errors here?
Thank you for your patience!
I suspect the file we see here isn’t reflecting the IR that is failing…
For example I can feed this IR through ./bin/mlir-opt /tmp/t.mlir -allow-unregistered-dialect without problem, how do you get this particular IR to fail?
I am really not seeing how this can fail with the error you posted, again the exact same file works for me, so I would double check that the string passed here through loading filename is really what you posted.
Beyond that, I would build in debug mode and run within gdb.
I learned that the ParseSourceFile function parses MLIR files in text format, so I manually modified and created sub functions and function call relationships in this file(filename),yes, I am using gdb in debug mode, but for this project, the parser source code is linked in the form of a static link library (. a file), so I cannot obtain the relevant data to verify the issue, considering whether it is an MLIR text issue.
It’s extremely unlikely: the IR you’re showing does not match the error. If you have gdb and building the project with debug info, you should be able to break emitOpError, run, and then print the full module by walking back the IR in gdb.