Skip to content

Conversation

@lukel97
Copy link
Contributor

@lukel97 lukel97 commented May 3, 2025

This adds a test that exercises the part of scalarizeBinOpOrCmp that produces immediate UB as described in #138095 (comment), but is fortunately currently folded into a correct transform.

I also noticed a bunch of immediate UB in some of the existing tests so this also cleans them up. They should still all be scalarized though.

@llvmbot
Copy link
Member

llvmbot commented May 3, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Luke Lau (lukel97)

Changes

This adds a test that exercises the part of scalarizeBinOpOrCmp that produces immediate UB, but is fortunately currently folded into a correct transform.

I also noticed a bunch of immediate UB in some of the existing tests so this also cleans them up. They should still all be scalarized though.


Full diff: https://github.com/llvm/llvm-project/pull/138395.diff

4 Files Affected:

  • (modified) llvm/test/Transforms/VectorCombine/AArch64/scalarize-scalable.ll (+1-1)
  • (modified) llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant-inseltpoison.ll (+18-18)
  • (modified) llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant.ll (+24-24)
  • (added) llvm/test/Transforms/VectorCombine/binop-scalarize.ll (+22)
diff --git a/llvm/test/Transforms/VectorCombine/AArch64/scalarize-scalable.ll b/llvm/test/Transforms/VectorCombine/AArch64/scalarize-scalable.ll
index ac7bc91fa3f25..6350ce7598a71 100644
--- a/llvm/test/Transforms/VectorCombine/AArch64/scalarize-scalable.ll
+++ b/llvm/test/Transforms/VectorCombine/AArch64/scalarize-scalable.ll
@@ -30,7 +30,7 @@ define <vscale x 4 x i32> @scalarize_scalable_udiv(i32 %x, i32 %y) {
 ; CHECK-NEXT:    ret <vscale x 4 x i32> [[R]]
 ;
   %splatx = insertelement <vscale x 4 x i32> poison, i32 %x, i64 0
-  %splaty = insertelement <vscale x 4 x i32> poison, i32 %y, i64 0
+  %splaty = insertelement <vscale x 4 x i32> splat (i32 1), i32 %y, i64 0
   %r = udiv <vscale x 4 x i32> %splatx, %splaty
   ret <vscale x 4 x i32> %r
 }
diff --git a/llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant-inseltpoison.ll b/llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant-inseltpoison.ll
index 05251cb829b2b..d45d5f4d44ff3 100644
--- a/llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant-inseltpoison.ll
+++ b/llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant-inseltpoison.ll
@@ -305,10 +305,10 @@ define <2 x i64> @lshr_constant_op1_not_undef_lane(i64 %x) {
 define <2 x i64> @urem_constant_op0(i64 %x) {
 ; CHECK-LABEL: @urem_constant_op0(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = urem i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> zeroinitializer, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> poison, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = urem <2 x i64> <i64 5, i64 undef>, %ins
   ret <2 x i64> %bo
 }
@@ -316,10 +316,10 @@ define <2 x i64> @urem_constant_op0(i64 %x) {
 define <2 x i64> @urem_constant_op0_not_undef_lane(i64 %x) {
 ; CHECK-LABEL: @urem_constant_op0_not_undef_lane(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = urem i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> zeroinitializer, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> poison, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = urem <2 x i64> <i64 5, i64 2>, %ins
   ret <2 x i64> %bo
 }
@@ -331,7 +331,7 @@ define <2 x i64> @urem_constant_op1(i64 %x) {
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
   %ins = insertelement <2 x i64> poison, i64 %x, i32 1
-  %bo = urem <2 x i64> %ins, <i64 undef, i64 2>
+  %bo = urem <2 x i64> %ins, <i64 2, i64 2>
   ret <2 x i64> %bo
 }
 
@@ -360,10 +360,10 @@ define <2 x i64> @srem_constant_op0(i64 %x) {
 define <2 x i64> @srem_constant_op0_not_undef_lane(i64 %x) {
 ; CHECK-LABEL: @srem_constant_op0_not_undef_lane(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = srem i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> zeroinitializer, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> poison, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = srem <2 x i64> <i64 5, i64 2>, %ins
   ret <2 x i64> %bo
 }
@@ -375,7 +375,7 @@ define <2 x i64> @srem_constant_op1(i64 %x) {
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
   %ins = insertelement <2 x i64> poison, i64 %x, i32 1
-  %bo = srem <2 x i64> %ins, <i64 undef, i64 2>
+  %bo = srem <2 x i64> %ins, <i64 2, i64 2>
   ret <2 x i64> %bo
 }
 
@@ -393,10 +393,10 @@ define <2 x i64> @srem_constant_op1_not_undef_lane(i64 %x) {
 define <2 x i64> @udiv_constant_op0(i64 %x) {
 ; CHECK-LABEL: @udiv_constant_op0(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = udiv exact i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> <i64 5, i64 undef>, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> poison, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = udiv exact <2 x i64> <i64 5, i64 undef>, %ins
   ret <2 x i64> %bo
 }
@@ -404,10 +404,10 @@ define <2 x i64> @udiv_constant_op0(i64 %x) {
 define <2 x i64> @udiv_constant_op0_not_undef_lane(i64 %x) {
 ; CHECK-LABEL: @udiv_constant_op0_not_undef_lane(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = udiv exact i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> <i64 5, i64 2>, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> poison, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = udiv exact <2 x i64> <i64 5, i64 2>, %ins
   ret <2 x i64> %bo
 }
@@ -419,7 +419,7 @@ define <2 x i64> @udiv_constant_op1(i64 %x) {
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
   %ins = insertelement <2 x i64> poison, i64 %x, i32 1
-  %bo = udiv <2 x i64> %ins, <i64 undef, i64 2>
+  %bo = udiv <2 x i64> %ins, <i64 2, i64 2>
   ret <2 x i64> %bo
 }
 
@@ -437,10 +437,10 @@ define <2 x i64> @udiv_constant_op1_not_undef_lane(i64 %x) {
 define <2 x i64> @sdiv_constant_op0(i64 %x) {
 ; CHECK-LABEL: @sdiv_constant_op0(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = sdiv i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> <i64 5, i64 undef>, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> poison, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = sdiv <2 x i64> <i64 5, i64 undef>, %ins
   ret <2 x i64> %bo
 }
@@ -448,10 +448,10 @@ define <2 x i64> @sdiv_constant_op0(i64 %x) {
 define <2 x i64> @sdiv_constant_op0_not_undef_lane(i64 %x) {
 ; CHECK-LABEL: @sdiv_constant_op0_not_undef_lane(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = sdiv i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> <i64 5, i64 2>, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> poison, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = sdiv <2 x i64> <i64 5, i64 2>, %ins
   ret <2 x i64> %bo
 }
@@ -463,7 +463,7 @@ define <2 x i64> @sdiv_constant_op1(i64 %x) {
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
   %ins = insertelement <2 x i64> poison, i64 %x, i32 1
-  %bo = sdiv exact <2 x i64> %ins, <i64 undef, i64 2>
+  %bo = sdiv exact <2 x i64> %ins, <i64 2, i64 2>
   ret <2 x i64> %bo
 }
 
diff --git a/llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant.ll b/llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant.ll
index bbdd76c58b58e..2b5a58ea44de4 100644
--- a/llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant.ll
+++ b/llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant.ll
@@ -305,10 +305,10 @@ define <2 x i64> @lshr_constant_op1_not_undef_lane(i64 %x) {
 define <2 x i64> @urem_constant_op0(i64 %x) {
 ; CHECK-LABEL: @urem_constant_op0(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = urem i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> zeroinitializer, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> undef, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = urem <2 x i64> <i64 5, i64 undef>, %ins
   ret <2 x i64> %bo
 }
@@ -316,10 +316,10 @@ define <2 x i64> @urem_constant_op0(i64 %x) {
 define <2 x i64> @urem_constant_op0_not_undef_lane(i64 %x) {
 ; CHECK-LABEL: @urem_constant_op0_not_undef_lane(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = urem i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> zeroinitializer, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> undef, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = urem <2 x i64> <i64 5, i64 2>, %ins
   ret <2 x i64> %bo
 }
@@ -327,11 +327,11 @@ define <2 x i64> @urem_constant_op0_not_undef_lane(i64 %x) {
 define <2 x i64> @urem_constant_op1(i64 %x) {
 ; CHECK-LABEL: @urem_constant_op1(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = urem i64 [[X:%.*]], 2
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> <i64 poison, i64 0>, i64 [[BO_SCALAR]], i64 1
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> zeroinitializer, i64 [[BO_SCALAR]], i64 1
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
   %ins = insertelement <2 x i64> undef, i64 %x, i32 1
-  %bo = urem <2 x i64> %ins, <i64 undef, i64 2>
+  %bo = urem <2 x i64> %ins, <i64 2, i64 2>
   ret <2 x i64> %bo
 }
 
@@ -349,10 +349,10 @@ define <2 x i64> @urem_constant_op1_not_undef_lane(i64 %x) {
 define <2 x i64> @srem_constant_op0(i64 %x) {
 ; CHECK-LABEL: @srem_constant_op0(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = srem i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> zeroinitializer, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> undef, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = srem <2 x i64> <i64 5, i64 undef>, %ins
   ret <2 x i64> %bo
 }
@@ -360,10 +360,10 @@ define <2 x i64> @srem_constant_op0(i64 %x) {
 define <2 x i64> @srem_constant_op0_not_undef_lane(i64 %x) {
 ; CHECK-LABEL: @srem_constant_op0_not_undef_lane(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = srem i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> zeroinitializer, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> undef, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = srem <2 x i64> <i64 5, i64 2>, %ins
   ret <2 x i64> %bo
 }
@@ -371,11 +371,11 @@ define <2 x i64> @srem_constant_op0_not_undef_lane(i64 %x) {
 define <2 x i64> @srem_constant_op1(i64 %x) {
 ; CHECK-LABEL: @srem_constant_op1(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = srem i64 [[X:%.*]], 2
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> <i64 poison, i64 0>, i64 [[BO_SCALAR]], i64 1
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> zeroinitializer, i64 [[BO_SCALAR]], i64 1
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
   %ins = insertelement <2 x i64> undef, i64 %x, i32 1
-  %bo = srem <2 x i64> %ins, <i64 undef, i64 2>
+  %bo = srem <2 x i64> %ins, <i64 2, i64 2>
   ret <2 x i64> %bo
 }
 
@@ -393,10 +393,10 @@ define <2 x i64> @srem_constant_op1_not_undef_lane(i64 %x) {
 define <2 x i64> @udiv_constant_op0(i64 %x) {
 ; CHECK-LABEL: @udiv_constant_op0(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = udiv exact i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> <i64 5, i64 undef>, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> undef, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = udiv exact <2 x i64> <i64 5, i64 undef>, %ins
   ret <2 x i64> %bo
 }
@@ -404,10 +404,10 @@ define <2 x i64> @udiv_constant_op0(i64 %x) {
 define <2 x i64> @udiv_constant_op0_not_undef_lane(i64 %x) {
 ; CHECK-LABEL: @udiv_constant_op0_not_undef_lane(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = udiv exact i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> <i64 5, i64 2>, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> undef, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = udiv exact <2 x i64> <i64 5, i64 2>, %ins
   ret <2 x i64> %bo
 }
@@ -415,11 +415,11 @@ define <2 x i64> @udiv_constant_op0_not_undef_lane(i64 %x) {
 define <2 x i64> @udiv_constant_op1(i64 %x) {
 ; CHECK-LABEL: @udiv_constant_op1(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = udiv i64 [[X:%.*]], 2
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> <i64 poison, i64 0>, i64 [[BO_SCALAR]], i64 1
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> zeroinitializer, i64 [[BO_SCALAR]], i64 1
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
   %ins = insertelement <2 x i64> undef, i64 %x, i32 1
-  %bo = udiv <2 x i64> %ins, <i64 undef, i64 2>
+  %bo = udiv <2 x i64> %ins, <i64 2, i64 2>
   ret <2 x i64> %bo
 }
 
@@ -437,10 +437,10 @@ define <2 x i64> @udiv_constant_op1_not_undef_lane(i64 %x) {
 define <2 x i64> @sdiv_constant_op0(i64 %x) {
 ; CHECK-LABEL: @sdiv_constant_op0(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = sdiv i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> <i64 5, i64 undef>, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> undef, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = sdiv <2 x i64> <i64 5, i64 undef>, %ins
   ret <2 x i64> %bo
 }
@@ -448,10 +448,10 @@ define <2 x i64> @sdiv_constant_op0(i64 %x) {
 define <2 x i64> @sdiv_constant_op0_not_undef_lane(i64 %x) {
 ; CHECK-LABEL: @sdiv_constant_op0_not_undef_lane(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = sdiv i64 5, [[X:%.*]]
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> poison, i64 [[BO_SCALAR]], i64 0
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> <i64 5, i64 2>, i64 [[BO_SCALAR]], i64 0
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
-  %ins = insertelement <2 x i64> undef, i64 %x, i32 0
+  %ins = insertelement <2 x i64> splat (i64 1), i64 %x, i32 0
   %bo = sdiv <2 x i64> <i64 5, i64 2>, %ins
   ret <2 x i64> %bo
 }
@@ -459,11 +459,11 @@ define <2 x i64> @sdiv_constant_op0_not_undef_lane(i64 %x) {
 define <2 x i64> @sdiv_constant_op1(i64 %x) {
 ; CHECK-LABEL: @sdiv_constant_op1(
 ; CHECK-NEXT:    [[BO_SCALAR:%.*]] = sdiv exact i64 [[X:%.*]], 2
-; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> <i64 poison, i64 0>, i64 [[BO_SCALAR]], i64 1
+; CHECK-NEXT:    [[BO:%.*]] = insertelement <2 x i64> zeroinitializer, i64 [[BO_SCALAR]], i64 1
 ; CHECK-NEXT:    ret <2 x i64> [[BO]]
 ;
   %ins = insertelement <2 x i64> undef, i64 %x, i32 1
-  %bo = sdiv exact <2 x i64> %ins, <i64 undef, i64 2>
+  %bo = sdiv exact <2 x i64> %ins, <i64 2, i64 2>
   ret <2 x i64> %bo
 }
 
diff --git a/llvm/test/Transforms/VectorCombine/binop-scalarize.ll b/llvm/test/Transforms/VectorCombine/binop-scalarize.ll
new file mode 100644
index 0000000000000..52a706a0b59a7
--- /dev/null
+++ b/llvm/test/Transforms/VectorCombine/binop-scalarize.ll
@@ -0,0 +1,22 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -S -p vector-combine | FileCheck %s
+
+; FIXME: The way we scalarize this today is by creating a
+;
+; %v = udiv <4 x i8> splat (i8 1), <i8 0, i8 1, i8 1, i8 1>
+;
+; to insert the scalar into, which is immediate UB.  Coincidentally, at creation
+; this gets constant folded into <i8 poison, i8 1, i8 1, i8 1>, but we shouldn't
+; rely on this.
+define <4 x i8> @udiv_ub(i8 %x, i8 %y) {
+; CHECK-LABEL: define <4 x i8> @udiv_ub(
+; CHECK-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) {
+; CHECK-NEXT:    [[V_SCALAR:%.*]] = udiv i8 [[X]], [[Y]]
+; CHECK-NEXT:    [[V:%.*]] = insertelement <4 x i8> <i8 poison, i8 1, i8 1, i8 1>, i8 [[V_SCALAR]], i64 0
+; CHECK-NEXT:    ret <4 x i8> [[V]]
+;
+  %x.insert = insertelement <4 x i8> splat (i8 1), i8 %x, i32 0
+  %y.insert = insertelement <4 x i8> <i8 0, i8 1, i8 1, i8 1>, i8 %y, i32 0
+  %v = udiv <4 x i8> %x.insert, %y.insert
+  ret <4 x i8> %v
+}

@github-actions
Copy link

github-actions bot commented May 3, 2025

⚠️ undef deprecator found issues in your code. ⚠️

You can test this locally with the following command:
git diff -U0 --pickaxe-regex -S '([^a-zA-Z0-9#_-]undef[^a-zA-Z0-9_-]|UndefValue::get)' 'HEAD~1' HEAD llvm/test/Transforms/VectorCombine/binop-scalarize.ll llvm/test/Transforms/VectorCombine/AArch64/scalarize-scalable.ll llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant-inseltpoison.ll llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant.ll

The following files introduce new uses of undef:

  • llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant-inseltpoison.ll
  • llvm/test/Transforms/VectorCombine/X86/insert-binop-with-constant.ll

Undef is now deprecated and should only be used in the rare cases where no replacement is possible. For example, a load of uninitialized memory yields undef. You should use poison values for placeholders instead.

In tests, avoid using undef and having tests that trigger undefined behavior. If you need an operand with some unimportant value, you can add a new argument to the function and use that instead.

For example, this is considered a bad practice:

define void @fn() {
  ...
  br i1 undef, ...
}

Please use the following instead:

define void @fn(i1 %cond) {
  ...
  br i1 %cond, ...
}

Please refer to the Undefined Behavior Manual for more information.

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you!

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - cheers

@lukel97 lukel97 merged commit 9a7e307 into llvm:main May 5, 2025
12 of 13 checks passed
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…ting tests. NFC (llvm#138395)

This adds a test that exercises the part of scalarizeBinOpOrCmp that
produces immediate UB as described in
llvm#138095 (comment),
but is fortunately currently folded into a correct transform.

I also noticed a bunch of immediate UB in some of the existing tests so
this also cleans them up. They should still all be scalarized though.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
…ting tests. NFC (llvm#138395)

This adds a test that exercises the part of scalarizeBinOpOrCmp that
produces immediate UB as described in
llvm#138095 (comment),
but is fortunately currently folded into a correct transform.

I also noticed a bunch of immediate UB in some of the existing tests so
this also cleans them up. They should still all be scalarized though.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants