[llvm] [VPlan] Fix miscompile when widening induction with loop-variant step (PR #206010)
David Sherwood via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 26 02:24:19 PDT 2026
================
@@ -0,0 +1,131 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=loop-vectorize -S -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at .str.1 = constant [21 x i8] c"counter = %d\0A\00"
+
+; Variant-step Add induction: counter increments by (cmp ? 1 : 0), which is loop-variant.
+; Without the fix, VPlan replaced the variant step with IndDesc.getStep() and computed
+; the wrong exit value, emitting 'zext i8 2 to i32' and printf(..., i32 2).
+define i32 @_Z1fhi() {
+; CHECK-LABEL: define i32 @_Z1fhi() {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: br label %[[WHILE_BODY:.*]]
+; CHECK: [[WHILE_BODY]]:
+; CHECK-NEXT: [[COUNTER_07:%.*]] = phi i8 [ 0, %[[ENTRY]] ], [ [[COUNTER_1:%.*]], %[[WHILE_BODY]] ]
+; CHECK-NEXT: [[SUMB_05:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[SUMB_1:%.*]], %[[WHILE_BODY]] ]
+; CHECK-NEXT: [[SUMB_1]] = or i32 0, [[SUMB_05]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[COUNTER_07]], 1
+; CHECK-NEXT: [[INC:%.*]] = zext i1 [[CMP]] to i8
+; CHECK-NEXT: [[COUNTER_1]] = add i8 [[COUNTER_07]], [[INC]]
+; CHECK-NEXT: br i1 [[CMP]], label %[[WHILE_BODY]], label %[[WHILE_END:.*]]
+; CHECK: [[WHILE_END]]:
+; CHECK-NEXT: [[COUNTER_1_LCSSA:%.*]] = phi i8 [ [[COUNTER_1]], %[[WHILE_BODY]] ]
+; CHECK-NEXT: [[SUMB_1_LCSSA:%.*]] = phi i32 [ [[SUMB_1]], %[[WHILE_BODY]] ]
+; CHECK-NEXT: [[CONV2:%.*]] = zext i8 [[COUNTER_1_LCSSA]] to i32
+; CHECK-NEXT: [[CALL3:%.*]] = tail call i32 (ptr, ...) @printf(ptr @.str.1, i32 [[CONV2]])
+; CHECK-NEXT: ret i32 0
+;
+entry:
+ br label %while.body
+
+while.body:
----------------
david-arm wrote:
nit: Can you rename this to something like `loop:`?
https://github.com/llvm/llvm-project/pull/206010
More information about the llvm-commits
mailing list