Skip to content

Commit 9fe1f29

Browse files
authored
[VPlan] Set flags when constructing zexts using VPWidenCastRecipe (llvm#164198)
createWidenCast doesn't set the flag type, so when we simplify trunc (zext nneg x) -> zext x we would hit an assertion in CSE that the flag types don't match with other VPWidenCastRecipes that weren't simplified. This fixes it the same way trunc flags are handled too. As an aside I think it should be correct to preserve the nneg flag in this case since the input operand is still non-negative after the transform. But that's left to another PR. Fixes llvm#164171
1 parent 94647ee commit 9fe1f29

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ class VPBuilder {
325325
VPIRFlags Flags;
326326
if (Opcode == Instruction::Trunc)
327327
Flags = VPIRFlags::TruncFlagsTy(false, false);
328+
else if (Opcode == Instruction::ZExt)
329+
Flags = VPIRFlags::NonNegFlagsTy(false);
328330
return tryInsertInstruction(
329331
new VPWidenCastRecipe(Opcode, Op, ResultTy, Flags));
330332
}

llvm/test/Transforms/LoopVectorize/cse-casts.ll

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,9 @@ define void @preserve_flags_narrowing_extends_and_truncs(ptr noalias %A, ptr noa
319319
; CHECK: [[PRED_STORE_CONTINUE60]]:
320320
; CHECK-NEXT: br label %[[MIDDLE_BLOCK:.*]]
321321
; CHECK: [[MIDDLE_BLOCK]]:
322-
; CHECK-NEXT: br [[EXIT:label %.*]]
323-
; CHECK: [[SCALAR_PH:.*:]]
322+
; CHECK-NEXT: br label %[[EXIT:.*]]
323+
; CHECK: [[EXIT]]:
324+
; CHECK-NEXT: ret void
324325
;
325326
entry:
326327
br label %loop
@@ -349,3 +350,45 @@ loop:
349350
exit:
350351
ret void
351352
}
353+
354+
define void @simplified_cast_preserves_irflag_type(ptr noalias %p, ptr noalias %q, ptr noalias %r) {
355+
; CHECK-LABEL: define void @simplified_cast_preserves_irflag_type(
356+
; CHECK-SAME: ptr noalias [[P:%.*]], ptr noalias [[Q:%.*]], ptr noalias [[R:%.*]]) {
357+
; CHECK-NEXT: [[ENTRY:.*:]]
358+
; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
359+
; CHECK: [[VECTOR_PH]]:
360+
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
361+
; CHECK: [[VECTOR_BODY]]:
362+
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
363+
; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[P]], align 1
364+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i8> poison, i8 [[TMP0]], i64 0
365+
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i8> [[BROADCAST_SPLATINSERT]], <4 x i8> poison, <4 x i32> zeroinitializer
366+
; CHECK-NEXT: [[TMP1:%.*]] = zext <4 x i8> [[BROADCAST_SPLAT]] to <4 x i16>
367+
; CHECK-NEXT: [[TMP2:%.*]] = extractelement <4 x i16> [[TMP1]], i32 3
368+
; CHECK-NEXT: store i16 [[TMP2]], ptr [[Q]], align 2
369+
; CHECK-NEXT: store i16 [[TMP2]], ptr [[R]], align 2
370+
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 8
371+
; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i64 [[INDEX_NEXT]], 48
372+
; CHECK-NEXT: br i1 [[TMP3]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
373+
; CHECK: [[MIDDLE_BLOCK]]:
374+
; CHECK-NEXT: br label %[[SCALAR_PH:.*]]
375+
; CHECK: [[SCALAR_PH]]:
376+
;
377+
entry:
378+
br label %loop
379+
380+
loop:
381+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
382+
%x = load i8, ptr %p
383+
%x.i32 = zext i8 %x to i32
384+
%trunc = trunc i32 %x.i32 to i16
385+
store i16 %trunc, ptr %q
386+
%x.i16 = zext i8 %x to i16
387+
store i16 %x.i16, ptr %r
388+
%iv.next = add i64 %iv, 2
389+
%ec = icmp eq i64 %iv.next, 100
390+
br i1 %ec, label %exit, label %loop
391+
392+
exit:
393+
ret void
394+
}

0 commit comments

Comments
 (0)