1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
//===----------------------------------------------------------------------===//
///
/// This pass will attempt to sink writes to the payload in closest-hit and
/// miss shaders into the inlined continuation (if it was inlined).
///
/// The goal is to eliminate loads in the inlined continuation that we already
/// know the value of. For example:
///
/// [shader("raygeneration")]
/// void MyRaygenShader()
/// {
/// RayPayload payload = { float4(0, 0, 0, 0) };
/// TraceRay(Scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, ~0, 0, 1, 0, ray, payload);
/// RenderTarget[DispatchRaysIndex().xy] = payload.color;
/// }
///
/// [shader("miss")]
/// void MyMissShader(inout RayPayload payload)
/// {
/// payload.color = float4(0, 0, 0, 1);
/// }
///
/// If we know the MaxTraceRecursionDepth == 1, we don't need to load the
/// payload pointer argument in the miss shader; we just need to find where
/// the payload is located in the SWStack of the raygen shader.
///
/// After inlining the continuation:
///
/// [shader("miss")]
/// void MyMissShader(inout RayPayload payload)
/// {
/// payload.color = float4(0, 0, 0, 1);
/// RenderTarget[DispatchRaysIndex().xy] = payload.color;
/// }
///
/// Which means we can just do:
///
/// [shader("miss")]
/// void MyMissShader(inout RayPayload payload)
/// {
/// payload.color = float4(0, 0, 0, 1);
/// RenderTarget[DispatchRaysIndex().xy] = float4(0, 0, 0, 1);
/// }
///
/// Later in compilation, it may even be possible to eliminate the payload.color
/// write if there is a StackIDRelease after it.
//===----------------------------------------------------------------------===//
#include "RTBuilder.h"
#include "Compiler/IGCPassSupport.h"
#include "iStdLib/utility.h"
#include "common/LLVMUtils.h"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/InstIterator.h>
#include <llvm/IR/Dominators.h>
#include <llvm/Analysis/CFG.h>
#include <llvm/ADT/SmallPtrSet.h>
#include "common/LLVMWarningsPop.hpp"
#include "Probe/Assertion.h"
#include "ShaderProperties.h"
#include "Utils.h"
using namespace llvm;
using namespace IGC;
using namespace ShaderProperties;
class PayloadSinkingPass : public FunctionPass
{
public:
PayloadSinkingPass() : FunctionPass(ID)
{
initializePayloadSinkingPassPass(*PassRegistry::getPassRegistry());
}
bool runOnFunction(Function &F) override;
StringRef getPassName() const override
{
return "PayloadSinkingPass";
}
void getAnalysisUsage(llvm::AnalysisUsage &AU) const override
{
AU.setPreservesCFG();
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<CodeGenContextWrapper>();
}
static char ID;
private:
bool canSink(const CodeGenContext &Ctx, Function& F) const;
};
char PayloadSinkingPass::ID = 0;
// Register pass to igc-opt
#define PASS_FLAG "payload-sinking"
#define PASS_DESCRIPTION "Sink payload stores into continuations"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(PayloadSinkingPass, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
IGC_INITIALIZE_PASS_END(PayloadSinkingPass, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
// Given a starting block, return true if all paths must hit at least one
// `StopBB` on the way to the exit.
static bool mustExecute(
const BasicBlock* From,
const SmallPtrSetImpl<const BasicBlock*>& StopBBs)
{
SmallPtrSet<const BasicBlock*, 32> Visited;
SmallVector<const BasicBlock*, 8> WorkList{ From };
do {
auto* BB = WorkList.pop_back_val();
if (!Visited.insert(BB).second)
continue;
if (StopBBs.count(BB) != 0)
continue;
if (isa<ReturnInst>(BB->getTerminator()))
return false;
WorkList.append(succ_begin(BB), succ_end(BB));
} while (!WorkList.empty());
return true;
}
// Conservatively find stores there are guaranteed to not alias each other.
// While we could opt for a heavier weight alias analysis if needed in the
// future, let's conservatively only sink stores that have constant offsets.
static SmallVector<StoreInst*, 4>
getNonAliasingStores(ArrayRef<PayloadUse> Uses, const DominatorTree *DT)
{
SmallVector<StoreInst*, 4> Stores;
for (auto& Use : Uses)
{
if (auto* SI = dyn_cast<StoreInst>(Use.I))
{
if (!std::any_of(Uses.begin(), Uses.end(), [&](const PayloadUse& A) {
if (A.I == SI)
return false;
if (!overlap(A.MemInterval, Use.MemInterval))
return false;
return isPotentiallyReachable(SI, A.I, nullptr, DT);
}))
{
Stores.push_back(SI);
}
}
}
return Stores;
}
// copies `SI` and all of its pointer arithmetic from the payload pointer
// base right after `Payload`. `Payload` is attached to be the new base of the
// pointer arithmetic.
static void sinkStore(StoreInst* SI, Instruction *Payload)
{
Instruction* I = cast<Instruction>(SI->getPointerOperand());
Instruction* CurI = nullptr;
Instruction* FirstI = Payload;
while (!isa<PayloadPtrIntrinsic>(I))
{
CurI = I->clone();
if (FirstI == Payload)
FirstI = CurI;
CurI->insertAfter(Payload);
CurI->setName(VALUE_NAME(Twine(I->getName()) + ".sink"));
I = cast<Instruction>(CurI->getOperand(0));
}
if (CurI)
CurI->setOperand(0, Payload);
auto* NewSI = cast<StoreInst>(SI->clone());
NewSI->insertAfter(FirstI);
NewSI->setOperand(1, FirstI);
}
// Determines whether stores in the function are eligible for sinking to
// continuations.
bool PayloadSinkingPass::canSink(
const CodeGenContext &Ctx, Function& F) const
{
ModuleMetaData* modMD = Ctx.getModuleMetaData();
auto &FuncMD = modMD->FuncMD;
auto MD = FuncMD.find(&F);
if (MD == FuncMD.end())
return false;
auto& rtInfo = MD->second.rtInfo;
auto ShaderTy = rtInfo.callableShaderType;
// If this shader returns to a continuation, this guarantees that all the
// inlined continuations collectively post dominate all payload writes
// in the current shader.
return (shaderReturnsToContinuation(ShaderTy) || ShaderTy == AnyHit) &&
!rtInfo.isContinuation &&
// Don't sink in callable since we don't know what the recursion
// limit is. If there is 1, that is safe.
(ShaderTy != Callable || modMD->rtInfo.NumContinuations == 1);
}
bool PayloadSinkingPass::runOnFunction(Function &F)
{
auto *CGCtx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
if (!canSink(*CGCtx, F))
return false;
SmallVector<PayloadPtrIntrinsic*, 1> PayloadPtrs;
SmallVector<ContinuationSignpostIntrinsic*, 4> Signposts;
for (auto& I : instructions(F))
{
if (auto* PI = dyn_cast<PayloadPtrIntrinsic>(&I))
PayloadPtrs.push_back(PI);
else if (auto* SPI = dyn_cast<ContinuationSignpostIntrinsic>(&I))
Signposts.push_back(SPI);
}
if (PayloadPtrs.size() != 1)
{
IGC_ASSERT_MESSAGE(PayloadPtrs.empty(), "this shouldn't happen!");
return false;
}
if (Signposts.empty())
return false;
auto& DL = F.getParent()->getDataLayout();
SmallVector<PayloadUse, 4> PayloadUses;
if (!collectAnalyzablePayloadUses(PayloadPtrs[0], DL, PayloadUses, 0))
return false;
auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
auto Stores = getNonAliasingStores(PayloadUses, DT);
bool Changed = false;
IRBuilder<> IRB(F.getContext());
for (auto* SI : Stores)
{
SmallVector<ContinuationSignpostIntrinsic*, 4> SinkLocs;
SmallPtrSet<const BasicBlock*, 4> StopBBs;
for (auto* I : Signposts)
{
if (DT->dominates(SI, I))
{
SinkLocs.push_back(I);
StopBBs.insert(I->getParent());
}
}
if (!mustExecute(SI->getParent(), StopBBs))
continue;
Changed = true;
// Note: if we move toward a hybrid approach of inlining some
// continuations and BTD to others, we'll need to tweak this to sink
// stores to the BTDs as well.
for (auto* Location : SinkLocs)
{
// Compute new payload pointer: FrameAddr + offset
uint32_t Addrspace = Location->getType()->getPointerAddressSpace();
IRB.SetInsertPoint(Location->getNextNode());
Value* NewPayload =
IRB.CreateBitCast(Location, IRB.getInt8PtrTy(Addrspace));
NewPayload = IRB.CreateGEP(IRB.getInt8Ty(), NewPayload, Location->getOffset());
NewPayload = IRB.CreateBitCast(NewPayload, PayloadPtrs[0]->getType());
sinkStore(SI, cast<Instruction>(NewPayload));
}
SI->eraseFromParent();
}
return Changed;
}
namespace IGC
{
Pass* createPayloadSinkingPass(void)
{
return new PayloadSinkingPass();
}
} // namespace IGC
|