[clang] [HLSL] Add support for SV_GroupIndex in SPIR-V (PR #130672)

2025-03-14 Thread Cassandra Beckley via cfe-commits

https://github.com/cassiebeckley edited 
https://github.com/llvm/llvm-project/pull/130672
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-04-01 Thread Cassandra Beckley via cfe-commits

cassiebeckley wrote:

@s-perron @Keenuts 

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-04-01 Thread Cassandra Beckley via cfe-commits

https://github.com/cassiebeckley created 
https://github.com/llvm/llvm-project/pull/134034

This implements the design proposed by [Representing SpirvType in Clang's Type 
System](https://github.com/llvm/wg-hlsl/pull/181). It creates 
`HLSLInlineSpirvType` as a new `Type` subclass, and `__hlsl_spirv_type` as a 
new builtin type template to create such a type.

This new type is lowered to the `spirv.Type` target extension type, as 
described in [Target Extension Types for Inline SPIR-V and Decorated 
Types](https://github.com/llvm/wg-hlsl/blob/main/proposals/0017-inline-spirv-and-decorated-types.md).

>From 78ac1bc4225b41bc4b9fbd9fd9ab9dc82a2953ca Mon Sep 17 00:00:00 2001
From: Cassandra Beckley 
Date: Tue, 1 Apr 2025 23:12:02 -0700
Subject: [PATCH] [HLSL] Implement `SpirvType` and `SpirvOpaqueType`

This implements the design proposed by [Representing SpirvType in
Clang's Type System](https://github.com/llvm/wg-hlsl/pull/181). It
creates `HLSLInlineSpirvType` as a new `Type` subclass, and
`__hlsl_spirv_type` as a new builtin type template to create such a
type.

This new type is lowered to the `spirv.Type` target extension type, as
described in [Target Extension Types for Inline SPIR-V and Decorated
Types](https://github.com/llvm/wg-hlsl/blob/main/proposals/0017-inline-spirv-and-decorated-types.md).
---
 clang/include/clang-c/Index.h |   3 +-
 clang/include/clang/AST/ASTContext.h  |   5 +
 clang/include/clang/AST/ASTNodeTraverser.h|  18 +++
 clang/include/clang/AST/PropertiesBase.td |   1 +
 clang/include/clang/AST/RecursiveASTVisitor.h |  11 ++
 clang/include/clang/AST/Type.h| 142 +-
 clang/include/clang/AST/TypeLoc.h |  19 +++
 clang/include/clang/AST/TypeProperties.td |  18 +++
 clang/include/clang/Basic/BuiltinTemplates.td |  18 ++-
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Basic/TypeNodes.td|   1 +
 .../clang/Serialization/ASTRecordReader.h |   2 +
 .../clang/Serialization/ASTRecordWriter.h |  14 ++
 .../clang/Serialization/TypeBitCodes.def  |   1 +
 clang/lib/AST/ASTContext.cpp  |  59 
 clang/lib/AST/ASTImporter.cpp |  42 ++
 clang/lib/AST/ASTStructuralEquivalence.cpp|  17 +++
 clang/lib/AST/ExprConstant.cpp|   1 +
 clang/lib/AST/ItaniumMangle.cpp   |  40 -
 clang/lib/AST/MicrosoftMangle.cpp |   5 +
 clang/lib/AST/Type.cpp|  14 ++
 clang/lib/AST/TypePrinter.cpp |  48 ++
 clang/lib/CodeGen/CGDebugInfo.cpp |   8 +
 clang/lib/CodeGen/CGDebugInfo.h   |   1 +
 clang/lib/CodeGen/CodeGenFunction.cpp |   2 +
 clang/lib/CodeGen/CodeGenTypes.cpp|   6 +
 clang/lib/CodeGen/ItaniumCXXABI.cpp   |   2 +
 clang/lib/CodeGen/Targets/SPIR.cpp|  90 ++-
 clang/lib/Headers/CMakeLists.txt  |   1 +
 clang/lib/Headers/hlsl.h  |   4 +
 clang/lib/Headers/hlsl/hlsl_spirv.h   |  30 
 clang/lib/Sema/SemaExpr.cpp   |   1 +
 clang/lib/Sema/SemaLookup.cpp |  21 ++-
 clang/lib/Sema/SemaTemplate.cpp   | 103 -
 clang/lib/Sema/SemaTemplateDeduction.cpp  |   2 +
 clang/lib/Sema/SemaType.cpp   |   1 +
 clang/lib/Sema/TreeTransform.h|   7 +
 clang/lib/Serialization/ASTReader.cpp |   9 ++
 clang/lib/Serialization/ASTWriter.cpp |   4 +
 .../test/AST/HLSL/Inputs/pch_spirv_type.hlsl  |   6 +
 clang/test/AST/HLSL/ast-dump-SpirvType.hlsl   |  27 
 clang/test/AST/HLSL/pch_spirv_type.hlsl   |  17 +++
 clang/test/AST/HLSL/vector-alias.hlsl | 105 +++--
 .../inline/SpirvType.alignment.hlsl   |  16 ++
 .../inline/SpirvType.dx.error.hlsl|  12 ++
 clang/test/CodeGenHLSL/inline/SpirvType.hlsl  |  68 +
 .../inline/SpirvType.incomplete.hlsl  |  14 ++
 .../inline/SpirvType.literal.error.hlsl   |  11 ++
 clang/tools/libclang/CIndex.cpp   |   5 +
 clang/tools/libclang/CXType.cpp   |   1 +
 .../TableGen/ClangBuiltinTemplatesEmitter.cpp |  72 +++--
 51 files changed, 1052 insertions(+), 76 deletions(-)
 create mode 100644 clang/lib/Headers/hlsl/hlsl_spirv.h
 create mode 100644 clang/test/AST/HLSL/Inputs/pch_spirv_type.hlsl
 create mode 100644 clang/test/AST/HLSL/ast-dump-SpirvType.hlsl
 create mode 100644 clang/test/AST/HLSL/pch_spirv_type.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.alignment.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.dx.error.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.incomplete.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.literal.error.hlsl

diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h
inde

[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-04-01 Thread Cassandra Beckley via cfe-commits

https://github.com/cassiebeckley updated 
https://github.com/llvm/llvm-project/pull/134034

>From 78ac1bc4225b41bc4b9fbd9fd9ab9dc82a2953ca Mon Sep 17 00:00:00 2001
From: Cassandra Beckley 
Date: Tue, 1 Apr 2025 23:12:02 -0700
Subject: [PATCH 1/2] [HLSL] Implement `SpirvType` and `SpirvOpaqueType`

This implements the design proposed by [Representing SpirvType in
Clang's Type System](https://github.com/llvm/wg-hlsl/pull/181). It
creates `HLSLInlineSpirvType` as a new `Type` subclass, and
`__hlsl_spirv_type` as a new builtin type template to create such a
type.

This new type is lowered to the `spirv.Type` target extension type, as
described in [Target Extension Types for Inline SPIR-V and Decorated
Types](https://github.com/llvm/wg-hlsl/blob/main/proposals/0017-inline-spirv-and-decorated-types.md).
---
 clang/include/clang-c/Index.h |   3 +-
 clang/include/clang/AST/ASTContext.h  |   5 +
 clang/include/clang/AST/ASTNodeTraverser.h|  18 +++
 clang/include/clang/AST/PropertiesBase.td |   1 +
 clang/include/clang/AST/RecursiveASTVisitor.h |  11 ++
 clang/include/clang/AST/Type.h| 142 +-
 clang/include/clang/AST/TypeLoc.h |  19 +++
 clang/include/clang/AST/TypeProperties.td |  18 +++
 clang/include/clang/Basic/BuiltinTemplates.td |  18 ++-
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Basic/TypeNodes.td|   1 +
 .../clang/Serialization/ASTRecordReader.h |   2 +
 .../clang/Serialization/ASTRecordWriter.h |  14 ++
 .../clang/Serialization/TypeBitCodes.def  |   1 +
 clang/lib/AST/ASTContext.cpp  |  59 
 clang/lib/AST/ASTImporter.cpp |  42 ++
 clang/lib/AST/ASTStructuralEquivalence.cpp|  17 +++
 clang/lib/AST/ExprConstant.cpp|   1 +
 clang/lib/AST/ItaniumMangle.cpp   |  40 -
 clang/lib/AST/MicrosoftMangle.cpp |   5 +
 clang/lib/AST/Type.cpp|  14 ++
 clang/lib/AST/TypePrinter.cpp |  48 ++
 clang/lib/CodeGen/CGDebugInfo.cpp |   8 +
 clang/lib/CodeGen/CGDebugInfo.h   |   1 +
 clang/lib/CodeGen/CodeGenFunction.cpp |   2 +
 clang/lib/CodeGen/CodeGenTypes.cpp|   6 +
 clang/lib/CodeGen/ItaniumCXXABI.cpp   |   2 +
 clang/lib/CodeGen/Targets/SPIR.cpp|  90 ++-
 clang/lib/Headers/CMakeLists.txt  |   1 +
 clang/lib/Headers/hlsl.h  |   4 +
 clang/lib/Headers/hlsl/hlsl_spirv.h   |  30 
 clang/lib/Sema/SemaExpr.cpp   |   1 +
 clang/lib/Sema/SemaLookup.cpp |  21 ++-
 clang/lib/Sema/SemaTemplate.cpp   | 103 -
 clang/lib/Sema/SemaTemplateDeduction.cpp  |   2 +
 clang/lib/Sema/SemaType.cpp   |   1 +
 clang/lib/Sema/TreeTransform.h|   7 +
 clang/lib/Serialization/ASTReader.cpp |   9 ++
 clang/lib/Serialization/ASTWriter.cpp |   4 +
 .../test/AST/HLSL/Inputs/pch_spirv_type.hlsl  |   6 +
 clang/test/AST/HLSL/ast-dump-SpirvType.hlsl   |  27 
 clang/test/AST/HLSL/pch_spirv_type.hlsl   |  17 +++
 clang/test/AST/HLSL/vector-alias.hlsl | 105 +++--
 .../inline/SpirvType.alignment.hlsl   |  16 ++
 .../inline/SpirvType.dx.error.hlsl|  12 ++
 clang/test/CodeGenHLSL/inline/SpirvType.hlsl  |  68 +
 .../inline/SpirvType.incomplete.hlsl  |  14 ++
 .../inline/SpirvType.literal.error.hlsl   |  11 ++
 clang/tools/libclang/CIndex.cpp   |   5 +
 clang/tools/libclang/CXType.cpp   |   1 +
 .../TableGen/ClangBuiltinTemplatesEmitter.cpp |  72 +++--
 51 files changed, 1052 insertions(+), 76 deletions(-)
 create mode 100644 clang/lib/Headers/hlsl/hlsl_spirv.h
 create mode 100644 clang/test/AST/HLSL/Inputs/pch_spirv_type.hlsl
 create mode 100644 clang/test/AST/HLSL/ast-dump-SpirvType.hlsl
 create mode 100644 clang/test/AST/HLSL/pch_spirv_type.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.alignment.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.dx.error.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.incomplete.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.literal.error.hlsl

diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h
index 38e2417dcd181..757f8a3afc758 100644
--- a/clang/include/clang-c/Index.h
+++ b/clang/include/clang-c/Index.h
@@ -3034,7 +3034,8 @@ enum CXTypeKind {
 
   /* HLSL Types */
   CXType_HLSLResource = 179,
-  CXType_HLSLAttributedResource = 180
+  CXType_HLSLAttributedResource = 180,
+  CXType_HLSLInlineSpirv = 181
 };
 
 /**
diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index a24f30815e6b9..c62f9f7672010 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/i

[clang] [HLSL][SPIRV] Add CLI option `-fspv-extension` (PR #137985)

2025-05-01 Thread Cassandra Beckley via cfe-commits


@@ -9213,6 +9213,11 @@ def metal : DXCFlag<"metal">, HelpText<"Generate Metal 
library">;
 def fspv_target_env_EQ : Joined<["-"], "fspv-target-env=">, Group,
   HelpText<"Specify the target environment">,
   Values<"vulkan1.2, vulkan1.3">;
+def fspv_extension_EQ
+: Joined<["-"], "fspv-extension=">,
+  Group,
+  HelpText<"Specify the available SPIR-V extensions. If this option is not 
"
+   "specificed, then all extensions are available.">;

cassiebeckley wrote:

nit:

```suggestion
   "specified, then all extensions are available.">;
```

https://github.com/llvm/llvm-project/pull/137985
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL][SPIRV] Add CLI option `-fspv-extension` (PR #137985)

2025-05-01 Thread Cassandra Beckley via cfe-commits

https://github.com/cassiebeckley approved this pull request.


https://github.com/llvm/llvm-project/pull/137985
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Add __spirv__ macro (PR #132848)

2025-03-25 Thread Cassandra Beckley via cfe-commits

https://github.com/cassiebeckley updated 
https://github.com/llvm/llvm-project/pull/132848

>From 959f0374df39a97d22740ae81d132bed300159ea Mon Sep 17 00:00:00 2001
From: Cassandra Beckley 
Date: Mon, 24 Mar 2025 17:36:46 -0700
Subject: [PATCH 1/2] [HLSL] Add __spirv__ macro

This macro can be used by HLSL code to detect that it is being compiled
for the SPIR-V target.
---
 clang/lib/Frontend/InitPreprocessor.cpp | 3 +++
 clang/test/Preprocessor/predefined-macros-hlsl.hlsl | 4 
 2 files changed, 7 insertions(+)

diff --git a/clang/lib/Frontend/InitPreprocessor.cpp 
b/clang/lib/Frontend/InitPreprocessor.cpp
index 1a816cb6269d4..c6d31e890c7f6 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -430,6 +430,9 @@ static void InitializeStandardPredefinedMacros(const 
TargetInfo &TI,
   unsigned Minor = Version.getMinor().value_or(0);
   Builder.defineMacro("__SHADER_TARGET_MINOR", Twine(Minor));
 }
+if (TI.getTriple().isSPIRV()) {
+  Builder.defineMacro("__spirv__");
+}
 return;
   }
   // C++ [cpp.predefined]p1:
diff --git a/clang/test/Preprocessor/predefined-macros-hlsl.hlsl 
b/clang/test/Preprocessor/predefined-macros-hlsl.hlsl
index cd211713bf892..26bda6b7be167 100644
--- a/clang/test/Preprocessor/predefined-macros-hlsl.hlsl
+++ b/clang/test/Preprocessor/predefined-macros-hlsl.hlsl
@@ -9,6 +9,8 @@
 // RUN: %clang_cc1 %s -E -dM -o - -triple dxil-pc-shadermodel6.0-vertex | 
FileCheck -match-full-lines %s --check-prefixes=CHECK,VERTEX,NOHALF
 // RUN: %clang_cc1 %s -E -dM -o - -triple dxil-pc-shadermodel6.3-vertex 
-fnative-half-type | FileCheck -match-full-lines %s 
--check-prefixes=CHECK,VERTEX,HALF
 
+// RUN: %clang_cc1 %s -E -dM -o - -triple spirv-unknown-vulkan-compute | 
FileCheck -match-full-lines %s --check-prefixes=CHECK,COMPUTE,NOHALF,SPIRV
+
 // HALF: #define __HLSL_ENABLE_16_BIT 1
 // NOHALF-NOT: __HLSL_ENABLE_16_BIT
 
@@ -34,6 +36,8 @@
 // PIXEL: #define __SHADER_TARGET_STAGE 0
 // VERTEX: #define __SHADER_TARGET_STAGE 1
 
+// SPIRV: #define __spirv__ 1
+
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library %s -E -dM -o - -x 
hlsl -std=hlsl2015 2>&1 | FileCheck -match-full-lines %s 
--check-prefixes=STD2015
 // STD2015: warning: support for HLSL language version hlsl2015 is incomplete, 
recommend using hlsl202x instead
 // STD2015: #define __HLSL_VERSION 2015

>From 5e5eacbd517b9e01f0be948cb5431beb29044a9e Mon Sep 17 00:00:00 2001
From: Cassandra Beckley 
Date: Tue, 25 Mar 2025 10:21:39 -0700
Subject: [PATCH 2/2] Define the __spirv__ macro in the SPIRV target

---
 clang/lib/Basic/Targets/SPIR.cpp| 1 +
 clang/lib/Frontend/InitPreprocessor.cpp | 3 ---
 2 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/clang/lib/Basic/Targets/SPIR.cpp b/clang/lib/Basic/Targets/SPIR.cpp
index 5c076f694dfa4..5b5f47f9647a2 100644
--- a/clang/lib/Basic/Targets/SPIR.cpp
+++ b/clang/lib/Basic/Targets/SPIR.cpp
@@ -59,6 +59,7 @@ void SPIR64TargetInfo::getTargetDefines(const LangOptions 
&Opts,
 void BaseSPIRVTargetInfo::getTargetDefines(const LangOptions &Opts,
MacroBuilder &Builder) const {
   DefineStd(Builder, "SPIRV", Opts);
+  DefineStd(Builder, "spirv", Opts);
 }
 
 void SPIRVTargetInfo::getTargetDefines(const LangOptions &Opts,
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp 
b/clang/lib/Frontend/InitPreprocessor.cpp
index c6d31e890c7f6..1a816cb6269d4 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -430,9 +430,6 @@ static void InitializeStandardPredefinedMacros(const 
TargetInfo &TI,
   unsigned Minor = Version.getMinor().value_or(0);
   Builder.defineMacro("__SHADER_TARGET_MINOR", Twine(Minor));
 }
-if (TI.getTriple().isSPIRV()) {
-  Builder.defineMacro("__spirv__");
-}
 return;
   }
   // C++ [cpp.predefined]p1:

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-04-02 Thread Cassandra Beckley via cfe-commits

https://github.com/cassiebeckley updated 
https://github.com/llvm/llvm-project/pull/134034

>From 78ac1bc4225b41bc4b9fbd9fd9ab9dc82a2953ca Mon Sep 17 00:00:00 2001
From: Cassandra Beckley 
Date: Tue, 1 Apr 2025 23:12:02 -0700
Subject: [PATCH 1/3] [HLSL] Implement `SpirvType` and `SpirvOpaqueType`

This implements the design proposed by [Representing SpirvType in
Clang's Type System](https://github.com/llvm/wg-hlsl/pull/181). It
creates `HLSLInlineSpirvType` as a new `Type` subclass, and
`__hlsl_spirv_type` as a new builtin type template to create such a
type.

This new type is lowered to the `spirv.Type` target extension type, as
described in [Target Extension Types for Inline SPIR-V and Decorated
Types](https://github.com/llvm/wg-hlsl/blob/main/proposals/0017-inline-spirv-and-decorated-types.md).
---
 clang/include/clang-c/Index.h |   3 +-
 clang/include/clang/AST/ASTContext.h  |   5 +
 clang/include/clang/AST/ASTNodeTraverser.h|  18 +++
 clang/include/clang/AST/PropertiesBase.td |   1 +
 clang/include/clang/AST/RecursiveASTVisitor.h |  11 ++
 clang/include/clang/AST/Type.h| 142 +-
 clang/include/clang/AST/TypeLoc.h |  19 +++
 clang/include/clang/AST/TypeProperties.td |  18 +++
 clang/include/clang/Basic/BuiltinTemplates.td |  18 ++-
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Basic/TypeNodes.td|   1 +
 .../clang/Serialization/ASTRecordReader.h |   2 +
 .../clang/Serialization/ASTRecordWriter.h |  14 ++
 .../clang/Serialization/TypeBitCodes.def  |   1 +
 clang/lib/AST/ASTContext.cpp  |  59 
 clang/lib/AST/ASTImporter.cpp |  42 ++
 clang/lib/AST/ASTStructuralEquivalence.cpp|  17 +++
 clang/lib/AST/ExprConstant.cpp|   1 +
 clang/lib/AST/ItaniumMangle.cpp   |  40 -
 clang/lib/AST/MicrosoftMangle.cpp |   5 +
 clang/lib/AST/Type.cpp|  14 ++
 clang/lib/AST/TypePrinter.cpp |  48 ++
 clang/lib/CodeGen/CGDebugInfo.cpp |   8 +
 clang/lib/CodeGen/CGDebugInfo.h   |   1 +
 clang/lib/CodeGen/CodeGenFunction.cpp |   2 +
 clang/lib/CodeGen/CodeGenTypes.cpp|   6 +
 clang/lib/CodeGen/ItaniumCXXABI.cpp   |   2 +
 clang/lib/CodeGen/Targets/SPIR.cpp|  90 ++-
 clang/lib/Headers/CMakeLists.txt  |   1 +
 clang/lib/Headers/hlsl.h  |   4 +
 clang/lib/Headers/hlsl/hlsl_spirv.h   |  30 
 clang/lib/Sema/SemaExpr.cpp   |   1 +
 clang/lib/Sema/SemaLookup.cpp |  21 ++-
 clang/lib/Sema/SemaTemplate.cpp   | 103 -
 clang/lib/Sema/SemaTemplateDeduction.cpp  |   2 +
 clang/lib/Sema/SemaType.cpp   |   1 +
 clang/lib/Sema/TreeTransform.h|   7 +
 clang/lib/Serialization/ASTReader.cpp |   9 ++
 clang/lib/Serialization/ASTWriter.cpp |   4 +
 .../test/AST/HLSL/Inputs/pch_spirv_type.hlsl  |   6 +
 clang/test/AST/HLSL/ast-dump-SpirvType.hlsl   |  27 
 clang/test/AST/HLSL/pch_spirv_type.hlsl   |  17 +++
 clang/test/AST/HLSL/vector-alias.hlsl | 105 +++--
 .../inline/SpirvType.alignment.hlsl   |  16 ++
 .../inline/SpirvType.dx.error.hlsl|  12 ++
 clang/test/CodeGenHLSL/inline/SpirvType.hlsl  |  68 +
 .../inline/SpirvType.incomplete.hlsl  |  14 ++
 .../inline/SpirvType.literal.error.hlsl   |  11 ++
 clang/tools/libclang/CIndex.cpp   |   5 +
 clang/tools/libclang/CXType.cpp   |   1 +
 .../TableGen/ClangBuiltinTemplatesEmitter.cpp |  72 +++--
 51 files changed, 1052 insertions(+), 76 deletions(-)
 create mode 100644 clang/lib/Headers/hlsl/hlsl_spirv.h
 create mode 100644 clang/test/AST/HLSL/Inputs/pch_spirv_type.hlsl
 create mode 100644 clang/test/AST/HLSL/ast-dump-SpirvType.hlsl
 create mode 100644 clang/test/AST/HLSL/pch_spirv_type.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.alignment.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.dx.error.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.incomplete.hlsl
 create mode 100644 clang/test/CodeGenHLSL/inline/SpirvType.literal.error.hlsl

diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h
index 38e2417dcd181..757f8a3afc758 100644
--- a/clang/include/clang-c/Index.h
+++ b/clang/include/clang-c/Index.h
@@ -3034,7 +3034,8 @@ enum CXTypeKind {
 
   /* HLSL Types */
   CXType_HLSLResource = 179,
-  CXType_HLSLAttributedResource = 180
+  CXType_HLSLAttributedResource = 180,
+  CXType_HLSLInlineSpirv = 181
 };
 
 /**
diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index a24f30815e6b9..c62f9f7672010 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/i

[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-04-02 Thread Cassandra Beckley via cfe-commits

cassiebeckley wrote:

@philnik777 fyi for the changes to builtin templates

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-09 Thread Cassandra Beckley via cfe-commits

https://github.com/cassiebeckley updated 
https://github.com/llvm/llvm-project/pull/134034



  



Rate limit · GitHub


  body {
background-color: #f6f8fa;
color: #24292e;
font-family: -apple-system,BlinkMacSystemFont,Segoe 
UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 14px;
line-height: 1.5;
margin: 0;
  }

  .container { margin: 50px auto; max-width: 600px; text-align: center; 
padding: 0 24px; }

  a { color: #0366d6; text-decoration: none; }
  a:hover { text-decoration: underline; }

  h1 { line-height: 60px; font-size: 48px; font-weight: 300; margin: 0px; 
text-shadow: 0 1px 0 #fff; }
  p { color: rgba(0, 0, 0, 0.5); margin: 20px 0 40px; }

  ul { list-style: none; margin: 25px 0; padding: 0; }
  li { display: table-cell; font-weight: bold; width: 1%; }

  .logo { display: inline-block; margin-top: 35px; }
  .logo-img-2x { display: none; }
  @media
  only screen and (-webkit-min-device-pixel-ratio: 2),
  only screen and (   min--moz-device-pixel-ratio: 2),
  only screen and ( -o-min-device-pixel-ratio: 2/1),
  only screen and (min-device-pixel-ratio: 2),
  only screen and (min-resolution: 192dpi),
  only screen and (min-resolution: 2dppx) {
.logo-img-1x { display: none; }
.logo-img-2x { display: inline-block; }
  }

  #suggestions {
margin-top: 35px;
color: #ccc;
  }
  #suggestions a {
color: #66;
font-weight: 200;
font-size: 14px;
margin: 0 10px;
  }


  
  



  Whoa there!
  You have exceeded a secondary rate limit.
Please wait a few minutes before you try again;
in some cases this may take up to an hour.
  
  
https://support.github.com/contact";>Contact Support —
https://githubstatus.com";>GitHub Status —
https://twitter.com/githubstatus";>@githubstatus
  

  

  

  

  

  


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits




cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -3332,6 +3388,39 @@ checkBuiltinTemplateIdType(Sema &SemaRef, 
BuiltinTemplateDecl *BTD,
 }
 return HasNoTypeMember;
   }
+
+  case BTK__hlsl_spirv_type: {
+assert(Converted.size() == 4);
+
+if (!Context.getTargetInfo().getTriple().isSPIRV()) {
+  SemaRef.Diag(TemplateLoc, diag::err_hlsl_spirv_only)
+  << "__hlsl_spirv_type";
+}
+
+if (llvm::any_of(Converted, [](auto &C) { return C.isDependent(); }))
+  return Context.getCanonicalTemplateSpecializationType(TemplateName(BTD),
+Converted);
+
+uint64_t Opcode = Converted[0].getAsIntegral().getZExtValue();
+uint64_t Size = Converted[1].getAsIntegral().getZExtValue();
+uint64_t Alignment = Converted[2].getAsIntegral().getZExtValue();
+
+ArrayRef OperandArgs = Converted[3].getPackAsArray();
+
+llvm::SmallVector Operands;
+
+for (auto &OperandTA : OperandArgs) {
+  QualType OperandArg = OperandTA.getAsType();
+  auto Operand = checkHLSLSpirvTypeOperand(SemaRef, OperandArg,
+   TemplateArgs[3].getLocation());
+  if (!Operand.isValid()) {
+return QualType();
+  }

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -6330,6 +6331,140 @@ class HLSLAttributedResourceType : public Type, public 
llvm::FoldingSetNode {
   findHandleTypeOnResource(const Type *RT);
 };
 
+/// Instances of this class represent operands to a SPIR-V type instruction.
+class SpirvOperand {
+public:
+  enum SpirvOperandKind : unsigned char {
+kInvalid,///< Uninitialized.

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -6165,6 +6254,18 @@ bool 
UnnamedLocalNoLinkageFinder::VisitHLSLAttributedResourceType(
   return Visit(T->getWrappedType());
 }
 
+bool UnnamedLocalNoLinkageFinder::VisitHLSLInlineSpirvType(
+const HLSLInlineSpirvType *T) {
+  for (auto &Operand : T->getOperands()) {
+if (Operand.isConstant() && Operand.isLiteral()) {
+  if (Visit(Operand.getResultType())) {
+return true;
+  }
+}
+  }

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -0,0 +1,28 @@
+//===- hlsl_spirv.h - HLSL definitions for SPIR-V target 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef _HLSL_HLSL_SPIRV_H_
+#define _HLSL_HLSL_SPIRV_H_
+
+namespace hlsl {
+namespace vk {
+template  struct integral_constant {
+  static constexpr T value = v;
+};
+
+template  struct Literal {};
+
+template 
+using SpirvType = __hlsl_spirv_type;
+
+template 
+using SpirvOpaqueType = __hlsl_spirv_type;

cassiebeckley wrote:

At the moment we're treating `SpirvOpaqueType` objects as defaulting to a 
32-bit integer for layout information.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -6330,6 +6331,140 @@ class HLSLAttributedResourceType : public Type, public 
llvm::FoldingSetNode {
   findHandleTypeOnResource(const Type *RT);
 };
 
+/// Instances of this class represent operands to a SPIR-V type instruction.
+class SpirvOperand {
+public:
+  enum SpirvOperandKind : unsigned char {
+kInvalid,///< Uninitialized.
+kConstantId, ///< Integral value to represent as a SPIR-V OpConstant
+ ///< instruction ID.
+kLiteral,///< Integral value to represent as an immediate literal.
+kTypeId, ///< Type to represent as a SPIR-V type ID.
+
+kMax,
+  };
+
+private:
+  SpirvOperandKind Kind = kInvalid;
+
+  QualType ResultType;
+  llvm::APInt Value; // Signedness of constants is represented by ResultType.
+
+public:
+  SpirvOperand() : Kind(kInvalid), ResultType() {}
+
+  SpirvOperand(SpirvOperandKind Kind, QualType ResultType, llvm::APInt Value)
+  : Kind(Kind), ResultType(ResultType), Value(Value) {}
+
+  SpirvOperand(const SpirvOperand &Other) { *this = Other; }
+  ~SpirvOperand() {}
+
+  SpirvOperand &operator=(const SpirvOperand &Other) {
+this->Kind = Other.Kind;
+this->ResultType = Other.ResultType;
+this->Value = Other.Value;
+return *this;
+  }
+
+  bool operator==(const SpirvOperand &Other) const {
+return Kind == Other.Kind && ResultType == Other.ResultType &&
+   Value == Other.Value;
+  }
+
+  bool operator!=(const SpirvOperand &Other) const { return !(*this == Other); 
}
+
+  SpirvOperandKind getKind() const { return Kind; }
+
+  bool isValid() const { return Kind != kInvalid && Kind < kMax; }
+  bool isConstant() const { return Kind == kConstantId; }
+  bool isLiteral() const { return Kind == kLiteral; }
+  bool isType() const { return Kind == kTypeId; }
+
+  llvm::APInt getValue() const {
+assert((isConstant() || isLiteral()) &&
+   "This is not an operand with a value!");
+return Value;
+  }
+
+  QualType getResultType() const {
+assert((isConstant() || isType()) &&
+   "This is not an operand with a result type!");
+return ResultType;
+  }
+
+  static SpirvOperand createConstant(QualType ResultType, llvm::APInt Val) {
+return SpirvOperand(kConstantId, ResultType, Val);
+  }
+
+  static SpirvOperand createLiteral(llvm::APInt Val) {
+return SpirvOperand(kLiteral, QualType(), Val);
+  }
+
+  static SpirvOperand createType(QualType T) {
+return SpirvOperand(kTypeId, T, llvm::APSInt());
+  }
+
+  void Profile(llvm::FoldingSetNodeID &ID) const {
+ID.AddInteger(Kind);
+ID.AddPointer(ResultType.getAsOpaquePtr());
+Value.Profile(ID);
+  }
+};
+
+/// Represents an arbitrary, user-specified SPIR-V type instruction.
+class HLSLInlineSpirvType final
+: public Type,
+  public llvm::FoldingSetNode,
+  private llvm::TrailingObjects {
+  friend class ASTContext; // ASTContext creates these
+  friend TrailingObjects;
+
+private:
+  uint32_t Opcode;
+  uint32_t Size;
+  uint32_t Alignment;
+  size_t NumOperands;
+
+  HLSLInlineSpirvType(uint32_t Opcode, uint32_t Size, uint32_t Alignment,
+  ArrayRef Operands)
+  : Type(HLSLInlineSpirv, QualType(), TypeDependence::None), 
Opcode(Opcode),
+Size(Size), Alignment(Alignment), NumOperands(Operands.size()) {
+for (size_t I = 0; I < NumOperands; I++) {
+  getTrailingObjects()[I] = Operands[I];
+}
+  }
+
+public:
+  uint32_t getOpcode() const { return Opcode; }
+  uint32_t getSize() const { return Size; }
+  uint32_t getAlignment() const { return Alignment; }
+  ArrayRef getOperands() const {
+return {getTrailingObjects(), NumOperands};
+  }
+
+  bool isSugared() const { return false; }
+  QualType desugar() const { return QualType(this, 0); }
+
+  void Profile(llvm::FoldingSetNodeID &ID) {
+Profile(ID, Opcode, Size, Alignment, getOperands());
+  }
+
+  static void Profile(llvm::FoldingSetNodeID &ID, uint32_t Opcode,
+  uint32_t Size, uint32_t Alignment,
+  ArrayRef Operands) {
+ID.AddInteger(Opcode);
+ID.AddInteger(Size);
+ID.AddInteger(Alignment);
+for (auto &Operand : Operands) {
+  Operand.Profile(ID);
+}

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -6330,6 +6331,140 @@ class HLSLAttributedResourceType : public Type, public 
llvm::FoldingSetNode {
   findHandleTypeOnResource(const Type *RT);
 };
 
+/// Instances of this class represent operands to a SPIR-V type instruction.
+class SpirvOperand {
+public:
+  enum SpirvOperandKind : unsigned char {
+kInvalid,///< Uninitialized.
+kConstantId, ///< Integral value to represent as a SPIR-V OpConstant
+ ///< instruction ID.
+kLiteral,///< Integral value to represent as an immediate literal.
+kTypeId, ///< Type to represent as a SPIR-V type ID.
+
+kMax,
+  };
+
+private:
+  SpirvOperandKind Kind = kInvalid;
+
+  QualType ResultType;
+  llvm::APInt Value; // Signedness of constants is represented by ResultType.
+
+public:
+  SpirvOperand() : Kind(kInvalid), ResultType() {}
+
+  SpirvOperand(SpirvOperandKind Kind, QualType ResultType, llvm::APInt Value)
+  : Kind(Kind), ResultType(ResultType), Value(Value) {}
+
+  SpirvOperand(const SpirvOperand &Other) { *this = Other; }
+  ~SpirvOperand() {}
+
+  SpirvOperand &operator=(const SpirvOperand &Other) {
+this->Kind = Other.Kind;
+this->ResultType = Other.ResultType;
+this->Value = Other.Value;
+return *this;
+  }
+
+  bool operator==(const SpirvOperand &Other) const {
+return Kind == Other.Kind && ResultType == Other.ResultType &&
+   Value == Other.Value;
+  }
+
+  bool operator!=(const SpirvOperand &Other) const { return !(*this == Other); 
}
+
+  SpirvOperandKind getKind() const { return Kind; }
+
+  bool isValid() const { return Kind != kInvalid && Kind < kMax; }
+  bool isConstant() const { return Kind == kConstantId; }
+  bool isLiteral() const { return Kind == kLiteral; }
+  bool isType() const { return Kind == kTypeId; }
+
+  llvm::APInt getValue() const {
+assert((isConstant() || isLiteral()) &&
+   "This is not an operand with a value!");
+return Value;
+  }
+
+  QualType getResultType() const {
+assert((isConstant() || isType()) &&
+   "This is not an operand with a result type!");
+return ResultType;
+  }
+
+  static SpirvOperand createConstant(QualType ResultType, llvm::APInt Val) {
+return SpirvOperand(kConstantId, ResultType, Val);
+  }
+
+  static SpirvOperand createLiteral(llvm::APInt Val) {
+return SpirvOperand(kLiteral, QualType(), Val);
+  }
+
+  static SpirvOperand createType(QualType T) {
+return SpirvOperand(kTypeId, T, llvm::APSInt());
+  }
+
+  void Profile(llvm::FoldingSetNodeID &ID) const {
+ID.AddInteger(Kind);
+ID.AddPointer(ResultType.getAsOpaquePtr());
+Value.Profile(ID);
+  }
+};
+
+/// Represents an arbitrary, user-specified SPIR-V type instruction.
+class HLSLInlineSpirvType final
+: public Type,
+  public llvm::FoldingSetNode,
+  private llvm::TrailingObjects {
+  friend class ASTContext; // ASTContext creates these
+  friend TrailingObjects;
+
+private:
+  uint32_t Opcode;
+  uint32_t Size;
+  uint32_t Alignment;
+  size_t NumOperands;
+
+  HLSLInlineSpirvType(uint32_t Opcode, uint32_t Size, uint32_t Alignment,
+  ArrayRef Operands)
+  : Type(HLSLInlineSpirv, QualType(), TypeDependence::None), 
Opcode(Opcode),
+Size(Size), Alignment(Alignment), NumOperands(Operands.size()) {
+for (size_t I = 0; I < NumOperands; I++) {
+  getTrailingObjects()[I] = Operands[I];
+}

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits

https://github.com/cassiebeckley updated 
https://github.com/llvm/llvm-project/pull/134034



  



Rate limit · GitHub


  body {
background-color: #f6f8fa;
color: #24292e;
font-family: -apple-system,BlinkMacSystemFont,Segoe 
UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 14px;
line-height: 1.5;
margin: 0;
  }

  .container { margin: 50px auto; max-width: 600px; text-align: center; 
padding: 0 24px; }

  a { color: #0366d6; text-decoration: none; }
  a:hover { text-decoration: underline; }

  h1 { line-height: 60px; font-size: 48px; font-weight: 300; margin: 0px; 
text-shadow: 0 1px 0 #fff; }
  p { color: rgba(0, 0, 0, 0.5); margin: 20px 0 40px; }

  ul { list-style: none; margin: 25px 0; padding: 0; }
  li { display: table-cell; font-weight: bold; width: 1%; }

  .logo { display: inline-block; margin-top: 35px; }
  .logo-img-2x { display: none; }
  @media
  only screen and (-webkit-min-device-pixel-ratio: 2),
  only screen and (   min--moz-device-pixel-ratio: 2),
  only screen and ( -o-min-device-pixel-ratio: 2/1),
  only screen and (min-device-pixel-ratio: 2),
  only screen and (min-resolution: 192dpi),
  only screen and (min-resolution: 2dppx) {
.logo-img-1x { display: none; }
.logo-img-2x { display: inline-block; }
  }

  #suggestions {
margin-top: 35px;
color: #ccc;
  }
  #suggestions a {
color: #66;
font-weight: 200;
font-size: 14px;
margin: 0 10px;
  }


  
  



  Whoa there!
  You have exceeded a secondary rate limit.
Please wait a few minutes before you try again;
in some cases this may take up to an hour.
  
  
https://support.github.com/contact";>Contact Support —
https://githubstatus.com";>GitHub Status —
https://twitter.com/githubstatus";>@githubstatus
  

  

  

  

  

  


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -5444,6 +5459,31 @@ QualType ASTContext::getHLSLAttributedResourceType(
 
   return QualType(Ty, 0);
 }
+
+QualType ASTContext::getHLSLInlineSpirvType(uint32_t Opcode, uint32_t Size,
+uint32_t Alignment,
+ArrayRef Operands) {
+  llvm::FoldingSetNodeID ID;
+  HLSLInlineSpirvType::Profile(ID, Opcode, Size, Alignment, Operands);
+
+  void *InsertPos = nullptr;
+  HLSLInlineSpirvType *Ty =
+  HLSLInlineSpirvTypes.FindNodeOrInsertPos(ID, InsertPos);
+  if (Ty)
+return QualType(Ty, 0);
+
+  unsigned size = sizeof(HLSLInlineSpirvType);
+  size += Operands.size() * sizeof(SpirvOperand);
+  void *mem = Allocate(size, alignof(HLSLInlineSpirvType));
+
+  Ty = new (mem) HLSLInlineSpirvType(Opcode, Size, Alignment, Operands);

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -3228,6 +3228,62 @@ static QualType builtinCommonTypeImpl(Sema &S, 
TemplateName BaseTemplate,
   }
 }
 
+static bool isInVkNamespace(const RecordType *RT) {
+  DeclContext *DC = RT->getDecl()->getDeclContext();
+  if (!DC)
+return false;
+
+  NamespaceDecl *ND = dyn_cast(DC);
+  if (!ND)
+return false;
+
+  return ND->getQualifiedNameAsString() == "hlsl::vk";
+}
+
+static SpirvOperand checkHLSLSpirvTypeOperand(Sema &SemaRef,
+  QualType OperandArg,
+  SourceLocation Loc) {
+  if (auto *RT = OperandArg->getAs()) {
+bool Literal = false;
+SourceLocation LiteralLoc;
+if (isInVkNamespace(RT) && RT->getDecl()->getName() == "Literal") {
+  auto SpecDecl = dyn_cast(RT->getDecl());
+  assert(SpecDecl);
+
+  const TemplateArgumentList &LiteralArgs = SpecDecl->getTemplateArgs();
+  QualType ConstantType = LiteralArgs[0].getAsType();
+  RT = ConstantType->getAs();
+  Literal = true;
+  LiteralLoc = SpecDecl->getSourceRange().getBegin();
+}
+
+if (RT && isInVkNamespace(RT) &&
+RT->getDecl()->getName() == "integral_constant") {
+  auto SpecDecl = dyn_cast(RT->getDecl());
+  assert(SpecDecl);
+
+  const TemplateArgumentList &ConstantArgs = SpecDecl->getTemplateArgs();
+
+  QualType ConstantType = ConstantArgs[0].getAsType();
+  llvm::APInt Value = ConstantArgs[1].getAsIntegral();
+
+  if (Literal) {
+return SpirvOperand::createLiteral(Value);
+  } else {
+return SpirvOperand::createConstant(ConstantType, Value);
+  }
+} else if (Literal) {
+  SemaRef.Diag(LiteralLoc, 
diag::err_hlsl_vk_literal_must_contain_constant);
+  return SpirvOperand();
+}
+  }
+  if (SemaRef.RequireCompleteType(Loc, OperandArg,
+  diag::err_call_incomplete_argument)) {
+return SpirvOperand();
+  }

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -3228,6 +3228,62 @@ static QualType builtinCommonTypeImpl(Sema &S, 
TemplateName BaseTemplate,
   }
 }
 
+static bool isInVkNamespace(const RecordType *RT) {
+  DeclContext *DC = RT->getDecl()->getDeclContext();
+  if (!DC)
+return false;
+
+  NamespaceDecl *ND = dyn_cast(DC);
+  if (!ND)
+return false;
+
+  return ND->getQualifiedNameAsString() == "hlsl::vk";
+}
+
+static SpirvOperand checkHLSLSpirvTypeOperand(Sema &SemaRef,
+  QualType OperandArg,
+  SourceLocation Loc) {
+  if (auto *RT = OperandArg->getAs()) {
+bool Literal = false;
+SourceLocation LiteralLoc;
+if (isInVkNamespace(RT) && RT->getDecl()->getName() == "Literal") {
+  auto SpecDecl = dyn_cast(RT->getDecl());
+  assert(SpecDecl);
+
+  const TemplateArgumentList &LiteralArgs = SpecDecl->getTemplateArgs();
+  QualType ConstantType = LiteralArgs[0].getAsType();
+  RT = ConstantType->getAs();
+  Literal = true;
+  LiteralLoc = SpecDecl->getSourceRange().getBegin();
+}
+
+if (RT && isInVkNamespace(RT) &&
+RT->getDecl()->getName() == "integral_constant") {
+  auto SpecDecl = dyn_cast(RT->getDecl());
+  assert(SpecDecl);
+
+  const TemplateArgumentList &ConstantArgs = SpecDecl->getTemplateArgs();
+
+  QualType ConstantType = ConstantArgs[0].getAsType();
+  llvm::APInt Value = ConstantArgs[1].getAsIntegral();
+
+  if (Literal) {
+return SpirvOperand::createLiteral(Value);
+  } else {
+return SpirvOperand::createConstant(ConstantType, Value);
+  }

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -369,14 +369,102 @@ llvm::Type 
*CommonSPIRTargetCodeGenInfo::getOpenCLType(CodeGenModule &CGM,
   return nullptr;
 }
 
+// Gets a spirv.IntegralConstant or spirv.Literal. If IntegralType is present,
+// returns an IntegralConstant, otherwise returns a Literal.
+static llvm::Type *getInlineSpirvConstant(CodeGenModule &CGM,
+  llvm::Type *IntegralType,
+  llvm::APInt Value) {
+  llvm::LLVMContext &Ctx = CGM.getLLVMContext();
+
+  // Convert the APInt value to an array of uint32_t words
+  llvm::SmallVector Words;
+
+  while (Value.ugt(0)) {
+uint32_t Word = Value.trunc(32).getZExtValue();
+Value.lshrInPlace(32);
+
+Words.push_back(Word);
+  }
+  if (Words.size() == 0)
+Words.push_back(0);
+
+  if (IntegralType) {
+return llvm::TargetExtType::get(Ctx, "spirv.IntegralConstant",
+{IntegralType}, Words);
+  } else {
+return llvm::TargetExtType::get(Ctx, "spirv.Literal", {}, Words);
+  }
+}
+
+static llvm::Type *getInlineSpirvType(CodeGenModule &CGM,
+  const HLSLInlineSpirvType *SpirvType) {
+  llvm::LLVMContext &Ctx = CGM.getLLVMContext();
+
+  llvm::SmallVector Operands;
+
+  for (auto &Operand : SpirvType->getOperands()) {
+using SpirvOperandKind = SpirvOperand::SpirvOperandKind;
+
+llvm::Type *Result = nullptr;
+switch (Operand.getKind()) {
+case SpirvOperandKind::kConstantId: {
+  llvm::Type *IntegralType =
+  CGM.getTypes().ConvertType(Operand.getResultType());
+  llvm::APInt Value = Operand.getValue();
+
+  Result = getInlineSpirvConstant(CGM, IntegralType, Value);
+  break;
+}
+case SpirvOperandKind::kLiteral: {
+  llvm::APInt Value = Operand.getValue();
+  Result = getInlineSpirvConstant(CGM, nullptr, Value);
+  break;
+}
+case SpirvOperandKind::kTypeId: {
+  QualType TypeOperand = Operand.getResultType();
+  if (auto *RT = TypeOperand->getAs()) {
+auto *RD = RT->getDecl();
+assert(RD->isCompleteDefinition() &&
+   "Type completion should have been required in Sema");
+
+const FieldDecl *HandleField = RD->findFirstNamedDataMember();
+if (HandleField) {
+  QualType ResourceType = HandleField->getType();
+  if (ResourceType->getAs()) {
+TypeOperand = ResourceType;
+  }
+}
+  }
+  Result = CGM.getTypes().ConvertType(TypeOperand);
+  break;
+}
+default:
+  llvm_unreachable("HLSLInlineSpirvType had invalid operand!");
+  break;
+}
+
+assert(Result);
+Operands.push_back(Result);
+  }
+
+  return llvm::TargetExtType::get(Ctx, "spirv.Type", Operands,
+  {SpirvType->getOpcode(), 
SpirvType->getSize(),
+   SpirvType->getAlignment()});
+}
+
 llvm::Type *CommonSPIRTargetCodeGenInfo::getHLSLType(
 CodeGenModule &CGM, const Type *Ty,
 const SmallVector *Packoffsets) const {
+  llvm::LLVMContext &Ctx = CGM.getLLVMContext();
+
+  if (auto *SpirvType = dyn_cast(Ty)) {
+return getInlineSpirvType(CGM, SpirvType);
+  }

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -11763,6 +11804,22 @@ QualType ASTContext::mergeTypes(QualType LHS, QualType 
RHS, bool OfBlockPointer,
   return LHS;
 return {};
   }
+  case Type::HLSLInlineSpirv:
+const HLSLInlineSpirvType *LHSTy = LHS->castAs();
+const HLSLInlineSpirvType *RHSTy = RHS->castAs();
+
+if (LHSTy->getOpcode() == RHSTy->getOpcode() &&
+LHSTy->getSize() == RHSTy->getSize() &&
+LHSTy->getAlignment() == RHSTy->getAlignment()) {
+  for (size_t I = 0; I < LHSTy->getOperands().size(); I++) {
+if (LHSTy->getOperands()[I] != RHSTy->getOperands()[I]) {
+  return {};
+}
+  }

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -1832,6 +1832,48 @@ ExpectedType 
clang::ASTNodeImporter::VisitHLSLAttributedResourceType(
   ToWrappedType, ToContainedType, ToAttrs);
 }
 
+ExpectedType clang::ASTNodeImporter::VisitHLSLInlineSpirvType(
+const clang::HLSLInlineSpirvType *T) {
+  Error Err = Error::success();
+
+  uint32_t ToOpcode = T->getOpcode();
+  uint32_t ToSize = T->getSize();
+  uint32_t ToAlignment = T->getAlignment();
+
+  size_t NumOperands = T->getOperands().size();
+
+  llvm::SmallVector ToOperands;
+
+  size_t I = 0;
+  for (auto &Operand : T->getOperands()) {
+using SpirvOperandKind = SpirvOperand::SpirvOperandKind;
+
+switch (Operand.getKind()) {
+case SpirvOperandKind::kConstantId:
+  ToOperands.push_back(SpirvOperand::createConstant(
+  importChecked(Err, Operand.getResultType()), Operand.getValue()));
+  break;
+case SpirvOperandKind::kLiteral:
+  ToOperands.push_back(SpirvOperand::createLiteral(Operand.getValue()));
+  break;
+case SpirvOperandKind::kTypeId:
+  ToOperands.push_back(SpirvOperand::createType(
+  importChecked(Err, Operand.getResultType(;
+  break;
+default:
+  llvm_unreachable("Invalid SpirvOperand kind");
+}
+
+if (Err)
+  return std::move(Err);
+  }
+
+  assert(I == NumOperands);

cassiebeckley wrote:

No, it is not. This is a remnant of an older approach that I was using, which 
was replaced. I'll remove the assert; I'm not sure why it wasn't already 
failing.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -369,14 +369,102 @@ llvm::Type 
*CommonSPIRTargetCodeGenInfo::getOpenCLType(CodeGenModule &CGM,
   return nullptr;
 }
 
+// Gets a spirv.IntegralConstant or spirv.Literal. If IntegralType is present,
+// returns an IntegralConstant, otherwise returns a Literal.
+static llvm::Type *getInlineSpirvConstant(CodeGenModule &CGM,
+  llvm::Type *IntegralType,
+  llvm::APInt Value) {
+  llvm::LLVMContext &Ctx = CGM.getLLVMContext();
+
+  // Convert the APInt value to an array of uint32_t words
+  llvm::SmallVector Words;
+
+  while (Value.ugt(0)) {
+uint32_t Word = Value.trunc(32).getZExtValue();
+Value.lshrInPlace(32);
+
+Words.push_back(Word);
+  }
+  if (Words.size() == 0)
+Words.push_back(0);
+
+  if (IntegralType) {
+return llvm::TargetExtType::get(Ctx, "spirv.IntegralConstant",
+{IntegralType}, Words);
+  } else {
+return llvm::TargetExtType::get(Ctx, "spirv.Literal", {}, Words);
+  }

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -877,6 +878,11 @@ bool CodeGenTypes::isZeroInitializable(QualType T) {
   if (const MemberPointerType *MPT = T->getAs())
 return getCXXABI().isZeroInitializable(MPT);
 
+  // HLSL Inline SPIR-V types are non-zero-initializable.
+  if (T->getAs()) {
+return false;
+  }

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.0-compute %s \
+// RUN:   -fsyntax-only -verify
+
+typedef vk::SpirvType<12, 2, 4, float> InvalidType1;  // expected-error {{use 
of undeclared identifier 'vk'}}
+vk::Literal Unused;  // expected-error {{use 
of undeclared identifier 'vk'}}
+vk::integral_constant Unused2; // expected-error {{use 
of undeclared identifier 'vk'}}
+typedef vk::SpirvOpaqueType<12, float> InvalidType2;  // expected-error {{use 
of undeclared identifier 'vk'}}
+
+[numthreads(1, 1, 1)]
+void main() {
+}

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -0,0 +1,68 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   spirv-unknown-vulkan-compute %s -emit-llvm -disable-llvm-passes \
+// RUN:   -o - | FileCheck %s
+
+template
+using Array = vk::SpirvOpaqueType>;
+
+template
+using ArrayBuffer = Array, Size>;
+
+typedef vk::SpirvType>, 
vk::Literal>> Int;
+
+typedef Array ArrayInt;
+
+// CHECK: %struct.S = type { target("spirv.Type", target("spirv.Image", float, 
5, 2, 0, 0, 2, 0), target("spirv.IntegralConstant", i64, 4), 28, 0, 0), 
target("spirv.Type", target("spirv.Literal", 32), target("spirv.Literal", 0), 
21, 4, 32) }
+struct S {
+ArrayBuffer<4> b;
+Int i;
+};
+
+// CHECK: define spir_func target("spirv.Type", target("spirv.Image", float, 
5, 2, 0, 0, 2, 0), target("spirv.IntegralConstant", i64, 4), 28, 0, 0) 
@_Z14getArrayBufferu17spirv_type_28_0_0U5_TypeN4hlsl8RWBufferIfEEU6_ConstLm4E(target("spirv.Type",
 target("spirv.Image", float, 5, 2, 0, 0, 2, 0), 
target("spirv.IntegralConstant", i64, 4), 28, 0, 0) %v) #0
+ArrayBuffer<4> getArrayBuffer(ArrayBuffer<4> v) {
+return v;
+}
+
+// CHECK: define spir_func target("spirv.Type", target("spirv.Literal", 32), 
target("spirv.Literal", 0), 21, 4, 32) 
@_Z6getIntu18spirv_type_21_4_32U4_LitLi32EU4_LitLi0E(target("spirv.Type", 
target("spirv.Literal", 32), target("spirv.Literal", 0), 21, 4, 32) %v) #0
+Int getInt(Int v) {
+return v;
+}
+
+// TODO: uncomment and test once CBuffer handles are implemented for SPIR-V
+// ArrayBuffer<4> g_buffers;
+// Int g_word;
+
+[numthreads(1, 1, 1)]
+void main() {
+// CHECK: %buffers = alloca target("spirv.Type", target("spirv.Image", 
float, 5, 2, 0, 0, 2, 0), target("spirv.IntegralConstant", i64, 4), 28, 0, 0), 
align 4

cassiebeckley wrote:

I've replaced the names with `.*`.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.0-compute %s \
+// RUN:   -fsyntax-only -verify
+
+typedef vk::SpirvType<12, 2, 4, float> InvalidType1;  // expected-error {{use 
of undeclared identifier 'vk'}}
+vk::Literal Unused;  // expected-error {{use 
of undeclared identifier 'vk'}}
+vk::integral_constant Unused2; // expected-error {{use 
of undeclared identifier 'vk'}}
+typedef vk::SpirvOpaqueType<12, float> InvalidType2;  // expected-error {{use 
of undeclared identifier 'vk'}}

cassiebeckley wrote:

Yeah, makes sense. Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits




cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits




cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-06 Thread Cassandra Beckley via cfe-commits


@@ -3332,6 +3388,39 @@ checkBuiltinTemplateIdType(Sema &SemaRef, 
BuiltinTemplateDecl *BTD,
 }
 return HasNoTypeMember;
   }
+
+  case BTK__hlsl_spirv_type: {
+assert(Converted.size() == 4);
+
+if (!Context.getTargetInfo().getTriple().isSPIRV()) {
+  SemaRef.Diag(TemplateLoc, diag::err_hlsl_spirv_only)
+  << "__hlsl_spirv_type";

cassiebeckley wrote:

Done.

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-15 Thread Cassandra Beckley via cfe-commits

https://github.com/cassiebeckley updated 
https://github.com/llvm/llvm-project/pull/134034



  



Rate limit · GitHub


  body {
background-color: #f6f8fa;
color: #24292e;
font-family: -apple-system,BlinkMacSystemFont,Segoe 
UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 14px;
line-height: 1.5;
margin: 0;
  }

  .container { margin: 50px auto; max-width: 600px; text-align: center; 
padding: 0 24px; }

  a { color: #0366d6; text-decoration: none; }
  a:hover { text-decoration: underline; }

  h1 { line-height: 60px; font-size: 48px; font-weight: 300; margin: 0px; 
text-shadow: 0 1px 0 #fff; }
  p { color: rgba(0, 0, 0, 0.5); margin: 20px 0 40px; }

  ul { list-style: none; margin: 25px 0; padding: 0; }
  li { display: table-cell; font-weight: bold; width: 1%; }

  .logo { display: inline-block; margin-top: 35px; }
  .logo-img-2x { display: none; }
  @media
  only screen and (-webkit-min-device-pixel-ratio: 2),
  only screen and (   min--moz-device-pixel-ratio: 2),
  only screen and ( -o-min-device-pixel-ratio: 2/1),
  only screen and (min-device-pixel-ratio: 2),
  only screen and (min-resolution: 192dpi),
  only screen and (min-resolution: 2dppx) {
.logo-img-1x { display: none; }
.logo-img-2x { display: inline-block; }
  }

  #suggestions {
margin-top: 35px;
color: #ccc;
  }
  #suggestions a {
color: #66;
font-weight: 200;
font-size: 14px;
margin: 0 10px;
  }


  
  



  Whoa there!
  You have exceeded a secondary rate limit.
Please wait a few minutes before you try again;
in some cases this may take up to an hour.
  
  
https://support.github.com/contact";>Contact Support —
https://githubstatus.com";>GitHub Status —
https://twitter.com/githubstatus";>@githubstatus
  

  

  

  

  

  


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Emit convergence tokens for loop in global array init (PR #140120)

2025-05-15 Thread Cassandra Beckley via cfe-commits

https://github.com/cassiebeckley approved this pull request.


https://github.com/llvm/llvm-project/pull/140120
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement `SpirvType` and `SpirvOpaqueType` (PR #134034)

2025-05-15 Thread Cassandra Beckley via cfe-commits

cassiebeckley wrote:

@llvm-beanz I believe I've addressed all of your feedback; let me know if 
there's anything else I should do

https://github.com/llvm/llvm-project/pull/134034
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits