Clarification on thread safety of a module pass with internal rewriter

Hi,

Can I have rewrite patterns on a nested op (lets say a nested MatmulOp) adding function prototypes (rw.create<FuncOp>) to the parent ModuleOp and function calls (rw.create<CallOp>) to the parent FuncOp? Is it thread safe to have this executed on a ModuleOp pass (std::unique_ptr<OperationPass<ModuleOp>> createMyModuleOpPass())?

In a ModuleOp pass, I am currently doing walks on the ModuleOp in search for the MatmulOps (module.walk([&](linalg::MatmulOp op), and using a OpBuilder to modify the op's parent module and function. This works ok.

Would it be ok to switch these Walks+OpBuilder transformations to rewrite patterns for the MatmulOp? If multiple MatmulOps exist would this be thread safe?

Patterns are not concerned with multithreading, only passes are. So as long as modifications performed by the pattern are within the scope of the operation on which the surrounding pass runs (module, in this case), there is no concurrency hazard.

2 Likes