[FlattenCFG] Fix the miscompilation where phi nodes exist in the merge point#81987
Merged
Conversation
Member
|
@llvm/pr-subscribers-llvm-transforms Author: Yingwei Zheng (dtcxzyw) ChangesWhen there are phi nodes in the merge point of the if-region, we cannot do the merge. Full diff: https://github.com/llvm/llvm-project/pull/81987.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Utils/FlattenCFG.cpp b/llvm/lib/Transforms/Utils/FlattenCFG.cpp
index db6ee39baece7b..5a444c503fc4e9 100644
--- a/llvm/lib/Transforms/Utils/FlattenCFG.cpp
+++ b/llvm/lib/Transforms/Utils/FlattenCFG.cpp
@@ -407,6 +407,10 @@ bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Block1, BasicBlock *Block2,
/// form, by inverting the condition and the branch successors. The same
/// approach goes for the opposite case.
bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder) {
+ // We cannot merge the if-region if the merge point has phi nodes.
+ if (isa<PHINode>(BB->front()))
+ return false;
+
BasicBlock *IfTrue2, *IfFalse2;
BranchInst *DomBI2 = GetIfCondition(BB, IfTrue2, IfFalse2);
if (!DomBI2)
diff --git a/llvm/test/Transforms/Util/flatten-cfg.ll b/llvm/test/Transforms/Util/flatten-cfg.ll
index d6e34bda1cc60f..038dcaa47419ad 100644
--- a/llvm/test/Transforms/Util/flatten-cfg.ll
+++ b/llvm/test/Transforms/Util/flatten-cfg.ll
@@ -77,13 +77,16 @@ define void @test_not_crash3(i32 %a) #0 {
; CHECK-SAME: (i32 [[A:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A_EQ_0:%.*]] = icmp eq i32 [[A]], 0
+; CHECK-NEXT: br i1 [[A_EQ_0]], label [[BB0:%.*]], label [[BB1:%.*]]
+; CHECK: bb0:
+; CHECK-NEXT: br label [[BB1]]
+; CHECK: bb1:
; CHECK-NEXT: [[A_EQ_1:%.*]] = icmp eq i32 [[A]], 1
-; CHECK-NEXT: [[TMP0:%.*]] = or i1 [[A_EQ_0]], [[A_EQ_1]]
-; CHECK-NEXT: br i1 [[TMP0]], label [[BB2:%.*]], label [[BB3:%.*]]
+; CHECK-NEXT: br i1 [[A_EQ_1]], label [[BB2:%.*]], label [[BB3:%.*]]
; CHECK: bb2:
; CHECK-NEXT: br label [[BB3]]
; CHECK: bb3:
-; CHECK-NEXT: [[CHECK_BADREF:%.*]] = phi i32 [ 17, [[ENTRY:%.*]] ], [ 11, [[BB2]] ]
+; CHECK-NEXT: [[CHECK_BADREF:%.*]] = phi i32 [ 17, [[BB1]] ], [ 11, [[BB2]] ]
; CHECK-NEXT: ret void
;
entry:
@@ -278,9 +281,9 @@ define i1 @test_cond_multi_use(i32 %x, i32 %y, i32 %z) {
; CHECK-NEXT: entry.x:
; CHECK-NEXT: [[CMP_X:%.*]] = icmp ne i32 [[X]], 0
; CHECK-NEXT: [[CMP_Y:%.*]] = icmp eq i32 [[Y]], 0
-; CHECK-NEXT: [[TMP0:%.*]] = xor i1 [[CMP_Y]], true
-; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[CMP_X]], [[TMP0]]
-; CHECK-NEXT: br i1 [[TMP1]], label [[IF_THEN_Y:%.*]], label [[EXIT:%.*]]
+; CHECK-NEXT: [[CMP_Y_NOT:%.*]] = xor i1 [[CMP_Y]], true
+; CHECK-NEXT: [[TMP0:%.*]] = or i1 [[CMP_X]], [[CMP_Y_NOT]]
+; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN_Y:%.*]], label [[EXIT:%.*]]
; CHECK: if.then.y:
; CHECK-NEXT: store i32 [[Z]], ptr @g, align 4
; CHECK-NEXT: br label [[EXIT]]
|
Member
Author
Yeah, the original patch(https://reviews.llvm.org/D68032) just fixed the crash in an incorrect way. |
Member
Author
|
@arsenm Any comments? |
arsenm
approved these changes
Feb 25, 2024
llvmbot
pushed a commit
to llvmbot/llvm-project
that referenced
this pull request
Feb 25, 2024
…e point (llvm#81987) When there are phi nodes in the merge point of the if-region, we cannot do the merge. Alive2: https://alive2.llvm.org/ce/z/DbgEan Fixes llvm#70900. (cherry picked from commit f920b74)
llvmbot
pushed a commit
to llvmbot/llvm-project
that referenced
this pull request
Feb 26, 2024
…e point (llvm#81987) When there are phi nodes in the merge point of the if-region, we cannot do the merge. Alive2: https://alive2.llvm.org/ce/z/DbgEan Fixes llvm#70900. (cherry picked from commit f920b74)
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When there are phi nodes in the merge point of the if-region, we cannot do the merge.
Alive2: https://alive2.llvm.org/ce/z/DbgEan
Fixes #70900.