Skip to content

Commit fe6e178

Browse files
authored
[VPlan] Don't build recipes for unconditional switches (#157323)
In #157322 we crash because we try to infer a type for a VPReplicate switch recipe. My understanding was that these switches should be removed by VPlanPredicator, but this switch survived through it because it was unconditional, i.e. had no cases other than the default case. This fixes #157322 by not emitting any recipes for unconditional switches to begin with, similar to how we treat unconditional branches.
1 parent 4c6a562 commit fe6e178

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
193193
}
194194

195195
if (auto *SI = dyn_cast<SwitchInst>(Inst)) {
196+
// Don't emit recipes for unconditional switch instructions.
197+
if (SI->getNumCases() == 0)
198+
continue;
196199
SmallVector<VPValue *> Ops = {getOrCreateVPOperand(SI->getCondition())};
197200
for (auto Case : SI->cases())
198201
Ops.push_back(getOrCreateVPOperand(Case.getCaseValue()));

llvm/test/Transforms/LoopVectorize/predicate-switch.ll

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,18 +490,114 @@ loop.latch:
490490
exit:
491491
ret void
492492
}
493+
494+
define void @switch_unconditional(ptr %start) {
495+
; IC1-LABEL: define void @switch_unconditional(
496+
; IC1-SAME: ptr [[START:%.*]]) {
497+
; IC1-NEXT: [[ENTRY:.*:]]
498+
; IC1-NEXT: br i1 false, label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
499+
; IC1: [[VECTOR_PH]]:
500+
; IC1-NEXT: br label %[[VECTOR_BODY:.*]]
501+
; IC1: [[VECTOR_BODY]]:
502+
; IC1-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
503+
; IC1-NEXT: [[TMP1:%.*]] = getelementptr i32, ptr [[START]], i64 [[INDEX]]
504+
; IC1-NEXT: store <2 x i32> zeroinitializer, ptr [[TMP1]], align 4
505+
; IC1-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
506+
; IC1-NEXT: [[TMP0:%.*]] = icmp eq i64 [[INDEX_NEXT]], 100
507+
; IC1-NEXT: br i1 [[TMP0]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
508+
; IC1: [[MIDDLE_BLOCK]]:
509+
; IC1-NEXT: br label %[[EXIT:.*]]
510+
; IC1: [[SCALAR_PH]]:
511+
; IC1-NEXT: br label %[[LOOP:.*]]
512+
; IC1: [[LOOP]]:
513+
; IC1-NEXT: [[IV:%.*]] = phi i64 [ 0, %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LATCH:.*]] ]
514+
; IC1-NEXT: [[GEP:%.*]] = getelementptr i32, ptr [[START]], i64 [[IV]]
515+
; IC1-NEXT: [[X:%.*]] = load i32, ptr [[GEP]], align 4
516+
; IC1-NEXT: switch i32 [[X]], label %[[FOO:.*]] [
517+
; IC1-NEXT: ]
518+
; IC1: [[FOO]]:
519+
; IC1-NEXT: br label %[[LATCH]]
520+
; IC1: [[LATCH]]:
521+
; IC1-NEXT: store i32 0, ptr [[GEP]], align 4
522+
; IC1-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1
523+
; IC1-NEXT: [[CMP:%.*]] = icmp eq i64 [[IV_NEXT]], 100
524+
; IC1-NEXT: br i1 [[CMP]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP7:![0-9]+]]
525+
; IC1: [[EXIT]]:
526+
; IC1-NEXT: ret void
527+
;
528+
; IC2-LABEL: define void @switch_unconditional(
529+
; IC2-SAME: ptr [[START:%.*]]) {
530+
; IC2-NEXT: [[ENTRY:.*:]]
531+
; IC2-NEXT: br i1 false, label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
532+
; IC2: [[VECTOR_PH]]:
533+
; IC2-NEXT: br label %[[VECTOR_BODY:.*]]
534+
; IC2: [[VECTOR_BODY]]:
535+
; IC2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
536+
; IC2-NEXT: [[TMP2:%.*]] = getelementptr i32, ptr [[START]], i64 [[INDEX]]
537+
; IC2-NEXT: [[TMP1:%.*]] = getelementptr i32, ptr [[TMP2]], i32 2
538+
; IC2-NEXT: store <2 x i32> zeroinitializer, ptr [[TMP2]], align 4
539+
; IC2-NEXT: store <2 x i32> zeroinitializer, ptr [[TMP1]], align 4
540+
; IC2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
541+
; IC2-NEXT: [[TMP0:%.*]] = icmp eq i64 [[INDEX_NEXT]], 100
542+
; IC2-NEXT: br i1 [[TMP0]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
543+
; IC2: [[MIDDLE_BLOCK]]:
544+
; IC2-NEXT: br label %[[EXIT:.*]]
545+
; IC2: [[SCALAR_PH]]:
546+
; IC2-NEXT: br label %[[LOOP:.*]]
547+
; IC2: [[LOOP]]:
548+
; IC2-NEXT: [[IV:%.*]] = phi i64 [ 0, %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LATCH:.*]] ]
549+
; IC2-NEXT: [[GEP:%.*]] = getelementptr i32, ptr [[START]], i64 [[IV]]
550+
; IC2-NEXT: [[X:%.*]] = load i32, ptr [[GEP]], align 4
551+
; IC2-NEXT: switch i32 [[X]], label %[[FOO:.*]] [
552+
; IC2-NEXT: ]
553+
; IC2: [[FOO]]:
554+
; IC2-NEXT: br label %[[LATCH]]
555+
; IC2: [[LATCH]]:
556+
; IC2-NEXT: store i32 0, ptr [[GEP]], align 4
557+
; IC2-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1
558+
; IC2-NEXT: [[CMP:%.*]] = icmp eq i64 [[IV_NEXT]], 100
559+
; IC2-NEXT: br i1 [[CMP]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP7:![0-9]+]]
560+
; IC2: [[EXIT]]:
561+
; IC2-NEXT: ret void
562+
;
563+
entry:
564+
br label %loop
565+
566+
loop:
567+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %latch ]
568+
%gep = getelementptr i32, ptr %start, i64 %iv
569+
%x = load i32, ptr %gep
570+
switch i32 %x, label %foo []
571+
572+
foo:
573+
br label %latch
574+
575+
latch:
576+
store i32 0, ptr %gep
577+
%iv.next = add i64 %iv, 1
578+
%cmp = icmp eq i64 %iv.next, 100
579+
br i1 %cmp, label %exit, label %loop
580+
581+
exit:
582+
ret void
583+
}
584+
493585
;.
494586
; IC1: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
495587
; IC1: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
496588
; IC1: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
497589
; IC1: [[LOOP3]] = distinct !{[[LOOP3]], [[META2]], [[META1]]}
498590
; IC1: [[LOOP4]] = distinct !{[[LOOP4]], [[META1]], [[META2]]}
499591
; IC1: [[LOOP5]] = distinct !{[[LOOP5]], [[META2]], [[META1]]}
592+
; IC1: [[LOOP6]] = distinct !{[[LOOP6]], [[META1]], [[META2]]}
593+
; IC1: [[LOOP7]] = distinct !{[[LOOP7]], [[META2]], [[META1]]}
500594
;.
501595
; IC2: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
502596
; IC2: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
503597
; IC2: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
504598
; IC2: [[LOOP3]] = distinct !{[[LOOP3]], [[META2]], [[META1]]}
505599
; IC2: [[LOOP4]] = distinct !{[[LOOP4]], [[META1]], [[META2]]}
506600
; IC2: [[LOOP5]] = distinct !{[[LOOP5]], [[META2]], [[META1]]}
601+
; IC2: [[LOOP6]] = distinct !{[[LOOP6]], [[META1]], [[META2]]}
602+
; IC2: [[LOOP7]] = distinct !{[[LOOP7]], [[META2]], [[META1]]}
507603
;.

0 commit comments

Comments
 (0)