Skip to content

Commit 39048b6

Browse files
authored
[DirectX] Move ResourceClass enum into DXILABI. NFC (#96335)
The resource class isn't HLSL specific, and we'll need to use it in the DirectX backend as well. I've also removed the "invalid" enum value since it isn't needed or used, which necessitates fixing up the clang attr emitter to handle external enum types that are fully covered by the attribute.
1 parent bf3e328 commit 39048b6

File tree

4 files changed

+58
-41
lines changed

4 files changed

+58
-41
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ class DefaultIntArgument<string name, int default> : IntArgument<name, 1> {
287287
// possible values, and a list of enumerators to map them to.
288288
class EnumArgument<string name, string type, bit is_string, list<string> values,
289289
list<string> enums, bit opt = 0, bit fake = 0,
290-
bit isExternalType = 0>
290+
bit isExternalType = 0, bit isCovered = 1>
291291
: Argument<name, opt, fake> {
292292
string Type = type;
293293
// When true, the argument will be parsed as an unevaluated string literal
@@ -296,13 +296,16 @@ class EnumArgument<string name, string type, bit is_string, list<string> values,
296296
list<string> Values = values;
297297
list<string> Enums = enums;
298298
bit IsExternalType = isExternalType;
299+
// We need to know whether an external enum is fully covered by the options
300+
// in order to decide whether to emit unreachable default labels in a switch.
301+
bit IsCovered = isCovered;
299302
}
300303

301304
// FIXME: There should be a VariadicArgument type that takes any other type
302305
// of argument and generates the appropriate type.
303306
class VariadicEnumArgument<string name, string type, bit is_string,
304307
list<string> values, list<string> enums,
305-
bit isExternalType = 0>
308+
bit isExternalType = 0, bit isCovered = 1>
306309
: Argument<name, 1> {
307310
string Type = type;
308311
// When true, the argument will be parsed as an unevaluated string literal
@@ -311,6 +314,9 @@ class VariadicEnumArgument<string name, string type, bit is_string,
311314
list<string> Values = values;
312315
list<string> Enums = enums;
313316
bit IsExternalType = isExternalType;
317+
// We need to know whether an external enum is fully covered by the options
318+
// in order to decide whether to emit unreachable default labels in a switch.
319+
bit IsCovered = isCovered;
314320
}
315321

316322
// Represents an attribute wrapped by another attribute.
@@ -2913,7 +2919,7 @@ def CodeModel : InheritableAttr, TargetSpecificAttr<TargetLoongArch> {
29132919
let Spellings = [GCC<"model">];
29142920
let Args = [EnumArgument<"Model", "llvm::CodeModel::Model", /*is_string=*/1,
29152921
["normal", "medium", "extreme"], ["Small", "Medium", "Large"],
2916-
/*opt=*/0, /*fake=*/0, /*isExternalType=*/1>];
2922+
/*opt=*/0, /*fake=*/0, /*isExternalType=*/1, /*isCovered=*/0>];
29172923
let Subjects = SubjectList<[NonTLSGlobalVar], ErrorDiag>;
29182924
let Documentation = [CodeModelDocs];
29192925
}
@@ -4472,7 +4478,7 @@ def HLSLShader : InheritableAttr {
44724478
["Pixel", "Vertex", "Geometry", "Hull", "Domain", "Compute",
44734479
"RayGeneration", "Intersection", "AnyHit", "ClosestHit",
44744480
"Miss", "Callable", "Mesh", "Amplification"],
4475-
/*opt=*/0, /*fake=*/0, /*isExternalType=*/1>
4481+
/*opt=*/0, /*fake=*/0, /*isExternalType=*/1, /*isCovered=*/0>
44764482
];
44774483
let Documentation = [HLSLSV_ShaderTypeAttrDocs];
44784484
let AdditionalMembers =
@@ -4487,30 +4493,31 @@ def HLSLResource : InheritableAttr {
44874493
let Spellings = [];
44884494
let Subjects = SubjectList<[Struct]>;
44894495
let LangOpts = [HLSL];
4490-
let Args = [EnumArgument<"ResourceClass", "llvm::hlsl::ResourceClass",
4491-
/*is_string=*/0,
4492-
["SRV", "UAV", "CBuffer", "Sampler"],
4493-
["SRV", "UAV", "CBuffer", "Sampler"],
4494-
/*opt=*/0, /*fake=*/0, /*isExternalType=*/1>,
4495-
EnumArgument<"ResourceKind", "llvm::hlsl::ResourceKind",
4496-
/*is_string=*/0,
4497-
["Texture1D", "Texture2D", "Texture2DMS",
4498-
"Texture3D", "TextureCube", "Texture1DArray",
4499-
"Texture2DArray", "Texture2DMSArray",
4500-
"TextureCubeArray", "TypedBuffer", "RawBuffer",
4501-
"StructuredBuffer", "CBuffer", "Sampler",
4502-
"TBuffer", "RTAccelerationStructure",
4503-
"FeedbackTexture2D", "FeedbackTexture2DArray"],
4504-
["Texture1D", "Texture2D", "Texture2DMS",
4505-
"Texture3D", "TextureCube", "Texture1DArray",
4506-
"Texture2DArray", "Texture2DMSArray",
4507-
"TextureCubeArray", "TypedBuffer", "RawBuffer",
4508-
"StructuredBuffer", "CBuffer", "Sampler",
4509-
"TBuffer", "RTAccelerationStructure",
4510-
"FeedbackTexture2D", "FeedbackTexture2DArray"],
4511-
/*opt=*/0, /*fake=*/0, /*isExternalType=*/1>,
4512-
DefaultBoolArgument<"isROV", /*default=*/0>
4513-
];
4496+
let Args = [
4497+
EnumArgument<"ResourceClass", "llvm::hlsl::ResourceClass",
4498+
/*is_string=*/0, ["SRV", "UAV", "CBuffer", "Sampler"],
4499+
["SRV", "UAV", "CBuffer", "Sampler"],
4500+
/*opt=*/0, /*fake=*/0, /*isExternalType=*/1>,
4501+
EnumArgument<
4502+
"ResourceKind", "llvm::hlsl::ResourceKind",
4503+
/*is_string=*/0,
4504+
[
4505+
"Texture1D", "Texture2D", "Texture2DMS", "Texture3D", "TextureCube",
4506+
"Texture1DArray", "Texture2DArray", "Texture2DMSArray",
4507+
"TextureCubeArray", "TypedBuffer", "RawBuffer", "StructuredBuffer",
4508+
"CBuffer", "Sampler", "TBuffer", "RTAccelerationStructure",
4509+
"FeedbackTexture2D", "FeedbackTexture2DArray"
4510+
],
4511+
[
4512+
"Texture1D", "Texture2D", "Texture2DMS", "Texture3D", "TextureCube",
4513+
"Texture1DArray", "Texture2DArray", "Texture2DMSArray",
4514+
"TextureCubeArray", "TypedBuffer", "RawBuffer", "StructuredBuffer",
4515+
"CBuffer", "Sampler", "TBuffer", "RTAccelerationStructure",
4516+
"FeedbackTexture2D", "FeedbackTexture2DArray"
4517+
],
4518+
/*opt=*/0, /*fake=*/0, /*isExternalType=*/1, /*isCovered=*/0>,
4519+
DefaultBoolArgument<"isROV", /*default=*/0>
4520+
];
45144521
let Documentation = [InternalOnly];
45154522
}
45164523

clang/utils/TableGen/ClangAttrEmitter.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -903,13 +903,15 @@ namespace {
903903
StringRef shortType;
904904
std::vector<StringRef> values, enums, uniques;
905905
bool isExternal;
906+
bool isCovered;
906907

907908
public:
908909
EnumArgument(const Record &Arg, StringRef Attr)
909910
: Argument(Arg, Attr), values(Arg.getValueAsListOfStrings("Values")),
910911
enums(Arg.getValueAsListOfStrings("Enums")),
911912
uniques(uniqueEnumsInOrder(enums)),
912-
isExternal(Arg.getValueAsBit("IsExternalType")) {
913+
isExternal(Arg.getValueAsBit("IsExternalType")),
914+
isCovered(Arg.getValueAsBit("IsCovered")) {
913915
StringRef Type = Arg.getValueAsString("Type");
914916
shortType = isExternal ? Type.rsplit("::").second : Type;
915917
// If shortType didn't contain :: at all rsplit will give us an empty
@@ -993,7 +995,7 @@ namespace {
993995
OS << " OS << \" " << I << "\";\n";
994996
OS << " break;\n";
995997
}
996-
if (isExternal) {
998+
if (!isCovered) {
997999
OS << " default:\n";
9981000
OS << " llvm_unreachable(\"Invalid attribute value\");\n";
9991001
}
@@ -1036,7 +1038,7 @@ namespace {
10361038
OS << " case " << fullType << "::" << enums[I] << ": return \""
10371039
<< values[I] << "\";\n";
10381040
}
1039-
if (isExternal) {
1041+
if (!isCovered) {
10401042
OS << " default: llvm_unreachable(\"Invalid attribute value\");\n";
10411043
}
10421044
OS << " }\n"
@@ -1050,6 +1052,7 @@ namespace {
10501052
StringRef shortType;
10511053
std::vector<StringRef> values, enums, uniques;
10521054
bool isExternal;
1055+
bool isCovered;
10531056

10541057
protected:
10551058
void writeValueImpl(raw_ostream &OS) const override {
@@ -1068,7 +1071,8 @@ namespace {
10681071
values(Arg.getValueAsListOfStrings("Values")),
10691072
enums(Arg.getValueAsListOfStrings("Enums")),
10701073
uniques(uniqueEnumsInOrder(enums)),
1071-
isExternal(Arg.getValueAsBit("IsExternalType")) {
1074+
isExternal(Arg.getValueAsBit("IsExternalType")),
1075+
isCovered(Arg.getValueAsBit("IsCovered")) {
10721076
StringRef Type = Arg.getValueAsString("Type");
10731077
shortType = isExternal ? Type.rsplit("::").second : Type;
10741078
// If shortType didn't contain :: at all rsplit will give us an empty
@@ -1111,6 +1115,10 @@ namespace {
11111115
OS << " OS << \" " << UI << "\";\n";
11121116
OS << " break;\n";
11131117
}
1118+
if (!isCovered) {
1119+
OS << " default:\n";
1120+
OS << " llvm_unreachable(\"Invalid attribute value\");\n";
1121+
}
11141122
OS << " }\n";
11151123
OS << " }\n";
11161124
}
@@ -1168,6 +1176,9 @@ namespace {
11681176
OS << " case " << fullType << "::" << enums[I] << ": return \""
11691177
<< values[I] << "\";\n";
11701178
}
1179+
if (!isCovered) {
1180+
OS << " default: llvm_unreachable(\"Invalid attribute value\");\n";
1181+
}
11711182
OS << " }\n"
11721183
<< " llvm_unreachable(\"No enumerator with that value\");\n"
11731184
<< "}\n";

llvm/include/llvm/Frontend/HLSL/HLSLResource.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,8 @@ class MDNode;
2121

2222
namespace hlsl {
2323

24-
enum class ResourceClass : uint8_t {
25-
SRV = 0,
26-
UAV,
27-
CBuffer,
28-
Sampler,
29-
Invalid,
30-
NumClasses = Invalid,
31-
};
32-
3324
// For now we use DXIL ABI enum values directly. This may change in the future.
25+
using dxil::ResourceClass;
3426
using dxil::ElementType;
3527
using dxil::ResourceKind;
3628

llvm/include/llvm/Support/DXILABI.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ enum class ParameterKind : uint8_t {
3939
DXILHandle,
4040
};
4141

42+
enum class ResourceClass : uint8_t {
43+
SRV = 0,
44+
UAV,
45+
CBuffer,
46+
Sampler,
47+
};
48+
4249
/// The kind of resource for an SRV or UAV resource. Sometimes referred to as
4350
/// "Shape" in the DXIL docs.
4451
enum class ResourceKind : uint32_t {

0 commit comments

Comments
 (0)