|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// // |
| 3 | +// DxilNonUniformResourceIndexInstrumentation.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 add instrumentation to determine missing usage of the // |
| 9 | +// NonUniformResourceIndex qualifier when dynamically indexing resources. // |
| 10 | +// Used by PIX. // |
| 11 | +// // |
| 12 | +/////////////////////////////////////////////////////////////////////////////// |
| 13 | + |
| 14 | +#include "PixPassHelpers.h" |
| 15 | +#include "dxc/DXIL/DxilInstructions.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 DxilNonUniformResourceIndexInstrumentation : public ModulePass { |
| 26 | + |
| 27 | +public: |
| 28 | + static char ID; // Pass identification, replacement for typeid |
| 29 | + explicit DxilNonUniformResourceIndexInstrumentation() : ModulePass(ID) {} |
| 30 | + StringRef getPassName() const override { |
| 31 | + return "DXIL NonUniformResourceIndex Instrumentation"; |
| 32 | + } |
| 33 | + bool runOnModule(Module &M) override; |
| 34 | +}; |
| 35 | + |
| 36 | +bool DxilNonUniformResourceIndexInstrumentation::runOnModule(Module &M) { |
| 37 | + // This pass adds instrumentation for incorrect NonUniformResourceIndex usage |
| 38 | + |
| 39 | + DxilModule &DM = M.GetOrCreateDxilModule(); |
| 40 | + LLVMContext &Ctx = M.getContext(); |
| 41 | + OP *HlslOP = DM.GetOP(); |
| 42 | + |
| 43 | + hlsl::DxilResource *PixUAVResource = nullptr; |
| 44 | + |
| 45 | + UndefValue *UndefArg = UndefValue::get(Type::getInt32Ty(Ctx)); |
| 46 | + |
| 47 | + // Use WaveActiveAllEqual to check if a dynamic index is uniform |
| 48 | + Function *WaveActiveAllEqualFunc = HlslOP->GetOpFunc( |
| 49 | + DXIL::OpCode::WaveActiveAllEqual, Type::getInt32Ty(Ctx)); |
| 50 | + Constant *WaveActiveAllEqualOpCode = |
| 51 | + HlslOP->GetI32Const((int32_t)DXIL::OpCode::WaveActiveAllEqual); |
| 52 | + |
| 53 | + // Atomic operation to use for writing to the result uav resource |
| 54 | + Function *AtomicOpFunc = |
| 55 | + HlslOP->GetOpFunc(OP::OpCode::AtomicBinOp, Type::getInt32Ty(Ctx)); |
| 56 | + Constant *AtomicBinOpcode = |
| 57 | + HlslOP->GetU32Const((uint32_t)OP::OpCode::AtomicBinOp); |
| 58 | + Constant *AtomicOr = HlslOP->GetU32Const((uint32_t)DXIL::AtomicBinOpCode::Or); |
| 59 | + |
| 60 | + std::map<Function *, CallInst *> FunctionToUAVHandle; |
| 61 | + |
| 62 | + // This is the main pass that will iterate through all of the resources that |
| 63 | + // are dynamically indexed. If not already marked NonUniformResourceIndex, |
| 64 | + // then insert WaveActiveAllEqual to determine if the index is uniform |
| 65 | + // and finally write to a UAV resource with the result. |
| 66 | + |
| 67 | + PIXPassHelpers::ForEachDynamicallyIndexedResource( |
| 68 | + DM, [&](bool IsNonUniformIndex, Instruction *CreateHandle, |
| 69 | + Value *IndexOperand) { |
| 70 | + if (IsNonUniformIndex) { |
| 71 | + // The NonUniformResourceIndex qualifier was used, continue. |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + if (!PixUAVResource) { |
| 76 | + PixUAVResource = |
| 77 | + PIXPassHelpers::CreateGlobalUAVResource(DM, 0, "PixUAVResource"); |
| 78 | + } |
| 79 | + |
| 80 | + CallInst *PixUAVHandle = nullptr; |
| 81 | + Function *F = CreateHandle->getParent()->getParent(); |
| 82 | + |
| 83 | + const auto FunctionToUAVHandleIter = FunctionToUAVHandle.lower_bound(F); |
| 84 | + |
| 85 | + if ((FunctionToUAVHandleIter != FunctionToUAVHandle.end()) && |
| 86 | + (FunctionToUAVHandleIter->first == F)) { |
| 87 | + PixUAVHandle = FunctionToUAVHandleIter->second; |
| 88 | + } else { |
| 89 | + IRBuilder<> Builder(F->getEntryBlock().getFirstInsertionPt()); |
| 90 | + |
| 91 | + PixUAVHandle = PIXPassHelpers::CreateHandleForResource( |
| 92 | + DM, Builder, PixUAVResource, "PixUAVHandle"); |
| 93 | + |
| 94 | + FunctionToUAVHandle.insert(FunctionToUAVHandleIter, |
| 95 | + {F, PixUAVHandle}); |
| 96 | + } |
| 97 | + |
| 98 | + IRBuilder<> Builder(CreateHandle); |
| 99 | + |
| 100 | + uint32_t InstructionNumber = 0; |
| 101 | + if (!pix_dxil::PixDxilInstNum::FromInst(CreateHandle, |
| 102 | + &InstructionNumber)) { |
| 103 | + DXASSERT_NOMSG(false); |
| 104 | + } |
| 105 | + |
| 106 | + // The output UAV is treated as a bit array where each bit corresponds |
| 107 | + // to an instruction number. This determines what byte offset to write |
| 108 | + // our result to based on the instruction number. |
| 109 | + const uint32_t InstructionNumByteOffset = |
| 110 | + (InstructionNumber / 32u) * sizeof(uint32_t); |
| 111 | + const uint32_t InstructionNumBitPosition = (InstructionNumber % 32u); |
| 112 | + const uint32_t InstructionNumBitMask = 1u << InstructionNumBitPosition; |
| 113 | + |
| 114 | + Constant *UAVByteOffsetArg = |
| 115 | + HlslOP->GetU32Const(InstructionNumByteOffset); |
| 116 | + |
| 117 | + CallInst *WaveActiveAllEqualCall = Builder.CreateCall( |
| 118 | + WaveActiveAllEqualFunc, {WaveActiveAllEqualOpCode, IndexOperand}); |
| 119 | + |
| 120 | + // This takes the result of the WaveActiveAllEqual result and shifts |
| 121 | + // it into the same bit position as the instruction number, followed |
| 122 | + // by an xor to determine what to write to the UAV |
| 123 | + Value *IsWaveEqual = |
| 124 | + Builder.CreateZExt(WaveActiveAllEqualCall, Builder.getInt32Ty()); |
| 125 | + Value *WaveEqualBitMask = |
| 126 | + Builder.CreateShl(IsWaveEqual, InstructionNumBitPosition); |
| 127 | + Value *FinalResult = |
| 128 | + Builder.CreateXor(WaveEqualBitMask, InstructionNumBitMask); |
| 129 | + |
| 130 | + // Generate instructions to bitwise OR a UAV value corresponding |
| 131 | + // to the instruction number and result of WaveActiveAllEqual. |
| 132 | + // If WaveActiveAllEqual was false, we write a 1, otherwise a 0. |
| 133 | + Builder.CreateCall( |
| 134 | + AtomicOpFunc, |
| 135 | + { |
| 136 | + AtomicBinOpcode, // i32, ; opcode |
| 137 | + PixUAVHandle, // %dx.types.Handle, ; resource handle |
| 138 | + AtomicOr, // i32, ; binary operation code : |
| 139 | + // EXCHANGE, IADD, AND, OR, XOR |
| 140 | + // IMIN, IMAX, UMIN, UMAX |
| 141 | + UAVByteOffsetArg, // i32, ; coordinate c0: byte offset |
| 142 | + UndefArg, // i32, ; coordinate c1 (unused) |
| 143 | + UndefArg, // i32, ; coordinate c2 (unused) |
| 144 | + FinalResult // i32); value |
| 145 | + }, |
| 146 | + "UAVInstructionNumberBitSet"); |
| 147 | + return true; |
| 148 | + }); |
| 149 | + |
| 150 | + const bool modified = (PixUAVResource != nullptr); |
| 151 | + |
| 152 | + if (modified) { |
| 153 | + DM.ReEmitDxilResources(); |
| 154 | + |
| 155 | + if (OSOverride != nullptr) { |
| 156 | + formatted_raw_ostream FOS(*OSOverride); |
| 157 | + FOS << "\nFoundDynamicIndexingNoNuri\n"; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return modified; |
| 162 | +} |
| 163 | + |
| 164 | +char DxilNonUniformResourceIndexInstrumentation::ID = 0; |
| 165 | + |
| 166 | +ModulePass *llvm::createDxilNonUniformResourceIndexInstrumentationPass() { |
| 167 | + return new DxilNonUniformResourceIndexInstrumentation(); |
| 168 | +} |
| 169 | + |
| 170 | +INITIALIZE_PASS(DxilNonUniformResourceIndexInstrumentation, |
| 171 | + "hlsl-dxil-non-uniform-resource-index-instrumentation", |
| 172 | + "HLSL DXIL NonUniformResourceIndex instrumentation for PIX", |
| 173 | + false, false) |
0 commit comments