Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[opt] Treat FuncCallStmt better in store-to-load forwarding in CFG (t…
…aichi-dev#8155) Issue: taichi-dev#602 Pass `gather_func_store_dests` gathers all destinations whose content may change after a real function is called. The change may happen in the real function or in another real function that the real function calls. This pass uses Tarjan's strongly connected components algorithm to find the store destinations for all real functions a kernel calls, and store them in `store_dests` of the respective function. The global pointers are lowered in `lower_access`, so we need to gather the store destinations twice: before and after pass `lower_access`. <!-- copilot:all --> ### <samp>🤖 Generated by Copilot at 2c5586e</samp> ### Summary 📝🛠️🚀 <!-- 1. 📝 This emoji represents the addition of a new file and a new analysis pass declaration, which are documentation-related changes. 2. 🛠️ This emoji represents the update of the `ControlFlowGraph` class and the removal of some redundant or incorrect checks, which are bug-fixing or improvement-related changes. 3. 🚀 This emoji represents the introduction of a new enum type, a new method, and a new parameter, which are feature-related changes. --> This pull request introduces a new analysis pass `gather_func_store_dests` that can handle function calls in the IR and optimize their memory access and aliasing. It updates the `Function`, `FuncCallStmt`, and `ControlFlowGraph` classes and the `compile_function` and `compile_taichi_functions` transforms to use a new enum type `IRStage` and a new parameter `target_stage` to track and control the IR stage of each function. It also modifies some existing analysis functions and adds some include directives and forward declarations to support the new pass. > _To optimize function calls in the IR_ > _We need a new pass to infer_ > _The store destinations_ > _At different stages_ > _And use `IRStage` instead of `IRType` for sure_ ### Walkthrough * Add a new analysis pass `gather_func_store_dests` to collect the store destinations of each function in the IR ([link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-0bfbe49ff08844a76d5d2e1c5b81c2cf813be4a9089422b997bc380ec9a68eadR1-R103), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-f6bc75768d2e24c782fefa45a7232d0e2b2bae091e697040e7f442a77d80ad45L216-R216)) * Modify the `FuncCallStmt` class to inherit from the `Store` trait and implement the `get_store_destination` method, using the arguments of the function call and the `store_dests` set of the called function ([link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-05e2a2d0a9c9879a4fb5fde9baf5a43738c7601fc53e234a40ab9bc27d1512a5R277-R289), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-917d9436dcaafa0f1e41ae9bad90273a303f036f00da94e417788a7fa1dc5260L1062-R1062), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-917d9436dcaafa0f1e41ae9bad90273a303f036f00da94e417788a7fa1dc5260R1074-R1080)) * Remove or modify the checks for `FuncCallStmt` in the `ControlFlowGraph` class, and use the `store_dests` set of the called function to update the reaching definition analysis ([link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-837b90142d1730f6a3ab20c91f1f35c95335ef82a021c74fd4dbdb05ff0e164fL164-L167), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-837b90142d1730f6a3ab20c91f1f35c95335ef82a021c74fd4dbdb05ff0e164fL219-R216), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-837b90142d1730f6a3ab20c91f1f35c95335ef82a021c74fd4dbdb05ff0e164fR695), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-837b90142d1730f6a3ab20c91f1f35c95335ef82a021c74fd4dbdb05ff0e164fL982-R977), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-837b90142d1730f6a3ab20c91f1f35c95335ef82a021c74fd4dbdb05ff0e164fR988-R990)) * Add a new member variable `func_store_dests` to the `ControlFlowGraph` class, which is a map from `Function` pointers to sets of `Stmt` pointers, representing the store destinations of each function in the IR ([link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-67e7205404aa056a1553f930af38b359e460f98a4ec335faec7d54aaf9df727fR117-R118)) * Replace the old enum type `IRType` with the new enum type `IRStage`, which has more values to indicate different IR stages of function compilation ([link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-aa860f71a793b08676a24cab247b43f5ed8d105a6493eeb1a035369b916bddc2L17-R17), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-aa860f71a793b08676a24cab247b43f5ed8d105a6493eeb1a035369b916bddc2L32-R32), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-af3316673541832f351d12d7c2f45b3c49ba5caeafdad3a6356cb13d2524be3dL9-R20), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-af3316673541832f351d12d7c2f45b3c49ba5caeafdad3a6356cb13d2524be3dL31-R50), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-f78d8ce92dcf8a10d2a446d35cc26f47fd2a42314b0799d263196b6eb858fe76L13-R33), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-f78d8ce92dcf8a10d2a446d35cc26f47fd2a42314b0799d263196b6eb858fe76L39-R48), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-8fde186587db97b3bbc8a856e59bc4467b30257335b0fad064b4eebd521a912bL330-R390)) * Modify the signature of the `compile_function` function to use the new parameter `target_stage` instead of the old parameter `start_from_ast`, to indicate the desired IR stage of the function compilation ([link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-448ac6e85e192a27e5ec7c54cd8a91545dc7c83f62d030eafb9c190383cfe934L199-R200), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-8fde186587db97b3bbc8a856e59bc4467b30257335b0fad064b4eebd521a912bL330-R390)) * Modify the definition of the `compile_to_offloads` function to add two calls to the new analysis pass `gather_func_store_dests`, before and after the call to the `compile_taichi_functions` function, and to pass different `target_stage` parameters to the `compile_taichi_functions` function ([link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-8fde186587db97b3bbc8a856e59bc4467b30257335b0fad064b4eebd521a912bL47-R51)) * Add or modify the include directives and forward declarations for the header files `function.h`, `statements.h`, and `unordered_set` in the source files and header files that use the `Function` class, the `FuncCallStmt` class, or the `std::unordered_set` container ([link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-837b90142d1730f6a3ab20c91f1f35c95335ef82a021c74fd4dbdb05ff0e164fR9), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-67e7205404aa056a1553f930af38b359e460f98a4ec335faec7d54aaf9df727fR10), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-05e2a2d0a9c9879a4fb5fde9baf5a43738c7601fc53e234a40ab9bc27d1512a5R5), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-448ac6e85e192a27e5ec7c54cd8a91545dc7c83f62d030eafb9c190383cfe934R20), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-af3316673541832f351d12d7c2f45b3c49ba5caeafdad3a6356cb13d2524be3dR3)) * Modify some comments in the header file `transforms.h` to remove the mentions of not demoting dense struct fors or reducing the number of statements before inlining, since these are no longer relevant or necessary after the new analysis pass ([link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-448ac6e85e192a27e5ec7c54cd8a91545dc7c83f62d030eafb9c190383cfe934L160-R161), [link](https://github.com/taichi-dev/taichi/pull/8155/files?diff=unified&w=0#diff-448ac6e85e192a27e5ec7c54cd8a91545dc7c83f62d030eafb9c190383cfe934L192-R190))
- Loading branch information