Skip to content

[HLSL][SPIRV] Implement the SPIR-V target type for cbuffers. #140061

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

s-perron
Copy link
Contributor

This change implement the type used to represent cbuffer for SPIR-V.

Fixes #138274.

@s-perron s-perron requested review from Keenuts and cassiebeckley May 15, 2025 14:06
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:codegen IR generation bugs: mangling, exceptions, etc. HLSL HLSL Language Support labels May 15, 2025
@llvmbot
Copy link
Member

llvmbot commented May 15, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: Steven Perron (s-perron)

Changes

This change implement the type used to represent cbuffer for SPIR-V.

Fixes #138274.


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

3 Files Affected:

  • (modified) clang/lib/Basic/Targets/SPIR.h (+1-1)
  • (modified) clang/lib/CodeGen/Targets/SPIR.cpp (+13-2)
  • (modified) clang/test/CodeGenHLSL/default_cbuffer.hlsl (+15-7)
diff --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h
index bf249e271a870..fcedce98c7067 100644
--- a/clang/lib/Basic/Targets/SPIR.h
+++ b/clang/lib/Basic/Targets/SPIR.h
@@ -47,7 +47,7 @@ static const unsigned SPIRDefIsPrivMap[] = {
     0,  // ptr32_uptr
     0,  // ptr64
     0,  // hlsl_groupshared
-    2,  // hlsl_constant
+    12, // hlsl_constant
     10, // hlsl_private
     11, // hlsl_device
     // Wasm address space values for this target are dummy values,
diff --git a/clang/lib/CodeGen/Targets/SPIR.cpp b/clang/lib/CodeGen/Targets/SPIR.cpp
index f35c124f50aa0..6163e3aa227d2 100644
--- a/clang/lib/CodeGen/Targets/SPIR.cpp
+++ b/clang/lib/CodeGen/Targets/SPIR.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "ABIInfoImpl.h"
+#include "HLSLBufferLayoutBuilder.h"
 #include "TargetInfo.h"
 
 using namespace clang;
@@ -410,9 +411,19 @@ llvm::Type *CommonSPIRTargetCodeGenInfo::getHLSLType(
                                     {RuntimeArrayType},
                                     {StorageClass, IsWritable});
   }
-  case llvm::dxil::ResourceClass::CBuffer:
-    llvm_unreachable("CBuffer handles are not implemented for SPIR-V yet");
+  case llvm::dxil::ResourceClass::CBuffer: {
+    QualType ContainedTy = ResType->getContainedType();
+    if (ContainedTy.isNull() || !ContainedTy->isStructureType())
+      return nullptr;
+
+    llvm::Type *BufferLayoutTy =
+        HLSLBufferLayoutBuilder(CGM, "spirv.Layout")
+            .createLayoutType(ContainedTy->getAsStructureType(), Packoffsets);
+    uint32_t StorageClass = /* Uniform storage class */ 2;
+    return llvm::TargetExtType::get(Ctx, "spirv.VulkanBuffer", {BufferLayoutTy},
+                                    {StorageClass, false});
     break;
+  }
   case llvm::dxil::ResourceClass::Sampler:
     return llvm::TargetExtType::get(Ctx, "spirv.Sampler");
   }
diff --git a/clang/test/CodeGenHLSL/default_cbuffer.hlsl b/clang/test/CodeGenHLSL/default_cbuffer.hlsl
index bec048a80d82b..fd0786a695ad7 100644
--- a/clang/test/CodeGenHLSL/default_cbuffer.hlsl
+++ b/clang/test/CodeGenHLSL/default_cbuffer.hlsl
@@ -1,12 +1,19 @@
-// RUN: %clang_cc1 -Wno-hlsl-implicit-binding -finclude-default-header -triple dxil-pc-shadermodel6.3-compute -fnative-half-type -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
+// RUN: %clang_cc1 -Wno-hlsl-implicit-binding -finclude-default-header -triple dxil-pc-shadermodel6.3-compute -fnative-half-type -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s --check-prefixes=CHECK,DXIL
+// RUN: %clang_cc1 -Wno-hlsl-implicit-binding -finclude-default-header -triple spirv-pc-vulkan1.3-compute -fnative-half-type -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s --check-prefixes=CHECK,SPIRV
 
-// CHECK: %"__cblayout_$Globals" = type <{ float, float, target("dx.Layout", %__cblayout_S, 4, 0) }>
+// DXIL: %"__cblayout_$Globals" = type <{ float, float, target("dx.Layout", %__cblayout_S, 4, 0) }>
+// SPIRV: %"__cblayout_$Globals" = type <{ float, float, target("spirv.Layout", %__cblayout_S, 4, 0) }>
 // CHECK: %__cblayout_S = type <{ float }>
 
-// CHECK-DAG: @"$Globals.cb" = global target("dx.CBuffer", target("dx.Layout", %"__cblayout_$Globals", 20, 0, 4, 16))
-// CHECK-DAG: @a = external addrspace(2) global float
-// CHECK-DAG: @g = external addrspace(2) global float
-// CHECK-DAG: @h = external addrspace(2) global target("dx.Layout", %__cblayout_S, 4, 0), align 4
+// DXIL-DAG: @"$Globals.cb" = global target("dx.CBuffer", target("dx.Layout", %"__cblayout_$Globals", 20, 0, 4, 16))
+// DXIL-DAG: @a = external addrspace(2) global float
+// DXIL-DAG: @g = external addrspace(2) global float
+// DXIL-DAG: @h = external addrspace(2) global target("dx.Layout", %__cblayout_S, 4, 0), align 4
+
+// SPIRV-DAG: @"$Globals.cb" = global target("spirv.VulkanBuffer", target("spirv.Layout", %"__cblayout_$Globals", 20, 0, 4, 16), 2, 0)
+// SPIRV-DAG: @a = external addrspace(12) global float
+// SPIRV-DAG: @g = external addrspace(12) global float
+// SPIRV-DAG: @h = external addrspace(12) global target("spirv.Layout", %__cblayout_S, 4, 0), align 4
 
 struct EmptyStruct {
 };
@@ -35,4 +42,5 @@ void main() {
 }
 
 // CHECK: !hlsl.cbs = !{![[CB:.*]]}
-// CHECK: ![[CB]] = !{ptr @"$Globals.cb", ptr addrspace(2) @a, ptr addrspace(2) @g, ptr addrspace(2) @h}
+// DXIL: ![[CB]] = !{ptr @"$Globals.cb", ptr addrspace(2) @a, ptr addrspace(2) @g, ptr addrspace(2) @h}
+// SPIRV: ![[CB]] = !{ptr @"$Globals.cb", ptr addrspace(12) @a, ptr addrspace(12) @g, ptr addrspace(12) @h}

@llvmbot
Copy link
Member

llvmbot commented May 15, 2025

@llvm/pr-subscribers-hlsl

Author: Steven Perron (s-perron)

Changes

This change implement the type used to represent cbuffer for SPIR-V.

Fixes #138274.


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

3 Files Affected:

  • (modified) clang/lib/Basic/Targets/SPIR.h (+1-1)
  • (modified) clang/lib/CodeGen/Targets/SPIR.cpp (+13-2)
  • (modified) clang/test/CodeGenHLSL/default_cbuffer.hlsl (+15-7)
diff --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h
index bf249e271a870..fcedce98c7067 100644
--- a/clang/lib/Basic/Targets/SPIR.h
+++ b/clang/lib/Basic/Targets/SPIR.h
@@ -47,7 +47,7 @@ static const unsigned SPIRDefIsPrivMap[] = {
     0,  // ptr32_uptr
     0,  // ptr64
     0,  // hlsl_groupshared
-    2,  // hlsl_constant
+    12, // hlsl_constant
     10, // hlsl_private
     11, // hlsl_device
     // Wasm address space values for this target are dummy values,
diff --git a/clang/lib/CodeGen/Targets/SPIR.cpp b/clang/lib/CodeGen/Targets/SPIR.cpp
index f35c124f50aa0..6163e3aa227d2 100644
--- a/clang/lib/CodeGen/Targets/SPIR.cpp
+++ b/clang/lib/CodeGen/Targets/SPIR.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "ABIInfoImpl.h"
+#include "HLSLBufferLayoutBuilder.h"
 #include "TargetInfo.h"
 
 using namespace clang;
@@ -410,9 +411,19 @@ llvm::Type *CommonSPIRTargetCodeGenInfo::getHLSLType(
                                     {RuntimeArrayType},
                                     {StorageClass, IsWritable});
   }
-  case llvm::dxil::ResourceClass::CBuffer:
-    llvm_unreachable("CBuffer handles are not implemented for SPIR-V yet");
+  case llvm::dxil::ResourceClass::CBuffer: {
+    QualType ContainedTy = ResType->getContainedType();
+    if (ContainedTy.isNull() || !ContainedTy->isStructureType())
+      return nullptr;
+
+    llvm::Type *BufferLayoutTy =
+        HLSLBufferLayoutBuilder(CGM, "spirv.Layout")
+            .createLayoutType(ContainedTy->getAsStructureType(), Packoffsets);
+    uint32_t StorageClass = /* Uniform storage class */ 2;
+    return llvm::TargetExtType::get(Ctx, "spirv.VulkanBuffer", {BufferLayoutTy},
+                                    {StorageClass, false});
     break;
+  }
   case llvm::dxil::ResourceClass::Sampler:
     return llvm::TargetExtType::get(Ctx, "spirv.Sampler");
   }
diff --git a/clang/test/CodeGenHLSL/default_cbuffer.hlsl b/clang/test/CodeGenHLSL/default_cbuffer.hlsl
index bec048a80d82b..fd0786a695ad7 100644
--- a/clang/test/CodeGenHLSL/default_cbuffer.hlsl
+++ b/clang/test/CodeGenHLSL/default_cbuffer.hlsl
@@ -1,12 +1,19 @@
-// RUN: %clang_cc1 -Wno-hlsl-implicit-binding -finclude-default-header -triple dxil-pc-shadermodel6.3-compute -fnative-half-type -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
+// RUN: %clang_cc1 -Wno-hlsl-implicit-binding -finclude-default-header -triple dxil-pc-shadermodel6.3-compute -fnative-half-type -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s --check-prefixes=CHECK,DXIL
+// RUN: %clang_cc1 -Wno-hlsl-implicit-binding -finclude-default-header -triple spirv-pc-vulkan1.3-compute -fnative-half-type -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s --check-prefixes=CHECK,SPIRV
 
-// CHECK: %"__cblayout_$Globals" = type <{ float, float, target("dx.Layout", %__cblayout_S, 4, 0) }>
+// DXIL: %"__cblayout_$Globals" = type <{ float, float, target("dx.Layout", %__cblayout_S, 4, 0) }>
+// SPIRV: %"__cblayout_$Globals" = type <{ float, float, target("spirv.Layout", %__cblayout_S, 4, 0) }>
 // CHECK: %__cblayout_S = type <{ float }>
 
-// CHECK-DAG: @"$Globals.cb" = global target("dx.CBuffer", target("dx.Layout", %"__cblayout_$Globals", 20, 0, 4, 16))
-// CHECK-DAG: @a = external addrspace(2) global float
-// CHECK-DAG: @g = external addrspace(2) global float
-// CHECK-DAG: @h = external addrspace(2) global target("dx.Layout", %__cblayout_S, 4, 0), align 4
+// DXIL-DAG: @"$Globals.cb" = global target("dx.CBuffer", target("dx.Layout", %"__cblayout_$Globals", 20, 0, 4, 16))
+// DXIL-DAG: @a = external addrspace(2) global float
+// DXIL-DAG: @g = external addrspace(2) global float
+// DXIL-DAG: @h = external addrspace(2) global target("dx.Layout", %__cblayout_S, 4, 0), align 4
+
+// SPIRV-DAG: @"$Globals.cb" = global target("spirv.VulkanBuffer", target("spirv.Layout", %"__cblayout_$Globals", 20, 0, 4, 16), 2, 0)
+// SPIRV-DAG: @a = external addrspace(12) global float
+// SPIRV-DAG: @g = external addrspace(12) global float
+// SPIRV-DAG: @h = external addrspace(12) global target("spirv.Layout", %__cblayout_S, 4, 0), align 4
 
 struct EmptyStruct {
 };
@@ -35,4 +42,5 @@ void main() {
 }
 
 // CHECK: !hlsl.cbs = !{![[CB:.*]]}
-// CHECK: ![[CB]] = !{ptr @"$Globals.cb", ptr addrspace(2) @a, ptr addrspace(2) @g, ptr addrspace(2) @h}
+// DXIL: ![[CB]] = !{ptr @"$Globals.cb", ptr addrspace(2) @a, ptr addrspace(2) @g, ptr addrspace(2) @h}
+// SPIRV: ![[CB]] = !{ptr @"$Globals.cb", ptr addrspace(12) @a, ptr addrspace(12) @g, ptr addrspace(12) @h}

@s-perron s-perron marked this pull request as draft May 15, 2025 17:31
This change implement the type used to represent cbuffer for SPIR-V.

Fixes llvm#138274.
@s-perron s-perron marked this pull request as ready for review May 16, 2025 15:20
@s-perron s-perron requested review from Keenuts and cassiebeckley May 16, 2025 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category HLSL HLSL Language Support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[HLSL][SPIRV] Implement CBuffers in CommonSPIRTargetCodeGenInfo::getHLSLType
2 participants