Skip to content

Commit e53dfa0

Browse files
scottp101igcbot
authored andcommitted
predicated stack ID release
predicated stack ID release
1 parent fb6f156 commit e53dfa0

File tree

16 files changed

+181
-7
lines changed

16 files changed

+181
-7
lines changed

IGC/AdaptorCommon/RayTracing/RTBuilder.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,8 @@ CallInst* RTBuilder::CreateBTDCall(Value* RecordPointer)
985985
return this->CreateCall(BTDCall, args);
986986
}
987987

988-
StackIDReleaseIntrinsic* RTBuilder::CreateStackIDRelease(Value* StackID)
988+
StackIDReleaseIntrinsic* RTBuilder::CreateStackIDRelease(
989+
Value* StackID, Value* Flag)
989990
{
990991
Module* module = this->GetInsertBlock()->getModule();
991992
Value* stackID = StackID ? StackID : this->getAsyncStackID();
@@ -994,7 +995,10 @@ StackIDReleaseIntrinsic* RTBuilder::CreateStackIDRelease(Value* StackID)
994995
module,
995996
GenISAIntrinsic::GenISA_StackIDRelease);
996997

997-
return cast<StackIDReleaseIntrinsic>(this->CreateCall(BTDCall, stackID));
998+
auto* Predicate = Flag ? Flag : getTrue();
999+
1000+
return cast<StackIDReleaseIntrinsic>(
1001+
this->CreateCall2(BTDCall, stackID, Predicate));
9981002
}
9991003

10001004
// Note: 'traceRayCtrl' should be already by 8 bits to its location

IGC/AdaptorCommon/RayTracing/RTBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ class RTBuilder : public IGCIRBuilder<>
401401
AsyncStackPointerVal* getAsyncStackPointer(bool BuildAddress = false);
402402
SyncStackPointerVal* getSyncStackPointer();
403403
CallInst* CreateBTDCall(Value* RecordPointer);
404-
StackIDReleaseIntrinsic* CreateStackIDRelease(Value* StackID = nullptr);
404+
StackIDReleaseIntrinsic* CreateStackIDRelease(
405+
Value* StackID = nullptr, Value* Flag = nullptr);
405406
CallInst* createMergeCall();
406407

407408
// Note: 'traceRayCtrl' should be already by 8 bits to its location

IGC/AdaptorCommon/RayTracing/RayTracingPasses.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ llvm::Pass* createPayloadSinkingAnalysisPass();
4242
llvm::Pass* createPayloadSinkingPass();
4343
llvm::Pass* createLowerGlobalRootSignaturePass();
4444
llvm::Pass* createRayTracingMemDSEPass();
45+
llvm::Pass* createRayTracingPredicatedStackIDReleasePass();
4546
llvm::Pass* createDeadPayloadStoreEliminationPass();
4647
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2022 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
//===----------------------------------------------------------------------===//
10+
///
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "Compiler/IGCPassSupport.h"
15+
#include "RTBuilder.h"
16+
17+
#include "common/LLVMWarningsPush.hpp"
18+
#include "llvm/IR/InstIterator.h"
19+
#include "llvm/Transforms/Utils/SSAUpdater.h"
20+
#include "common/LLVMWarningsPop.hpp"
21+
22+
using namespace llvm;
23+
using namespace IGC;
24+
25+
class RayTracingPredicatedStackIDReleasePass : public FunctionPass
26+
{
27+
public:
28+
RayTracingPredicatedStackIDReleasePass() : FunctionPass(ID)
29+
{
30+
initializeRayTracingPredicatedStackIDReleasePassPass(*PassRegistry::getPassRegistry());
31+
}
32+
33+
void getAnalysisUsage(AnalysisUsage& AU) const override
34+
{
35+
AU.setPreservesCFG();
36+
AU.addRequired<CodeGenContextWrapper>();
37+
}
38+
39+
bool runOnFunction(Function& M) override;
40+
StringRef getPassName() const override
41+
{
42+
return "RayTracingPredicatedStackIDReleasePass";
43+
}
44+
45+
static char ID;
46+
};
47+
48+
static bool isKnownFalse(const Value* V)
49+
{
50+
if (auto* CI = dyn_cast<ConstantInt>(V))
51+
{
52+
if (CI->isZero())
53+
return true;
54+
}
55+
return false;
56+
}
57+
58+
char RayTracingPredicatedStackIDReleasePass::ID = 0;
59+
// Register pass to igc-opt
60+
#define PASS_FLAG2 "rt-predicated-stack-id-release"
61+
#define PASS_DESCRIPTION2 "Emit single predicated release at end of shader"
62+
#define PASS_CFG_ONLY2 false
63+
#define PASS_ANALYSIS2 false
64+
IGC_INITIALIZE_PASS_BEGIN(RayTracingPredicatedStackIDReleasePass, PASS_FLAG2, PASS_DESCRIPTION2, PASS_CFG_ONLY2, PASS_ANALYSIS2)
65+
IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
66+
IGC_INITIALIZE_PASS_END(RayTracingPredicatedStackIDReleasePass, PASS_FLAG2, PASS_DESCRIPTION2, PASS_CFG_ONLY2, PASS_ANALYSIS2)
67+
68+
bool RayTracingPredicatedStackIDReleasePass::runOnFunction(Function& F)
69+
{
70+
auto *Ctx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
71+
SmallVector<StackIDReleaseIntrinsic*, 4> Releases;
72+
SmallVector<ReturnInst*, 4> Returns;
73+
74+
for (auto& I : instructions(F))
75+
{
76+
if (auto *SIR = dyn_cast<StackIDReleaseIntrinsic>(&I))
77+
Releases.push_back(SIR);
78+
else if (auto* RI = dyn_cast<ReturnInst>(&I))
79+
Returns.push_back(RI);
80+
}
81+
82+
if (Releases.empty())
83+
return false;
84+
85+
RTBuilder RTB(F.getContext(), *Ctx);
86+
87+
SSAUpdater Updater;
88+
// Since this is run post-legalization, we don't want any phis with i1 type.
89+
Updater.Initialize(RTB.getInt16Ty(), VALUE_NAME("StackIDReleasePred"));
90+
Updater.AddAvailableValue(&F.getEntryBlock(), RTB.getInt16(0));
91+
92+
for (auto* SIR : Releases)
93+
{
94+
RTB.SetInsertPoint(SIR);
95+
Updater.AddAvailableValue(
96+
SIR->getParent(),
97+
RTB.CreateZExt(SIR->getPredicate(), RTB.getInt16Ty()));
98+
SIR->eraseFromParent();
99+
}
100+
101+
for (auto* RI : Returns)
102+
{
103+
Value* Pred = Updater.GetValueAtEndOfBlock(RI->getParent());
104+
if (!isKnownFalse(Pred))
105+
{
106+
RTB.SetInsertPoint(RI);
107+
Pred = RTB.CreateTrunc(Pred, RTB.getInt1Ty());
108+
RTB.CreateStackIDRelease(nullptr, Pred);
109+
}
110+
}
111+
112+
return true;
113+
}
114+
115+
namespace IGC
116+
{
117+
118+
Pass* createRayTracingPredicatedStackIDReleasePass(void)
119+
{
120+
return new RayTracingPredicatedStackIDReleasePass();
121+
}
122+
123+
} // namespace IGC

IGC/Compiler/CISACodeGen/EmitVISAPass.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22719,6 +22719,7 @@ void EmitPass::emitBTD(
2271922719
CVariable* GlobalBufferPtr,
2272022720
CVariable* StackID,
2272122721
CVariable* ShaderRecord,
22722+
CVariable* Flag,
2272222723
bool releaseStackID)
2272322724
{
2272422725

@@ -22791,6 +22792,7 @@ void EmitPass::emitBTD(
2279122792
true);
2279222793
m_encoder->Push();
2279322794

22795+
m_encoder->SetPredicate(Flag);
2279422796
m_encoder->Sends(
2279522797
nullptr,
2279622798
payload,
@@ -22821,14 +22823,21 @@ void EmitPass::emitBindlessThreadDispatch(BTDIntrinsic* I)
2282122823
CVariable* stackID = GetSymbol(I->getStackID());
2282222824
CVariable* shaderRecord = GetSymbol(I->getShaderRecordAddress());
2282322825

22824-
emitBTD(globalBufferPtr, stackID, shaderRecord, false);
22826+
emitBTD(globalBufferPtr, stackID, shaderRecord, nullptr, false);
2282522827
}
2282622828

2282722829
void EmitPass::emitStackIDRelease(StackIDReleaseIntrinsic* I)
2282822830
{
2282922831
CVariable* stackID = GetSymbol(I->getStackID());
22832+
CVariable* flag = nullptr;
22833+
22834+
if (auto* CI = dyn_cast<ConstantInt>(I->getPredicate());
22835+
!CI || !CI->isAllOnesValue())
22836+
{
22837+
flag = GetSymbol(I->getPredicate());
22838+
}
2283022839

22831-
emitBTD(nullptr, stackID, nullptr, true);
22840+
emitBTD(nullptr, stackID, nullptr, flag, true);
2283222841
}
2283322842

2283422843
void EmitPass::emitGetShaderRecordPtr(GetShaderRecordPtrIntrinsic* I)

IGC/Compiler/CISACodeGen/EmitVISAPass.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ class EmitPass : public llvm::FunctionPass
475475
CVariable* GlobalBufferPtr,
476476
CVariable* StackID,
477477
CVariable* ShaderRecord,
478+
CVariable* Flag,
478479
bool releaseStackID);
479480
void emitBindlessThreadDispatch(llvm::BTDIntrinsic *I);
480481
void emitStackIDRelease(llvm::StackIDReleaseIntrinsic *I);

IGC/Compiler/CISACodeGen/Platform.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,12 @@ bool hasNoFullI64Support() const
728728
return (hasNoInt64Inst() || hasPartialInt64Support());
729729
}
730730

731+
bool WaPredicatedStackIDRelease() const
732+
{
733+
return m_WaTable.Wa_22014559856 &&
734+
IGC_IS_FLAG_ENABLED(EnablePredicatedStackIDRelease);
735+
}
736+
731737
// This returns the current maximum size that we recommend for performance.
732738
// SIMD32 is still allowed and we may relax this in the future.
733739
SIMDMode getMaxRayQuerySIMDSize() const

IGC/Compiler/CISACodeGen/ShaderCodeGen.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ SPDX-License-Identifier: MIT
170170
#include "Compiler/SampleCmpToDiscard.h"
171171
#include "Compiler/Optimizer/IGCInstCombiner/IGCInstructionCombining.hpp"
172172
#include "DebugInfo.hpp"
173+
#include "AdaptorCommon/RayTracing/RayTracingPasses.hpp"
173174
#include "AdaptorCommon/RayTracing/RayTracingAddressSpaceAliasAnalysis.h"
174175
#include "Compiler/SamplerPerfOptPass.hpp"
175176
#include "Compiler/CISACodeGen/HalfPromotion.h"
@@ -928,6 +929,9 @@ static void AddLegalizationPasses(CodeGenContext& ctx, IGCPassManager& mpm, PSSi
928929

929930
if (ctx.type == ShaderType::RAYTRACING_SHADER)
930931
{
932+
if (ctx.platform.WaPredicatedStackIDRelease())
933+
mpm.add(createRayTracingPredicatedStackIDReleasePass());
934+
931935
if (IGC_IS_FLAG_DISABLED(DisableRTFenceElision))
932936
mpm.add(createSynchronizationObjectCoalescing());
933937
}

IGC/Compiler/InitializePasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ void initializePromoteInt8TypePass(llvm::PassRegistry&);
138138
void initializeDpasFuncsResolutionPass(llvm::PassRegistry&);
139139
void initializeLSCFuncsResolutionPass(llvm::PassRegistry&);
140140
void initializePrepareLoadsStoresPassPass(llvm::PassRegistry&);
141+
void initializeRayTracingPredicatedStackIDReleasePassPass(llvm::PassRegistry&);
141142
void initializeVectorBitCastOptPass(llvm::PassRegistry&);
142143
void initializeVectorPreProcessPass(llvm::PassRegistry&);
143144
void initializeVectorProcessPass(llvm::PassRegistry&);

IGC/DriverInterface/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ endif()
4848
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/RayTracing/RTBuilder.cpp"
4949
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/RayTracing/RTLoggingManager.cpp"
5050
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/RayTracing/RTStackFormat.cpp"
51+
"${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorCommon/RayTracing/RayTracingPredicatedStackIDReleasePass.cpp"
5152
)
5253
list(APPEND IGC_BUILD__SRC__DriverInterface ${IGC_BUILD__SRC__RAYTRACING})
5354

0 commit comments

Comments
 (0)