-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[HLSL] Raise Diag for Invalid CounterDirection #137697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
50a114f
66f32e4
4dfa76d
75bd70c
e108206
3ff8c89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
//===- 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 "DXILConstants.h" | ||
#include "DXILIntrinsicExpansion.h" | ||
#include "DXILOpBuilder.h" | ||
#include "DXILShaderFlags.h" | ||
#include "DirectX.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/Analysis/DXILMetadataAnalysis.h" | ||
#include "llvm/Analysis/DXILResource.h" | ||
#include "llvm/CodeGen/Passes.h" | ||
#include "llvm/IR/DiagnosticInfo.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/IR/Instruction.h" | ||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/IR/Intrinsics.h" | ||
#include "llvm/IR/IntrinsicsDirectX.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/IR/PassManager.h" | ||
#include "llvm/InitializePasses.h" | ||
#include "llvm/Pass.h" | ||
#include "llvm/Support/ErrorHandling.h" | ||
#include <cstdio> | ||
|
||
#define DEBUG_TYPE "dxil-post-optimization-validation" | ||
|
||
using namespace llvm; | ||
using namespace llvm::dxil; | ||
|
||
namespace { | ||
class DXILValidator { | ||
V-FEXrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Module &M; | ||
DXILResourceMap &DRM; | ||
|
||
public: | ||
DXILValidator(Module &M, DXILResourceMap &DRM) : M(M), DRM(DRM) {} | ||
|
||
void validate() { | ||
for (const auto &UAV : DRM.uavs()) { | ||
if (UAV.CounterDirection != ResourceCounterDirection::Invalid) | ||
continue; | ||
|
||
CallInst *ResourceHandle = nullptr; | ||
for (CallInst *MaybeHandle : DRM.calls()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very inefficient solution to this problem but I wanted to put it up for review anyways. It may be okay to leave this since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is ok to iterate over all I don't understand your reason 1 though - why is the number of Invalid CounterDirections always going to be under 10? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sorry I asserted the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The loop is iterating over all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The inner loop does, but the outer loop checks for invalid direction first so the inner loop only runs once for each invalid direction uav |
||
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); | ||
|
||
DXILValidator(M, DRM).validate(); | ||
V-FEXrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return PreservedAnalyses::all(); | ||
} | ||
|
||
namespace { | ||
class DXILPostOptimizationValidationLegacy : public ModulePass { | ||
public: | ||
bool runOnModule(Module &M) override { | ||
DXILResourceMap &DRM = | ||
getAnalysis<DXILResourceWrapperPass>().getResourceMap(); | ||
|
||
DXILValidator(M, DRM).validate(); | ||
|
||
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(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//===- DXILPostOptimizationValidation.h - Opt DXIL Validations -*- C++ -*--===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// \file Pass for validating DXIL after lowering and optimizations are applied. | ||
V-FEXrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIB_TARGET_DIRECTX_DXILPOSTOPTIMIZATIONVALIDATION_H | ||
#define LLVM_LIB_TARGET_DIRECTX_DXILPOSTOPTIMIZATIONVALIDATION_H | ||
|
||
#include "llvm/IR/PassManager.h" | ||
|
||
namespace llvm { | ||
|
||
class DXILPostOptimizationValidation | ||
: public PassInfoMixin<DXILPostOptimizationValidation> { | ||
public: | ||
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); | ||
}; | ||
|
||
} // namespace llvm | ||
|
||
#endif // LLVM_LIB_TARGET_DIRECTX_DXILPOSTOPTIMIZATIONVALIDATION_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
; RUN: not opt -S -passes='dxil-post-optimization-validation' -mtriple=dxil-pc-shadermodel6.3-library %s 2>&1 | FileCheck %s | ||
; CHECK: RWStructuredBuffers may increment or decrement their counters, but not both. | ||
|
||
define void @inc_and_dec() { | ||
entry: | ||
%handle = call target("dx.RawBuffer", float, 1, 0) @llvm.dx.resource.handlefrombinding(i32 1, i32 2, i32 3, i32 4, i1 false) | ||
call i32 @llvm.dx.resource.updatecounter(target("dx.RawBuffer", float, 1, 0) %handle, i8 -1) | ||
call i32 @llvm.dx.resource.updatecounter(target("dx.RawBuffer", float, 1, 0) %handle, i8 1) | ||
ret void | ||
} | ||
hekota marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.