[llvm] [DirectX] Add DXIL_MODULE_FLAG for ShaderFlags. (PR #83217)

via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 27 18:38:57 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-directx

Author: Xiang Li (python3kgae)

<details>
<summary>Changes</summary>

Add DXILConstants.def which stores DXILModuleFlags.

Use DXILModuleFlags for ComputedShaderFlags instead of ShaderFeatureFlags.

ComputedShaderFlags::getFeatureInfo() was added to get FeatureInfoFlags.

Fixes #<!-- -->57925

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


13 Files Affected:

- (modified) llvm/include/llvm/BinaryFormat/DXContainerConstants.def (+4-1) 
- (added) llvm/include/llvm/Support/DXILConstants.def (+76) 
- (modified) llvm/lib/Target/DirectX/DXContainerGlobals.cpp (+11-8) 
- (modified) llvm/lib/Target/DirectX/DXILShaderFlags.cpp (+4-4) 
- (modified) llvm/lib/Target/DirectX/DXILShaderFlags.h (+19-9) 
- (modified) llvm/test/CodeGen/DirectX/ShaderFlags/double-extensions.ll (+1-1) 
- (modified) llvm/test/CodeGen/DirectX/ShaderFlags/doubles.ll (+2-2) 
- (modified) llvm/test/CodeGen/DirectX/lib_entry.ll (+1-1) 
- (modified) llvm/test/ObjectYAML/DXContainer/DomainMaskVectors.yaml (+3) 
- (modified) llvm/test/ObjectYAML/DXContainer/GeometryMaskVectors.yaml (+3) 
- (modified) llvm/test/ObjectYAML/DXContainer/HullMaskVectors.yaml (+3) 
- (modified) llvm/test/tools/obj2yaml/DXContainer/ShaderFlags.yaml (+6) 
- (modified) llvm/test/tools/obj2yaml/DXContainer/ShaderFlagsEmpty.yaml (+3) 


``````````diff
diff --git a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
index 80ed86bc3a499e..28bb8ab3f14c4a 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
+++ b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
@@ -44,8 +44,11 @@ SHADER_FEATURE_FLAG(27, RESERVED, "<RESERVED>")
 SHADER_FEATURE_FLAG(28, AtomicInt64OnHeapResource, "64-bit Atomics on Heap Resources")
 SHADER_FEATURE_FLAG(29, AdvancedTextureOps, "Advanced Texture Ops")
 SHADER_FEATURE_FLAG(30, WriteableMSAATextures, "Writeable MSAA Textures")
+SHADER_FEATURE_FLAG(31, WaveMMA, "Wave Matrix Multiply Accumulate")
+SHADER_FEATURE_FLAG(32, SampleCmpGradientOrBias, "SampleCmpGradient or SampleCmpLevelZeroBias")
+SHADER_FEATURE_FLAG(33, ExtendedCommandInfo, "StartVertexLocation and StartInstanceLocation")
 
-SHADER_FEATURE_FLAG(31, NextUnusedBit, "Next reserved shader flag bit (not a flag)")
+SHADER_FEATURE_FLAG(34, NextUnusedBit, "Next reserved shader flag bit (not a flag)")
 
 #undef SHADER_FEATURE_FLAG
 #endif
diff --git a/llvm/include/llvm/Support/DXILConstants.def b/llvm/include/llvm/Support/DXILConstants.def
new file mode 100644
index 00000000000000..e7e8fd7089505f
--- /dev/null
+++ b/llvm/include/llvm/Support/DXILConstants.def
@@ -0,0 +1,76 @@
+
+#ifdef DXIL_MODULE_FLAG
+
+// DXIL_MODULE_FLAG(bit offset for the flag, bit offset for feature flag (-1 for no feature flag), name, description.
+DXIL_MODULE_FLAG( 0, -1,  DisableOptimizations,   "D3D11_1_SB_GLOBAL_FLAG_SKIP_OPTIMIZATION")
+DXIL_MODULE_FLAG( 1, -1,  DisableMathRefactoring, "D3D10_SB_GLOBAL_FLAG_REFACTORING_ALLOWED")
+DXIL_MODULE_FLAG( 2,  0,  EnableDoublePrecision,  "Double-precision floating point")
+DXIL_MODULE_FLAG( 3, -1,  ForceEarlyDepthStencil, "D3D11_SB_GLOBAL_FLAG_FORCE_EARLY_DEPTH_STENCIL")
+
+DXIL_MODULE_FLAG( 4,  1,  EnableRawAndStructuredBuffers,   "Raw and Structured buffers")
+DXIL_MODULE_FLAG( 5,  4,  LowPrecisionPresent, "Minimum-precision data types")
+DXIL_MODULE_FLAG( 6,  5,  EnableDoubleExtensions,  "Double-precision extensions for 11.1")
+DXIL_MODULE_FLAG( 7,  6,  EnableMSAD, "Shader extensions for 11.1")
+
+DXIL_MODULE_FLAG( 8,  1,  AllResourcesBound,   "D3D12_SB_GLOBAL_FLAG_ALL_RESOURCES_BOUND")
+DXIL_MODULE_FLAG( 9, 13,  ViewportAndRTArrayIndex, "SV_RenderTargetArrayIndex or SV_ViewportArrayIndex from any shader feeding rasterizer")
+DXIL_MODULE_FLAG(10, 10,  InnerCoverage,  "PS Inner Coverage")
+DXIL_MODULE_FLAG(11,  9,  StencilRef, "PS Output Stencil Ref")
+
+DXIL_MODULE_FLAG(12,  8,  TiledResources,   "Tiled resources")
+DXIL_MODULE_FLAG(13, 11,  UAVLoadAdditionalFormats, "Typed UAV Load Additional Formats")
+DXIL_MODULE_FLAG(14,  7,  Level9ComparisonFiltering,  "Comparison filtering for feature level 9")
+DXIL_MODULE_FLAG(15,  3,  Max64UAVs, "64 UAV slots")
+
+DXIL_MODULE_FLAG(16,  2,  UAVsAtEveryStage,   "UAVs at every shader stage")
+DXIL_MODULE_FLAG(17, -1,  CSRawAndStructuredViaShader4X, "SHADER_FEATURE_COMPUTE_SHADERS_PLUS_RAW_AND_STRUCTURED_BUFFERS_VIA_SHADER_4_X")
+DXIL_MODULE_FLAG(18, 12,  ROVS,  "Raster Ordered UAVs")
+DXIL_MODULE_FLAG(19,  14,  WaveOps, "Wave level operations")
+
+DXIL_MODULE_FLAG(20, 15,  Int64Ops,   "64-Bit integer")
+
+// SM 6.1+
+DXIL_MODULE_FLAG(21, 16,  ViewID, "View Instancing")
+DXIL_MODULE_FLAG(22, 17,  Barycentrics,  "Barycentrics")
+
+// SM 6.2+
+DXIL_MODULE_FLAG(23, 18,  UseNativeLowPrecision, "Use native low precision")
+
+// SM 6.4+
+DXIL_MODULE_FLAG(24, 19,  ShadingRate,   "Shading Rate")
+
+// SM 6.5+
+DXIL_MODULE_FLAG(25, 20,  RaytracingTier1_1, "Raytracing tier 1.1 features")
+DXIL_MODULE_FLAG(26, 21,  SamplerFeedback,  "Sampler feedback")
+
+// SM 6.6+
+DXIL_MODULE_FLAG(27, 22,  AtomicInt64OnTypedResource, "64-bit Atomics on Typed Resources")
+
+DXIL_MODULE_FLAG(28, 23,  AtomicInt64OnGroupShared,   "64-bit Atomics on Group Shared")
+DXIL_MODULE_FLAG(29, 24,  DerivativesInMeshAndAmpShaders, "Derivatives in mesh and amplification shaders")
+DXIL_MODULE_FLAG(30, 25,  ResourceDescriptorHeapIndexing,  "Resource descriptor heap indexing")
+DXIL_MODULE_FLAG(31, 26,  SamplerDescriptorHeapIndexing, "Sampler descriptor heap indexing")
+
+DXIL_MODULE_FLAG(32, 28,  AtomicInt64OnHeapResource,   "64-bit Atomics on Heap Resources")
+
+// SM 6.7+
+// Global flag indicating that any UAV may not alias any other UAV.
+// Set if UAVs are used, unless -res-may-alias was specified.
+// For modules compiled against validator version < 1.7, this flag will be
+// cleared, and it must be assumed that UAV resources may alias.
+DXIL_MODULE_FLAG(33, -1,  ResMayNotAlias, "Any UAV may not alias any other UAV")
+
+DXIL_MODULE_FLAG(34, 29,  AdvancedTextureOps,  "Advanced Texture Ops")
+DXIL_MODULE_FLAG(35, 30,  WriteableMSAATextures, "Writeable MSAA Textures")
+
+// Experimental SM 6.9+ - Reserved, not yet supported.
+DXIL_MODULE_FLAG(36, 31,  WaveMMA,   "Wave Matrix Multiply Accumulate")
+
+// SM 6.8+
+DXIL_MODULE_FLAG(37, 32,  SampleCmpGradientOrBias, "SampleCmpGradient or SampleCmpLevelZeroBias")
+DXIL_MODULE_FLAG(38, 33,  ExtendedCommandInfo,  "StartVertexLocation and StartInstanceLocation")
+DXIL_MODULE_FLAG(39, -1,  UsesDerivatives, "SHADER_FEATURE_OPT_USES_DERIVATIVES")
+
+#undef DXIL_MODULE_FLAG
+#endif
+
diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index 56063b487f6847..b716d12b3076f1 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -28,7 +28,7 @@ using namespace llvm::dxil;
 namespace {
 class DXContainerGlobals : public llvm::ModulePass {
 
-  GlobalVariable *getShaderFlags(Module &M);
+  GlobalVariable *getShaderFeatureInfo(Module &M);
   GlobalVariable *computeShaderHash(Module &M);
 
 public:
@@ -53,21 +53,24 @@ class DXContainerGlobals : public llvm::ModulePass {
 
 bool DXContainerGlobals::runOnModule(Module &M) {
   llvm::SmallVector<GlobalValue *> Globals;
-  Globals.push_back(getShaderFlags(M));
+  Globals.push_back(getShaderFeatureInfo(M));
   Globals.push_back(computeShaderHash(M));
 
   appendToCompilerUsed(M, Globals);
   return true;
 }
 
-GlobalVariable *DXContainerGlobals::getShaderFlags(Module &M) {
-  const uint64_t Flags =
-      (uint64_t)(getAnalysis<ShaderFlagsAnalysisWrapper>().getShaderFlags());
+GlobalVariable *DXContainerGlobals::getShaderFeatureInfo(Module &M) {
+  const uint64_t FeatureInfo =
+      (uint64_t)(getAnalysis<ShaderFlagsAnalysisWrapper>()
+                     .getShaderFlags()
+                     .getFeatureInfo());
 
-  Constant *FlagsConstant = ConstantInt::get(M.getContext(), APInt(64, Flags));
-  auto *GV = new llvm::GlobalVariable(M, FlagsConstant->getType(), true,
+  Constant *FeatureInfoConstant =
+      ConstantInt::get(M.getContext(), APInt(64, FeatureInfo));
+  auto *GV = new llvm::GlobalVariable(M, FeatureInfoConstant->getType(), true,
                                       GlobalValue::PrivateLinkage,
-                                      FlagsConstant, "dx.sfi0");
+                                      FeatureInfoConstant, "dx.sfi0");
   GV->setSection("SFI0");
   GV->setAlignment(Align(4));
   return GV;
diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
index 66a9dc46bcbfbf..487d691d64a6ba 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
@@ -23,14 +23,14 @@ using namespace llvm::dxil;
 static void updateFlags(ComputedShaderFlags &Flags, const Instruction &I) {
   Type *Ty = I.getType();
   if (Ty->isDoubleTy()) {
-    Flags.Doubles = true;
+    Flags.EnableDoublePrecision = true;
     switch (I.getOpcode()) {
     case Instruction::FDiv:
     case Instruction::UIToFP:
     case Instruction::SIToFP:
     case Instruction::FPToUI:
     case Instruction::FPToSI:
-      Flags.DX11_1_DoubleExtensions = true;
+      Flags.EnableDoubleExtensions = true;
       break;
     }
   }
@@ -51,10 +51,10 @@ void ComputedShaderFlags::print(raw_ostream &OS) const {
   if (FlagVal == 0)
     return;
   OS << "; Note: shader requires additional functionality:\n";
-#define SHADER_FEATURE_FLAG(bit, FlagName, Str)                                \
+#define DXIL_MODULE_FLAG(bit, featureBit, FlagName, Str)                       \
   if (FlagName)                                                                \
     OS << ";       " Str "\n";
-#include "llvm/BinaryFormat/DXContainerConstants.def"
+#include "llvm/Support/DXILConstants.def"
   OS << ";\n";
 }
 
diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.h b/llvm/lib/Target/DirectX/DXILShaderFlags.h
index 574a7b090f5281..8b69ebce9743e1 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.h
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.h
@@ -14,7 +14,6 @@
 #ifndef LLVM_TARGET_DIRECTX_DXILSHADERFLAGS_H
 #define LLVM_TARGET_DIRECTX_DXILSHADERFLAGS_H
 
-#include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/Compiler.h"
@@ -29,20 +28,31 @@ class GlobalVariable;
 namespace dxil {
 
 struct ComputedShaderFlags {
-#define SHADER_FEATURE_FLAG(bit, FlagName, Str) bool FlagName : 1;
-#include "llvm/BinaryFormat/DXContainerConstants.def"
+#define DXIL_MODULE_FLAG(bit, featureBit, FlagName, Str) bool FlagName : 1;
+#include "llvm/Support/DXILConstants.def"
 
-#define SHADER_FEATURE_FLAG(bit, FlagName, Str) FlagName = false;
+#define DXIL_MODULE_FLAG(bit, featureBit, FlagName, Str) FlagName = false;
   ComputedShaderFlags() {
-#include "llvm/BinaryFormat/DXContainerConstants.def"
+#include "llvm/Support/DXILConstants.def"
+  }
+  static uint64_t getFeatureInfoMask(int featureBit) {
+    return featureBit != -1 ? (uint64_t)1 << featureBit : 0ull;
+  }
+  uint64_t getFeatureInfo() const {
+    uint64_t FeatureInfo = 0;
+#define DXIL_MODULE_FLAG(bit, featureBit, FlagName, Str)                       \
+  FeatureInfo |= FlagName ? getFeatureInfoMask(featureBit) : 0ull;
+
+#include "llvm/Support/DXILConstants.def"
+
+    return FeatureInfo;
   }
 
   operator uint64_t() const {
     uint64_t FlagValue = 0;
-#define SHADER_FEATURE_FLAG(bit, FlagName, Str)                                \
-  FlagValue |=                                                                 \
-      FlagName ? static_cast<uint64_t>(dxbc::FeatureFlags::FlagName) : 0ull;
-#include "llvm/BinaryFormat/DXContainerConstants.def"
+#define DXIL_MODULE_FLAG(bit, featureBit, FlagName, Str)                       \
+  FlagValue |= FlagName ? (uint64_t)1 << bit : 0ull;
+#include "llvm/Support/DXILConstants.def"
     return FlagValue;
   }
 
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/double-extensions.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/double-extensions.ll
index 865fefeac335dd..687e0036db78f2 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/double-extensions.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/double-extensions.ll
@@ -3,7 +3,7 @@
 
 target triple = "dxil-pc-shadermodel6.7-library"
 
-; CHECK: ; Shader Flags Value: 0x00000021
+; CHECK: ; Shader Flags Value: 0x00000044
 ; CHECK: ; Note: shader requires additional functionality:
 ; CHECK-NEXT: ;       Double-precision floating point
 ; CHECK-NEXT: ;       Double-precision extensions for 11.1
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/doubles.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/doubles.ll
index f90db61661f09f..70b1ac798b15e2 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/doubles.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/doubles.ll
@@ -3,10 +3,10 @@
 
 target triple = "dxil-pc-shadermodel6.7-library"
 
-; CHECK: ; Shader Flags Value: 0x00000001
+; CHECK: ; Shader Flags Value: 0x00000004
 ; CHECK: ; Note: shader requires additional functionality:
 ; CHECK-NEXT: ;       Double-precision floating point
-; CHECK-NEXT: {{^;$}}
+
 define double @add(double %a, double %b) {
   %sum = fadd double %a, %b
   ret double %sum
diff --git a/llvm/test/CodeGen/DirectX/lib_entry.ll b/llvm/test/CodeGen/DirectX/lib_entry.ll
index 9208d6d3f32246..5254a088055888 100644
--- a/llvm/test/CodeGen/DirectX/lib_entry.ll
+++ b/llvm/test/CodeGen/DirectX/lib_entry.ll
@@ -7,7 +7,7 @@ target triple = "dxil-unknown-shadermodel6.7-library"
 ; Make sure generate empty entry for lib profile.
 ;CHECK:![[empty_entry]] = !{null, !"", null, null, ![[shader_flags:[0-9]+]]}
 ; Make sure double is marked for shader flags.
-;CHECK:![[shader_flags]] = !{i32 0, i64 1}
+;CHECK:![[shader_flags]] = !{i32 0, i64 4}
 ;CHECK:![[entry]] = !{ptr @entry, !"entry", null, null, ![[extra:[0-9]+]]}
 ;CHECK:![[extra]] = !{i32 8, i32 5, i32 4, ![[numthreads:[0-9]+]]}
 ;CHECK:![[numthreads]] = !{i32 1, i32 2, i32 1}
diff --git a/llvm/test/ObjectYAML/DXContainer/DomainMaskVectors.yaml b/llvm/test/ObjectYAML/DXContainer/DomainMaskVectors.yaml
index 713fbc61e094b5..dbf93149c06606 100644
--- a/llvm/test/ObjectYAML/DXContainer/DomainMaskVectors.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/DomainMaskVectors.yaml
@@ -44,6 +44,9 @@ Parts:
       AtomicInt64OnHeapResource: false
       AdvancedTextureOps: false
       WriteableMSAATextures: false
+      WaveMMA: false
+      SampleCmpGradientOrBias: false
+      ExtendedCommandInfo: false
       NextUnusedBit:   false
   - Name:            ISG1
     Size:            52
diff --git a/llvm/test/ObjectYAML/DXContainer/GeometryMaskVectors.yaml b/llvm/test/ObjectYAML/DXContainer/GeometryMaskVectors.yaml
index bf29d0866b3628..b27b37eb86af2d 100644
--- a/llvm/test/ObjectYAML/DXContainer/GeometryMaskVectors.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/GeometryMaskVectors.yaml
@@ -44,6 +44,9 @@ Parts:
       AtomicInt64OnHeapResource: false
       AdvancedTextureOps: false
       WriteableMSAATextures: false
+      WaveMMA: false
+      SampleCmpGradientOrBias: false
+      ExtendedCommandInfo: false
       NextUnusedBit:   false
   - Name:            ISG1
     Size:            120
diff --git a/llvm/test/ObjectYAML/DXContainer/HullMaskVectors.yaml b/llvm/test/ObjectYAML/DXContainer/HullMaskVectors.yaml
index 0eaa2f8e97fb99..e997ca30350774 100644
--- a/llvm/test/ObjectYAML/DXContainer/HullMaskVectors.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/HullMaskVectors.yaml
@@ -44,6 +44,9 @@ Parts:
       AtomicInt64OnHeapResource: false
       AdvancedTextureOps: false
       WriteableMSAATextures: false
+      WaveMMA: false
+      SampleCmpGradientOrBias: false
+      ExtendedCommandInfo: false
       NextUnusedBit:   false
   - Name:            ISG1
     Size:            60
diff --git a/llvm/test/tools/obj2yaml/DXContainer/ShaderFlags.yaml b/llvm/test/tools/obj2yaml/DXContainer/ShaderFlags.yaml
index 165b397c145771..73707356608afb 100644
--- a/llvm/test/tools/obj2yaml/DXContainer/ShaderFlags.yaml
+++ b/llvm/test/tools/obj2yaml/DXContainer/ShaderFlags.yaml
@@ -44,6 +44,9 @@ Parts:
       AtomicInt64OnHeapResource: false
       AdvancedTextureOps: true
       WriteableMSAATextures: false
+      WaveMMA: false
+      SampleCmpGradientOrBias: false
+      ExtendedCommandInfo: false
       NextUnusedBit:   true
   - Name:            FKE0
     Size:            8
@@ -84,4 +87,7 @@ Parts:
 # CHECK-NEXT:       AtomicInt64OnHeapResource: false
 # CHECK-NEXT:       AdvancedTextureOps: true
 # CHECK-NEXT:       WriteableMSAATextures: false
+# CHECK-NEXT:       WaveMMA: false
+# CHECK-NEXT:       SampleCmpGradientOrBias: false
+# CHECK-NEXT:       ExtendedCommandInfo: false
 # CHECK-NEXT:       NextUnusedBit:   true
diff --git a/llvm/test/tools/obj2yaml/DXContainer/ShaderFlagsEmpty.yaml b/llvm/test/tools/obj2yaml/DXContainer/ShaderFlagsEmpty.yaml
index 086c7e90dcd5fe..4f8a080774c25c 100644
--- a/llvm/test/tools/obj2yaml/DXContainer/ShaderFlagsEmpty.yaml
+++ b/llvm/test/tools/obj2yaml/DXContainer/ShaderFlagsEmpty.yaml
@@ -43,6 +43,9 @@ Parts:
       AtomicInt64OnHeapResource: false
       AdvancedTextureOps: false
       WriteableMSAATextures: false
+      WaveMMA: false
+      SampleCmpGradientOrBias: false
+      ExtendedCommandInfo: false
       NextUnusedBit:   false
   - Name:            FKE0
     Size:            8

``````````

</details>


https://github.com/llvm/llvm-project/pull/83217


More information about the llvm-commits mailing list