[PATCH] D131776: [GVN] Fix miscompile shown in github issue #57025
Bjorn Pettersson via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 12 07:08:01 PDT 2022
bjope created this revision.
bjope added reviewers: nikic, fhahn.
Herald added a subscriber: hiraditya.
Herald added a project: All.
bjope requested review of this revision.
Herald added a project: LLVM.
Make sure that we are conservative in AnalyzeLoadAvailability when
finding an Unknown memdep, also when the dependent BB is dead.
Fixes: https://github.com/llvm/llvm-project/issues/57025
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D131776
Files:
llvm/lib/Transforms/Scalar/GVN.cpp
llvm/test/Transforms/GVN/memdep-unknown-deadblocks.ll
Index: llvm/test/Transforms/GVN/memdep-unknown-deadblocks.ll
===================================================================
--- llvm/test/Transforms/GVN/memdep-unknown-deadblocks.ll
+++ llvm/test/Transforms/GVN/memdep-unknown-deadblocks.ll
@@ -34,8 +34,7 @@
; CHECK-GVN-NEXT: [[ARR:%.*]] = alloca [4 x i16], align 1
; CHECK-GVN-NEXT: br label [[FOR_BODY:%.*]]
; CHECK-GVN: for.body:
-; CHECK-GVN-NEXT: [[VALUE2:%.*]] = phi i16 [ undef, [[ENTRY:%.*]] ], [ [[DEAD:%.*]], [[WHILE_END:%.*]] ]
-; CHECK-GVN-NEXT: [[I:%.*]] = phi i16 [ 0, [[ENTRY]] ], [ [[NEXT_I:%.*]], [[WHILE_END]] ]
+; CHECK-GVN-NEXT: [[I:%.*]] = phi i16 [ 0, [[ENTRY:%.*]] ], [ [[NEXT_I:%.*]], [[WHILE_END:%.*]] ]
; CHECK-GVN-NEXT: [[CMP0:%.*]] = icmp eq i16 [[I]], 0
; CHECK-GVN-NEXT: br i1 [[CMP0]], label [[STORE_IDX_0:%.*]], label [[STORE_IDX_I:%.*]]
; CHECK-GVN: store.idx.0:
@@ -46,7 +45,6 @@
; CHECK-GVN-NEXT: store i16 42, ptr [[ARR_I]], align 1
; CHECK-GVN-NEXT: br label [[STORE_DONE]]
; CHECK-GVN: store.done:
-; CHECK-GVN-NEXT: [[VALUE:%.*]] = phi i16 [ 42, [[STORE_IDX_I]] ], [ [[VALUE2]], [[STORE_IDX_0]] ]
; CHECK-GVN-NEXT: br label [[WHILE_BODY:%.*]]
; CHECK-GVN: while.body:
; CHECK-GVN-NEXT: br i1 false, label [[WHILE_BODY_WHILE_BODY_CRIT_EDGE:%.*]], label [[WHILE_END]]
@@ -54,10 +52,10 @@
; CHECK-GVN-NEXT: br label [[WHILE_BODY]]
; CHECK-GVN: while.end:
; CHECK-GVN-NEXT: [[ARR_J:%.*]] = getelementptr [4 x i16], ptr [[ARR]], i16 0, i16 [[I]]
+; CHECK-GVN-NEXT: [[VALUE:%.*]] = load i16, ptr [[ARR_J]], align 1
; CHECK-GVN-NEXT: tail call void @verify(i16 [[VALUE]])
; CHECK-GVN-NEXT: [[NEXT_I]] = add i16 [[I]], 1
; CHECK-GVN-NEXT: [[ARR_NEXT_I:%.*]] = getelementptr [4 x i16], ptr [[ARR]], i16 0, i16 [[NEXT_I]]
-; CHECK-GVN-NEXT: [[DEAD]] = load i16, ptr [[ARR_NEXT_I]], align 1
; CHECK-GVN-NEXT: [[CMP4:%.*]] = icmp slt i16 [[NEXT_I]], 3
; CHECK-GVN-NEXT: br i1 [[CMP4]], label [[FOR_BODY]], label [[FOR_END:%.*]]
; CHECK-GVN: for.end:
@@ -65,7 +63,7 @@
;
; CHECK-GVN-O1-LABEL: @test(
; CHECK-GVN-O1-NEXT: entry:
-; CHECK-GVN-O1-NEXT: tail call void @verify(i16 42)
+; CHECK-GVN-O1-NEXT: tail call void @verify(i16 7)
; CHECK-GVN-O1-NEXT: tail call void @verify(i16 42)
; CHECK-GVN-O1-NEXT: tail call void @verify(i16 42)
; CHECK-GVN-O1-NEXT: ret void
Index: llvm/lib/Transforms/Scalar/GVN.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/GVN.cpp
+++ llvm/lib/Transforms/Scalar/GVN.cpp
@@ -1290,6 +1290,14 @@
MemDepResult DepInfo = Deps[i].getResult();
if (DeadBlocks.count(DepBB)) {
+ // A non-local dependency means that the analysis might have given up
+ // e.g. based on phi translation failure (at least if the dependency is
+ // Unknown). Need to be conservative and say that the value isn't
+ // available.
+ if (DepInfo.isUnknown()) {
+ UnavailableBlocks.push_back(DepBB);
+ continue;
+ }
// Dead dependent mem-op disguise as a load evaluating the same value
// as the load in question.
ValuesPerBlock.push_back(AvailableValueInBlock::getUndef(DepBB));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131776.452177.patch
Type: text/x-patch
Size: 3230 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220812/a7edf999/attachment.bin>
More information about the llvm-commits
mailing list