Skip to content

Commit d56fa96

Browse files
joaosaffranjoaosaffranJoao Saffranjoaosaffran-zz
authored
[DirectX] Add Range Overlap validation (#152229)
As part of the Root Signature Spec, we need to validate if Root Signatures are not defining overlapping ranges. Closes: #126645 --------- Co-authored-by: joaosaffran <[email protected]> Co-authored-by: Joao Saffran <{ID}+{username}@users.noreply.github.com> Co-authored-by: Joao Saffran <[email protected]>
1 parent ca9ddd5 commit d56fa96

20 files changed

+319
-45
lines changed

llvm/include/llvm/BinaryFormat/DXContainer.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,6 @@ enum class FeatureFlags : uint64_t {
158158
static_assert((uint64_t)FeatureFlags::NextUnusedBit <= 1ull << 63,
159159
"Shader flag bits exceed enum size.");
160160

161-
LLVM_ABI ArrayRef<EnumEntry<llvm::dxil::ResourceClass>> getResourceClasses();
162-
163161
#define ROOT_SIGNATURE_FLAG(Num, Val) Val = Num,
164162
enum class RootFlags : uint32_t {
165163
#include "DXContainerConstants.def"

llvm/include/llvm/Support/DXILABI.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef LLVM_SUPPORT_DXILABI_H
1818
#define LLVM_SUPPORT_DXILABI_H
1919

20+
#include "llvm/ADT/StringRef.h"
21+
#include "llvm/Support/ScopedPrinter.h"
2022
#include <cstdint>
2123

2224
namespace llvm {
@@ -99,6 +101,10 @@ enum class SamplerFeedbackType : uint32_t {
99101
const unsigned MinWaveSize = 4;
100102
const unsigned MaxWaveSize = 128;
101103

104+
LLVM_ABI ArrayRef<EnumEntry<ResourceClass>> getResourceClasses();
105+
106+
LLVM_ABI StringRef getResourceClassName(ResourceClass RC);
107+
102108
} // namespace dxil
103109
} // namespace llvm
104110

llvm/lib/Analysis/DXILResource.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/IR/Metadata.h"
2121
#include "llvm/IR/Module.h"
2222
#include "llvm/InitializePasses.h"
23+
#include "llvm/Support/DXILABI.h"
2324
#include "llvm/Support/FormatVariadic.h"
2425
#include <cstdint>
2526
#include <optional>
@@ -29,20 +30,6 @@
2930
using namespace llvm;
3031
using namespace dxil;
3132

32-
static StringRef getResourceClassName(ResourceClass RC) {
33-
switch (RC) {
34-
case ResourceClass::SRV:
35-
return "SRV";
36-
case ResourceClass::UAV:
37-
return "UAV";
38-
case ResourceClass::CBuffer:
39-
return "CBuffer";
40-
case ResourceClass::Sampler:
41-
return "Sampler";
42-
}
43-
llvm_unreachable("Unhandled ResourceClass");
44-
}
45-
4633
static StringRef getResourceKindName(ResourceKind RK) {
4734
switch (RK) {
4835
case ResourceKind::Texture1D:

llvm/lib/BinaryFormat/DXContainer.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,6 @@ ArrayRef<EnumEntry<SigComponentType>> dxbc::getSigComponentTypes() {
6060
return ArrayRef(SigComponentTypes);
6161
}
6262

63-
static const EnumEntry<dxil::ResourceClass> ResourceClassNames[] = {
64-
{"SRV", llvm::dxil::ResourceClass::SRV},
65-
{"UAV", llvm::dxil::ResourceClass::UAV},
66-
{"CBV", llvm::dxil::ResourceClass::CBuffer},
67-
{"Sampler", llvm::dxil::ResourceClass::Sampler},
68-
};
69-
70-
ArrayRef<EnumEntry<llvm::dxil::ResourceClass>> dxbc::getResourceClasses() {
71-
return ArrayRef(ResourceClassNames);
72-
}
73-
7463
static const EnumEntry<RootFlags> RootFlagNames[] = {
7564
#define ROOT_SIGNATURE_FLAG(Val, Enum) {#Enum, RootFlags::Enum},
7665
#include "llvm/BinaryFormat/DXContainerConstants.def"

llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static raw_ostream &operator<<(raw_ostream &OS,
9494

9595
static raw_ostream &operator<<(raw_ostream &OS, const ClauseType &Type) {
9696
OS << enumToStringRef(dxil::ResourceClass(llvm::to_underlying(Type)),
97-
dxbc::getResourceClasses());
97+
dxil::getResourceClasses());
9898

9999
return OS;
100100
}

llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ MDNode *MetadataBuilder::BuildRootDescriptor(const RootDescriptor &Descriptor) {
121121
IRBuilder<> Builder(Ctx);
122122
StringRef ResName =
123123
enumToStringRef(dxil::ResourceClass(to_underlying(Descriptor.Type)),
124-
dxbc::getResourceClasses());
124+
dxil::getResourceClasses());
125125
assert(!ResName.empty() && "Provided an invalid Resource Class");
126126
SmallString<7> Name({"Root", ResName});
127127
Metadata *Operands[] = {
@@ -163,7 +163,7 @@ MDNode *MetadataBuilder::BuildDescriptorTableClause(
163163
IRBuilder<> Builder(Ctx);
164164
StringRef ResName =
165165
enumToStringRef(dxil::ResourceClass(to_underlying(Clause.Type)),
166-
dxbc::getResourceClasses());
166+
dxil::getResourceClasses());
167167
assert(!ResName.empty() && "Provided an invalid Resource Class");
168168
Metadata *Operands[] = {
169169
MDString::get(Ctx, ResName),

llvm/lib/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ add_llvm_component_library(LLVMSupport
182182
DivisionByConstantInfo.cpp
183183
DAGDeltaAlgorithm.cpp
184184
DJB.cpp
185+
DXILABI.cpp
185186
DynamicAPInt.cpp
186187
ELFAttributes.cpp
187188
ELFAttrParserCompact.cpp

llvm/lib/Support/DXILABI.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===-- DXILABI.cpp - ABI Sensitive Values for DXIL -----------------------===//
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+
// This file contains definitions of various constants and enums that are
10+
// required to remain stable as per the DXIL format's requirements.
11+
//
12+
// Documentation for DXIL can be found in
13+
// https://github.com/Microsoft/DirectXShaderCompiler/blob/main/docs/DXIL.rst.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#include "llvm/Support/DXILABI.h"
18+
#include "llvm/Support/ScopedPrinter.h"
19+
using namespace llvm;
20+
21+
static const EnumEntry<dxil::ResourceClass> ResourceClassNames[] = {
22+
{"SRV", llvm::dxil::ResourceClass::SRV},
23+
{"UAV", llvm::dxil::ResourceClass::UAV},
24+
{"CBV", llvm::dxil::ResourceClass::CBuffer},
25+
{"Sampler", llvm::dxil::ResourceClass::Sampler},
26+
};
27+
28+
ArrayRef<EnumEntry<llvm::dxil::ResourceClass>> dxil::getResourceClasses() {
29+
return ArrayRef(ResourceClassNames);
30+
}
31+
32+
StringRef dxil::getResourceClassName(dxil::ResourceClass RC) {
33+
return enumToStringRef(RC, getResourceClasses());
34+
}

llvm/lib/Target/DirectX/DXContainerGlobals.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ void DXContainerGlobals::addRootSignature(Module &M,
162162

163163
auto &RSA = getAnalysis<RootSignatureAnalysisWrapper>().getRSInfo();
164164
const Function *EntryFunction = MMI.EntryPropertyVec[0].Entry;
165-
const std::optional<mcdxbc::RootSignatureDesc> &RS =
166-
RSA.getDescForFunction(EntryFunction);
165+
const mcdxbc::RootSignatureDesc *RS = RSA.getDescForFunction(EntryFunction);
167166

168167
if (!RS)
169168
return;

llvm/lib/Target/DirectX/DXILOpLowering.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "DXILOpLowering.h"
1010
#include "DXILConstants.h"
1111
#include "DXILOpBuilder.h"
12+
#include "DXILRootSignature.h"
1213
#include "DXILShaderFlags.h"
1314
#include "DirectX.h"
1415
#include "llvm/ADT/SmallVector.h"
@@ -918,6 +919,7 @@ PreservedAnalyses DXILOpLowering::run(Module &M, ModuleAnalysisManager &MAM) {
918919
PA.preserve<DXILResourceAnalysis>();
919920
PA.preserve<DXILMetadataAnalysis>();
920921
PA.preserve<ShaderFlagsAnalysis>();
922+
PA.preserve<RootSignatureAnalysis>();
921923
return PA;
922924
}
923925

@@ -945,6 +947,7 @@ class DXILOpLoweringLegacy : public ModulePass {
945947
AU.addPreserved<DXILResourceWrapperPass>();
946948
AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
947949
AU.addPreserved<ShaderFlagsAnalysisWrapper>();
950+
AU.addPreserved<RootSignatureAnalysisWrapper>();
948951
}
949952
};
950953
char DXILOpLoweringLegacy::ID = 0;

0 commit comments

Comments
 (0)