Skip to content

[HLSL] Add __spirv__ macro #132848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2025
Merged

[HLSL] Add __spirv__ macro #132848

merged 2 commits into from
Mar 28, 2025

Conversation

cassiebeckley
Copy link
Member

This macro can be used by HLSL code to detect that it is being compiled for the SPIR-V target.

This macro can be used by HLSL code to detect that it is being compiled
for the SPIR-V target.
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Mar 25, 2025
@cassiebeckley
Copy link
Member Author

@s-perron @Keenuts

@llvmbot
Copy link
Member

llvmbot commented Mar 25, 2025

@llvm/pr-subscribers-clang

Author: Cassandra Beckley (cassiebeckley)

Changes

This macro can be used by HLSL code to detect that it is being compiled for the SPIR-V target.


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

2 Files Affected:

  • (modified) clang/lib/Frontend/InitPreprocessor.cpp (+3)
  • (modified) clang/test/Preprocessor/predefined-macros-hlsl.hlsl (+4)
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

Copy link
Contributor

@s-perron s-perron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I was thinking of adding this myself, but it was pushed off. I don't know the best place to put it. I'll let others who know the FE review.

@llvmbot llvmbot added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Mar 25, 2025
@cassiebeckley cassiebeckley requested a review from farzonl March 25, 2025 17:22
Copy link
Contributor

@s-perron s-perron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is useful because to will match the DXC behaviour, and make code more easier to port. Even if we have __SPIRV__ is to good to have __spirv__.

@s-perron s-perron merged commit 9ce7725 into llvm:main Mar 28, 2025
12 checks passed
@cassiebeckley cassiebeckley deleted the hlsl-spirv-macro branch March 28, 2025 17:55
@AaronBallman
Copy link
Collaborator

Hi! I'm wondering why this macro needs to be exposed outside of HLSL given that there's already __SPIRV__ exposed for this exact purpose. These changes turn out to be disruptive in a downstream SYCL implementation (intel/llvm#18219) because of a namespace named __spirv.

@Keenuts
Copy link
Contributor

Keenuts commented Jun 2, 2025

Hi, I don't think there is an issue with only emitting the __spirv__ macro with HLSL. Sent out #142401

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants