[Clang] Support __bf16 type for SPIR/SPIR-V#169012
Merged
Conversation
SPIR/SPIR-V are generic targets. Assume they support __bf16.
Member
|
@llvm/pr-subscribers-clang Author: Wenju He (wenju-he) ChangesSPIR/SPIR-V are generic targets. Assume they support __bf16. Full diff: https://github.com/llvm/llvm-project/pull/169012.diff 3 Files Affected:
diff --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h
index 22b2799518dd0..332bf79e2babd 100644
--- a/clang/lib/Basic/Targets/SPIR.h
+++ b/clang/lib/Basic/Targets/SPIR.h
@@ -108,6 +108,10 @@ class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo {
UseAddrSpaceMapMangling = true;
HasFastHalfType = 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;
@@ -427,9 +431,6 @@ class LLVM_LIBRARY_VISIBILITY SPIRV64AMDGCNTargetInfo final
resetDataLayout("e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-"
"v256:256-v512:512-v1024:1024-n32:64-S32-G1-P4-A0");
- BFloat16Width = BFloat16Align = 16;
- BFloat16Format = &llvm::APFloat::BFloat();
-
HasFastHalfType = true;
HasFloat16 = true;
HalfArgsAndReturns = true;
@@ -437,8 +438,6 @@ class LLVM_LIBRARY_VISIBILITY SPIRV64AMDGCNTargetInfo final
MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
}
- bool hasBFloat16Type() const override { return true; }
-
ArrayRef<const char *> getGCCRegNames() const override;
BuiltinVaListKind getBuiltinVaListKind() const override {
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);
});
|
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR enables __bf16 (bfloat16) type support for SPIR and SPIR-V targets by configuring the base target class with bfloat16 properties and removing target-specific overrides that are now redundant.
Key changes:
- Configured bfloat16 support in the base
BaseSPIRTargetInfoclass, making it available to all SPIR/SPIR-V targets - Removed redundant bfloat16 configuration from
SPIRV64AMDGCNTargetInfosubclass - Updated test expectations to reflect that
__bf16is now supported without diagnostics
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| clang/test/SemaSYCL/bf16.cpp | Updated test expectations to verify __bf16 is now accepted without errors on SPIR targets |
| clang/test/CodeGenOpenCL/__bf16.cl | Added new test validating code generation for __bf16 operations on SPIR/SPIR-V targets |
| clang/lib/Basic/Targets/SPIR.h | Moved bfloat16 configuration to base class and removed redundant subclass overrides |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
🐧 Linux x64 Test Results
|
svenvh
approved these changes
Nov 21, 2025
bader
approved these changes
Nov 21, 2025
aadeshps-mcw
pushed a commit
to aadeshps-mcw/llvm-project
that referenced
this pull request
Nov 26, 2025
SPIR/SPIR-V are generic targets. Assume they support __bf16.
Priyanshu3820
pushed a commit
to Priyanshu3820/llvm-project
that referenced
this pull request
Nov 26, 2025
SPIR/SPIR-V are generic targets. Assume they support __bf16.
wenju-he
added a commit
to wenju-he/llvm-project
that referenced
this pull request
Dec 2, 2025
SPIR/SPIR-V are generic targets. Assume they support __bf16. (cherry picked from commit c4254cd)
wenju-he
added a commit
to wenju-he/llvm-project
that referenced
this pull request
Dec 2, 2025
SPIR/SPIR-V are generic targets. Assume they support __bf16. (cherry picked from commit c4254cd)
wenju-he
added a commit
to wenju-he/llvm-project
that referenced
this pull request
Dec 2, 2025
SPIR/SPIR-V are generic targets. Assume they support __bf16. (cherry picked from commit c4254cd)
wenju-he
added a commit
to wenju-he/llvm-project
that referenced
this pull request
Dec 2, 2025
SPIR/SPIR-V are generic targets. Assume they support __bf16. (cherry picked from commit c4254cd)
wenju-he
added a commit
to wenju-he/llvm-project
that referenced
this pull request
Dec 2, 2025
SPIR/SPIR-V are generic targets. Assume they support __bf16. (cherry picked from commit c4254cd)
augusto2112
pushed a commit
to augusto2112/llvm-project
that referenced
this pull request
Dec 3, 2025
SPIR/SPIR-V are generic targets. Assume they support __bf16.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SPIR/SPIR-V are generic targets. Assume they support __bf16.