Skip to content

Commit e54be64

Browse files
Anton Sidorenkoigcbot
authored andcommitted
Add switch to jump table lowering
1 parent 07d74a9 commit e54be64

15 files changed

+353
-39
lines changed

IGC/VectorCompiler/include/vc/GenXCodeGen/GenXWrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ struct CompileOptions {
154154
bool NoVecDecomp = false;
155155
bool EmitDebugInfo = false;
156156

157+
bool NoJumpTables = false;
157158
OptimizerLevel OptLevel = OptimizerLevel::Full;
158159
llvm::Optional<unsigned> StackMemSize;
159160

IGC/VectorCompiler/include/vc/Support/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def no_vector_decomposition_old : Flag<["-"], "no_vector_decomposition">,
8383
let Flags = [ApiOption, IgcmcApiOption];
8484
}
8585

86+
def fno_jump_tables : Flag<["-"], "fno-jump-tables">,
87+
HelpText<"Disable the use of jump tables for lowering switches">;
88+
8689
def Xfinalizer : Separate<["-"], "Xfinalizer">,
8790
HelpText<"Pass <arg> to the visa finalizer">, MetaVarName<"<arg>">;
8891

IGC/VectorCompiler/lib/GenXCodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ set(CODEGEN_SOURCES
3737
GenXLiveness.cpp
3838
GenXLowering.cpp
3939
GenXLowerAggrCopies.cpp
40+
GenXLowerJmpTableSwitch.cpp
4041
GenXEmulate.cpp
4142
GenXModule.cpp
4243
GenXNumbering.cpp

IGC/VectorCompiler/lib/GenXCodeGen/GenX.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ FunctionPass *createGenXInstCombineCleanup();
8181
FunctionPass *createGenXInlineAsmLoweringPass();
8282
FunctionPass *createGenXLoweringPass();
8383
FunctionPass *createGenXLowerAggrCopiesPass();
84+
FunctionPass *createGenXLowerJmpTableSwitchPass();
8485
FunctionPass *createGenXGEPLoweringPass();
8586
FunctionPass *createGenXRegionCollapsingPass();
8687
FunctionPass *createGenXExtractVectorizerPass();

IGC/VectorCompiler/lib/GenXCodeGen/GenX.td

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ def FeatureVectorDecomp : SubtargetFeature<"disable_vec_decomp",
6060
"true",
6161
"disable vector decomposition pass">;
6262

63+
def FeatureNoJumpTables : SubtargetFeature<"disable_jump_tables", "DisableJumpTables",
64+
"true", "disable switch to jump tables lowering">;
65+
66+
def FeatureSwitchjmp : SubtargetFeature<"switchjmp", "HasSwitchjmp", "true",
67+
"supports switchjmp visa instruction">;
68+
6369
def WarnCallable : SubtargetFeature<"warn_callable", "WarnCallable",
6470
"true", "warn instead of error on callable violation">;
6571

@@ -75,16 +81,16 @@ class Proc<string Name, list<SubtargetFeature> Features>
7581
: Processor<Name, NoItineraries, Features>;
7682

7783
def : Proc<"generic", []>;
78-
def : Proc<"HSW", [FeatureLongLongEmulation]>;
79-
def : Proc<"BDW", [FeatureLongLong]>;
80-
def : Proc<"CHV", [FeatureLongLong]>;
81-
def : Proc<"SKL", [FeatureLongLong]>;
82-
def : Proc<"BXT", [FeatureLongLong]>;
83-
def : Proc<"KBL", [FeatureLongLong]>;
84-
def : Proc<"GLK", [FeatureLongLong]>;
85-
def : Proc<"CNL", [FeatureLongLong]>;
86-
def : Proc<"ICL", [FeatureLongLong]>;
87-
def : Proc<"ICLLP", [FeatureLongLongEmulation]>;
84+
def : Proc<"HSW", [FeatureLongLongEmulation, FeatureSwitchjmp]>;
85+
def : Proc<"BDW", [FeatureLongLong, FeatureSwitchjmp]>;
86+
def : Proc<"CHV", [FeatureLongLong, FeatureSwitchjmp]>;
87+
def : Proc<"SKL", [FeatureLongLong, FeatureSwitchjmp]>;
88+
def : Proc<"BXT", [FeatureLongLong, FeatureSwitchjmp]>;
89+
def : Proc<"KBL", [FeatureLongLong, FeatureSwitchjmp]>;
90+
def : Proc<"GLK", [FeatureLongLong, FeatureSwitchjmp]>;
91+
def : Proc<"CNL", [FeatureLongLong, FeatureSwitchjmp]>;
92+
def : Proc<"ICL", [FeatureLongLong, FeatureSwitchjmp]>;
93+
def : Proc<"ICLLP", [FeatureLongLongEmulation, FeatureSwitchjmp]>;
8894
def : Proc<"TGLLP", [FeatureLongLongEmulation]>;
8995

9096
def GenX : Target {

IGC/VectorCompiler/lib/GenXCodeGen/GenXCisaBuilder.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ class GenXKernelBuilder {
565565
void buildControlRegUpdate(unsigned Mask, bool Clear);
566566
void buildJoin(CallInst *Join, BranchInst *Branch);
567567
bool buildBranch(BranchInst *Branch);
568+
void buildIndirectBr(IndirectBrInst *Br);
568569
void buildIntrinsic(CallInst *CI, unsigned IntrinID, genx::BaleInfo BI,
569570
unsigned Mod, const DstOpndDesc &DstDesc);
570571
void buildInputs(Function *F, bool NeedRetIP);
@@ -2581,6 +2582,8 @@ bool GenXKernelBuilder::buildMainInst(Instruction *Inst, BaleInfo BI,
25812582
buildRet(RI);
25822583
} else if (BranchInst *BR = dyn_cast<BranchInst>(Inst)) {
25832584
return buildBranch(BR);
2585+
} else if (IndirectBrInst *IBR = dyn_cast<IndirectBrInst>(Inst)) {
2586+
buildIndirectBr(IBR);
25842587
} else if (CmpInst *Cmp = dyn_cast<CmpInst>(Inst)) {
25852588
buildCmp(Cmp, BI, DstDesc);
25862589
} else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Inst)) {
@@ -2650,6 +2653,7 @@ bool GenXKernelBuilder::buildMainInst(Instruction *Inst, BaleInfo BI,
26502653
case GenXIntrinsic::genx_predefined_surface:
26512654
case GenXIntrinsic::genx_output:
26522655
case GenXIntrinsic::genx_output_1:
2656+
case GenXIntrinsic::genx_jump_table:
26532657
// ignore
26542658
break;
26552659
case GenXIntrinsic::genx_simdcf_goto:
@@ -3764,6 +3768,32 @@ bool GenXKernelBuilder::buildBranch(BranchInst *Branch) {
37643768
return false;
37653769
}
37663770

3771+
/***********************************************************************
3772+
* buildIndirectBr : build an indirect branch
3773+
*
3774+
* Indirectbr instructions are used only for jump tables.
3775+
*
3776+
* Enter: Br = indirect branch inst
3777+
*/
3778+
void GenXKernelBuilder::buildIndirectBr(IndirectBrInst *Br) {
3779+
IGC_ASSERT(Subtarget->hasSwitchjmp());
3780+
Value *Addr = Br->getAddress();
3781+
auto JumpTable = cast<IntrinsicInst>(Addr);
3782+
unsigned IID = GenXIntrinsic::getAnyIntrinsicID(JumpTable);
3783+
IGC_ASSERT(IID == GenXIntrinsic::genx_jump_table);
3784+
Value *Idx = JumpTable->getArgOperand(0);
3785+
3786+
VISA_VectorOpnd *JMPIdx = createSource(Idx, UNSIGNED);
3787+
unsigned NumDest = Br->getNumDestinations();
3788+
std::vector<VISA_LabelOpnd *> JMPLabels(NumDest, nullptr);
3789+
for (unsigned I = 0; I < NumDest; ++I)
3790+
JMPLabels[I] = Labels[getOrCreateLabel(Br->getDestination(I), LABEL_BLOCK)];
3791+
3792+
addDebugInfo();
3793+
CISA_CALL(
3794+
Kernel->AppendVISACFSwitchJMPInst(JMPIdx, NumDest, JMPLabels.data()));
3795+
}
3796+
37673797
/***********************************************************************
37683798
* buildJoin : build a join
37693799
*

IGC/VectorCompiler/lib/GenXCodeGen/GenXLiveness.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -433,15 +433,7 @@ void GenXLiveness::rebuildLiveRangeForValue(LiveRange *LR, SimpleValue SV)
433433
// all of them at the end).
434434
*BBRange = Segment(Numbering->getNumber(BB), Num);
435435
// Push this block's predecessors onto the stack.
436-
// (A basic block's predecessors are those blocks containing a
437-
// TerminatorInst that uses the basic block.)
438-
for (Value::use_iterator i = BB->use_begin(), e = BB->use_end();
439-
i != e; ++i) {
440-
Instruction *TI = dyn_cast<Instruction>(i->getUser());
441-
IGC_ASSERT(TI);
442-
if (TI->isTerminator())
443-
Stack.push_back(TI->getParent());
444-
}
436+
std::copy(pred_begin(BB), pred_end(BB), std::back_inserter(Stack));
445437
// Process stack until empty.
446438
while (Stack.size()) {
447439
BB = Stack.back();
@@ -459,15 +451,7 @@ void GenXLiveness::rebuildLiveRangeForValue(LiveRange *LR, SimpleValue SV)
459451
// all of them at the end).
460452
BBRange->setStartEnd(Numbering->getNumber(BB), BBNum->EndNumber);
461453
// Push this block's predecessors onto the stack.
462-
// (A basic block's predecessors are those blocks containing a
463-
// TerminatorInst that uses the basic block.)
464-
for (Value::use_iterator i = BB->use_begin(), e = BB->use_end();
465-
i != e; ++i) {
466-
Instruction *TI = dyn_cast<Instruction>(i->getUser());
467-
IGC_ASSERT(TI);
468-
if (TI->isTerminator())
469-
Stack.push_back(TI->getParent());
470-
}
454+
std::copy(pred_begin(BB), pred_end(BB), std::back_inserter(Stack));
471455
}
472456
}
473457
// Now we can build the live range.

0 commit comments

Comments
 (0)