| //===- DXILPostOptimizationValidation.cpp - Opt DXIL validation ----------===// |
| // |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| // See https://llvm.org/LICENSE.txt for license information. |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| // |
| //===----------------------------------------------------------------------===// |
| |
| #include "DXILPostOptimizationValidation.h" |
| #include "DXILShaderFlags.h" |
| #include "DirectX.h" |
| #include "llvm/Analysis/DXILMetadataAnalysis.h" |
| #include "llvm/Analysis/DXILResource.h" |
| #include "llvm/IR/DiagnosticInfo.h" |
| #include "llvm/IR/Instructions.h" |
| #include "llvm/IR/IntrinsicsDirectX.h" |
| #include "llvm/IR/Module.h" |
| #include "llvm/InitializePasses.h" |
| |
| #define DEBUG_TYPE "dxil-post-optimization-validation" |
| |
| using namespace llvm; |
| using namespace llvm::dxil; |
| |
| namespace { |
| |
| static void reportInvalidDirection(Module &M, DXILResourceMap &DRM) { |
| for (const auto &UAV : DRM.uavs()) { |
| if (UAV.CounterDirection != ResourceCounterDirection::Invalid) |
| continue; |
| |
| CallInst *ResourceHandle = nullptr; |
| for (CallInst *MaybeHandle : DRM.calls()) { |
| if (*DRM.find(MaybeHandle) == UAV) { |
| ResourceHandle = MaybeHandle; |
| break; |
| } |
| } |
| |
| StringRef Message = "RWStructuredBuffers may increment or decrement their " |
| "counters, but not both."; |
| for (const auto &U : ResourceHandle->users()) { |
| const CallInst *CI = dyn_cast<CallInst>(U); |
| if (!CI && CI->getIntrinsicID() != Intrinsic::dx_resource_updatecounter) |
| continue; |
| |
| M.getContext().diagnose(DiagnosticInfoGenericWithLoc( |
| Message, *CI->getFunction(), CI->getDebugLoc())); |
| } |
| } |
| } |
| |
| } // namespace |
| |
| PreservedAnalyses |
| DXILPostOptimizationValidation::run(Module &M, ModuleAnalysisManager &MAM) { |
| DXILResourceMap &DRM = MAM.getResult<DXILResourceAnalysis>(M); |
| |
| if (DRM.hasInvalidCounterDirection()) |
| reportInvalidDirection(M, DRM); |
| |
| return PreservedAnalyses::all(); |
| } |
| |
| namespace { |
| class DXILPostOptimizationValidationLegacy : public ModulePass { |
| public: |
| bool runOnModule(Module &M) override { |
| DXILResourceMap &DRM = |
| getAnalysis<DXILResourceWrapperPass>().getResourceMap(); |
| |
| if (DRM.hasInvalidCounterDirection()) |
| reportInvalidDirection(M, DRM); |
| |
| return false; |
| } |
| StringRef getPassName() const override { |
| return "DXIL Post Optimization Validation"; |
| } |
| DXILPostOptimizationValidationLegacy() : ModulePass(ID) {} |
| |
| static char ID; // Pass identification. |
| void getAnalysisUsage(llvm::AnalysisUsage &AU) const override { |
| AU.addRequired<DXILResourceWrapperPass>(); |
| AU.addPreserved<DXILResourceWrapperPass>(); |
| AU.addPreserved<DXILMetadataAnalysisWrapperPass>(); |
| AU.addPreserved<ShaderFlagsAnalysisWrapper>(); |
| } |
| }; |
| char DXILPostOptimizationValidationLegacy::ID = 0; |
| } // end anonymous namespace |
| |
| INITIALIZE_PASS_BEGIN(DXILPostOptimizationValidationLegacy, DEBUG_TYPE, |
| "DXIL Post Optimization Validation", false, false) |
| INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass) |
| INITIALIZE_PASS_DEPENDENCY(DXILResourceWrapperPass) |
| INITIALIZE_PASS_END(DXILPostOptimizationValidationLegacy, DEBUG_TYPE, |
| "DXIL Post Optimization Validation", false, false) |
| |
| ModulePass *llvm::createDXILPostOptimizationValidationLegacyPass() { |
| return new DXILPostOptimizationValidationLegacy(); |
| } |