[llvm] Set shader analysis flags EnableRawAndStructuredBuffers for RawBuffer and StructuredBuffer resource types (PR #114449)

Cooper Partin via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 6 13:47:07 PST 2024


https://github.com/coopp updated https://github.com/llvm/llvm-project/pull/114449

>From cf6b6d87493da685455d41c6deac069ed2a663c2 Mon Sep 17 00:00:00 2001
From: Cooper Partin <coopp at ntdev.microsoft.com>
Date: Fri, 25 Oct 2024 13:01:52 -0700
Subject: [PATCH 01/10] Add simple shader flags tests for structured and raw
 buffer

---
 .../DirectX/ShaderFlags/raw_buffers.ll        | 23 ++++++++++++++++++
 .../DirectX/ShaderFlags/structured_buffers.ll | 24 +++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll

diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll
new file mode 100644
index 00000000000000..5486c3d8b6ce6c
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll
@@ -0,0 +1,23 @@
+; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
+; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC
+
+target triple = "dxil-pc-shadermodel6.7-library"
+
+; CHECK: ; Shader Flags Value: 0x00020000
+; CHECK: ; Note: shader requires additional functionality:
+; CHECK-NEXT: ;       Raw and Structured buffers
+; CHECK-NEXT: ; Note: extra DXIL module flags:
+; CHECK-NEXT: {{^;$}}
+
+%"class.hlsl::RWBuffer" = type { target("dx.TypedBuffer", <4 x float>, 1, 0, 0), <4 x float> }
+
+attributes #0 = { noinline nounwind memory(none) }
+
+; DXC: - Name:            SFI0
+; DXC-NEXT:     Size:            8
+; DXC-NEXT:     Flags:
+; DXC-NEXT:       Doubles: false
+; DXC-NEXT:       ComputeShadersPlusRawAndStructuredBuffers:         true
+; DXC-NOT:   {{[A-Za-z]+: +true}}
+; DXC:       NextUnusedBit:   false
+; DXC: ...
\ No newline at end of file
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll
new file mode 100644
index 00000000000000..7a52c2a0d7e7dd
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll
@@ -0,0 +1,24 @@
+; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
+; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC
+
+target triple = "dxil-pc-shadermodel6.7-library"
+
+; CHECK: ; Shader Flags Value: 0x00020000
+; CHECK: ; Note: shader requires additional functionality:
+; CHECK-NEXT: ;       Raw and Structured buffers
+; CHECK-NEXT: ; Note: extra DXIL module flags:
+; CHECK-NEXT: {{^;$}}
+
+%"class.hlsl::StructuredBuffer" = type { target("dx.RawBuffer", { <4 x float> }, 0, 0), %struct.MyStruct }
+%struct.MyStruct = type { <4 x float> }
+
+attributes #0 = { noinline nounwind memory(none) }
+
+; DXC: - Name:            SFI0
+; DXC-NEXT:     Size:            8
+; DXC-NEXT:     Flags:
+; DXC-NEXT:       Doubles: false
+; DXC-NEXT:       ComputeShadersPlusRawAndStructuredBuffers:         true
+; DXC-NOT:   {{[A-Za-z]+: +true}}
+; DXC:       NextUnusedBit:   false
+; DXC: ...
\ No newline at end of file

>From 28580c216cedd25ae9ad9eace1caa0c76de92daa Mon Sep 17 00:00:00 2001
From: Cooper Partin <coopp at ntdev.microsoft.com>
Date: Tue, 29 Oct 2024 14:56:33 -0700
Subject: [PATCH 02/10] Fix raw_buffers test

---
 .../DirectX/ShaderFlags/raw_buffers.ll        | 20 ++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll
index 5486c3d8b6ce6c..3ef2bad406465d 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll
@@ -3,16 +3,21 @@
 
 target triple = "dxil-pc-shadermodel6.7-library"
 
+ at G = external constant <4 x float>, align 4
+
+define void @test_typedbuffer() {
+
+; RWBuffer<int> Buf : register(u7, space2)
+  %uav0 = call target("dx.TypedBuffer", i32, 1, 0, 1)
+      @llvm.dx.handle.fromBinding.tdx.TypedBuffer_i32_1_0t(
+          i32 2, i32 7, i32 1, i32 0, i1 false)
+
 ; CHECK: ; Shader Flags Value: 0x00020000
 ; CHECK: ; Note: shader requires additional functionality:
 ; CHECK-NEXT: ;       Raw and Structured buffers
 ; CHECK-NEXT: ; Note: extra DXIL module flags:
 ; CHECK-NEXT: {{^;$}}
 
-%"class.hlsl::RWBuffer" = type { target("dx.TypedBuffer", <4 x float>, 1, 0, 0), <4 x float> }
-
-attributes #0 = { noinline nounwind memory(none) }
-
 ; DXC: - Name:            SFI0
 ; DXC-NEXT:     Size:            8
 ; DXC-NEXT:     Flags:
@@ -20,4 +25,9 @@ attributes #0 = { noinline nounwind memory(none) }
 ; DXC-NEXT:       ComputeShadersPlusRawAndStructuredBuffers:         true
 ; DXC-NOT:   {{[A-Za-z]+: +true}}
 ; DXC:       NextUnusedBit:   false
-; DXC: ...
\ No newline at end of file
+; DXC: ...
+
+  ret void
+}
+
+attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }

>From cecb423d7aa66929651a38a9f3bef00be77e2e04 Mon Sep 17 00:00:00 2001
From: Cooper Partin <coopp at ntdev.microsoft.com>
Date: Tue, 29 Oct 2024 15:00:21 -0700
Subject: [PATCH 03/10] cleanup tests

---
 .../DirectX/ShaderFlags/raw_buffers.ll        |  4 ++--
 .../DirectX/ShaderFlags/structured_buffers.ll | 22 ++++++++++++++-----
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll
index 3ef2bad406465d..c84137cde9bc31 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll
@@ -5,9 +5,9 @@ target triple = "dxil-pc-shadermodel6.7-library"
 
 @G = external constant <4 x float>, align 4
 
-define void @test_typedbuffer() {
+define void @test_bufferflags() {
 
-; RWBuffer<int> Buf : register(u7, space2)
+  ; RWBuffer<int> Buf : register(u7, space2)
   %uav0 = call target("dx.TypedBuffer", i32, 1, 0, 1)
       @llvm.dx.handle.fromBinding.tdx.TypedBuffer_i32_1_0t(
           i32 2, i32 7, i32 1, i32 0, i1 false)
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll
index 7a52c2a0d7e7dd..ba546e08a89cef 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll
@@ -3,17 +3,22 @@
 
 target triple = "dxil-pc-shadermodel6.7-library"
 
+ at G = external constant <4 x float>, align 4
+
+define void @test_bufferflags() {
+
+  ; struct S { float4 a; uint4 b; };
+  ; StructuredBuffer<S> Buf : register(t2, space4)
+  %struct0 = call target("dx.RawBuffer", {<4 x float>, <4 x i32>}, 0, 0)
+      @llvm.dx.handle.fromBinding.tdx.RawBuffer_sl_v4f32v4i32s_0_0t(
+          i32 4, i32 2, i32 1, i32 10, i1 true)
+
 ; CHECK: ; Shader Flags Value: 0x00020000
 ; CHECK: ; Note: shader requires additional functionality:
 ; CHECK-NEXT: ;       Raw and Structured buffers
 ; CHECK-NEXT: ; Note: extra DXIL module flags:
 ; CHECK-NEXT: {{^;$}}
 
-%"class.hlsl::StructuredBuffer" = type { target("dx.RawBuffer", { <4 x float> }, 0, 0), %struct.MyStruct }
-%struct.MyStruct = type { <4 x float> }
-
-attributes #0 = { noinline nounwind memory(none) }
-
 ; DXC: - Name:            SFI0
 ; DXC-NEXT:     Size:            8
 ; DXC-NEXT:     Flags:
@@ -21,4 +26,9 @@ attributes #0 = { noinline nounwind memory(none) }
 ; DXC-NEXT:       ComputeShadersPlusRawAndStructuredBuffers:         true
 ; DXC-NOT:   {{[A-Za-z]+: +true}}
 ; DXC:       NextUnusedBit:   false
-; DXC: ...
\ No newline at end of file
+; DXC: ...
+
+  ret void
+}
+
+attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }

>From 98e896994fa2a9f903d6fc095efc2290183ae60e Mon Sep 17 00:00:00 2001
From: Cooper Partin <coopp at ntdev.microsoft.com>
Date: Wed, 30 Oct 2024 13:44:12 -0700
Subject: [PATCH 04/10] Update tests with new IR

---
 .../test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll | 13 ++-----------
 .../DirectX/ShaderFlags/structured_buffers.ll       | 13 ++-----------
 2 files changed, 4 insertions(+), 22 deletions(-)

diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll
index c84137cde9bc31..95a5ce4cf0ada4 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll
@@ -1,5 +1,4 @@
 ; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
-; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC
 
 target triple = "dxil-pc-shadermodel6.7-library"
 
@@ -12,21 +11,13 @@ define void @test_bufferflags() {
       @llvm.dx.handle.fromBinding.tdx.TypedBuffer_i32_1_0t(
           i32 2, i32 7, i32 1, i32 0, i1 false)
 
-; CHECK: ; Shader Flags Value: 0x00020000
+; CHECK: ; Shader Flags Value: 0x00020010
 ; CHECK: ; Note: shader requires additional functionality:
 ; CHECK-NEXT: ;       Raw and Structured buffers
 ; CHECK-NEXT: ; Note: extra DXIL module flags:
+; CHECK-NEXT: ; D3D11_SB_GLOBAL_FLAG_ENABLE_RAW_AND_STRUCTURED_BUFFERS
 ; CHECK-NEXT: {{^;$}}
 
-; DXC: - Name:            SFI0
-; DXC-NEXT:     Size:            8
-; DXC-NEXT:     Flags:
-; DXC-NEXT:       Doubles: false
-; DXC-NEXT:       ComputeShadersPlusRawAndStructuredBuffers:         true
-; DXC-NOT:   {{[A-Za-z]+: +true}}
-; DXC:       NextUnusedBit:   false
-; DXC: ...
-
   ret void
 }
 
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll
index ba546e08a89cef..d9a04369e8fea0 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll
@@ -1,5 +1,4 @@
 ; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
-; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC
 
 target triple = "dxil-pc-shadermodel6.7-library"
 
@@ -13,21 +12,13 @@ define void @test_bufferflags() {
       @llvm.dx.handle.fromBinding.tdx.RawBuffer_sl_v4f32v4i32s_0_0t(
           i32 4, i32 2, i32 1, i32 10, i1 true)
 
-; CHECK: ; Shader Flags Value: 0x00020000
+; CHECK: ; Shader Flags Value: 0x00020010
 ; CHECK: ; Note: shader requires additional functionality:
 ; CHECK-NEXT: ;       Raw and Structured buffers
 ; CHECK-NEXT: ; Note: extra DXIL module flags:
+; CHECK-NEXT: ; D3D11_SB_GLOBAL_FLAG_ENABLE_RAW_AND_STRUCTURED_BUFFERS
 ; CHECK-NEXT: {{^;$}}
 
-; DXC: - Name:            SFI0
-; DXC-NEXT:     Size:            8
-; DXC-NEXT:     Flags:
-; DXC-NEXT:       Doubles: false
-; DXC-NEXT:       ComputeShadersPlusRawAndStructuredBuffers:         true
-; DXC-NOT:   {{[A-Za-z]+: +true}}
-; DXC:       NextUnusedBit:   false
-; DXC: ...
-
   ret void
 }
 

>From 607fdc136f6b35c13a55aa161bfa4ec8fc9902b7 Mon Sep 17 00:00:00 2001
From: Cooper Partin <coopp at ntdev.microsoft.com>
Date: Wed, 30 Oct 2024 13:44:59 -0700
Subject: [PATCH 05/10] rename test

---
 .../CodeGen/DirectX/ShaderFlags/{raw_buffers.ll => buffers.ll}    | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename llvm/test/CodeGen/DirectX/ShaderFlags/{raw_buffers.ll => buffers.ll} (100%)

diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/buffers.ll
similarity index 100%
rename from llvm/test/CodeGen/DirectX/ShaderFlags/raw_buffers.ll
rename to llvm/test/CodeGen/DirectX/ShaderFlags/buffers.ll

>From a3c0af06d71068565738b64ec09799e088fd64e2 Mon Sep 17 00:00:00 2001
From: Cooper Partin <coopp at ntdev.microsoft.com>
Date: Wed, 30 Oct 2024 14:26:32 -0700
Subject: [PATCH 06/10] Work in progress (contains checks for shader profile
 type, shader model version etc.)

---
 llvm/lib/Target/DirectX/DXILShaderFlags.cpp | 41 ++++++++++++++++++++-
 llvm/lib/Target/DirectX/DXILShaderFlags.h   |  5 ++-
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
index 9fa137b4c025e1..ad6dd3cb0e1d99 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
@@ -13,8 +13,11 @@
 
 #include "DXILShaderFlags.h"
 #include "DirectX.h"
+#include "llvm/Analysis/DXILMetadataAnalysis.h"
+#include "llvm/Analysis/DXILResource.h"
 #include "llvm/IR/Instruction.h"
 #include "llvm/IR/Module.h"
+#include "llvm/Support/DXILABI.h"
 #include "llvm/Support/FormatVariadic.h"
 
 using namespace llvm;
@@ -36,8 +39,42 @@ static void updateFlags(ComputedShaderFlags &Flags, const Instruction &I) {
   }
 }
 
-ComputedShaderFlags ComputedShaderFlags::computeFlags(Module &M) {
+static void updateResourceFlags(ComputedShaderFlags &Flags, Module &M,
+                                ModuleAnalysisManager &AM) {
+  const DXILResourceMap &DRM = AM.getResult<DXILResourceAnalysis>(M);
+  if (DRM.empty())
+    return;
+
+  const dxil::ModuleMetadataInfo &MMDI = AM.getResult<DXILMetadataAnalysis>(M);
+  VersionTuple SM = MMDI.ShaderModelVersion;
+  Triple::EnvironmentType SP = MMDI.ShaderProfile;
+
+  // StructuredBuffer
+  // for (const ResourceInfo &RI : DRM.srvs()) {
+  //  if (RI.getResourceKind() ==
+  //      ResourceKind::RawBuffer) {
+  //    Flags.EnableRawAndStructuredBuffers = true;
+  //    Flags.ComputeShadersPlusRawAndStructuredBuffers = (SM.getMajor() == 4);
+  //    break;
+  //  }
+  //}
+
+  // RWBuffer
+  for (const ResourceInfo &RI : DRM.uavs()) {
+    if (RI.getResourceKind() == ResourceKind::TypedBuffer) {
+      Flags.EnableRawAndStructuredBuffers = true;
+      Flags.ComputeShadersPlusRawAndStructuredBuffers =
+          (SP == Triple::EnvironmentType::Compute && SM.getMajor() == 4);
+      break;
+    }
+  }
+}
+
+ComputedShaderFlags
+ComputedShaderFlags::computeFlags(Module &M, ModuleAnalysisManager &AM) {
   ComputedShaderFlags Flags;
+  updateResourceFlags(Flags, M, AM);
+
   for (const auto &F : M)
     for (const auto &BB : F)
       for (const auto &I : BB)
@@ -67,7 +104,7 @@ AnalysisKey ShaderFlagsAnalysis::Key;
 
 ComputedShaderFlags ShaderFlagsAnalysis::run(Module &M,
                                              ModuleAnalysisManager &AM) {
-  return ComputedShaderFlags::computeFlags(M);
+  return ComputedShaderFlags::computeFlags(M, AM);
 }
 
 PreservedAnalyses ShaderFlagsAnalysisPrinter::run(Module &M,
diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.h b/llvm/lib/Target/DirectX/DXILShaderFlags.h
index 1df7d27de13d3c..f2ca0985d0508a 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.h
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.h
@@ -60,7 +60,7 @@ struct ComputedShaderFlags {
     return FeatureFlags;
   }
 
-  static ComputedShaderFlags computeFlags(Module &M);
+  static ComputedShaderFlags computeFlags(Module &M, ModuleAnalysisManager &AM);
   void print(raw_ostream &OS = dbgs()) const;
   LLVM_DUMP_METHOD void dump() const { print(); }
 };
@@ -102,7 +102,8 @@ class ShaderFlagsAnalysisWrapper : public ModulePass {
   const ComputedShaderFlags &getShaderFlags() { return Flags; }
 
   bool runOnModule(Module &M) override {
-    Flags = ComputedShaderFlags::computeFlags(M);
+    auto AM = ModuleAnalysisManager();
+    Flags = ComputedShaderFlags::computeFlags(M, AM);
     return false;
   }
 

>From 87a63a607cc93dac3dbd9fe277f7f562a2baf00a Mon Sep 17 00:00:00 2001
From: Cooper Partin <coopp at ntdev.microsoft.com>
Date: Wed, 30 Oct 2024 14:57:18 -0700
Subject: [PATCH 07/10] Add cs4 test and updated logic to handle legacy pass
 manager

---
 llvm/lib/Target/DirectX/DXILShaderFlags.cpp   | 13 ++++++----
 llvm/lib/Target/DirectX/DXILShaderFlags.h     |  5 ++--
 .../CodeGen/DirectX/ShaderFlags/buffers.ll    |  5 ++--
 .../DirectX/ShaderFlags/buffers_cs4.ll        | 24 +++++++++++++++++++
 4 files changed, 36 insertions(+), 11 deletions(-)
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/buffers_cs4.ll

diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
index ad6dd3cb0e1d99..9098c25728863c 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
@@ -40,12 +40,15 @@ static void updateFlags(ComputedShaderFlags &Flags, const Instruction &I) {
 }
 
 static void updateResourceFlags(ComputedShaderFlags &Flags, Module &M,
-                                ModuleAnalysisManager &AM) {
-  const DXILResourceMap &DRM = AM.getResult<DXILResourceAnalysis>(M);
+                                ModuleAnalysisManager *AM) {
+  if (!AM)
+    return;
+
+  const DXILResourceMap &DRM = AM->getResult<DXILResourceAnalysis>(M);
   if (DRM.empty())
     return;
 
-  const dxil::ModuleMetadataInfo &MMDI = AM.getResult<DXILMetadataAnalysis>(M);
+  const dxil::ModuleMetadataInfo &MMDI = AM->getResult<DXILMetadataAnalysis>(M);
   VersionTuple SM = MMDI.ShaderModelVersion;
   Triple::EnvironmentType SP = MMDI.ShaderProfile;
 
@@ -71,7 +74,7 @@ static void updateResourceFlags(ComputedShaderFlags &Flags, Module &M,
 }
 
 ComputedShaderFlags
-ComputedShaderFlags::computeFlags(Module &M, ModuleAnalysisManager &AM) {
+ComputedShaderFlags::computeFlags(Module &M, ModuleAnalysisManager *AM) {
   ComputedShaderFlags Flags;
   updateResourceFlags(Flags, M, AM);
 
@@ -104,7 +107,7 @@ AnalysisKey ShaderFlagsAnalysis::Key;
 
 ComputedShaderFlags ShaderFlagsAnalysis::run(Module &M,
                                              ModuleAnalysisManager &AM) {
-  return ComputedShaderFlags::computeFlags(M, AM);
+  return ComputedShaderFlags::computeFlags(M, &AM);
 }
 
 PreservedAnalyses ShaderFlagsAnalysisPrinter::run(Module &M,
diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.h b/llvm/lib/Target/DirectX/DXILShaderFlags.h
index f2ca0985d0508a..86d0a70dcbee10 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.h
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.h
@@ -60,7 +60,7 @@ struct ComputedShaderFlags {
     return FeatureFlags;
   }
 
-  static ComputedShaderFlags computeFlags(Module &M, ModuleAnalysisManager &AM);
+  static ComputedShaderFlags computeFlags(Module &M, ModuleAnalysisManager *AM);
   void print(raw_ostream &OS = dbgs()) const;
   LLVM_DUMP_METHOD void dump() const { print(); }
 };
@@ -102,8 +102,7 @@ class ShaderFlagsAnalysisWrapper : public ModulePass {
   const ComputedShaderFlags &getShaderFlags() { return Flags; }
 
   bool runOnModule(Module &M) override {
-    auto AM = ModuleAnalysisManager();
-    Flags = ComputedShaderFlags::computeFlags(M, AM);
+    Flags = ComputedShaderFlags::computeFlags(M, nullptr);
     return false;
   }
 
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/buffers.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/buffers.ll
index 95a5ce4cf0ada4..93346c2d5587db 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/buffers.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/buffers.ll
@@ -1,6 +1,6 @@
 ; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
 
-target triple = "dxil-pc-shadermodel6.7-library"
+target triple = "dxil-pc-shadermodel6.3-compute"
 
 @G = external constant <4 x float>, align 4
 
@@ -11,9 +11,8 @@ define void @test_bufferflags() {
       @llvm.dx.handle.fromBinding.tdx.TypedBuffer_i32_1_0t(
           i32 2, i32 7, i32 1, i32 0, i1 false)
 
-; CHECK: ; Shader Flags Value: 0x00020010
+; CHECK: ; Shader Flags Value: 0x00000010
 ; CHECK: ; Note: shader requires additional functionality:
-; CHECK-NEXT: ;       Raw and Structured buffers
 ; CHECK-NEXT: ; Note: extra DXIL module flags:
 ; CHECK-NEXT: ; D3D11_SB_GLOBAL_FLAG_ENABLE_RAW_AND_STRUCTURED_BUFFERS
 ; CHECK-NEXT: {{^;$}}
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/buffers_cs4.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/buffers_cs4.ll
new file mode 100644
index 00000000000000..21b05e4ef59a22
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/buffers_cs4.ll
@@ -0,0 +1,24 @@
+; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
+
+target triple = "dxil-pc-shadermodel4.0-compute"
+
+ at G = external constant <4 x float>, align 4
+
+define void @test_bufferflags() {
+
+  ; RWBuffer<int> Buf : register(u7, space2)
+  %uav0 = call target("dx.TypedBuffer", i32, 1, 0, 1)
+      @llvm.dx.handle.fromBinding.tdx.TypedBuffer_i32_1_0t(
+          i32 2, i32 7, i32 1, i32 0, i1 false)
+
+; CHECK: ; Shader Flags Value: 0x00020010
+; CHECK: ; Note: shader requires additional functionality:
+; CHECK-NEXT: ;       Raw and Structured buffers
+; CHECK-NEXT: ; Note: extra DXIL module flags:
+; CHECK-NEXT: ; D3D11_SB_GLOBAL_FLAG_ENABLE_RAW_AND_STRUCTURED_BUFFERS
+; CHECK-NEXT: {{^;$}}
+
+  ret void
+}
+
+attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }

>From 0d820cc53904ded55799ef0ffde4ef3dd0335c54 Mon Sep 17 00:00:00 2001
From: Cooper Partin <coopp at ntdev.microsoft.com>
Date: Thu, 31 Oct 2024 10:02:40 -0700
Subject: [PATCH 08/10] Remove structured buffer because it is not working for
 some reason

---
 llvm/lib/Target/DirectX/DXILShaderFlags.cpp   | 10 --------
 .../DirectX/ShaderFlags/structured_buffers.ll | 25 -------------------
 2 files changed, 35 deletions(-)
 delete mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll

diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
index 9098c25728863c..e24b8b5e8e594f 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
@@ -52,16 +52,6 @@ static void updateResourceFlags(ComputedShaderFlags &Flags, Module &M,
   VersionTuple SM = MMDI.ShaderModelVersion;
   Triple::EnvironmentType SP = MMDI.ShaderProfile;
 
-  // StructuredBuffer
-  // for (const ResourceInfo &RI : DRM.srvs()) {
-  //  if (RI.getResourceKind() ==
-  //      ResourceKind::RawBuffer) {
-  //    Flags.EnableRawAndStructuredBuffers = true;
-  //    Flags.ComputeShadersPlusRawAndStructuredBuffers = (SM.getMajor() == 4);
-  //    break;
-  //  }
-  //}
-
   // RWBuffer
   for (const ResourceInfo &RI : DRM.uavs()) {
     if (RI.getResourceKind() == ResourceKind::TypedBuffer) {
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll
deleted file mode 100644
index d9a04369e8fea0..00000000000000
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/structured_buffers.ll
+++ /dev/null
@@ -1,25 +0,0 @@
-; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
-
-target triple = "dxil-pc-shadermodel6.7-library"
-
- at G = external constant <4 x float>, align 4
-
-define void @test_bufferflags() {
-
-  ; struct S { float4 a; uint4 b; };
-  ; StructuredBuffer<S> Buf : register(t2, space4)
-  %struct0 = call target("dx.RawBuffer", {<4 x float>, <4 x i32>}, 0, 0)
-      @llvm.dx.handle.fromBinding.tdx.RawBuffer_sl_v4f32v4i32s_0_0t(
-          i32 4, i32 2, i32 1, i32 10, i1 true)
-
-; CHECK: ; Shader Flags Value: 0x00020010
-; CHECK: ; Note: shader requires additional functionality:
-; CHECK-NEXT: ;       Raw and Structured buffers
-; CHECK-NEXT: ; Note: extra DXIL module flags:
-; CHECK-NEXT: ; D3D11_SB_GLOBAL_FLAG_ENABLE_RAW_AND_STRUCTURED_BUFFERS
-; CHECK-NEXT: {{^;$}}
-
-  ret void
-}
-
-attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }

>From 931a6842cf3f8d8811d06f48f903bb81ae380140 Mon Sep 17 00:00:00 2001
From: Cooper Partin <coopp at ntdev.microsoft.com>
Date: Tue, 5 Nov 2024 15:33:38 -0800
Subject: [PATCH 09/10] Added more tests and updated logic

---
 llvm/lib/Target/DirectX/DXILShaderFlags.cpp   | 34 ++++++++++++++-----
 .../ShaderFlags/{buffers.ll => buffer.ll}     | 18 +++++-----
 .../{buffers_cs4.ll => buffer_cs4.ll}         | 18 +++++-----
 .../CodeGen/DirectX/ShaderFlags/rwbuffer.ll   | 23 +++++++++++++
 .../DirectX/ShaderFlags/rwstructuredbuffer.ll | 24 +++++++++++++
 .../DirectX/ShaderFlags/structuredbuffer.ll   | 24 +++++++++++++
 6 files changed, 115 insertions(+), 26 deletions(-)
 rename llvm/test/CodeGen/DirectX/ShaderFlags/{buffers.ll => buffer.ll} (69%)
 rename llvm/test/CodeGen/DirectX/ShaderFlags/{buffers_cs4.ll => buffer_cs4.ll} (71%)
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/rwbuffer.ll
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/rwstructuredbuffer.ll
 create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/structuredbuffer.ll

diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
index e24b8b5e8e594f..5df9ed518436f8 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
@@ -48,19 +48,37 @@ static void updateResourceFlags(ComputedShaderFlags &Flags, Module &M,
   if (DRM.empty())
     return;
 
-  const dxil::ModuleMetadataInfo &MMDI = AM->getResult<DXILMetadataAnalysis>(M);
-  VersionTuple SM = MMDI.ShaderModelVersion;
-  Triple::EnvironmentType SP = MMDI.ShaderProfile;
-
-  // RWBuffer
   for (const ResourceInfo &RI : DRM.uavs()) {
-    if (RI.getResourceKind() == ResourceKind::TypedBuffer) {
+    switch (RI.getResourceKind()) {
+    case ResourceKind::RawBuffer:
+    case ResourceKind::StructuredBuffer:
       Flags.EnableRawAndStructuredBuffers = true;
-      Flags.ComputeShadersPlusRawAndStructuredBuffers =
-          (SP == Triple::EnvironmentType::Compute && SM.getMajor() == 4);
+      break;
+    default:
       break;
     }
   }
+
+  for (const ResourceInfo &RI : DRM.srvs()) {
+    switch (RI.getResourceKind()) {
+    case ResourceKind::RawBuffer:
+    case ResourceKind::StructuredBuffer:
+      Flags.EnableRawAndStructuredBuffers = true;
+      break;
+    default:
+      break;
+    }
+  }
+
+  if (Flags.EnableRawAndStructuredBuffers) {
+    const dxil::ModuleMetadataInfo &MMDI =
+        AM->getResult<DXILMetadataAnalysis>(M);
+    VersionTuple SM = MMDI.ShaderModelVersion;
+    Triple::EnvironmentType SP = MMDI.ShaderProfile;
+
+    Flags.ComputeShadersPlusRawAndStructuredBuffers =
+        (SP == Triple::EnvironmentType::Compute && SM.getMajor() == 4);
+  }
 }
 
 ComputedShaderFlags
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/buffers.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/buffer.ll
similarity index 69%
rename from llvm/test/CodeGen/DirectX/ShaderFlags/buffers.ll
rename to llvm/test/CodeGen/DirectX/ShaderFlags/buffer.ll
index 93346c2d5587db..c31ecbf4385d95 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/buffers.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/buffer.ll
@@ -6,18 +6,18 @@ target triple = "dxil-pc-shadermodel6.3-compute"
 
 define void @test_bufferflags() {
 
-  ; RWBuffer<int> Buf : register(u7, space2)
-  %uav0 = call target("dx.TypedBuffer", i32, 1, 0, 1)
-      @llvm.dx.handle.fromBinding.tdx.TypedBuffer_i32_1_0t(
-          i32 2, i32 7, i32 1, i32 0, i1 false)
+  ; ByteAddressBuffer Buf : register(t8, space1)
+  %srv0 = call target("dx.RawBuffer", i8, 0, 0)
+      @llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_0_0t(
+          i32 1, i32 8, i32 1, i32 0, i1 false)
+
+  ret void
+}
+
+attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
 
 ; CHECK: ; Shader Flags Value: 0x00000010
 ; CHECK: ; Note: shader requires additional functionality:
 ; CHECK-NEXT: ; Note: extra DXIL module flags:
 ; CHECK-NEXT: ; D3D11_SB_GLOBAL_FLAG_ENABLE_RAW_AND_STRUCTURED_BUFFERS
 ; CHECK-NEXT: {{^;$}}
-
-  ret void
-}
-
-attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/buffers_cs4.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/buffer_cs4.ll
similarity index 71%
rename from llvm/test/CodeGen/DirectX/ShaderFlags/buffers_cs4.ll
rename to llvm/test/CodeGen/DirectX/ShaderFlags/buffer_cs4.ll
index 21b05e4ef59a22..574dd652f4e890 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/buffers_cs4.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/buffer_cs4.ll
@@ -6,10 +6,15 @@ target triple = "dxil-pc-shadermodel4.0-compute"
 
 define void @test_bufferflags() {
 
-  ; RWBuffer<int> Buf : register(u7, space2)
-  %uav0 = call target("dx.TypedBuffer", i32, 1, 0, 1)
-      @llvm.dx.handle.fromBinding.tdx.TypedBuffer_i32_1_0t(
-          i32 2, i32 7, i32 1, i32 0, i1 false)
+  ; ByteAddressBuffer Buf : register(t8, space1)
+  %srv0 = call target("dx.RawBuffer", i8, 0, 0)
+      @llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_0_0t(
+          i32 1, i32 8, i32 1, i32 0, i1 false)
+
+  ret void
+}
+
+attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
 
 ; CHECK: ; Shader Flags Value: 0x00020010
 ; CHECK: ; Note: shader requires additional functionality:
@@ -17,8 +22,3 @@ define void @test_bufferflags() {
 ; CHECK-NEXT: ; Note: extra DXIL module flags:
 ; CHECK-NEXT: ; D3D11_SB_GLOBAL_FLAG_ENABLE_RAW_AND_STRUCTURED_BUFFERS
 ; CHECK-NEXT: {{^;$}}
-
-  ret void
-}
-
-attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/rwbuffer.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/rwbuffer.ll
new file mode 100644
index 00000000000000..5cbe79305329f0
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/rwbuffer.ll
@@ -0,0 +1,23 @@
+; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
+
+target triple = "dxil-pc-shadermodel6.3-compute"
+
+ at G = external constant <4 x float>, align 4
+
+define void @test_bufferflags() {
+
+  ; RWByteAddressBuffer Buf : register(u8, space1)
+  %uav0 = call target("dx.RawBuffer", i8, 1, 0)
+      @llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_0_0t(
+          i32 1, i32 8, i32 1, i32 0, i1 false)
+
+  ret void
+}
+
+attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
+
+; CHECK: ; Shader Flags Value: 0x00000010
+; CHECK: ; Note: shader requires additional functionality:
+; CHECK-NEXT: ; Note: extra DXIL module flags:
+; CHECK-NEXT: ; D3D11_SB_GLOBAL_FLAG_ENABLE_RAW_AND_STRUCTURED_BUFFERS
+; CHECK-NEXT: {{^;$}}
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/rwstructuredbuffer.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/rwstructuredbuffer.ll
new file mode 100644
index 00000000000000..e442c01c628860
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/rwstructuredbuffer.ll
@@ -0,0 +1,24 @@
+; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
+
+target triple = "dxil-pc-shadermodel6.7-library"
+
+ at G = external constant <4 x float>, align 4
+
+define void @test_bufferflags() {
+
+  ; struct S { float4 a; uint4 b; };
+  ; RWStructuredBuffer<S> Buf : register(u2, space4)
+  %struct0 = call target("dx.RawBuffer", {<4 x float>, <4 x i32>}, 1, 0)
+      @llvm.dx.handle.fromBinding.tdx.RawBuffer_sl_v4f32v4i32s_0_0t(
+          i32 4, i32 2, i32 1, i32 10, i1 true)
+
+  ret void
+}
+
+attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
+
+; CHECK: ; Shader Flags Value: 0x00000010
+; CHECK: ; Note: shader requires additional functionality:
+; CHECK-NEXT: ; Note: extra DXIL module flags:
+; CHECK-NEXT: ; D3D11_SB_GLOBAL_FLAG_ENABLE_RAW_AND_STRUCTURED_BUFFERS
+; CHECK-NEXT: {{^;$}}
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/structuredbuffer.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/structuredbuffer.ll
new file mode 100644
index 00000000000000..e45eaaec652a65
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/structuredbuffer.ll
@@ -0,0 +1,24 @@
+; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
+
+target triple = "dxil-pc-shadermodel6.7-library"
+
+ at G = external constant <4 x float>, align 4
+
+define void @test_bufferflags() {
+
+  ; struct S { float4 a; uint4 b; };
+  ; StructuredBuffer<S> Buf : register(t2, space4)
+  %struct0 = call target("dx.RawBuffer", {<4 x float>, <4 x i32>}, 0, 0)
+      @llvm.dx.handle.fromBinding.tdx.RawBuffer_sl_v4f32v4i32s_0_0t(
+          i32 4, i32 2, i32 1, i32 10, i1 true)
+
+  ret void
+}
+
+attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
+
+; CHECK: ; Shader Flags Value: 0x00000010
+; CHECK: ; Note: shader requires additional functionality:
+; CHECK-NEXT: ; Note: extra DXIL module flags:
+; CHECK-NEXT: ; D3D11_SB_GLOBAL_FLAG_ENABLE_RAW_AND_STRUCTURED_BUFFERS
+; CHECK-NEXT: {{^;$}}

>From adb6619cfaf07cc2a782666b1ced65633fee1137 Mon Sep 17 00:00:00 2001
From: Cooper Partin <coopp at ntdev.microsoft.com>
Date: Wed, 6 Nov 2024 13:44:30 -0800
Subject: [PATCH 10/10] Remove setting
 ComputeShadersPlusRawAndStructuredBuffers flag and test that went along with
 it

---
 llvm/lib/Target/DirectX/DXILShaderFlags.cpp   | 10 --------
 .../CodeGen/DirectX/ShaderFlags/buffer_cs4.ll | 24 -------------------
 2 files changed, 34 deletions(-)
 delete mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/buffer_cs4.ll

diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
index 5df9ed518436f8..3cea08b7ae18e5 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
@@ -69,16 +69,6 @@ static void updateResourceFlags(ComputedShaderFlags &Flags, Module &M,
       break;
     }
   }
-
-  if (Flags.EnableRawAndStructuredBuffers) {
-    const dxil::ModuleMetadataInfo &MMDI =
-        AM->getResult<DXILMetadataAnalysis>(M);
-    VersionTuple SM = MMDI.ShaderModelVersion;
-    Triple::EnvironmentType SP = MMDI.ShaderProfile;
-
-    Flags.ComputeShadersPlusRawAndStructuredBuffers =
-        (SP == Triple::EnvironmentType::Compute && SM.getMajor() == 4);
-  }
 }
 
 ComputedShaderFlags
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/buffer_cs4.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/buffer_cs4.ll
deleted file mode 100644
index 574dd652f4e890..00000000000000
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/buffer_cs4.ll
+++ /dev/null
@@ -1,24 +0,0 @@
-; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
-
-target triple = "dxil-pc-shadermodel4.0-compute"
-
- at G = external constant <4 x float>, align 4
-
-define void @test_bufferflags() {
-
-  ; ByteAddressBuffer Buf : register(t8, space1)
-  %srv0 = call target("dx.RawBuffer", i8, 0, 0)
-      @llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_0_0t(
-          i32 1, i32 8, i32 1, i32 0, i1 false)
-
-  ret void
-}
-
-attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
-
-; CHECK: ; Shader Flags Value: 0x00020010
-; CHECK: ; Note: shader requires additional functionality:
-; CHECK-NEXT: ;       Raw and Structured buffers
-; CHECK-NEXT: ; Note: extra DXIL module flags:
-; CHECK-NEXT: ; D3D11_SB_GLOBAL_FLAG_ENABLE_RAW_AND_STRUCTURED_BUFFERS
-; CHECK-NEXT: {{^;$}}



More information about the llvm-commits mailing list