|
| 1 | +//===- DXILPostOptimizationValidation.cpp - Opt DXIL validation ----------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "DXILPostOptimizationValidation.h" |
| 10 | +#include "DXILShaderFlags.h" |
| 11 | +#include "DirectX.h" |
| 12 | +#include "llvm/Analysis/DXILMetadataAnalysis.h" |
| 13 | +#include "llvm/Analysis/DXILResource.h" |
| 14 | +#include "llvm/IR/DiagnosticInfo.h" |
| 15 | +#include "llvm/IR/Instructions.h" |
| 16 | +#include "llvm/IR/IntrinsicsDirectX.h" |
| 17 | +#include "llvm/IR/Module.h" |
| 18 | +#include "llvm/InitializePasses.h" |
| 19 | + |
| 20 | +#define DEBUG_TYPE "dxil-post-optimization-validation" |
| 21 | + |
| 22 | +using namespace llvm; |
| 23 | +using namespace llvm::dxil; |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +static void reportInvalidDirection(Module &M, DXILResourceMap &DRM) { |
| 28 | + for (const auto &UAV : DRM.uavs()) { |
| 29 | + if (UAV.CounterDirection != ResourceCounterDirection::Invalid) |
| 30 | + continue; |
| 31 | + |
| 32 | + CallInst *ResourceHandle = nullptr; |
| 33 | + for (CallInst *MaybeHandle : DRM.calls()) { |
| 34 | + if (*DRM.find(MaybeHandle) == UAV) { |
| 35 | + ResourceHandle = MaybeHandle; |
| 36 | + break; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + StringRef Message = "RWStructuredBuffers may increment or decrement their " |
| 41 | + "counters, but not both."; |
| 42 | + for (const auto &U : ResourceHandle->users()) { |
| 43 | + const CallInst *CI = dyn_cast<CallInst>(U); |
| 44 | + if (!CI && CI->getIntrinsicID() != Intrinsic::dx_resource_updatecounter) |
| 45 | + continue; |
| 46 | + |
| 47 | + M.getContext().diagnose(DiagnosticInfoGenericWithLoc( |
| 48 | + Message, *CI->getFunction(), CI->getDebugLoc())); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +} // namespace |
| 54 | + |
| 55 | +PreservedAnalyses |
| 56 | +DXILPostOptimizationValidation::run(Module &M, ModuleAnalysisManager &MAM) { |
| 57 | + DXILResourceMap &DRM = MAM.getResult<DXILResourceAnalysis>(M); |
| 58 | + |
| 59 | + if (DRM.hasInvalidCounterDirection()) |
| 60 | + reportInvalidDirection(M, DRM); |
| 61 | + |
| 62 | + return PreservedAnalyses::all(); |
| 63 | +} |
| 64 | + |
| 65 | +namespace { |
| 66 | +class DXILPostOptimizationValidationLegacy : public ModulePass { |
| 67 | +public: |
| 68 | + bool runOnModule(Module &M) override { |
| 69 | + DXILResourceMap &DRM = |
| 70 | + getAnalysis<DXILResourceWrapperPass>().getResourceMap(); |
| 71 | + |
| 72 | + if (DRM.hasInvalidCounterDirection()) |
| 73 | + reportInvalidDirection(M, DRM); |
| 74 | + |
| 75 | + return false; |
| 76 | + } |
| 77 | + StringRef getPassName() const override { |
| 78 | + return "DXIL Post Optimization Validation"; |
| 79 | + } |
| 80 | + DXILPostOptimizationValidationLegacy() : ModulePass(ID) {} |
| 81 | + |
| 82 | + static char ID; // Pass identification. |
| 83 | + void getAnalysisUsage(llvm::AnalysisUsage &AU) const override { |
| 84 | + AU.addRequired<DXILResourceWrapperPass>(); |
| 85 | + AU.addPreserved<DXILResourceWrapperPass>(); |
| 86 | + AU.addPreserved<DXILMetadataAnalysisWrapperPass>(); |
| 87 | + AU.addPreserved<ShaderFlagsAnalysisWrapper>(); |
| 88 | + } |
| 89 | +}; |
| 90 | +char DXILPostOptimizationValidationLegacy::ID = 0; |
| 91 | +} // end anonymous namespace |
| 92 | + |
| 93 | +INITIALIZE_PASS_BEGIN(DXILPostOptimizationValidationLegacy, DEBUG_TYPE, |
| 94 | + "DXIL Post Optimization Validation", false, false) |
| 95 | +INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass) |
| 96 | +INITIALIZE_PASS_DEPENDENCY(DXILResourceWrapperPass) |
| 97 | +INITIALIZE_PASS_END(DXILPostOptimizationValidationLegacy, DEBUG_TYPE, |
| 98 | + "DXIL Post Optimization Validation", false, false) |
| 99 | + |
| 100 | +ModulePass *llvm::createDXILPostOptimizationValidationLegacyPass() { |
| 101 | + return new DXILPostOptimizationValidationLegacy(); |
| 102 | +} |
0 commit comments