Skip to content

Commit 69effe0

Browse files
committed
[SLP]Check for potential safety of the truncation for vectorized scalars with multi uses
If the vectorized scalars has multiple uses, need to check if it is safe to truncate the vectorized value, before actually trying doing it. Otherwise, the compiler may loose some important bits, which may lead to a miscompilation. Fixes #129057
1 parent 6a5bb4c commit 69effe0

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18071,6 +18071,12 @@ bool BoUpSLP::collectValuesToDemote(
1807118071
(void)for_each(E.Scalars, std::bind(IsPotentiallyTruncated, _1,
1807218072
std::ref(BitWidth)));
1807318073
} else {
18074+
// Several vectorized uses? Check if we can truncate it, otherwise -
18075+
// exit.
18076+
if (any_of(E.Scalars, [&](Value *V) {
18077+
return !V->hasOneUse() && !IsPotentiallyTruncated(V, BitWidth);
18078+
}))
18079+
return false;
1807418080
bool NeedToExit = false;
1807518081
if (Checker && !AttemptCheckBitwidth(Checker, NeedToExit))
1807618082
return false;

llvm/test/Transforms/SLPVectorizer/X86/ext-used-scalar-different-bitwidth.ll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ define i32 @test() {
1212
; CHECK-NEXT: [[TMP0:%.*]] = insertelement <8 x i32> <i32 poison, i32 83886080, i32 83886080, i32 83886080, i32 83886080, i32 83886080, i32 83886080, i32 83886080>, i32 [[ADD_I_I]], i32 0
1313
; CHECK-NEXT: [[TMP1:%.*]] = add <8 x i32> <i32 83886080, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0>, [[TMP0]]
1414
; CHECK-NEXT: [[TMP2:%.*]] = ashr <8 x i32> [[TMP1]], splat (i32 24)
15-
; CHECK-NEXT: [[TMP3:%.*]] = trunc <8 x i32> [[TMP2]] to <8 x i8>
16-
; CHECK-NEXT: [[TMP4:%.*]] = and <8 x i8> [[TMP3]], <i8 -65, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
17-
; CHECK-NEXT: [[TMP5:%.*]] = zext <8 x i8> [[TMP4]] to <8 x i32>
15+
; CHECK-NEXT: [[TMP5:%.*]] = and <8 x i32> [[TMP2]], <i32 66440127, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>
1816
; CHECK-NEXT: [[TMP6:%.*]] = shufflevector <8 x i32> [[TMP5]], <8 x i32> poison, <2 x i32> <i32 0, i32 poison>
1917
; CHECK-NEXT: [[TMP7:%.*]] = shufflevector <2 x i32> [[TMP6]], <2 x i32> <i32 poison, i32 0>, <2 x i32> <i32 0, i32 3>
2018
; CHECK-NEXT: [[TMP8:%.*]] = icmp ugt <2 x i32> [[TMP7]], <i32 33554431, i32 0>
2119
; CHECK-NEXT: [[TMP9:%.*]] = call <8 x i1> @llvm.vector.insert.v8i1.v2i1(<8 x i1> <i1 poison, i1 poison, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false>, <2 x i1> [[TMP8]], i64 0)
22-
; CHECK-NEXT: [[TMP10:%.*]] = select <8 x i1> [[TMP9]], <8 x i8> zeroinitializer, <8 x i8> <i8 6, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>
23-
; CHECK-NEXT: [[TMP11:%.*]] = shl <8 x i8> [[TMP4]], [[TMP10]]
20+
; CHECK-NEXT: [[TMP10:%.*]] = select <8 x i1> [[TMP9]], <8 x i32> zeroinitializer, <8 x i32> <i32 6, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0>
21+
; CHECK-NEXT: [[TMP13:%.*]] = shl <8 x i32> [[TMP5]], [[TMP10]]
22+
; CHECK-NEXT: [[TMP11:%.*]] = trunc <8 x i32> [[TMP13]] to <8 x i8>
2423
; CHECK-NEXT: [[TMP12:%.*]] = call i8 @llvm.vector.reduce.and.v8i8(<8 x i8> [[TMP11]])
2524
; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[TMP12]] to i32
2625
; CHECK-NEXT: ret i32 [[CONV]]

0 commit comments

Comments
 (0)