We have a some use cases in conversion in Flang where we transform operation into runtime calls. At this time, we need to introduce the corresponding FuncOp that represent the runtime function.
So basically what we do is smth like:
if (auto funcOp = mod.lookupSymbol<mlir::func::FuncOp>(name)) return funcOp; mlir::OpBuilder modBuilder(module.getBodyRegion()); modBuilder.setInsertionPointToEnd(module.getBody()); auto result = modBuilder.create<mlir::func::FuncOp>(loc, name, type, attrs);
When we have conversion that run on operation we can end up with race condition and multiple FuncOp being created for the same runtime function.
We have couple of solutions to this problem but we wanted to double check if someone has anything better.
- Have a Module pass before that insert the needed FuncOp for runtime function.
- Add a lock in the conversion and acquire the lock around FuncOp lookup/creation.
- Switch the conversion to be runOnModule instead of runOnOperation but this seems a bit overkill.
We are currently experiencing we solution number 2 and it works fine but we are wondering if it will not have more serious implication.