[llvm] a8e486b - [Bitcode] Fix constexpr expansion creating invalid PHIs (#141560)
via llvm-commits
llvm-commits at lists.llvm.org
Tue May 27 06:51:52 PDT 2025
Author: Timothy Werquin
Date: 2025-05-27T15:51:48+02:00
New Revision: a8e486bfc4960a532d81f41c56d25e46e559157a
URL: https://github.com/llvm/llvm-project/commit/a8e486bfc4960a532d81f41c56d25e46e559157a
DIFF: https://github.com/llvm/llvm-project/commit/a8e486bfc4960a532d81f41c56d25e46e559157a.diff
LOG: [Bitcode] Fix constexpr expansion creating invalid PHIs (#141560)
Fixes errors about duplicate PHI edges when the input had duplicates
with constexprs in them. The constexpr translation makes new basic
blocks, causing the verifier to complain about duplicate entries in PHI
nodes.
Added:
llvm/test/Bitcode/constexpr-to-instr-dups.ll
llvm/test/Bitcode/constexpr-to-instr-dups.ll.bc
Modified:
llvm/lib/Bitcode/Reader/BitcodeReader.cpp
llvm/test/Bitcode/constexpr-to-instr.ll
Removed:
################################################################################
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 58625ee49c8ad..ce7b1ef65eed7 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -6095,14 +6095,18 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
// seen value here, to avoid expanding a constant expression multiple
// times.
auto It = Args.find(BB);
+ BasicBlock *EdgeBB = ConstExprEdgeBBs.lookup({BB, CurBB});
if (It != Args.end()) {
- PN->addIncoming(It->second, BB);
+ // If this predecessor was also replaced with a constexpr basic
+ // block, it must be de-duplicated.
+ if (!EdgeBB) {
+ PN->addIncoming(It->second, BB);
+ }
continue;
}
// If there already is a block for this edge (from a
diff erent phi),
// use it.
- BasicBlock *EdgeBB = ConstExprEdgeBBs.lookup({BB, CurBB});
if (!EdgeBB) {
// Otherwise, use a temporary block (that we will discard if it
// turns out to be unnecessary).
diff --git a/llvm/test/Bitcode/constexpr-to-instr-dups.ll b/llvm/test/Bitcode/constexpr-to-instr-dups.ll
new file mode 100644
index 0000000000000..dee9490b616fa
--- /dev/null
+++ b/llvm/test/Bitcode/constexpr-to-instr-dups.ll
@@ -0,0 +1,30 @@
+; RUN: opt -expand-constant-exprs %s.bc -S | FileCheck %s
+ at foo = external constant i32
+
+define i32 @test(i32 %arg) {
+entry:
+ switch i32 %arg, label %cont [
+ i32 1, label %cont
+ i32 2, label %nonconst
+ ]
+
+nonconst:
+ %cmp = icmp ne i32 %arg, 2
+ br i1 %cmp, label %cont, label %cont
+
+; CHECK-LABEL: phi.constexpr:
+; CHECK-NEXT: %constexpr = ptrtoint ptr @foo to i32
+; CHECK-NEXT: %constexpr1 = or i32 %constexpr, 5
+; CHECK-NEXT: br label %cont
+
+
+; CHECK-LABEL: cont:
+; CHECK-NEXT: %res = phi i32 [ %constexpr1, %phi.constexpr ], [ 1, %nonconst ], [ 1, %nonconst ]
+; CHECK-NEXT: ret i32 %res
+cont:
+ %res = phi i32 [or (i32 5, i32 ptrtoint (ptr @foo to i32)), %entry],
+ [or (i32 5, i32 ptrtoint (ptr @foo to i32)), %entry],
+ [1, %nonconst],
+ [1, %nonconst]
+ ret i32 %res
+}
diff --git a/llvm/test/Bitcode/constexpr-to-instr-dups.ll.bc b/llvm/test/Bitcode/constexpr-to-instr-dups.ll.bc
new file mode 100644
index 0000000000000..7897f51322fcc
Binary files /dev/null and b/llvm/test/Bitcode/constexpr-to-instr-dups.ll.bc
diff er
diff --git a/llvm/test/Bitcode/constexpr-to-instr.ll b/llvm/test/Bitcode/constexpr-to-instr.ll
index c099273de810f..c6c0f449ef29d 100644
--- a/llvm/test/Bitcode/constexpr-to-instr.ll
+++ b/llvm/test/Bitcode/constexpr-to-instr.ll
@@ -1,4 +1,4 @@
-; RUN: llvm-dis -expand-constant-exprs < %s.bc | FileCheck %s
+; RUN: opt -expand-constant-exprs -S %s.bc | FileCheck %s
@g = extern_weak global i32
@g2 = extern_weak global i32
@@ -225,7 +225,7 @@ define i64 @test_phi_multiple_identical_predecessors(i32 %x) {
; CHECK-NEXT: %constexpr = ptrtoint ptr @g to i64
; CHECK-NEXT: br label %join
; CHECK: join:
-; CHECK-NEXT: %phi = phi i64 [ %constexpr, %phi.constexpr ], [ %constexpr, %phi.constexpr ], [ 0, %default ]
+; CHECK-NEXT: %phi = phi i64 [ %constexpr, %phi.constexpr ], [ 0, %default ]
; CHECK-NEXT: ret i64 %phi
entry:
switch i32 %x, label %default [
More information about the llvm-commits
mailing list