Author: eaeltsin Date: 2026-11-15T21:03:52+01:00 New Revision: 54dc6b01555f8310ee0db8e6a395b8435d250d81
URL: https://github.com/llvm/llvm-project/commit/54dc6b01555f8310ee0db8e6a395b8435d250d81 DIFF: https://github.com/llvm/llvm-project/commit/54dc6b01555f8310ee0db8e6a395b8435d250d81.diff LOG: [LoopSimplify] Use SmallSetVector for dead predecessor removal (#231155) In `simplifyOneLoop`, `BadPreds` was previously declared as `SmallPtrSet<BasicBlock*, 4>`. When a non-header loop block had 5 or more unreachable out-of-loop predecessors, `SmallPtrSet` overflowed its inline small array and iterated over `BasicBlock*` pointers in hash order under ASLR. Because `PHINode::removeIncomingValue` swaps each removed predecessor slot with the last `PHINode` operand, removing dead predecessors in pointer-hash order permuted the remaining live incoming blocks in the `PHINode` and produced non-deterministic IR and ThinLTO bitcode. Replace `SmallPtrSet<BasicBlock*, 4>` with `SmallSetVector<BasicBlock *, 4>` to deduplicate and remove dead predecessors in deterministic `predecessors(BB)` order. Fixes #231143 (cherry picked from commit 921c2c0b929eb05dd8560c6bead0b1e5345e35ca) Added: Modified: llvm/lib/Transforms/Utils/LoopSimplify.cpp llvm/test/Transforms/LoopSimplify/unreachable-loop-pred.ll Removed: ################################################################################ diff --git a/llvm/lib/Transforms/Utils/LoopSimplify.cpp b/llvm/lib/Transforms/Utils/LoopSimplify.cpp index 1a148809499078..334861b50ce6a6 100644 --- a/llvm/lib/Transforms/Utils/LoopSimplify.cpp +++ b/llvm/lib/Transforms/Utils/LoopSimplify.cpp @@ -490,7 +490,7 @@ static bool simplifyOneLoop(Loop *L, SmallVectorImpl<Loop *> &Worklist, if (BB == L->getHeader()) continue; - SmallPtrSet<BasicBlock*, 4> BadPreds; + SmallSetVector<BasicBlock *, 4> BadPreds; for (BasicBlock *P : predecessors(BB)) if (!L->contains(P)) BadPreds.insert(P); diff --git a/llvm/test/Transforms/LoopSimplify/unreachable-loop-pred.ll b/llvm/test/Transforms/LoopSimplify/unreachable-loop-pred.ll index 5234325b83c1e4..c61de4d7fa6122 100644 --- a/llvm/test/Transforms/LoopSimplify/unreachable-loop-pred.ll +++ b/llvm/test/Transforms/LoopSimplify/unreachable-loop-pred.ll @@ -1,4 +1,4 @@ -; RUN: opt -S -passes=loop-simplify -disable-output -verify-loop-info -verify-dom-info < %s +; RUN: opt -S -passes=loop-simplify -verify-loop-info -verify-dom-info < %s | FileCheck %s ; PR5235 ; When loopsimplify inserts a preheader for this loop, it should add the new @@ -62,6 +62,62 @@ retblock: ret void } +; Ensure that removing >= 5 unreachable out-of-loop predecessors preserves a +; deterministic incoming block order in the remaining PHINode operands. +define i32 @deterministic_dead_pred_removal(i32 %x, i1 %cond) { +; CHECK-LABEL: define i32 @deterministic_dead_pred_removal( +; CHECK: latch: +; CHECK-NEXT: %v = phi i32 [ 10, %live1 ], [ 20, %live2 ], [ 30, %live3 ], [ 40, %live4 ], [ 50, %live5 ] +; CHECK-NEXT: br i1 %cond, label %header, label %exit +entry: + br label %header + +header: + switch i32 %x, label %live1 [ + i32 1, label %live2 + i32 2, label %live3 + i32 3, label %live4 + i32 4, label %live5 + ] + +live1: + br label %latch + +live2: + br label %latch + +live3: + br label %latch + +live4: + br label %latch + +live5: + br label %latch + +dead1: + br label %latch + +dead2: + br label %latch + +dead3: + br label %latch + +dead4: + br label %latch + +dead5: + br label %latch + +latch: + %v = phi i32 [ 1, %dead1 ], [ 2, %dead2 ], [ 3, %dead3 ], [ 4, %dead4 ], [ 5, %dead5 ], [ 10, %live1 ], [ 20, %live2 ], [ 30, %live3 ], [ 40, %live4 ], [ 50, %live5 ] + br i1 %cond, label %header, label %exit + +exit: + ret i32 %v +} + ; Function Attrs: nounwind declare ptr @wobble() _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
