|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// // |
| 3 | +// DxilDebugBreakInstrumentation.cpp // |
| 4 | +// Copyright (C) Microsoft Corporation. All rights reserved. // |
| 5 | +// This file is distributed under the University of Illinois Open Source // |
| 6 | +// License. See LICENSE.TXT for details. // |
| 7 | +// // |
| 8 | +// Provides a pass to instrument DebugBreak() calls for PIX. Each // |
| 9 | +// DebugBreak call is replaced with a UAV bit-write so PIX can detect // |
| 10 | +// which DebugBreak locations were hit without halting the GPU. // |
| 11 | +// // |
| 12 | +/////////////////////////////////////////////////////////////////////////////// |
| 13 | + |
| 14 | +#include "PixPassHelpers.h" |
| 15 | +#include "dxc/DXIL/DxilOperations.h" |
| 16 | +#include "dxc/DxilPIXPasses/DxilPIXPasses.h" |
| 17 | +#include "dxc/DxilPIXPasses/DxilPIXVirtualRegisters.h" |
| 18 | +#include "dxc/Support/Global.h" |
| 19 | +#include "llvm/IR/Module.h" |
| 20 | +#include "llvm/Support/FormattedStream.h" |
| 21 | + |
| 22 | +using namespace llvm; |
| 23 | +using namespace hlsl; |
| 24 | + |
| 25 | +class DxilDebugBreakInstrumentation : public ModulePass { |
| 26 | + |
| 27 | +public: |
| 28 | + static char ID; // Pass identification, replacement for typeid |
| 29 | + explicit DxilDebugBreakInstrumentation() : ModulePass(ID) {} |
| 30 | + StringRef getPassName() const override { |
| 31 | + return "DXIL DebugBreak Instrumentation"; |
| 32 | + } |
| 33 | + bool runOnModule(Module &M) override; |
| 34 | +}; |
| 35 | + |
| 36 | +bool DxilDebugBreakInstrumentation::runOnModule(Module &M) { |
| 37 | + DxilModule &DM = M.GetOrCreateDxilModule(); |
| 38 | + LLVMContext &Ctx = M.getContext(); |
| 39 | + OP *HlslOP = DM.GetOP(); |
| 40 | + |
| 41 | + hlsl::DxilResource *PixUAVResource = nullptr; |
| 42 | + |
| 43 | + UndefValue *UndefArg = UndefValue::get(Type::getInt32Ty(Ctx)); |
| 44 | + |
| 45 | + // Atomic operation to use for writing to the result UAV resource |
| 46 | + Function *AtomicOpFunc = |
| 47 | + HlslOP->GetOpFunc(OP::OpCode::AtomicBinOp, Type::getInt32Ty(Ctx)); |
| 48 | + Constant *AtomicBinOpcode = |
| 49 | + HlslOP->GetU32Const((uint32_t)OP::OpCode::AtomicBinOp); |
| 50 | + Constant *AtomicOr = HlslOP->GetU32Const((uint32_t)DXIL::AtomicBinOpCode::Or); |
| 51 | + |
| 52 | + std::map<Function *, CallInst *> FunctionToUAVHandle; |
| 53 | + |
| 54 | + // Collect all DebugBreak calls first, then modify. |
| 55 | + // This avoids invalidating iterators during modification. |
| 56 | + std::vector<CallInst *> DebugBreakCalls; |
| 57 | + |
| 58 | + Function *DebugBreakFunc = |
| 59 | + HlslOP->GetOpFunc(OP::OpCode::DebugBreak, Type::getVoidTy(Ctx)); |
| 60 | + for (const Use &U : DebugBreakFunc->uses()) { |
| 61 | + DebugBreakCalls.push_back(cast<CallInst>(U.getUser())); |
| 62 | + } |
| 63 | + |
| 64 | + for (CallInst *CI : DebugBreakCalls) { |
| 65 | + if (!PixUAVResource) |
| 66 | + PixUAVResource = |
| 67 | + PIXPassHelpers::CreateGlobalUAVResource(DM, 0, "PixUAVResource"); |
| 68 | + |
| 69 | + Function *F = CI->getParent()->getParent(); |
| 70 | + |
| 71 | + CallInst *PixUAVHandle = nullptr; |
| 72 | + const auto FunctionToUAVHandleIter = FunctionToUAVHandle.lower_bound(F); |
| 73 | + |
| 74 | + if ((FunctionToUAVHandleIter != FunctionToUAVHandle.end()) && |
| 75 | + (FunctionToUAVHandleIter->first == F)) { |
| 76 | + PixUAVHandle = FunctionToUAVHandleIter->second; |
| 77 | + } else { |
| 78 | + IRBuilder<> Builder(F->getEntryBlock().getFirstInsertionPt()); |
| 79 | + |
| 80 | + PixUAVHandle = PIXPassHelpers::CreateHandleForResource( |
| 81 | + DM, Builder, PixUAVResource, "PixUAVHandle"); |
| 82 | + |
| 83 | + FunctionToUAVHandle.insert(FunctionToUAVHandleIter, {F, PixUAVHandle}); |
| 84 | + } |
| 85 | + |
| 86 | + IRBuilder<> Builder(CI); |
| 87 | + |
| 88 | + uint32_t InstructionNumber = 0; |
| 89 | + if (!pix_dxil::PixDxilInstNum::FromInst(CI, &InstructionNumber)) { |
| 90 | + DXASSERT(false, "Failed to extract PIX instruction number metadata from " |
| 91 | + "DebugBreak call"); |
| 92 | + } |
| 93 | + |
| 94 | + // The output UAV is treated as a bit array where each bit corresponds |
| 95 | + // to an instruction number. |
| 96 | + const uint32_t InstructionNumByteOffset = |
| 97 | + (InstructionNumber / 32u) * sizeof(uint32_t); |
| 98 | + const uint32_t InstructionNumBitPosition = (InstructionNumber % 32u); |
| 99 | + const uint32_t InstructionNumBitMask = 1u << InstructionNumBitPosition; |
| 100 | + |
| 101 | + Constant *UAVByteOffsetArg = HlslOP->GetU32Const(InstructionNumByteOffset); |
| 102 | + Constant *BitMaskArg = HlslOP->GetU32Const(InstructionNumBitMask); |
| 103 | + |
| 104 | + // Write a 1 bit at the position corresponding to this DebugBreak's |
| 105 | + // instruction number, indicating it was hit. |
| 106 | + Builder.CreateCall( |
| 107 | + AtomicOpFunc, |
| 108 | + { |
| 109 | + AtomicBinOpcode, // i32, ; opcode |
| 110 | + PixUAVHandle, // %dx.types.Handle, ; resource handle |
| 111 | + AtomicOr, // i32, ; binary operation code |
| 112 | + UAVByteOffsetArg, // i32, ; coordinate c0: byte offset |
| 113 | + UndefArg, // i32, ; coordinate c1 (unused) |
| 114 | + UndefArg, // i32, ; coordinate c2 (unused) |
| 115 | + BitMaskArg // i32); value |
| 116 | + }, |
| 117 | + "DebugBreakBitSet"); |
| 118 | + |
| 119 | + // Remove the original DebugBreak call to prevent GPU halt |
| 120 | + CI->eraseFromParent(); |
| 121 | + } |
| 122 | + |
| 123 | + // Clean up the now-unused declaration. Not strictly required for |
| 124 | + // correctness, but keeps the module free of dead references. |
| 125 | + if (DebugBreakFunc->use_empty()) |
| 126 | + DebugBreakFunc->eraseFromParent(); |
| 127 | + |
| 128 | + const bool modified = (PixUAVResource != nullptr); |
| 129 | + |
| 130 | + if (modified) { |
| 131 | + DM.ReEmitDxilResources(); |
| 132 | + |
| 133 | + if (OSOverride != nullptr) { |
| 134 | + formatted_raw_ostream FOS(*OSOverride); |
| 135 | + FOS << "\nFoundDebugBreak\n"; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return modified; |
| 140 | +} |
| 141 | + |
| 142 | +char DxilDebugBreakInstrumentation::ID = 0; |
| 143 | + |
| 144 | +ModulePass *llvm::createDxilDebugBreakInstrumentationPass() { |
| 145 | + return new DxilDebugBreakInstrumentation(); |
| 146 | +} |
| 147 | + |
| 148 | +INITIALIZE_PASS(DxilDebugBreakInstrumentation, |
| 149 | + "hlsl-dxil-debugbreak-instrumentation", |
| 150 | + "HLSL DXIL DebugBreak instrumentation for PIX", false, false) |
0 commit comments