Skip to content

Commit 6f2c8c5

Browse files
pmkippesigcbot
authored andcommitted
Rework limit large kernels from LICM
Changes pass to a function pass and adds additional criteria for what is considered a "large" kernel.
1 parent 995301c commit 6f2c8c5

8 files changed

+75
-49
lines changed

IGC/Compiler/CISACodeGen/ShaderCodeGen.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*========================== begin_copyright_notice ============================
22
3-
Copyright (C) 2017-2022 Intel Corporation
3+
Copyright (C) 2017-2024 Intel Corporation
44
55
SPDX-License-Identifier: MIT
66
@@ -502,7 +502,7 @@ void AddLegalizationPasses(CodeGenContext& ctx, IGCPassManager& mpm, PSSignature
502502

503503
if (IGC_IS_FLAG_ENABLED(allowLICM) && ctx.m_retryManager.AllowLICM())
504504
{
505-
mpm.add(createDisableLICMForSpecificLoops());
505+
mpm.add(createSpecialCasesDisableLICM());
506506
mpm.add(llvm::createLICMPass());
507507
}
508508
mpm.add(llvm::createLoopSimplifyPass());
@@ -891,7 +891,7 @@ void AddLegalizationPasses(CodeGenContext& ctx, IGCPassManager& mpm, PSSignature
891891
}
892892
if (!fastCompile && !highAllocaPressure && !isPotentialHPCKernel && IGC_IS_FLAG_ENABLED(allowLICM) && ctx.m_retryManager.AllowLICM())
893893
{
894-
mpm.add(createDisableLICMForSpecificLoops());
894+
mpm.add(createSpecialCasesDisableLICM());
895895
mpm.add(createLICMPass());
896896
mpm.add(llvm::createEarlyCSEPass());
897897
}
@@ -1466,7 +1466,7 @@ void OptimizeIR(CodeGenContext* const pContext)
14661466

14671467
if (IGC_IS_FLAG_ENABLED(allowLICM) && pContext->m_retryManager.AllowLICM())
14681468
{
1469-
mpm.add(createDisableLICMForSpecificLoops());
1469+
mpm.add(createSpecialCasesDisableLICM());
14701470
int licmTh = IGC_GET_FLAG_VALUE(LICMStatThreshold);
14711471
mpm.add(new InstrStatistic(pContext, LICM_STAT, InstrStatStage::BEGIN, licmTh));
14721472
mpm.add(llvm::createLICMPass());
@@ -1523,7 +1523,7 @@ void OptimizeIR(CodeGenContext* const pContext)
15231523

15241524
if (IGC_IS_FLAG_ENABLED(allowLICM) && pContext->m_retryManager.AllowLICM())
15251525
{
1526-
mpm.add(createDisableLICMForSpecificLoops());
1526+
mpm.add(createSpecialCasesDisableLICM());
15271527
mpm.add(llvm::createLICMPass());
15281528
}
15291529

IGC/Compiler/CustomLoopOpt.cpp

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*========================== begin_copyright_notice ============================
22
3-
Copyright (C) 2017-2021 Intel Corporation
3+
Copyright (C) 2017-2024 Intel Corporation
44
55
SPDX-License-Identifier: MIT
66
@@ -1160,70 +1160,96 @@ namespace IGC
11601160
}
11611161
}
11621162

1163-
class DisableLICMForSpecificLoops : public llvm::LoopPass
1163+
// This pass disables LICM optimization by adding llvm.licm.disable
1164+
// - when Loop depends on SIMD Lane Id and operates on local memory or
1165+
// - when the loops are part of a large function and the number of loops
1166+
// is sizable where a potential stack overflow from memory SSA updater
1167+
// could occur.
1168+
1169+
class SpecialCasesDisableLICM : public llvm::FunctionPass
11641170
{
11651171
public:
11661172
static char ID;
11671173

1168-
DisableLICMForSpecificLoops();
1174+
SpecialCasesDisableLICM();
11691175

11701176
void getAnalysisUsage(llvm::AnalysisUsage& AU) const
11711177
{
11721178
AU.addPreservedID(LCSSAID);
1179+
AU.addRequired<llvm::LoopInfoWrapperPass>();
11731180
}
11741181

1175-
bool runOnLoop(Loop* L, LPPassManager& LPM);
1182+
bool runOnFunction(Function& F);
11761183
bool LoopHasLoadFromLocalAddressSpace(const Loop& L);
11771184
bool LoopDependsOnSIMDLaneId(const Loop& L);
11781185
bool AddLICMDisableMedatadaToSpecificLoop(Loop& L);
11791186

11801187
llvm::StringRef getPassName() const
11811188
{
1182-
return "IGC disable LICM for specific loops";
1189+
return "IGC special cases disable LICM";
11831190
}
11841191
};
1192+
11851193
#undef PASS_FLAG
11861194
#undef PASS_DESC
11871195
#undef PASS_CFG_ONLY
11881196
#undef PASS_ANALYSIS
1189-
#define PASS_FLAG "igc-disable-licm-for-specific-loops"
1190-
#define PASS_DESC "IGC disable LICM for specific loops"
1197+
#define PASS_FLAG "igc-special-cases-disable-licm"
1198+
#define PASS_DESC "IGC special cases disable LICM"
11911199
#define PASS_CFG_ONLY false
11921200
#define PASS_ANALYSIS false
1193-
IGC_INITIALIZE_PASS_BEGIN(DisableLICMForSpecificLoops, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
1194-
IGC_INITIALIZE_PASS_END(DisableLICMForSpecificLoops, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
1195-
1201+
IGC_INITIALIZE_PASS_BEGIN(SpecialCasesDisableLICM, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
1202+
IGC_INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
1203+
IGC_INITIALIZE_PASS_END(SpecialCasesDisableLICM, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
11961204

1197-
char DisableLICMForSpecificLoops::ID = 0;
1205+
char SpecialCasesDisableLICM::ID = 0;
11981206

1199-
DisableLICMForSpecificLoops::DisableLICMForSpecificLoops() : LoopPass(ID)
1207+
SpecialCasesDisableLICM::SpecialCasesDisableLICM() : FunctionPass(ID)
12001208
{
1201-
initializeDisableLICMForSpecificLoopsPass(*PassRegistry::getPassRegistry());
1209+
initializeSpecialCasesDisableLICMPass(*PassRegistry::getPassRegistry());
12021210
}
12031211

1204-
bool DisableLICMForSpecificLoops::runOnLoop(Loop* L, LPPassManager& LPM)
1212+
bool SpecialCasesDisableLICM::runOnFunction(llvm::Function& F)
12051213
{
12061214
bool Changed = false;
1215+
LoopInfo* LI = nullptr;
12071216

1208-
if (!L->getHeader() || !L->getLoopLatch())
1209-
return false;
1217+
auto getLoopInfo = [&]() -> LoopInfo* {
1218+
if (nullptr == LI)
1219+
return &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1220+
return LI;
1221+
};
12101222

1211-
// Disable LICM optimization by adding llvm.licm.disable
1212-
// - when Loop depends on SIMD Lane Id and operates on local memory
1213-
// - when shader contains a large number of BBs that may trigger stack overflow
1214-
// from memory SSA updater
1223+
if (constexpr size_t HIGH_BB_THRESHOLD_FOR_LICM = 2500;
1224+
F.size() > HIGH_BB_THRESHOLD_FOR_LICM)
1225+
{
1226+
LI = getLoopInfo();
1227+
1228+
if (constexpr size_t HIGH_LOOP_THRESHOLD_FOR_LICM = 450;
1229+
llvm::size(*LI) > HIGH_LOOP_THRESHOLD_FOR_LICM)
1230+
{
1231+
for (auto* L : *LI)
1232+
AddLICMDisableMedatadaToSpecificLoop(*L);
12151233

1216-
if (constexpr size_t BB_LIMIT_FOR_LICM = 2500;
1217-
L->getHeader()->getParent()->size() > BB_LIMIT_FOR_LICM ||
1218-
(LoopHasLoadFromLocalAddressSpace(*L) && LoopDependsOnSIMDLaneId(*L)))
1234+
Changed = true;
1235+
}
1236+
}
1237+
1238+
if (!Changed)
12191239
{
1220-
Changed |= AddLICMDisableMedatadaToSpecificLoop(*L);
1240+
LI = getLoopInfo();
1241+
1242+
for (auto* L : *LI)
1243+
{
1244+
if (LoopHasLoadFromLocalAddressSpace(*L) && LoopDependsOnSIMDLaneId(*L))
1245+
Changed |= AddLICMDisableMedatadaToSpecificLoop(*L);
1246+
}
12211247
}
12221248

12231249
return Changed;
12241250
}
12251251

1226-
bool DisableLICMForSpecificLoops::LoopHasLoadFromLocalAddressSpace(const Loop& L)
1252+
bool SpecialCasesDisableLICM::LoopHasLoadFromLocalAddressSpace(const Loop& L)
12271253
{
12281254
for (BasicBlock* BB : L.blocks())
12291255
{
@@ -1236,7 +1262,7 @@ bool DisableLICMForSpecificLoops::LoopHasLoadFromLocalAddressSpace(const Loop& L
12361262
return false;
12371263
}
12381264

1239-
bool DisableLICMForSpecificLoops::LoopDependsOnSIMDLaneId(const Loop& L)
1265+
bool SpecialCasesDisableLICM::LoopDependsOnSIMDLaneId(const Loop& L)
12401266
{
12411267
auto ComeFromSIMDLaneID = [](Value* I)
12421268
{
@@ -1264,7 +1290,7 @@ bool DisableLICMForSpecificLoops::LoopDependsOnSIMDLaneId(const Loop& L)
12641290
return false;
12651291
}
12661292

1267-
bool DisableLICMForSpecificLoops::AddLICMDisableMedatadaToSpecificLoop(Loop& L)
1293+
bool SpecialCasesDisableLICM::AddLICMDisableMedatadaToSpecificLoop(Loop& L)
12681294
{
12691295
LLVMContext& context = L.getHeader()->getContext();
12701296

@@ -1283,9 +1309,9 @@ bool DisableLICMForSpecificLoops::AddLICMDisableMedatadaToSpecificLoop(Loop& L)
12831309

12841310
namespace IGC
12851311
{
1286-
LoopPass* createDisableLICMForSpecificLoops()
1312+
FunctionPass* createSpecialCasesDisableLICM()
12871313
{
1288-
return new DisableLICMForSpecificLoops();
1314+
return new SpecialCasesDisableLICM();
12891315
}
12901316
}
12911317

IGC/Compiler/CustomLoopOpt.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ namespace IGC
159159

160160
llvm::LoopPass* createLoopHoistConstant();
161161

162-
llvm::LoopPass* createDisableLICMForSpecificLoops();
162+
llvm::FunctionPass* createSpecialCasesDisableLICM();
163163

164164
llvm::FunctionPass *createLoopSplitWidePHIs();
165165

IGC/Compiler/InitializePasses.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*========================== begin_copyright_notice ============================
22
3-
Copyright (C) 2017-2022 Intel Corporation
3+
Copyright (C) 2017-2024 Intel Corporation
44
55
SPDX-License-Identifier: MIT
66
@@ -209,7 +209,7 @@ void initializeSampleMultiversioningPass(llvm::PassRegistry&);
209209
void initializeLoopCanonicalizationPass(llvm::PassRegistry&);
210210
void initializeLoopSplitWidePHIsPass(llvm::PassRegistry&);
211211
void initializeLoopHoistConstantPass(llvm::PassRegistry&);
212-
void initializeDisableLICMForSpecificLoopsPass(llvm::PassRegistry&);
212+
void initializeSpecialCasesDisableLICMPass(llvm::PassRegistry&);
213213
void initializeMemOptPass(llvm::PassRegistry&);
214214
void initializeLdStCombinePass(llvm::PassRegistry&);
215215
void initializeBIFTransformsPass(llvm::PassRegistry&);

IGC/Compiler/tests/DisableLICMForSpecificLoops/add_licm_disable_metadata.ll renamed to IGC/Compiler/tests/SpecialCasesDisableLICM/add_licm_disable_metadata.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
;=========================== begin_copyright_notice ============================
22
;
3-
; Copyright (C) 2023 Intel Corporation
3+
; Copyright (C) 2024 Intel Corporation
44
;
55
; SPDX-License-Identifier: MIT
66
;
77
;============================ end_copyright_notice =============================
88
;
9-
; RUN: igc_opt --igc-disable-licm-for-specific-loops -S < %s | FileCheck %s
9+
; RUN: igc_opt --igc-special-cases-disable-licm -S < %s | FileCheck %s
1010
; ------------------------------------------------
11-
; DisableLICMForSpecificLoops
11+
; SpecialCasesDisableLICM
1212
; ------------------------------------------------
1313

1414

IGC/Compiler/tests/DisableLICMForSpecificLoops/add_licm_disable_metadata_multipleLoops.ll renamed to IGC/Compiler/tests/SpecialCasesDisableLICM/add_licm_disable_metadata_multipleLoops.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
;=========================== begin_copyright_notice ============================
22
;
3-
; Copyright (C) 2023 Intel Corporation
3+
; Copyright (C) 2024 Intel Corporation
44
;
55
; SPDX-License-Identifier: MIT
66
;
77
;============================ end_copyright_notice =============================
88
;
9-
; RUN: igc_opt --igc-disable-licm-for-specific-loops -S < %s | FileCheck %s
9+
; RUN: igc_opt --igc-special-cases-disable-licm -S < %s | FileCheck %s
1010
; ------------------------------------------------
11-
; DisableLICMForSpecificLoops
11+
; SpecialCasesDisableLICM
1212
; ------------------------------------------------
1313

1414

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
;=========================== begin_copyright_notice ============================
22
;
3-
; Copyright (C) 2023 Intel Corporation
3+
; Copyright (C) 2024 Intel Corporation
44
;
55
; SPDX-License-Identifier: MIT
66
;
77
;============================ end_copyright_notice =============================
88
;
9-
; RUN: igc_opt --igc-disable-licm-for-specific-loops -S < %s | FileCheck %s
9+
; RUN: igc_opt --igc-special-cases-disable-licm -S < %s | FileCheck %s
1010
; ------------------------------------------------
11-
; DisableLICMForSpecificLoops
11+
; SpecialCasesDisableLICM
1212
; ------------------------------------------------
1313

1414

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
;=========================== begin_copyright_notice ============================
22
;
3-
; Copyright (C) 2023 Intel Corporation
3+
; Copyright (C) 2024 Intel Corporation
44
;
55
; SPDX-License-Identifier: MIT
66
;
77
;============================ end_copyright_notice =============================
88
;
9-
; RUN: igc_opt --igc-disable-licm-for-specific-loops -S < %s | FileCheck %s
9+
; RUN: igc_opt --igc-special-cases-disable-licm -S < %s | FileCheck %s
1010
; ------------------------------------------------
11-
; DisableLICMForSpecificLoops
11+
; SpecialCasesDisableLICM
1212
; ------------------------------------------------
1313

1414

0 commit comments

Comments
 (0)