-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
[HLSL] Add __spirv__ macro #132848
Conversation
This macro can be used by HLSL code to detect that it is being compiled for the SPIR-V target.
@llvm/pr-subscribers-clang Author: Cassandra Beckley (cassiebeckley) ChangesThis 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:
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
|
There was a problem hiding this 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.
There was a problem hiding this 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__
.
Hi! I'm wondering why this macro needs to be exposed outside of HLSL given that there's already |
Hi, I don't think there is an issue with only emitting the |
This macro can be used by HLSL code to detect that it is being compiled for the SPIR-V target.