Skip to content

[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

Merged

Conversation

joaosaffran
Copy link
Contributor

This patch refactors DXILABI to remove the dependency on scope printer.
Closes: #153827

@llvmbot
Copy link
Member

llvmbot commented Aug 15, 2025

@llvm/pr-subscribers-hlsl

@llvm/pr-subscribers-llvm-support

Author: None (joaosaffran)

Changes

This patch refactors DXILABI to remove the dependency on scope printer.
Closes: #153827


Full diff: https://github.com/llvm/llvm-project/pull/153840.diff

4 Files Affected:

  • (modified) llvm/include/llvm/Support/DXILABI.h (+1-3)
  • (modified) llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp (+2-3)
  • (modified) llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp (+2-6)
  • (modified) llvm/lib/Support/DXILABI.cpp (+5-1)
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());
+}

@llvmbot
Copy link
Member

llvmbot commented Aug 15, 2025

@llvm/pr-subscribers-backend-directx

Author: None (joaosaffran)

Changes

This patch refactors DXILABI to remove the dependency on scope printer.
Closes: #153827


Full diff: https://github.com/llvm/llvm-project/pull/153840.diff

4 Files Affected:

  • (modified) llvm/include/llvm/Support/DXILABI.h (+1-3)
  • (modified) llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp (+2-3)
  • (modified) llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp (+2-6)
  • (modified) llvm/lib/Support/DXILABI.cpp (+5-1)
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());
+}

Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Developer Policy and LLVM Discourse for more information.

Copy link

github-actions bot commented Aug 15, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

LLVM_ABI StringRef getResourceClassName(ResourceClass RC);
LLVM_ABI StringRef getResourceClassName(uint8_t RC);
Copy link
Contributor

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?

Comment on lines 32 to 34
StringRef dxil::getResourceClassName(dxil::ResourceClass RC) {
return enumToStringRef(RC, getResourceClasses());
}
Copy link
Contributor

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?

Copy link
Collaborator

@llvm-beanz llvm-beanz left a 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.

Copy link
Collaborator

@llvm-beanz llvm-beanz left a 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.

@damyanp
Copy link
Contributor

damyanp commented Aug 15, 2025

@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.

@damyanp
Copy link
Contributor

damyanp commented Aug 15, 2025

Another alternative would be to fix the (I think) 3 cases of misused to_underlying that are modified in this PR, and subsequently fix all the others.

@llvm-beanz
Copy link
Collaborator

Another alternative would be to fix the (I think) 3 cases of misused to_underlying that are modified in this PR, and subsequently fix all the others.

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 =
Copy link
Contributor Author

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

Copy link
Collaborator

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;

Joao Saffran added 2 commits August 15, 2025 15:12
@llvm-beanz
Copy link
Collaborator

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo. Please turn off Keep my email addresses private setting in your account. See LLVM Developer Policy and LLVM Discourse for more information.

@joaosaffran you should fix this

@joaosaffran joaosaffran merged commit 37729d8 into llvm:main Aug 16, 2025
10 checks passed
@@ -18,7 +18,6 @@
#define LLVM_SUPPORT_DXILABI_H

#include "llvm/ADT/StringRef.h"
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[HLSL] Refactor DXILABI.h to not depend on ScopedPrinter.h
8 participants