[mlir][CSE] Introduce hoist-pure-ops logic to CSE pass - #180556
linuxlonelyeagle wants to merge 13 commits into
Conversation
cb5ac21 to
c41619a
Compare
|
It looks like the CI failed to build because it couldn't pull LLVM. |
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
|
Apologies, I'm running into some issues with Flang and MLIR Python bindings. I'm currently investigating. |
after cse of PR, It report, Can we allow pure operations to exist within the 'hlfir.forall' op's body region? cc: @tarunprabhu @banach-space @eugeneepshteyn @joker-eph @ftynse @matthias-springer @kuhar . I would appreciate any suggestions you might have. Thanks! |
after cse.I've gone through some test files, and it might make sense to add the IsIsolatedFromAbove trait to irdl.operation. |
2f2cdf6 to
809d7f5
Compare
…eInfo> comment (#190471) The original comment claimed that DominanceInfo and PostDominanceInfo could be preserved because region operations are not removed. However, the real reason was that the original CSE only deleted redundant operations without moving any operation to a different block, leaving the dominance tree structure unchanged. Part of #180556.
…ostDominanceInfo> comment (#190471) The original comment claimed that DominanceInfo and PostDominanceInfo could be preserved because region operations are not removed. However, the real reason was that the original CSE only deleted redundant operations without moving any operation to a different block, leaving the dominance tree structure unchanged. Part of llvm/llvm-project#180556.
Let me address this question now, llvm-project/mlir/lib/IR/Dominance.cpp Line 299 in 682ae8b We only rely on the Dominator Tree when determining dominance relationships between different blocks within the same region. In all other cases, dominance can be inferred from the IR's relative positions. The implementation of DominanceInfo leverages LLVM's DominatorTreeBase, which aligns perfectly with LLVM IR's design. This means that although I am hoisting operations here, it does not fundamentally change the relative positioning of blocks within the region. Therefore, DominanceInfo and PostDominanceInfo remain valid.
I’ve added some test cases and would love to hear your thoughts. I’m definitely looking for more feedback on this part. 😉. |
Contrary to LLVM, we do have regions: when we CSE an operation with regions it means we're deleting blocks. Are we deleting the entry for these regions in the analysis cache? |
Thanks for bringing this up. It really pushed me to think more deeply about this issue. To be honest, this issue still persists in the original CSE, such as in the |
In DominanceInfoBase, a DenseMap is used to store the dominator tree for each region. I think we should also remove the stale cache from this map when a region-carrying operation is deleted. We could explicitly add a method to DominanceInfoBase to erase a region's dominator tree; otherwise, the memory occupied by these trees won't be released until DominanceInfo itself is destroyed. |
This PR fixes a regression where the numCSE statistic was being incremented twice for a single operation elimination. The numCSE counter is already internally incremented within the replaceUsesAndDelete function. Manually incrementing it again after the function call leads to an inaccurate total count. This is part of the llvm/llvm-project#180556.
…ostDominanceInfo> comment (#190471) The original comment claimed that DominanceInfo and PostDominanceInfo could be preserved because region operations are not removed. However, the real reason was that the original CSE only deleted redundant operations without moving any operation to a different block, leaving the dominance tree structure unchanged. Part of llvm/llvm-project#180556.
Co-authored-by: Mehdi Amini <joker.eph@gmail.com>
Co-authored-by: Mehdi Amini <joker.eph@gmail.com>
ee867ea to
23c9035
Compare
…eInfo> comment (llvm#190471) The original comment claimed that DominanceInfo and PostDominanceInfo could be preserved because region operations are not removed. However, the real reason was that the original CSE only deleted redundant operations without moving any operation to a different block, leaving the dominance tree structure unchanged. Part of llvm#180556.
This PR fixes a regression where the numCSE statistic was being incremented twice for a single operation elimination. The numCSE counter is already internally incremented within the replaceUsesAndDelete function. Manually incrementing it again after the function call leads to an inaccurate total count. This is part of the llvm/llvm-project#180556.
|
Please feel free to continue the review when you have time. Thank you all! |
This PR is based on this theory:
if an Op is a Pure Op, we have the opportunity to hoist its position based on SSA dominance. This logic has now been incorporated into the CSE pass, now we can use it to further optimize the IR to achieve more concise code.RFC: https://discourse.llvm.org/t/rfc-mlir-introduce-hoist-pure-ops-pass/88903