https://github.com/schittir updated https://github.com/llvm/llvm-project/pull/227665
>From f3dab416c11d0eb14e00ade6e58c60ef948060f8 Mon Sep 17 00:00:00 2001 From: Sindhu Chittireddy <[email protected]> Date: Tue, 29 Sep 2026 18:23:58 -0700 Subject: [PATCH 01/13] [clang][SPIR-V] Add sse/sse2 for MSVC hosts and x86 calling conventions This patch adds sse and sse2 to initFeatureMap when the host predefines _M_X64, as MSVC's STL headers declare always_inline _mm_* intrinsics requiring them. It also permits CC_X86VectorCall and CC_X86RegCall calling conventions. This affects only compilations with no x86 auxiliary target. --- clang/lib/Basic/Targets/SPIR.cpp | 16 ++++++++ clang/lib/Basic/Targets/SPIR.h | 10 ++++- .../spirv-host-adaptation-features.cpp | 37 +++++++++++++++++++ .../spirv-host-adaptation-macros.cpp | 14 +++++++ clang/test/SemaSYCL/sycl-cconv.cpp | 4 ++ 5 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp diff --git a/clang/lib/Basic/Targets/SPIR.cpp b/clang/lib/Basic/Targets/SPIR.cpp index 3eb62c9051b58c..b84cb88eac8b26 100644 --- a/clang/lib/Basic/Targets/SPIR.cpp +++ b/clang/lib/Basic/Targets/SPIR.cpp @@ -110,6 +110,22 @@ void SPIRV64TargetInfo::getTargetDefines(const LangOptions &Opts, DefineStd(Builder, "SPIRV64", Opts); } +bool BaseSPIRTargetInfo::initFeatureMap( + llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU, + const std::vector<std::string> &FeaturesVec) const { + // When the host predefines _M_X64, MSVC STL headers use always_inline _mm_* + // intrinsics, which require sse/sse2 in the device feature set. + if (const TargetInfo *Host = getHostTarget()) { + const llvm::Triple &HT = Host->getTriple(); + if (HT.isWindowsMSVCEnvironment() && + (HT.getArch() == llvm::Triple::x86_64 || HT.isWindowsArm64EC())) { + Features["sse"] = true; + Features["sse2"] = true; + } + } + return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec); +} + static const AMDGPUTargetInfo AMDGPUTI(llvm::Triple(llvm::Triple::amdgpu, llvm::Triple::NoSubArch, llvm::Triple::AMD, llvm::Triple::AMDHSA), diff --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h index c24a55ecfc9704..566f4cf9abc9e9 100644 --- a/clang/lib/Basic/Targets/SPIR.h +++ b/clang/lib/Basic/Targets/SPIR.h @@ -185,9 +185,17 @@ class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo { } CallingConvCheckResult checkCallingConvention(CallingConv CC) const override { - return (CC == CC_C || CC == CC_DeviceKernel) ? CCCR_OK : CCCR_Warning; + return (CC == CC_C || CC == CC_DeviceKernel || CC == CC_X86RegCall || + CC == CC_X86VectorCall) + ? CCCR_OK + : CCCR_Warning; } + bool + initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, + StringRef CPU, + const std::vector<std::string> &FeaturesVec) const override; + void setAddressSpaceMap(bool DefaultIsGeneric) { AddrSpaceMap = DefaultIsGeneric ? &SPIRDefIsGenMap : &SPIRDefIsPrivMap; } diff --git a/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp b/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp new file mode 100644 index 00000000000000..8b054e0e9291a3 --- /dev/null +++ b/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp @@ -0,0 +1,37 @@ +/// Check the sse/sse2 device features derived from the host target. + +// RUN: %clang_cc1 -triple spir64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple arm64ec-pc-windows-msvc \ +// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s +// RUN: %clang_cc1 -triple spir-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s +// RUN: %clang_cc1 -triple spirv32-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s +/// Windows ARM64 without EC is not an x86 host and does not predefine _M_X64. +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple aarch64-pc-windows-msvc \ +// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s + +/// 32-bit Windows hosts predefine _M_IX86 rather than _M_X64. +// RUN: %clang_cc1 -triple spirv32-unknown-unknown -aux-triple i386-pc-windows-msvc \ +// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s + +/// Non-MSVC x86_64 hosts do not predefine _M_X64, whether or not they are +/// Windows hosts. +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-unknown-linux-gnu \ +// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-gnu \ +// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-uefi \ +// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s +// RUN: %clang_cc1 -triple spirv64-unknown-unknown \ +// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s + +[[clang::sycl_external]] void test() {} + +// SSE2: define {{.*}}spir_func void @_Z4testv() #[[ATTR:[0-9]+]] +// SSE2: attributes #[[ATTR]] = {{{.*}}"target-features"="+sse,+sse2" + +// NO-SSE2-NOT: "target-features" diff --git a/clang/test/Preprocessor/spirv-host-adaptation-macros.cpp b/clang/test/Preprocessor/spirv-host-adaptation-macros.cpp index 786059fee4cf0f..852ea9c3c1544d 100644 --- a/clang/test/Preprocessor/spirv-host-adaptation-macros.cpp +++ b/clang/test/Preprocessor/spirv-host-adaptation-macros.cpp @@ -7,6 +7,8 @@ // RUN: -fsycl-is-device -E -dM %s | FileCheck --check-prefix=WIN64 %s // RUN: %clang_cc1 -triple spirv32-unknown-unknown -aux-triple i386-unknown-linux-gnu \ // RUN: -fsycl-is-device -E -dM %s | FileCheck --check-prefix=LINUX32 %s +// RUN: %clang_cc1 -triple spirv32-unknown-unknown -aux-triple i386-pc-windows-msvc \ +// RUN: -fsycl-is-device -E -dM %s | FileCheck --check-prefix=WIN32 %s // RUN: %clang_cc1 -triple spirv64-unknown-unknown \ // RUN: -fsycl-is-device -E -dM %s | FileCheck --check-prefix=NOHOST64 %s // RUN: %clang_cc1 -triple spirv32-unknown-unknown \ @@ -38,6 +40,14 @@ // LINUX32-DAG: #define __SIZEOF_PTRDIFF_T__ 4 // LINUX32-DAG: #define __SIZEOF_POINTER__ 4 +// Windows i386 host (ILP32) +// WIN32-DAG: #define __SIZE_TYPE__ unsigned int +// WIN32-DAG: #define __PTRDIFF_TYPE__ int +// WIN32-DAG: #define __INTPTR_TYPE__ int +// WIN32-DAG: #define __SIZEOF_SIZE_T__ 4 +// WIN32-DAG: #define __SIZEOF_PTRDIFF_T__ 4 +// WIN32-DAG: #define __SIZEOF_POINTER__ 4 + // No host (SPIRV64 defaults) // NOHOST64-DAG: #define __SIZE_TYPE__ long unsigned int // NOHOST64-DAG: #define __PTRDIFF_TYPE__ long int @@ -59,6 +69,8 @@ // WIN64-DAG: #define _WIN64 1 // WIN64-DAG: #define _M_X64 100 // WIN64-DAG: #define _M_AMD64 100 +// WIN32-DAG: #define _WIN32 1 +// WIN32-DAG: #define _M_IX86 600 // LINUX64-DAG: #define __linux__ 1 // LINUX64-DAG: #define __x86_64__ 1 @@ -67,6 +79,8 @@ // LINUX64-DAG: #define __SPIRV64__ 1 // WIN64-DAG: #define __SPIRV__ 1 // WIN64-DAG: #define __SPIRV64__ 1 +// WIN32-DAG: #define __SPIRV__ 1 +// WIN32-DAG: #define __SPIRV32__ 1 // NOHOST64-DAG: #define __SPIRV__ 1 // NOHOST64-DAG: #define __SPIRV64__ 1 // NOHOST32-DAG: #define __SPIRV__ 1 diff --git a/clang/test/SemaSYCL/sycl-cconv.cpp b/clang/test/SemaSYCL/sycl-cconv.cpp index 1b250676cf4784..8661aa3a372d5c 100644 --- a/clang/test/SemaSYCL/sycl-cconv.cpp +++ b/clang/test/SemaSYCL/sycl-cconv.cpp @@ -15,6 +15,10 @@ void bar() { printf("hello\n"); } +// Accepted by the SPIR-V target itself, with no host to fall back on. +void __attribute__((regcall)) rcall(int a, int b) {} +void __attribute__((vectorcall)) vcall(float a, float b) {} + // Check some weird calling convention that is not supported even by x86_64 aux. // no-aux-warning@+1 {{'__swiftasynccall__' calling convention is not supported for this target}} void __attribute__((__swiftasynccall__)) g(void) {} >From 43a10425cca3151f55f9544b5ff1a78832699a7e Mon Sep 17 00:00:00 2001 From: Sindhu Chittireddy <[email protected]> Date: Wed, 30 Sep 2026 07:45:38 -0700 Subject: [PATCH 02/13] Use a switch in checkCallingConvention --- clang/lib/Basic/Targets/SPIR.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h index 566f4cf9abc9e9..e2cd809cfd16fa 100644 --- a/clang/lib/Basic/Targets/SPIR.h +++ b/clang/lib/Basic/Targets/SPIR.h @@ -185,10 +185,15 @@ class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo { } CallingConvCheckResult checkCallingConvention(CallingConv CC) const override { - return (CC == CC_C || CC == CC_DeviceKernel || CC == CC_X86RegCall || - CC == CC_X86VectorCall) - ? CCCR_OK - : CCCR_Warning; + switch (CC) { + case CC_C: + case CC_DeviceKernel: + case CC_X86RegCall: + case CC_X86VectorCall: + return CCCR_OK; + default: + return CCCR_Warning; + } } bool >From 58193c3132462d4632c23feadadfc4a3e715d3be Mon Sep 17 00:00:00 2001 From: Sindhu Chittireddy <[email protected]> Date: Wed, 30 Sep 2026 08:29:36 -0700 Subject: [PATCH 03/13] Add test coverage for explicitly disabled sse/sse2 --- .../CodeGenSYCL/spirv-host-adaptation-features.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp b/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp index 8b054e0e9291a3..18ff40b6483064 100644 --- a/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp +++ b/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp @@ -10,6 +10,14 @@ // RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s // RUN: %clang_cc1 -triple spirv32-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ // RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s +/// An explicit -target-feature overrides the derived default. +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ +// RUN: -target-feature -sse2 -fsycl-is-device -emit-llvm -o - %s \ +// RUN: | FileCheck --check-prefix=NO-SSE2-FLAG %s +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ +// RUN: -target-feature -sse -fsycl-is-device -emit-llvm -o - %s \ +// RUN: | FileCheck --check-prefix=NO-SSE-FLAG %s + /// Windows ARM64 without EC is not an x86 host and does not predefine _M_X64. // RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple aarch64-pc-windows-msvc \ // RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s @@ -35,3 +43,6 @@ // SSE2: attributes #[[ATTR]] = {{{.*}}"target-features"="+sse,+sse2" // NO-SSE2-NOT: "target-features" + +// NO-SSE2-FLAG: attributes #{{[0-9]+}} = {{{.*}}"target-features"="+sse,-sse2" +// NO-SSE-FLAG: attributes #{{[0-9]+}} = {{{.*}}"target-features"="+sse2,-sse" >From 1da8d7dfe2e64e46a03ffc68ddaeb3d52ab8094f Mon Sep 17 00:00:00 2001 From: Sindhu Chittireddy <[email protected]> Date: Wed, 30 Sep 2026 10:18:15 -0700 Subject: [PATCH 04/13] Add release notes for the sse/sse2 and calling convention changes --- clang/docs/ReleaseNotes.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index c778703e8cc6fc..b7fef479a2b555 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1056,6 +1056,15 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use #### Improvements +- The `__regcall` and `__vectorcall` calling conventions are now accepted on + SPIR and SPIR-V targets. Previously they were diagnosed as unsupported when + compiling without an x86 auxiliary target. + +- SPIR and SPIR-V targets now enable `sse` and `sse2` by default when the host + target predefines `_M_X64`, as the MSVC STL headers declare `always_inline` + `_mm_*` intrinsics that require them. An explicit `-target-feature` still + overrides the default. + ## Additional Information A wide variety of additional information is available on the [Clang web >From 6d807b4182b83db700cec56ac02c692f41ebcd0a Mon Sep 17 00:00:00 2001 From: Sindhu Chittireddy <[email protected]> Date: Wed, 30 Sep 2026 12:06:42 -0700 Subject: [PATCH 05/13] Reword the host target comments and fix test indentation --- clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp | 7 +------ clang/test/SemaSYCL/sycl-cconv.cpp | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp b/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp index 18ff40b6483064..9decbbc736f5ba 100644 --- a/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp +++ b/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp @@ -18,16 +18,11 @@ // RUN: -target-feature -sse -fsycl-is-device -emit-llvm -o - %s \ // RUN: | FileCheck --check-prefix=NO-SSE-FLAG %s -/// Windows ARM64 without EC is not an x86 host and does not predefine _M_X64. +/// No sse/sse2 unless the host is 64-bit x86 MSVC // RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple aarch64-pc-windows-msvc \ // RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s - -/// 32-bit Windows hosts predefine _M_IX86 rather than _M_X64. // RUN: %clang_cc1 -triple spirv32-unknown-unknown -aux-triple i386-pc-windows-msvc \ // RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s - -/// Non-MSVC x86_64 hosts do not predefine _M_X64, whether or not they are -/// Windows hosts. // RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-unknown-linux-gnu \ // RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s // RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-gnu \ diff --git a/clang/test/SemaSYCL/sycl-cconv.cpp b/clang/test/SemaSYCL/sycl-cconv.cpp index 8661aa3a372d5c..5af1e31ef9c929 100644 --- a/clang/test/SemaSYCL/sycl-cconv.cpp +++ b/clang/test/SemaSYCL/sycl-cconv.cpp @@ -36,7 +36,7 @@ int main() { //expected-error@+1 {{SYCL device code does not support variadic functions}} sycl_entry_point<class kn>([]() { printf("world\n"); moo(); - //expected-error@+1 {{SYCL device code does not support variadic functions}} + //expected-error@+1 {{SYCL device code does not support variadic functions}} foo(1,2); }); bar(); return 0; >From a9efd05fc0be54cf6afc059678fce249cd3f3995 Mon Sep 17 00:00:00 2001 From: Sindhu Chittireddy <[email protected]> Date: Thu, 1 Oct 2026 12:43:32 -0700 Subject: [PATCH 06/13] Don't accept regcall and vectorcall on SPIR-V targets Test hosts that lack these calling conventions and state the host condition accurately in the comments and release note. --- clang/docs/ReleaseNotes.md | 10 +++------- clang/lib/Basic/Targets/SPIR.cpp | 2 +- clang/lib/Basic/Targets/SPIR.h | 2 -- clang/test/SemaSYCL/sycl-cconv.cpp | 16 +++++++++++++--- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index b7fef479a2b555..6e0c175bf41964 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1056,14 +1056,10 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use #### Improvements -- The `__regcall` and `__vectorcall` calling conventions are now accepted on - SPIR and SPIR-V targets. Previously they were diagnosed as unsupported when - compiling without an x86 auxiliary target. - - SPIR and SPIR-V targets now enable `sse` and `sse2` by default when the host - target predefines `_M_X64`, as the MSVC STL headers declare `always_inline` - `_mm_*` intrinsics that require them. An explicit `-target-feature` still - overrides the default. + target is x86-64 or ARM64EC in an MSVC environment, as the MSVC STL headers + declare `always_inline` `_mm_*` intrinsics that require them. An explicit + `-target-feature` still overrides the default. ## Additional Information diff --git a/clang/lib/Basic/Targets/SPIR.cpp b/clang/lib/Basic/Targets/SPIR.cpp index b84cb88eac8b26..79a12ce2b20d80 100644 --- a/clang/lib/Basic/Targets/SPIR.cpp +++ b/clang/lib/Basic/Targets/SPIR.cpp @@ -113,7 +113,7 @@ void SPIRV64TargetInfo::getTargetDefines(const LangOptions &Opts, bool BaseSPIRTargetInfo::initFeatureMap( llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU, const std::vector<std::string> &FeaturesVec) const { - // When the host predefines _M_X64, MSVC STL headers use always_inline _mm_* + // On x86-64 and ARM64EC MSVC hosts, the STL headers use always_inline _mm_* // intrinsics, which require sse/sse2 in the device feature set. if (const TargetInfo *Host = getHostTarget()) { const llvm::Triple &HT = Host->getTriple(); diff --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h index e2cd809cfd16fa..579e6549d20eb8 100644 --- a/clang/lib/Basic/Targets/SPIR.h +++ b/clang/lib/Basic/Targets/SPIR.h @@ -188,8 +188,6 @@ class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo { switch (CC) { case CC_C: case CC_DeviceKernel: - case CC_X86RegCall: - case CC_X86VectorCall: return CCCR_OK; default: return CCCR_Warning; diff --git a/clang/test/SemaSYCL/sycl-cconv.cpp b/clang/test/SemaSYCL/sycl-cconv.cpp index 5af1e31ef9c929..257329b1a41817 100644 --- a/clang/test/SemaSYCL/sycl-cconv.cpp +++ b/clang/test/SemaSYCL/sycl-cconv.cpp @@ -1,5 +1,7 @@ // RUN: %clang_cc1 -isystem %S/Inputs/ -fsycl-is-device -triple spirv64 -aux-triple x86_64-pc-windows-msvc -fsyntax-only -verify %s -// RUN: %clang_cc1 -isystem %S/Inputs/ -fsycl-is-device -triple spirv64 -fsyntax-only -verify=expected,no-aux %s +// RUN: %clang_cc1 -isystem %S/Inputs/ -fsycl-is-device -triple spirv64 -aux-triple x86_64-unknown-linux-gnu -fsyntax-only -verify %s +// RUN: %clang_cc1 -isystem %S/Inputs/ -fsycl-is-device -triple spirv64 -aux-triple aarch64-unknown-linux-gnu -fsyntax-only -verify=expected,no-x86-host %s +// RUN: %clang_cc1 -isystem %S/Inputs/ -fsycl-is-device -triple spirv64 -fsyntax-only -verify=expected,no-aux,no-x86-host %s // Check that there is no error/warning emitted for cdecl functions compiled for // SYCL device. Make sure variadic calls from within device code are diagnosed. @@ -15,8 +17,11 @@ void bar() { printf("hello\n"); } -// Accepted by the SPIR-V target itself, with no host to fall back on. +// Accepted when the auxiliary host target supports the calling convention and +// diagnosed when neither the device nor the host does. +// no-x86-host-warning@+1 {{'regcall' calling convention is not supported for this target}} void __attribute__((regcall)) rcall(int a, int b) {} +// no-x86-host-warning@+1 {{'vectorcall' calling convention is not supported for this target}} void __attribute__((vectorcall)) vcall(float a, float b) {} // Check some weird calling convention that is not supported even by x86_64 aux. @@ -37,7 +42,12 @@ int main() { sycl_entry_point<class kn>([]() { printf("world\n"); moo(); //expected-error@+1 {{SYCL device code does not support variadic functions}} - foo(1,2); }); + foo(1,2); + // Calls are not diagnosed; an unsupported calling convention is diagnosed + // on the declaration instead. + rcall(1, 2); + vcall(1.0f, 2.0f); + g(); }); bar(); return 0; } >From 57abe19a25d4de1f485c5cbb32c2d5a41c11df3b Mon Sep 17 00:00:00 2001 From: Sindhu Chittireddy <[email protected]> Date: Thu, 1 Oct 2026 15:36:46 -0700 Subject: [PATCH 07/13] Add immintrin.h coverage for the host-derived sse/sse2 features Check that the x86 intrinsic headers parse and compile for a SPIR-V device with an x86-64 MSVC host, that dropping sse/sse2 reinstates the always_inline target feature errors, and that intrinsics lowering to x86 builtins stay unavailable to device code. --- .../spirv-host-adaptation-immintrin.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp diff --git a/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp b/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp new file mode 100644 index 00000000000000..7871f1b704170c --- /dev/null +++ b/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp @@ -0,0 +1,48 @@ +/// Check that the x86 intrinsic headers parse and compile for a SPIR-V device +/// when the auxiliary host target is x86-64 MSVC, which is what the sse/sse2 +/// device features derived from that host are for. + +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fsycl-is-device -ffreestanding -emit-llvm -o - %s | FileCheck %s + +/// With sse/sse2 disabled, the always_inline intrinsics cannot be inlined into +/// device code; this is the failure the derived features prevent. +/// Codegen stops at the first failing function, so only add() is diagnosed. +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fsycl-is-device -ffreestanding -target-feature -sse -target-feature -sse2 \ +// RUN: -emit-llvm -verify=no-sse -o - %s + +/// Intrinsics that lower to x86 builtins are rejected for the device. +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fsycl-is-device -ffreestanding -DX86_BUILTIN -emit-llvm -verify=x86-builtin \ +// RUN: -o - %s + +#include <immintrin.h> + +// CHECK-LABEL: define {{.*}}spir_func noundef float @_Z3addff +[[clang::sycl_external]] float add(float x, float y) { + // no-sse-error@+1 {{always_inline function '_mm_set1_ps' requires target feature 'sse', but would be inlined into function 'add' that is compiled without support for 'sse'}} + __m128 a = _mm_set1_ps(x); + // no-sse-error@+1 {{always_inline function '_mm_set1_ps' requires target feature 'sse', but would be inlined into function 'add' that is compiled without support for 'sse'}} + __m128 b = _mm_set1_ps(y); + // CHECK: fadd <4 x float> + // no-sse-error@+1 {{always_inline function '_mm_add_ps' requires target feature 'sse', but would be inlined into function 'add' that is compiled without support for 'sse'}} + __m128 c = _mm_add_ps(a, b); + return c[0]; +} + +// CHECK-LABEL: define {{.*}}spir_func void @_Z4sqrtPd +[[clang::sycl_external]] void sqrt(double *p) { + __m128d a = _mm_set1_pd(*p); + // CHECK: call {{.*}}<2 x double> @llvm.sqrt.v2f64 + __m128d b = _mm_sqrt_pd(a); + _mm_storeu_pd(p, b); +} + +#ifdef X86_BUILTIN +// The diagnostic is reported in the intrinsic header, not here. +// x86-builtin-error@*:* {{cannot compile this builtin function yet}} +[[clang::sycl_external]] __m128i madd(__m128i a, __m128i b) { + return _mm_madd_epi16(a, b); +} +#endif >From 3f0eafc0e862f5a597e9f28424caf2871db13c76 Mon Sep 17 00:00:00 2001 From: Sindhu Chittireddy <[email protected]> Date: Fri, 2 Oct 2026 08:36:34 -0700 Subject: [PATCH 08/13] Don't derive sse/sse2 from an ARM64EC host The device gets its __SSE__/__SSE2__ predefines from the host, and AArch64 defines neither, so the x86 intrinsic headers are unusable with an ARM64EC host whether or not the features are set. Limit the condition to x86-64 MSVC hosts. --- clang/docs/ReleaseNotes.md | 4 ++-- clang/lib/Basic/Targets/SPIR.cpp | 7 +++---- clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 511333c11fbbce..0d43d5a5ebd38b 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1092,8 +1092,8 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use #### Improvements - SPIR and SPIR-V targets now enable `sse` and `sse2` by default when the host - target is x86-64 or ARM64EC in an MSVC environment, as the MSVC STL headers - declare `always_inline` `_mm_*` intrinsics that require them. An explicit + target is x86-64 in an MSVC environment, as the MSVC STL headers declare + `always_inline` `_mm_*` intrinsics that require them. An explicit `-target-feature` still overrides the default. ## Additional Information diff --git a/clang/lib/Basic/Targets/SPIR.cpp b/clang/lib/Basic/Targets/SPIR.cpp index 79a12ce2b20d80..06467ee8ecfcd0 100644 --- a/clang/lib/Basic/Targets/SPIR.cpp +++ b/clang/lib/Basic/Targets/SPIR.cpp @@ -113,12 +113,11 @@ void SPIRV64TargetInfo::getTargetDefines(const LangOptions &Opts, bool BaseSPIRTargetInfo::initFeatureMap( llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU, const std::vector<std::string> &FeaturesVec) const { - // On x86-64 and ARM64EC MSVC hosts, the STL headers use always_inline _mm_* - // intrinsics, which require sse/sse2 in the device feature set. + // On x86-64 MSVC hosts, the STL headers use always_inline _mm_* intrinsics, + // which require sse/sse2 in the device feature set. if (const TargetInfo *Host = getHostTarget()) { const llvm::Triple &HT = Host->getTriple(); - if (HT.isWindowsMSVCEnvironment() && - (HT.getArch() == llvm::Triple::x86_64 || HT.isWindowsArm64EC())) { + if (HT.isWindowsMSVCEnvironment() && HT.getArch() == llvm::Triple::x86_64) { Features["sse"] = true; Features["sse2"] = true; } diff --git a/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp b/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp index 9decbbc736f5ba..ffcc96d7a6ed26 100644 --- a/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp +++ b/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp @@ -4,8 +4,6 @@ // RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s // RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ // RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s -// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple arm64ec-pc-windows-msvc \ -// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s // RUN: %clang_cc1 -triple spir-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ // RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s // RUN: %clang_cc1 -triple spirv32-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ @@ -21,6 +19,8 @@ /// No sse/sse2 unless the host is 64-bit x86 MSVC // RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple aarch64-pc-windows-msvc \ // RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple arm64ec-pc-windows-msvc \ +// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s // RUN: %clang_cc1 -triple spirv32-unknown-unknown -aux-triple i386-pc-windows-msvc \ // RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s // RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-unknown-linux-gnu \ >From 9d24075c58cbf012785d2de5acc1daa2f65d810b Mon Sep 17 00:00:00 2001 From: Sindhu Chittireddy <[email protected]> Date: Fri, 2 Oct 2026 11:45:58 -0700 Subject: [PATCH 09/13] Undo switch case --- clang/lib/Basic/Targets/SPIR.h | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h index 579e6549d20eb8..fdc420cd790ede 100644 --- a/clang/lib/Basic/Targets/SPIR.h +++ b/clang/lib/Basic/Targets/SPIR.h @@ -185,13 +185,7 @@ class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo { } CallingConvCheckResult checkCallingConvention(CallingConv CC) const override { - switch (CC) { - case CC_C: - case CC_DeviceKernel: - return CCCR_OK; - default: - return CCCR_Warning; - } + return (CC == CC_C || CC == CC_DeviceKernel) ? CCCR_OK : CCCR_Warning; } bool >From c8693222de4516de3ea7329c9998fce1cbb9d72f Mon Sep 17 00:00:00 2001 From: Sindhu Chittireddy <[email protected]> Date: Tue, 27 Oct 2026 09:12:15 -0700 Subject: [PATCH 10/13] [clang][SYCL] Update immintrin test for the host target builtin diagnostic Host target builtins in device code are now diagnosed in Sema instead of failing in CodeGen. --- clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp b/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp index 7871f1b704170c..dcbb905ae83333 100644 --- a/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp +++ b/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp @@ -41,8 +41,9 @@ #ifdef X86_BUILTIN // The diagnostic is reported in the intrinsic header, not here. -// x86-builtin-error@*:* {{cannot compile this builtin function yet}} +// x86-builtin-error@*:* {{builtin is not supported on this target}} [[clang::sycl_external]] __m128i madd(__m128i a, __m128i b) { + // x86-builtin-note@+1 {{called by 'madd'}} return _mm_madd_epi16(a, b); } #endif >From befb465419f68ee5335907095da530f684bec386 Mon Sep 17 00:00:00 2001 From: Sindhu Chittireddy <[email protected]> Date: Fri, 13 Nov 2026 12:10:45 -0800 Subject: [PATCH 11/13] Do not enable sse/sse2 for x86-64 MSVC hosts Enabling sse/sse2 for the device let Clang's always_inline x86 intrinsics compile into SPIR-V device code instead of being diagnosed, so remove it and add a test for MSVC's extern intrinsic declarations. --- clang/docs/ReleaseNotes.md | 5 --- clang/lib/Basic/Targets/SPIR.cpp | 15 ------- clang/lib/Basic/Targets/SPIR.h | 5 --- .../spirv-host-adaptation-features.cpp | 43 ------------------- .../spirv-host-adaptation-immintrin.cpp | 33 ++++---------- ...pirv-host-adaptation-msvc-intrin-decls.cpp | 28 ++++++++++++ 6 files changed, 37 insertions(+), 92 deletions(-) delete mode 100644 clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp create mode 100644 clang/test/CodeGenSYCL/spirv-host-adaptation-msvc-intrin-decls.cpp diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 09823b34b91e5e..1b555228135c9d 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1510,11 +1510,6 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use #### Improvements -- SPIR and SPIR-V targets now enable `sse` and `sse2` by default when the host - target is x86-64 in an MSVC environment, as the MSVC STL headers declare - `always_inline` `_mm_*` intrinsics that require them. An explicit - `-target-feature` still overrides the default. - ### Clang IR Improvements - Clang builds now build ClangIR support by default. Clang IR is expected to work with most Linux/X86-64 workloads. Normal builds are unchanged, however those who wish to test out the experimental `-fclangir` flag can now do so with a Clang build. diff --git a/clang/lib/Basic/Targets/SPIR.cpp b/clang/lib/Basic/Targets/SPIR.cpp index 06467ee8ecfcd0..3eb62c9051b58c 100644 --- a/clang/lib/Basic/Targets/SPIR.cpp +++ b/clang/lib/Basic/Targets/SPIR.cpp @@ -110,21 +110,6 @@ void SPIRV64TargetInfo::getTargetDefines(const LangOptions &Opts, DefineStd(Builder, "SPIRV64", Opts); } -bool BaseSPIRTargetInfo::initFeatureMap( - llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU, - const std::vector<std::string> &FeaturesVec) const { - // On x86-64 MSVC hosts, the STL headers use always_inline _mm_* intrinsics, - // which require sse/sse2 in the device feature set. - if (const TargetInfo *Host = getHostTarget()) { - const llvm::Triple &HT = Host->getTriple(); - if (HT.isWindowsMSVCEnvironment() && HT.getArch() == llvm::Triple::x86_64) { - Features["sse"] = true; - Features["sse2"] = true; - } - } - return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec); -} - static const AMDGPUTargetInfo AMDGPUTI(llvm::Triple(llvm::Triple::amdgpu, llvm::Triple::NoSubArch, llvm::Triple::AMD, llvm::Triple::AMDHSA), diff --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h index b32ef9161e5cb8..43a94cb9d31c89 100644 --- a/clang/lib/Basic/Targets/SPIR.h +++ b/clang/lib/Basic/Targets/SPIR.h @@ -188,11 +188,6 @@ class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo { return (CC == CC_C || CC == CC_DeviceKernel) ? CCCR_OK : CCCR_Warning; } - bool - initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, - StringRef CPU, - const std::vector<std::string> &FeaturesVec) const override; - void setAddressSpaceMap(bool DefaultIsGeneric) { AddrSpaceMap = DefaultIsGeneric ? &SPIRDefIsGenMap : &SPIRDefIsPrivMap; } diff --git a/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp b/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp deleted file mode 100644 index ffcc96d7a6ed26..00000000000000 --- a/clang/test/CodeGenSYCL/spirv-host-adaptation-features.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/// Check the sse/sse2 device features derived from the host target. - -// RUN: %clang_cc1 -triple spir64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ -// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s -// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ -// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s -// RUN: %clang_cc1 -triple spir-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ -// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s -// RUN: %clang_cc1 -triple spirv32-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ -// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=SSE2 %s -/// An explicit -target-feature overrides the derived default. -// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ -// RUN: -target-feature -sse2 -fsycl-is-device -emit-llvm -o - %s \ -// RUN: | FileCheck --check-prefix=NO-SSE2-FLAG %s -// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ -// RUN: -target-feature -sse -fsycl-is-device -emit-llvm -o - %s \ -// RUN: | FileCheck --check-prefix=NO-SSE-FLAG %s - -/// No sse/sse2 unless the host is 64-bit x86 MSVC -// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple aarch64-pc-windows-msvc \ -// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s -// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple arm64ec-pc-windows-msvc \ -// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s -// RUN: %clang_cc1 -triple spirv32-unknown-unknown -aux-triple i386-pc-windows-msvc \ -// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s -// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-unknown-linux-gnu \ -// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s -// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-gnu \ -// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s -// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-uefi \ -// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s -// RUN: %clang_cc1 -triple spirv64-unknown-unknown \ -// RUN: -fsycl-is-device -emit-llvm -o - %s | FileCheck --check-prefix=NO-SSE2 %s - -[[clang::sycl_external]] void test() {} - -// SSE2: define {{.*}}spir_func void @_Z4testv() #[[ATTR:[0-9]+]] -// SSE2: attributes #[[ATTR]] = {{{.*}}"target-features"="+sse,+sse2" - -// NO-SSE2-NOT: "target-features" - -// NO-SSE2-FLAG: attributes #{{[0-9]+}} = {{{.*}}"target-features"="+sse,-sse2" -// NO-SSE-FLAG: attributes #{{[0-9]+}} = {{{.*}}"target-features"="+sse2,-sse" diff --git a/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp b/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp index dcbb905ae83333..d4d017fbf590b0 100644 --- a/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp +++ b/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp @@ -1,16 +1,10 @@ -/// Check that the x86 intrinsic headers parse and compile for a SPIR-V device -/// when the auxiliary host target is x86-64 MSVC, which is what the sse/sse2 -/// device features derived from that host are for. +/// Check that use of Clang's x86 intrinsics in SPIR-V device code is diagnosed +/// at compile time when the auxiliary host target is x86-64 MSVC. +/// The always_inline intrinsics require sse/sse2, which are not enabled for the +/// device. // RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ -// RUN: -fsycl-is-device -ffreestanding -emit-llvm -o - %s | FileCheck %s - -/// With sse/sse2 disabled, the always_inline intrinsics cannot be inlined into -/// device code; this is the failure the derived features prevent. -/// Codegen stops at the first failing function, so only add() is diagnosed. -// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ -// RUN: -fsycl-is-device -ffreestanding -target-feature -sse -target-feature -sse2 \ -// RUN: -emit-llvm -verify=no-sse -o - %s +// RUN: -fsycl-is-device -ffreestanding -emit-llvm -verify=no-sse -o - %s /// Intrinsics that lower to x86 builtins are rejected for the device. // RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ @@ -19,31 +13,22 @@ #include <immintrin.h> -// CHECK-LABEL: define {{.*}}spir_func noundef float @_Z3addff +#ifndef X86_BUILTIN [[clang::sycl_external]] float add(float x, float y) { // no-sse-error@+1 {{always_inline function '_mm_set1_ps' requires target feature 'sse', but would be inlined into function 'add' that is compiled without support for 'sse'}} __m128 a = _mm_set1_ps(x); // no-sse-error@+1 {{always_inline function '_mm_set1_ps' requires target feature 'sse', but would be inlined into function 'add' that is compiled without support for 'sse'}} __m128 b = _mm_set1_ps(y); - // CHECK: fadd <4 x float> // no-sse-error@+1 {{always_inline function '_mm_add_ps' requires target feature 'sse', but would be inlined into function 'add' that is compiled without support for 'sse'}} __m128 c = _mm_add_ps(a, b); return c[0]; } - -// CHECK-LABEL: define {{.*}}spir_func void @_Z4sqrtPd -[[clang::sycl_external]] void sqrt(double *p) { - __m128d a = _mm_set1_pd(*p); - // CHECK: call {{.*}}<2 x double> @llvm.sqrt.v2f64 - __m128d b = _mm_sqrt_pd(a); - _mm_storeu_pd(p, b); -} - -#ifdef X86_BUILTIN +#else // The diagnostic is reported in the intrinsic header, not here. // x86-builtin-error@*:* {{builtin is not supported on this target}} [[clang::sycl_external]] __m128i madd(__m128i a, __m128i b) { - // x86-builtin-note@+1 {{called by 'madd'}} + // x86-builtin-note@+2 {{called by 'madd'}} + // x86-builtin-error@+1 {{always_inline function '_mm_madd_epi16' requires target feature 'sse2', but would be inlined into function 'madd' that is compiled without support for 'sse2'}} return _mm_madd_epi16(a, b); } #endif diff --git a/clang/test/CodeGenSYCL/spirv-host-adaptation-msvc-intrin-decls.cpp b/clang/test/CodeGenSYCL/spirv-host-adaptation-msvc-intrin-decls.cpp new file mode 100644 index 00000000000000..78cce63cbbc3f1 --- /dev/null +++ b/clang/test/CodeGenSYCL/spirv-host-adaptation-msvc-intrin-decls.cpp @@ -0,0 +1,28 @@ +/// The MSVC intrinsic headers declare most _mm_* intrinsics as plain extern +/// functions, which Clang does not treat as builtins. Device code that calls +/// them is not diagnosed and leaves unresolved external references. + +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fsycl-is-device -fms-extensions -emit-llvm -verify -o - %s | FileCheck %s + +// expected-no-diagnostics + +typedef union __declspec(align(16)) __m128 { + float m128_f32[4]; +} __m128; + +extern "C" { +extern __m128 __cdecl _mm_set_ps1(float); +extern __m128 __cdecl _mm_add_ps(__m128, __m128); +} +#define _mm_set1_ps _mm_set_ps1 + +// CHECK: call spir_func {{.*}}@_mm_set_ps1( +// CHECK: call spir_func {{.*}}@_mm_add_ps( +[[clang::sycl_external]] float add(float x) { + __m128 a = _mm_set1_ps(x); + return _mm_add_ps(a, a).m128_f32[0]; +} + +// CHECK: declare spir_func {{.*}}@_mm_set_ps1( +// CHECK: declare spir_func {{.*}}@_mm_add_ps( >From 32caf52620df3e19b121fd33b78cd8d937db62ac Mon Sep 17 00:00:00 2001 From: Sindhu Chittireddy <[email protected]> Date: Fri, 13 Nov 2026 12:45:01 -0800 Subject: [PATCH 12/13] Diagnose calls to host-only target functions in device code Diagnose calls from SYCL device code to functions whose target attribute requires host target features or a host CPU, even if those features are enabled for the device. --- .../clang/Basic/DiagnosticSemaKinds.td | 3 ++ clang/lib/Sema/SemaChecking.cpp | 16 +++++++++ .../spirv-host-adaptation-immintrin.cpp | 22 ++++++++----- .../test/SemaSYCL/sycl-device-target-attr.cpp | 33 +++++++++++++++++++ 4 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 clang/test/SemaSYCL/sycl-device-target-attr.cpp diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 3eba313f0b0e6e..b2e0d0d162afc3 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -9820,6 +9820,9 @@ def warn_kern_is_inline : Warning< def err_variadic_device_fn : Error< "%enum_select<OffloadLang>{%CUDA{CUDA}|%SYCL{SYCL}}0 device code does not " "support variadic functions">; +def err_sycl_device_target_attr : Error< + "SYCL device code cannot call %0, which requires " + "%select{the '%2' host feature|a '%2' host}1">; def err_va_arg_in_device : Error< "CUDA device code does not support va_arg">; def err_alias_not_supported_on_nvptx : Error<"CUDA older than 10.0 does not support .alias">; diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index cbadd5cf6fb0c0..29cbab5a34ea2a 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -4872,6 +4872,22 @@ void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto, SYCL().DiagIfDeviceCode(Loc, diag::err_variadic_device_fn) << diag::OffloadLang::SYCL; + // Functions requiring host target features or a host CPU are host-only. + if (FD && getLangOpts().SYCLIsDevice && !isUnevaluatedContext()) + if (const auto *TA = FD->getAttr<TargetAttr>()) + if (const TargetInfo *Aux = Context.getAuxTargetInfo()) { + ParsedTargetAttr P = Aux->parseTargetAttr(TA->getFeaturesStr()); + auto It = llvm::find_if(P.Features, [&](StringRef F) { + return F.consume_front("+") && Aux->isValidFeatureName(F); + }); + if (It != P.Features.end()) + SYCL().DiagIfDeviceCode(Loc, diag::err_sycl_device_target_attr) + << FD << 0 << StringRef(*It).drop_front(); + else if (!P.CPU.empty() && Aux->isValidCPUName(P.CPU)) + SYCL().DiagIfDeviceCode(Loc, diag::err_sycl_device_target_attr) + << FD << 1 << P.CPU; + } + if (FD) diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc); } diff --git a/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp b/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp index d4d017fbf590b0..e562ba907bc7c8 100644 --- a/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp +++ b/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp @@ -1,25 +1,31 @@ /// Check that use of Clang's x86 intrinsics in SPIR-V device code is diagnosed -/// at compile time when the auxiliary host target is x86-64 MSVC. +/// at compile time when the auxiliary host target is x86-64 MSVC, including +/// when sse/sse2 are enabled explicitly for the device. -/// The always_inline intrinsics require sse/sse2, which are not enabled for the -/// device. +/// The intrinsics require host target features, so they are host-only. // RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ -// RUN: -fsycl-is-device -ffreestanding -emit-llvm -verify=no-sse -o - %s +// RUN: -fsycl-is-device -ffreestanding -emit-llvm -verify=sse -o - %s +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fsycl-is-device -ffreestanding -target-feature +sse -target-feature +sse2 \ +// RUN: -emit-llvm -verify=sse -o - %s /// Intrinsics that lower to x86 builtins are rejected for the device. // RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ // RUN: -fsycl-is-device -ffreestanding -DX86_BUILTIN -emit-llvm -verify=x86-builtin \ // RUN: -o - %s +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fsycl-is-device -ffreestanding -target-feature +sse -target-feature +sse2 \ +// RUN: -DX86_BUILTIN -emit-llvm -verify=x86-builtin -o - %s #include <immintrin.h> #ifndef X86_BUILTIN [[clang::sycl_external]] float add(float x, float y) { - // no-sse-error@+1 {{always_inline function '_mm_set1_ps' requires target feature 'sse', but would be inlined into function 'add' that is compiled without support for 'sse'}} + // sse-error@+1 {{SYCL device code cannot call '_mm_set1_ps', which requires the 'sse' host feature}} __m128 a = _mm_set1_ps(x); - // no-sse-error@+1 {{always_inline function '_mm_set1_ps' requires target feature 'sse', but would be inlined into function 'add' that is compiled without support for 'sse'}} + // sse-error@+1 {{SYCL device code cannot call '_mm_set1_ps', which requires the 'sse' host feature}} __m128 b = _mm_set1_ps(y); - // no-sse-error@+1 {{always_inline function '_mm_add_ps' requires target feature 'sse', but would be inlined into function 'add' that is compiled without support for 'sse'}} + // sse-error@+1 {{SYCL device code cannot call '_mm_add_ps', which requires the 'sse' host feature}} __m128 c = _mm_add_ps(a, b); return c[0]; } @@ -28,7 +34,7 @@ // x86-builtin-error@*:* {{builtin is not supported on this target}} [[clang::sycl_external]] __m128i madd(__m128i a, __m128i b) { // x86-builtin-note@+2 {{called by 'madd'}} - // x86-builtin-error@+1 {{always_inline function '_mm_madd_epi16' requires target feature 'sse2', but would be inlined into function 'madd' that is compiled without support for 'sse2'}} + // x86-builtin-error@+1 {{SYCL device code cannot call '_mm_madd_epi16', which requires the 'sse2' host feature}} return _mm_madd_epi16(a, b); } #endif diff --git a/clang/test/SemaSYCL/sycl-device-target-attr.cpp b/clang/test/SemaSYCL/sycl-device-target-attr.cpp new file mode 100644 index 00000000000000..10d955042268d5 --- /dev/null +++ b/clang/test/SemaSYCL/sycl-device-target-attr.cpp @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -fsycl-is-device -triple spirv64-unknown-unknown \ +// RUN: -aux-triple x86_64-pc-windows-msvc -fsyntax-only -verify %s +// RUN: %clang_cc1 -fsycl-is-device -triple spirv64-unknown-unknown \ +// RUN: -aux-triple x86_64-unknown-linux-gnu -fsyntax-only -verify %s +// RUN: %clang_cc1 -fsycl-is-device -triple spirv64-unknown-unknown \ +// RUN: -aux-triple x86_64-unknown-linux-gnu -target-feature +sse2 \ +// RUN: -fsyntax-only -verify %s + +// Check that functions requiring host target features or a host CPU are +// diagnosed only when called from code that is reachable from device code. + +__attribute__((target("sse2"))) int sse2_add(int x) { return x; } +__attribute__((target("arch=haswell"))) int haswell_add(int x) { return x; } + +[[clang::sycl_external]] int direct(int x) { + // expected-error@+2 {{SYCL device code cannot call 'sse2_add', which requires the 'sse2' host feature}} + // expected-error@+1 {{SYCL device code cannot call 'haswell_add', which requires a 'haswell' host}} + return sse2_add(x) + haswell_add(x); +} + +inline int helper(int x) { + // expected-error@+1 {{SYCL device code cannot call 'sse2_add', which requires the 'sse2' host feature}} + return sse2_add(x); +} + +// expected-note@+1 {{called by 'indirect'}} +[[clang::sycl_external]] int indirect(int x) { return helper(x); } + +// Not reachable from device code. +int host_only(int x) { return sse2_add(x) + haswell_add(x); } + +// Not evaluated. +[[clang::sycl_external]] int unevaluated(int x) { return sizeof(sse2_add(x)); } >From c83a20b29158ecf632ee1902f271dd9f591214b8 Mon Sep 17 00:00:00 2001 From: Sindhu Chittireddy <[email protected]> Date: Mon, 16 Nov 2026 14:06:28 -0800 Subject: [PATCH 13/13] Replace the MSVC intrinsic declarations test Clang requires its own intrinsic headers, so emulating the MSVC ones tests an unsupported configuration. Instead, test that intrinsics unreachable from device code are not diagnosed. --- .../spirv-host-adaptation-immintrin.cpp | 5 ++++ ...pirv-host-adaptation-msvc-intrin-decls.cpp | 28 ------------------- 2 files changed, 5 insertions(+), 28 deletions(-) delete mode 100644 clang/test/CodeGenSYCL/spirv-host-adaptation-msvc-intrin-decls.cpp diff --git a/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp b/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp index e562ba907bc7c8..dae9c1a9da5712 100644 --- a/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp +++ b/clang/test/CodeGenSYCL/spirv-host-adaptation-immintrin.cpp @@ -19,6 +19,11 @@ #include <immintrin.h> +// Not reachable from device code, as in MSVC STL headers that use the +// intrinsics on the host only. +inline float unused(float x) { return _mm_set1_ps(x)[0]; } +float host_only(float x) { return _mm_add_ps(_mm_set1_ps(x), _mm_set1_ps(x))[0]; } + #ifndef X86_BUILTIN [[clang::sycl_external]] float add(float x, float y) { // sse-error@+1 {{SYCL device code cannot call '_mm_set1_ps', which requires the 'sse' host feature}} diff --git a/clang/test/CodeGenSYCL/spirv-host-adaptation-msvc-intrin-decls.cpp b/clang/test/CodeGenSYCL/spirv-host-adaptation-msvc-intrin-decls.cpp deleted file mode 100644 index 78cce63cbbc3f1..00000000000000 --- a/clang/test/CodeGenSYCL/spirv-host-adaptation-msvc-intrin-decls.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/// The MSVC intrinsic headers declare most _mm_* intrinsics as plain extern -/// functions, which Clang does not treat as builtins. Device code that calls -/// them is not diagnosed and leaves unresolved external references. - -// RUN: %clang_cc1 -triple spirv64-unknown-unknown -aux-triple x86_64-pc-windows-msvc \ -// RUN: -fsycl-is-device -fms-extensions -emit-llvm -verify -o - %s | FileCheck %s - -// expected-no-diagnostics - -typedef union __declspec(align(16)) __m128 { - float m128_f32[4]; -} __m128; - -extern "C" { -extern __m128 __cdecl _mm_set_ps1(float); -extern __m128 __cdecl _mm_add_ps(__m128, __m128); -} -#define _mm_set1_ps _mm_set_ps1 - -// CHECK: call spir_func {{.*}}@_mm_set_ps1( -// CHECK: call spir_func {{.*}}@_mm_add_ps( -[[clang::sycl_external]] float add(float x) { - __m128 a = _mm_set1_ps(x); - return _mm_add_ps(a, a).m128_f32[0]; -} - -// CHECK: declare spir_func {{.*}}@_mm_set_ps1( -// CHECK: declare spir_func {{.*}}@_mm_add_ps( _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
