Author: Tavva Dinesh Reddy Date: 2026-10-27T18:58:14Z New Revision: 7ddf6cc19e82e40d76afe34264ad4d206f6117a4
URL: https://github.com/llvm/llvm-project/commit/7ddf6cc19e82e40d76afe34264ad4d206f6117a4 DIFF: https://github.com/llvm/llvm-project/commit/7ddf6cc19e82e40d76afe34264ad4d206f6117a4.diff LOG: [TailCallElim] Drop poison-generating attributes and metadata from accumulator (#233527) When accumulator recursion elimination rewrites the accumulator instruction to use the accumulator PHI (and clones it at each return in `cleanupAndFinalize()`), its operands change, so poison-generating annotations derived from the original operands may no longer hold. Only flags were dropped so far (#200624); return attributes like `range()` and metadata like `!range` were kept. In the reproducer from the issue, `umax(%r, 1)` carries `range(i8 1, 3)`, and the per-return copy `umax(%accumulator.tr, %k)` kept it although it can return 0, so `-O2` inferred `range(i8 1, 3)` for the whole function (miscompiling the Rust code linked in the issue). This patch uses `dropPoisonGeneratingAnnotations()` instead of `dropPoisonGeneratingFlags()`. Fixes #233085 (cherry picked from commit 21c1bcd7059ca56dee6bc377e60a06a3b18a1d69) Added: Modified: llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp llvm/test/Transforms/TailCallElim/accum_recursion.ll Removed: ################################################################################ diff --git a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp index 3c38171292718b..a59e7b3b0c603f 100644 --- a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp +++ b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp @@ -722,9 +722,10 @@ bool TailRecursionEliminator::eliminateCall(CallInst *CI) { // inserted. AccRecInstr->setOperand(AccRecInstr->getOperand(0) != CI, AccPN); - // Reassociating into the loop reorders the operands, so flags from the - // original order (nsw/nuw/exact/...) may no longer hold. - AccRecInstr->dropPoisonGeneratingFlags(); + // Reassociating into the loop reorders the operands, so poison-generating + // flags (nsw/nuw/exact/...), attributes (range/nonnull/...) and metadata + // (!range/...) from the original order may no longer hold. + AccRecInstr->dropPoisonGeneratingAnnotations(); } // Update our return value tracking diff --git a/llvm/test/Transforms/TailCallElim/accum_recursion.ll b/llvm/test/Transforms/TailCallElim/accum_recursion.ll index 0ee581c006950c..07066d1098bd52 100644 --- a/llvm/test/Transforms/TailCallElim/accum_recursion.ll +++ b/llvm/test/Transforms/TailCallElim/accum_recursion.ll @@ -367,6 +367,42 @@ rec: ret i32 %acc } +; The per-return copy of the accumulator must not keep poison-generating +; attributes or metadata (like range). +define i8 @test_drop_poison_attrs(i8 %n, i8 %r) { +; CHECK-LABEL: define i8 @test_drop_poison_attrs( +; CHECK-SAME: i8 [[N:%.*]], i8 [[R:%.*]]) { +; CHECK-NEXT: entry: +; CHECK-NEXT: br label [[TAILRECURSE:%.*]] +; CHECK: tailrecurse: +; CHECK-NEXT: [[ACCUMULATOR_TR:%.*]] = phi i8 [ 0, [[ENTRY:%.*]] ], [ [[M:%.*]], [[RECURSE:%.*]] ] +; CHECK-NEXT: [[N_TR:%.*]] = phi i8 [ [[N]], [[ENTRY]] ], [ [[SUB:%.*]], [[RECURSE]] ] +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[N_TR]], 0 +; CHECK-NEXT: br i1 [[CMP]], label [[RETURN:%.*]], label [[RECURSE]] +; CHECK: recurse: +; CHECK-NEXT: [[SUB]] = sub i8 [[N_TR]], 1 +; CHECK-NEXT: [[M]] = tail call i8 @llvm.umax.i8(i8 [[ACCUMULATOR_TR]], i8 1) +; CHECK-NEXT: br label [[TAILRECURSE]] +; CHECK: return: +; CHECK-NEXT: [[ACCUMULATOR_RET_TR:%.*]] = tail call i8 @llvm.umax.i8(i8 [[ACCUMULATOR_TR]], i8 [[R]]) +; CHECK-NEXT: ret i8 [[ACCUMULATOR_RET_TR]] +; +entry: + %cmp = icmp eq i8 %n, 0 + br i1 %cmp, label %return, label %recurse + +recurse: + %sub = sub i8 %n, 1 + %call = tail call i8 @test_drop_poison_attrs(i8 %sub, i8 %r) + %m = call range(i8 1, 3) i8 @llvm.umax.i8(i8 %call, i8 1) + ret i8 %m + +return: + ret i8 %r +} + +declare i8 @llvm.umax.i8(i8, i8) + %struct.VecListNode = type { <4 x i32>, ptr } ; The identity for a vector add accumulator is the zero vector. _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
