-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[HLSL] Refactoring DXILABI.h to not depend on scope printer #153840
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
[HLSL] Refactoring DXILABI.h to not depend on scope printer #153840
Conversation
@llvm/pr-subscribers-hlsl @llvm/pr-subscribers-llvm-support Author: None (joaosaffran) ChangesThis patch refactors DXILABI to remove the dependency on scope printer. Full diff: https://github.com/llvm/llvm-project/pull/153840.diff 4 Files Affected:
diff --git a/llvm/include/llvm/Support/DXILABI.h b/llvm/include/llvm/Support/DXILABI.h
index 2dcdd73415be2..47b3a6619344f 100644
--- a/llvm/include/llvm/Support/DXILABI.h
+++ b/llvm/include/llvm/Support/DXILABI.h
@@ -18,7 +18,6 @@
#define LLVM_SUPPORT_DXILABI_H
#include "llvm/ADT/StringRef.h"
-#include "llvm/Support/ScopedPrinter.h"
#include <cstdint>
namespace llvm {
@@ -101,9 +100,8 @@ enum class SamplerFeedbackType : uint32_t {
const unsigned MinWaveSize = 4;
const unsigned MaxWaveSize = 128;
-LLVM_ABI ArrayRef<EnumEntry<ResourceClass>> getResourceClasses();
-
LLVM_ABI StringRef getResourceClassName(ResourceClass RC);
+LLVM_ABI StringRef getResourceClassName(uint8_t RC);
} // namespace dxil
} // namespace llvm
diff --git a/llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp b/llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp
index 050cc46e8c9b0..30b597dbad497 100644
--- a/llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp
+++ b/llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
+#include "llvm/Support/DXILABI.h"
#include "llvm/Support/ScopedPrinter.h"
namespace llvm {
@@ -93,9 +94,7 @@ static raw_ostream &operator<<(raw_ostream &OS,
}
static raw_ostream &operator<<(raw_ostream &OS, const ClauseType &Type) {
- OS << enumToStringRef(dxil::ResourceClass(llvm::to_underlying(Type)),
- dxil::getResourceClasses());
-
+ OS << dxil::getResourceClassName(llvm::to_underlying(Type));
return OS;
}
diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
index 157bfc665b207..c24475ed1bcf6 100644
--- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
+++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
@@ -119,9 +119,7 @@ MDNode *MetadataBuilder::BuildRootConstants(const RootConstants &Constants) {
MDNode *MetadataBuilder::BuildRootDescriptor(const RootDescriptor &Descriptor) {
IRBuilder<> Builder(Ctx);
- StringRef ResName =
- enumToStringRef(dxil::ResourceClass(to_underlying(Descriptor.Type)),
- dxil::getResourceClasses());
+ StringRef ResName = dxil::getResourceClassName(to_underlying(Descriptor.Type));
assert(!ResName.empty() && "Provided an invalid Resource Class");
SmallString<7> Name({"Root", ResName});
Metadata *Operands[] = {
@@ -161,9 +159,7 @@ MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) {
MDNode *MetadataBuilder::BuildDescriptorTableClause(
const DescriptorTableClause &Clause) {
IRBuilder<> Builder(Ctx);
- StringRef ResName =
- enumToStringRef(dxil::ResourceClass(to_underlying(Clause.Type)),
- dxil::getResourceClasses());
+ StringRef ResName = dxil::getResourceClassName(to_underlying(Clause.Type));
assert(!ResName.empty() && "Provided an invalid Resource Class");
Metadata *Operands[] = {
MDString::get(Ctx, ResName),
diff --git a/llvm/lib/Support/DXILABI.cpp b/llvm/lib/Support/DXILABI.cpp
index 261fe1ef98278..5b7c0669881ea 100644
--- a/llvm/lib/Support/DXILABI.cpp
+++ b/llvm/lib/Support/DXILABI.cpp
@@ -25,10 +25,14 @@ static const EnumEntry<dxil::ResourceClass> ResourceClassNames[] = {
{"Sampler", llvm::dxil::ResourceClass::Sampler},
};
-ArrayRef<EnumEntry<llvm::dxil::ResourceClass>> dxil::getResourceClasses() {
+ArrayRef<EnumEntry<llvm::dxil::ResourceClass>> getResourceClasses() {
return ArrayRef(ResourceClassNames);
}
StringRef dxil::getResourceClassName(dxil::ResourceClass RC) {
return enumToStringRef(RC, getResourceClasses());
}
+
+StringRef dxil::getResourceClassName(uint8_t RC) {
+ return enumToStringRef(ResourceClass(RC), getResourceClasses());
+}
|
@llvm/pr-subscribers-backend-directx Author: None (joaosaffran) ChangesThis patch refactors DXILABI to remove the dependency on scope printer. Full diff: https://github.com/llvm/llvm-project/pull/153840.diff 4 Files Affected:
diff --git a/llvm/include/llvm/Support/DXILABI.h b/llvm/include/llvm/Support/DXILABI.h
index 2dcdd73415be2..47b3a6619344f 100644
--- a/llvm/include/llvm/Support/DXILABI.h
+++ b/llvm/include/llvm/Support/DXILABI.h
@@ -18,7 +18,6 @@
#define LLVM_SUPPORT_DXILABI_H
#include "llvm/ADT/StringRef.h"
-#include "llvm/Support/ScopedPrinter.h"
#include <cstdint>
namespace llvm {
@@ -101,9 +100,8 @@ enum class SamplerFeedbackType : uint32_t {
const unsigned MinWaveSize = 4;
const unsigned MaxWaveSize = 128;
-LLVM_ABI ArrayRef<EnumEntry<ResourceClass>> getResourceClasses();
-
LLVM_ABI StringRef getResourceClassName(ResourceClass RC);
+LLVM_ABI StringRef getResourceClassName(uint8_t RC);
} // namespace dxil
} // namespace llvm
diff --git a/llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp b/llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp
index 050cc46e8c9b0..30b597dbad497 100644
--- a/llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp
+++ b/llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
+#include "llvm/Support/DXILABI.h"
#include "llvm/Support/ScopedPrinter.h"
namespace llvm {
@@ -93,9 +94,7 @@ static raw_ostream &operator<<(raw_ostream &OS,
}
static raw_ostream &operator<<(raw_ostream &OS, const ClauseType &Type) {
- OS << enumToStringRef(dxil::ResourceClass(llvm::to_underlying(Type)),
- dxil::getResourceClasses());
-
+ OS << dxil::getResourceClassName(llvm::to_underlying(Type));
return OS;
}
diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
index 157bfc665b207..c24475ed1bcf6 100644
--- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
+++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
@@ -119,9 +119,7 @@ MDNode *MetadataBuilder::BuildRootConstants(const RootConstants &Constants) {
MDNode *MetadataBuilder::BuildRootDescriptor(const RootDescriptor &Descriptor) {
IRBuilder<> Builder(Ctx);
- StringRef ResName =
- enumToStringRef(dxil::ResourceClass(to_underlying(Descriptor.Type)),
- dxil::getResourceClasses());
+ StringRef ResName = dxil::getResourceClassName(to_underlying(Descriptor.Type));
assert(!ResName.empty() && "Provided an invalid Resource Class");
SmallString<7> Name({"Root", ResName});
Metadata *Operands[] = {
@@ -161,9 +159,7 @@ MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) {
MDNode *MetadataBuilder::BuildDescriptorTableClause(
const DescriptorTableClause &Clause) {
IRBuilder<> Builder(Ctx);
- StringRef ResName =
- enumToStringRef(dxil::ResourceClass(to_underlying(Clause.Type)),
- dxil::getResourceClasses());
+ StringRef ResName = dxil::getResourceClassName(to_underlying(Clause.Type));
assert(!ResName.empty() && "Provided an invalid Resource Class");
Metadata *Operands[] = {
MDString::get(Ctx, ResName),
diff --git a/llvm/lib/Support/DXILABI.cpp b/llvm/lib/Support/DXILABI.cpp
index 261fe1ef98278..5b7c0669881ea 100644
--- a/llvm/lib/Support/DXILABI.cpp
+++ b/llvm/lib/Support/DXILABI.cpp
@@ -25,10 +25,14 @@ static const EnumEntry<dxil::ResourceClass> ResourceClassNames[] = {
{"Sampler", llvm::dxil::ResourceClass::Sampler},
};
-ArrayRef<EnumEntry<llvm::dxil::ResourceClass>> dxil::getResourceClasses() {
+ArrayRef<EnumEntry<llvm::dxil::ResourceClass>> getResourceClasses() {
return ArrayRef(ResourceClassNames);
}
StringRef dxil::getResourceClassName(dxil::ResourceClass RC) {
return enumToStringRef(RC, getResourceClasses());
}
+
+StringRef dxil::getResourceClassName(uint8_t RC) {
+ return enumToStringRef(ResourceClass(RC), getResourceClasses());
+}
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
llvm/include/llvm/Support/DXILABI.h
Outdated
LLVM_ABI StringRef getResourceClassName(ResourceClass RC); | ||
LLVM_ABI StringRef getResourceClassName(uint8_t RC); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a good API to add - why would we want anything other than the strongly typed version of this function?
llvm/lib/Support/DXILABI.cpp
Outdated
StringRef dxil::getResourceClassName(dxil::ResourceClass RC) { | ||
return enumToStringRef(RC, getResourceClasses()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using ScopedPrinter for this seems like overkill if we aren't using the EnumEntry
thing for anything else. Would it be better to just implement dxil::getResourceClassName
as a simple switch statement?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of to_underlying
in this PR (and other changes) is deeply wrong.
In this PR your to_underlying
calls are basically doing enum->int->enum conversions.
Please look critically at all the places you're using to_underlying
to ensure the conversions make sense and aren't just shedding type safety for no discernable benefit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not merge this without fixing the to_underlying
conversions.
@llvm-beanz - this is in response to #152229 (comment). Since this change is moving around existing to_underlying things rather than introducing new ones, woudl it be ok to get this in (thus fixing the 2% build time increase) and then prioritize landing #151032 (and making sure that the cases in this PR are addressed in that one as well)? @joaosaffran - it would be good if the PR description more directly explained the motivation for this change without having to read through issues and links to comments on PRs. |
Another alternative would be to fix the (I think) 3 cases of misused |
This is what I intended in my comment. We should not be changing lines of code from one wrong state to another wrong state. The use of to_underlying in the modified lines here is clearly wrong, so we should just fix it. |
@@ -161,9 +160,8 @@ MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) { | |||
MDNode *MetadataBuilder::BuildDescriptorTableClause( | |||
const DescriptorTableClause &Clause) { | |||
IRBuilder<> Builder(Ctx); | |||
StringRef ResName = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not the same enum @llvm-beanz
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using ClauseType = llvm::dxil::ResourceClass; |
@joaosaffran you should fix this |
@@ -18,7 +18,6 @@ | |||
#define LLVM_SUPPORT_DXILABI_H | |||
|
|||
#include "llvm/ADT/StringRef.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can also replace this StringRef.h include with a forward declaration.
This patch refactors DXILABI to remove the dependency on scope printer.
Closes: #153827