Skip to content

[SPIR-V] Only emit __spirv__ when targeting HLSL #142401

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 1 commit into from
Jun 2, 2025

Conversation

Keenuts
Copy link
Contributor

@Keenuts Keenuts commented Jun 2, 2025

OpenCL translator has a __spirv namespace, and defining the __spirv__ macro causes issues downstream on the OpenCL side. This macro is needed to keep compatibility with HLSL/DXC, but can be avoided for other targets/languages.

OpenCL translator had a __spirv namespace, and defining the __spirv__
macro causes issues downstream on the OpenCL side.
This macro is needed to keep compatibility with HLSL/DXC, but can
be avoided for other targets/languages.
@Keenuts Keenuts requested review from AaronBallman and s-perron June 2, 2025 14:19
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 2, 2025

@llvm/pr-subscribers-clang

Author: Nathan Gauër (Keenuts)

Changes

OpenCL translator had a __spirv namespace, and defining the spirv macro causes issues downstream on the OpenCL side. This macro is needed to keep compatibility with HLSL/DXC, but can be avoided for other targets/languages.


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

2 Files Affected:

  • (modified) clang/lib/Basic/Targets/SPIR.cpp (+2-1)
  • (modified) clang/test/Preprocessor/predefined-macros.c (+3)
diff --git a/clang/lib/Basic/Targets/SPIR.cpp b/clang/lib/Basic/Targets/SPIR.cpp
index 7ab5014a06647..2336fb3ef0495 100644
--- a/clang/lib/Basic/Targets/SPIR.cpp
+++ b/clang/lib/Basic/Targets/SPIR.cpp
@@ -87,7 +87,8 @@ void SPIR64TargetInfo::getTargetDefines(const LangOptions &Opts,
 void BaseSPIRVTargetInfo::getTargetDefines(const LangOptions &Opts,
                                            MacroBuilder &Builder) const {
   DefineStd(Builder, "SPIRV", Opts);
-  DefineStd(Builder, "spirv", Opts);
+  if (Opts.HLSL)
+    DefineStd(Builder, "spirv", Opts);
 }
 
 void SPIRVTargetInfo::getTargetDefines(const LangOptions &Opts,
diff --git a/clang/test/Preprocessor/predefined-macros.c b/clang/test/Preprocessor/predefined-macros.c
index b7765bfa2fb14..da25b1efa3984 100644
--- a/clang/test/Preprocessor/predefined-macros.c
+++ b/clang/test/Preprocessor/predefined-macros.c
@@ -228,6 +228,7 @@
 // CHECK-SPIRV32-DAG: #define __SPIRV__ 1
 // CHECK-SPIRV32-DAG: #define __SPIRV32__ 1
 // CHECK-SPIRV32-NOT: #define __SPIRV64__ 1
+// CHECK-SPIRV32-NOT: #define __spirv__ 1
 
 // RUN: %clang_cc1 %s -E -dM -o - -x cl -triple spirv64-unknown-unknown \
 // RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-SPIRV64
@@ -235,6 +236,7 @@
 // CHECK-SPIRV64-DAG: #define __SPIRV__ 1
 // CHECK-SPIRV64-DAG: #define __SPIRV64__ 1
 // CHECK-SPIRV64-NOT: #define __SPIRV32__ 1
+// CHECK-SPIRV64-NOT: #define __spirv__ 1
 
 // RUN: %clang_cc1 %s -E -dM -o - -x cl -triple spirv64-amd-amdhsa \
 // RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-SPIRV64-AMDGCN
@@ -245,6 +247,7 @@
 // CHECK-SPIRV64-AMDGCN-DAG: #define __AMDGCN__ 1
 // CHECK-SPIRV64-AMDGCN-DAG: #define __AMDGPU__ 1
 // CHECK-SPIRV64-AMDGCN-NOT: #define __SPIRV32__ 1
+// CHECK-SPIRV64-AMDGCN-NOT: #define __spirv__ 1
 
 // RUN: %clang_cc1 %s -E -dM -o - -x hip -triple x86_64-unknown-linux-gnu \
 // RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-HIP

@AaronBallman AaronBallman requested a review from jsji June 2, 2025 14:25
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM! @jsji does this resolve the issue on your end?

Copy link
Member

@jsji jsji left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for prompt fixing.

@jsji jsji merged commit df5f65d into llvm:main Jun 2, 2025
14 checks passed
sallto pushed a commit to sallto/llvm-project that referenced this pull request Jun 3, 2025
OpenCL translator has a `__spirv` namespace, and defining the
`__spirv__` macro causes issues downstream on the OpenCL side. This
macro is needed to keep compatibility with HLSL/DXC, but can be avoided
for other targets/languages.
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