Skip to content

Commit 90bc133

Browse files
fhahnkrishna2803
authored andcommitted
[VPlan] Update type of cloned instruction in scalarizeInstruction.
The operands of the replicate recipe may have been narrowed, resulting in a narrower result type. Update the type of the cloned instruction to the correct type. Fixes llvm#151392.
1 parent c16b4f5 commit 90bc133

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,12 +2836,12 @@ static void scalarizeInstruction(const Instruction *Instr,
28362836
Instruction *Cloned = Instr->clone();
28372837
if (!IsVoidRetTy) {
28382838
Cloned->setName(Instr->getName() + ".cloned");
2839-
#if !defined(NDEBUG)
2840-
// Verify that VPlan type inference results agree with the type of the
2841-
// generated values.
2842-
assert(State.TypeAnalysis.inferScalarType(RepRecipe) == Cloned->getType() &&
2843-
"inferred type and type from generated instructions do not match");
2844-
#endif
2839+
Type *ResultTy = State.TypeAnalysis.inferScalarType(RepRecipe);
2840+
// The operands of the replicate recipe may have been narrowed, resulting in
2841+
// a narrower result type. Update the type of the cloned instruction to the
2842+
// correct type.
2843+
if (ResultTy != Cloned->getType())
2844+
Cloned->mutateType(ResultTy);
28452845
}
28462846

28472847
RepRecipe->applyFlags(*Cloned);

llvm/test/Transforms/LoopVectorize/single-scalar-cast-minbw.ll

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,76 @@ loop:
6262
exit:
6363
ret void
6464
}
65+
66+
; Test case for https://github.com/llvm/llvm-project/issues/151392.
67+
define void @single_scalar_cast_stored(ptr %src, ptr %dst, i32 %n) {
68+
; CHECK-LABEL: define void @single_scalar_cast_stored(
69+
; CHECK-SAME: ptr [[SRC:%.*]], ptr [[DST:%.*]], i32 [[N:%.*]]) {
70+
; CHECK-NEXT: [[ENTRY:.*]]:
71+
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[N]], 4
72+
; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_MEMCHECK:.*]]
73+
; CHECK: [[VECTOR_MEMCHECK]]:
74+
; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[DST]], i64 2
75+
; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i8, ptr [[SRC]], i64 2
76+
; CHECK-NEXT: [[BOUND0:%.*]] = icmp ult ptr [[DST]], [[SCEVGEP1]]
77+
; CHECK-NEXT: [[BOUND1:%.*]] = icmp ult ptr [[SRC]], [[SCEVGEP]]
78+
; CHECK-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
79+
; CHECK-NEXT: br i1 [[FOUND_CONFLICT]], label %[[SCALAR_PH]], label %[[VECTOR_PH:.*]]
80+
; CHECK: [[VECTOR_PH]]:
81+
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i32 [[N]], 4
82+
; CHECK-NEXT: [[N_VEC:%.*]] = sub i32 [[N]], [[N_MOD_VF]]
83+
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
84+
; CHECK: [[VECTOR_BODY]]:
85+
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
86+
; CHECK-NEXT: [[TMP0:%.*]] = load i16, ptr [[SRC]], align 2, !alias.scope [[META4:![0-9]+]]
87+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i16> poison, i16 [[TMP0]], i64 0
88+
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i16> [[BROADCAST_SPLATINSERT]], <4 x i16> poison, <4 x i32> zeroinitializer
89+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <4 x i16> [[BROADCAST_SPLAT]], zeroinitializer
90+
; CHECK-NEXT: [[TMP2:%.*]] = and <4 x i16> [[BROADCAST_SPLAT]], splat (i16 15)
91+
; CHECK-NEXT: [[TMP3:%.*]] = extractelement <4 x i1> [[TMP1]], i32 0
92+
; CHECK-NEXT: [[TMP4:%.*]] = extractelement <4 x i16> [[TMP2]], i32 0
93+
; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP3]], i16 0, i16 [[TMP4]]
94+
; CHECK-NEXT: store i16 [[TMP5]], ptr [[DST]], align 2, !alias.scope [[META7:![0-9]+]], !noalias [[META4]]
95+
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
96+
; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
97+
; CHECK-NEXT: br i1 [[TMP6]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP9:![0-9]+]]
98+
; CHECK: [[MIDDLE_BLOCK]]:
99+
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[N]], [[N_VEC]]
100+
; CHECK-NEXT: br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
101+
; CHECK: [[SCALAR_PH]]:
102+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ], [ 0, %[[VECTOR_MEMCHECK]] ]
103+
; CHECK-NEXT: br label %[[LOOP:.*]]
104+
; CHECK: [[LOOP]]:
105+
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
106+
; CHECK-NEXT: [[L:%.*]] = load i16, ptr [[SRC]], align 2
107+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i16 [[L]], 0
108+
; CHECK-NEXT: [[L_EXT:%.*]] = zext i16 [[L]] to i32
109+
; CHECK-NEXT: [[AND:%.*]] = and i32 [[L_EXT]], 15
110+
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 0, i32 [[AND]]
111+
; CHECK-NEXT: [[SEL_TRUNC:%.*]] = trunc i32 [[SEL]] to i16
112+
; CHECK-NEXT: store i16 [[SEL_TRUNC]], ptr [[DST]], align 2
113+
; CHECK-NEXT: [[IV_NEXT]] = add nuw i32 [[IV]], 1
114+
; CHECK-NEXT: [[EC:%.*]] = icmp ne i32 [[IV_NEXT]], [[N]]
115+
; CHECK-NEXT: br i1 [[EC]], label %[[LOOP]], label %[[EXIT]], !llvm.loop [[LOOP10:![0-9]+]]
116+
; CHECK: [[EXIT]]:
117+
; CHECK-NEXT: ret void
118+
;
119+
entry:
120+
br label %loop
121+
122+
loop:
123+
%iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
124+
%l = load i16, ptr %src, align 2
125+
%cmp = icmp eq i16 %l, 0
126+
%l.ext = zext i16 %l to i32
127+
%and = and i32 %l.ext, 15
128+
%sel = select i1 %cmp, i32 0, i32 %and
129+
%sel.trunc = trunc i32 %sel to i16
130+
store i16 %sel.trunc, ptr %dst, align 2
131+
%iv.next = add nuw i32 %iv, 1
132+
%ec = icmp ne i32 %iv.next, %n
133+
br i1 %ec, label %loop, label %exit
134+
135+
exit:
136+
ret void
137+
}

0 commit comments

Comments
 (0)