Skip to content

Conversation

@wenju-he
Copy link
Contributor

@wenju-he wenju-he commented Dec 2, 2025

SPIR/SPIR-V are generic targets. Assume they support __bf16.

(cherry picked from commit c4254cd)

SPIR/SPIR-V are generic targets. Assume they support __bf16.

(cherry picked from commit c4254cd)
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Dec 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 2, 2025

@llvm/pr-subscribers-clang

Author: Wenju He (wenju-he)

Changes

SPIR/SPIR-V are generic targets. Assume they support __bf16.

(cherry picked from commit c4254cd)


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

3 Files Affected:

  • (modified) clang/lib/Basic/Targets/SPIR.h (+4-2)
  • (added) clang/test/CodeGenOpenCL/__bf16.cl (+31)
  • (modified) clang/test/SemaSYCL/bf16.cpp (+4-3)
diff --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h
index c0849b69dcdb3..694053012b86c 100644
--- a/clang/lib/Basic/Targets/SPIR.h
+++ b/clang/lib/Basic/Targets/SPIR.h
@@ -103,6 +103,10 @@ class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo {
     UseAddrSpaceMapMangling = true;
     HasLegalHalfType = true;
     HasFloat16 = true;
+    HasBFloat16 = true;
+    HasFullBFloat16 = true;
+    BFloat16Width = BFloat16Align = 16;
+    BFloat16Format = &llvm::APFloat::BFloat();
     // Define available target features
     // These must be defined in sorted order!
     NoAsmVariants = true;
@@ -397,8 +401,6 @@ class LLVM_LIBRARY_VISIBILITY SPIRV64AMDGCNTargetInfo final
     HalfArgsAndReturns = true;
   }
 
-  bool hasBFloat16Type() const override { return true; }
-
   ArrayRef<const char *> getGCCRegNames() const override;
 
   bool initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
diff --git a/clang/test/CodeGenOpenCL/__bf16.cl b/clang/test/CodeGenOpenCL/__bf16.cl
new file mode 100644
index 0000000000000..a40a795bc1600
--- /dev/null
+++ b/clang/test/CodeGenOpenCL/__bf16.cl
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 %s -cl-std=cl3.0 -emit-llvm -o - -triple spir-unknown-unknown | FileCheck %s
+// RUN: %clang_cc1 %s -cl-std=cl3.0 -emit-llvm -o - -triple spirv64-unknown-unknown | FileCheck %s
+
+kernel void test(global __bf16 *a, global __bf16 *b){
+// CHECK-LABEL: spir_kernel void @test(
+// CHECK: fadd bfloat
+// CHECK: fsub bfloat
+// CHECK: fmul bfloat
+// CHECK: fdiv bfloat
+
+  *b += *a;
+  *b -= *a;
+  *b *= *a;
+  *b /= *a;
+}
+
+typedef __bf16 __bf16v4 __attribute((ext_vector_type(4)));
+
+kernel void test_v4(global __bf16v4 *a, global __bf16v4 *b){
+// CHECK-LABEL: spir_kernel void @test_v4(
+// CHECK: fadd <4 x bfloat>
+// CHECK: fsub <4 x bfloat>
+// CHECK: fmul <4 x bfloat>
+// CHECK: fdiv <4 x bfloat>
+
+  *b += *a;
+  *b -= *a;
+  *b *= *a;
+  *b /= *a;
+}
+
diff --git a/clang/test/SemaSYCL/bf16.cpp b/clang/test/SemaSYCL/bf16.cpp
index d1b4776f34044..2ffb277b4eea1 100644
--- a/clang/test/SemaSYCL/bf16.cpp
+++ b/clang/test/SemaSYCL/bf16.cpp
@@ -1,8 +1,9 @@
 // RUN: %clang_cc1 -triple spir64 -aux-triple x86_64-unknown-linux-gnu -fsycl-is-device -verify -fsyntax-only %s
+// expected-no-diagnostics
 
 template <typename Name, typename Func>
 __attribute__((sycl_kernel)) void kernel(const Func &kernelFunc) {
-  kernelFunc(); // expected-note {{called by 'kernel}}
+  kernelFunc();
 }
 
 void host_ok(void) {
@@ -11,9 +12,9 @@ void host_ok(void) {
 
 int main()
 {  host_ok();
-  __bf16 var; // expected-note {{'var' defined here}}
+  __bf16 var;
   kernel<class variables>([=]() {
-    (void)var; // expected-error {{'var' requires 16 bit size '__bf16' type support, but target 'spir64' does not support it}}
+    (void)var;
     int B = sizeof(__bf16);
   });
 

@wenju-he wenju-he requested review from bader and svenvh December 2, 2025 01:07
@wenju-he
Copy link
Contributor Author

wenju-he commented Dec 2, 2025

This is backport of c4254cd to 20.x branch

@wenju-he wenju-he added this to the LLVM 20.X Release milestone Dec 2, 2025
@zwuis
Copy link
Contributor

zwuis commented Dec 2, 2025

Only 21.x branch is maintained currently.

https://llvm.org/docs/HowToReleaseLLVM.html#annual-release-schedule

@cor3ntin cor3ntin closed this Dec 2, 2025
@wenju-he wenju-he deleted the 20.x-cherry-pick-Support-__bf16-SPIR branch December 2, 2025 12:12
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.

4 participants