Skip to content

[clang] Add __bf16 Type Support Macros With Literal Suffix Support #134214

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,26 @@ DWARF Support in Clang

Floating Point Support in Clang
-------------------------------
- Added 15 ``__BFLT16_`` macros to indicate support of ``__bf16`` type. The new macros are:

+ ``__BFLT16_DECIMAL_DIG__``
+ ``__BFLT16_DENORM_MIN__``
+ ``__BFLT16_DIG__``
+ ``__BFLT16_EPSILON__``
+ ``__BFLT16_HAS_DENORM__``
+ ``__BFLT16_HAS_INFINITY__``
+ ``__BFLT16_HAS_QUIET_NAN__``
+ ``__BFLT16_MANT_DIG__``
+ ``__BFLT16_MAX_10_EXP__``
+ ``__BFLT16_MAX_EXP__``
+ ``__BFLT16_MAX__``
+ ``__BFLT16_MIN_10_EXP__``
+ ``__BFLT16_MIN_EXP__``
+ ``__BFLT16_MIN__``
+ ``__BFLT16_NORM_MAX__``

- Added brain float literal suffix (``1.0bf16`` and ``1.0BF16``).


Fixed Point Support in Clang
----------------------------
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Lex/LiteralSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class NumericLiteralParser {
bool isFloat : 1; // 1.0f
bool isImaginary : 1; // 1.0i
bool isFloat16 : 1; // 1.0f16
bool isBFloat16 : 1; // 1.0bf16
bool isFloat128 : 1; // 1.0q
bool isFract : 1; // 1.0hr/r/lr/uhr/ur/ulr
bool isAccum : 1; // 1.0hk/k/lk/uhk/uk/ulk
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Basic/Targets/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,13 @@ void AMDGPUTargetInfo::setAuxTarget(const TargetInfo *Aux) {
// not supported by AMDGPU. 128-bit floating point format is also not
// supported by AMDGPU. Therefore keep its own format for these two types.
auto SaveLongDoubleFormat = LongDoubleFormat;
auto SaveBFloat16Format = BFloat16Format;
auto SaveFloat128Format = Float128Format;
auto SaveLongDoubleWidth = LongDoubleWidth;
auto SaveLongDoubleAlign = LongDoubleAlign;
copyAuxTarget(Aux);
LongDoubleFormat = SaveLongDoubleFormat;
BFloat16Format = SaveBFloat16Format;
Float128Format = SaveFloat128Format;
LongDoubleWidth = SaveLongDoubleWidth;
LongDoubleAlign = SaveLongDoubleAlign;
Expand Down
52 changes: 30 additions & 22 deletions clang/lib/Frontend/InitPreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP,
/// PickFP - This is used to pick a value based on the FP semantics of the
/// specified FP model.
template <typename T>
static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal,
T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
T IEEEQuadVal) {
static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T BFloatVal,
T IEEESingleVal, T IEEEDoubleVal, T X87DoubleExtendedVal,
T PPCDoubleDoubleVal, T IEEEQuadVal) {
if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEhalf())
return IEEEHalfVal;
if (Sem == (const llvm::fltSemantics *)&llvm::APFloat::BFloat())
return BFloatVal;
if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle())
return IEEESingleVal;
if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble())
Expand All @@ -114,30 +116,34 @@ static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal,
static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix,
const llvm::fltSemantics *Sem, StringRef Ext) {
const char *DenormMin, *NormMax, *Epsilon, *Max, *Min;
NormMax = PickFP(Sem, "6.5504e+4", "3.40282347e+38",
"1.7976931348623157e+308", "1.18973149535723176502e+4932",
"8.98846567431157953864652595394501e+307",
"1.18973149535723176508575932662800702e+4932");
DenormMin = PickFP(Sem, "5.9604644775390625e-8", "1.40129846e-45",
"4.9406564584124654e-324", "3.64519953188247460253e-4951",
"4.94065645841246544176568792868221e-324",
"6.47517511943802511092443895822764655e-4966");
int Digits = PickFP(Sem, 3, 6, 15, 18, 31, 33);
int DecimalDigits = PickFP(Sem, 5, 9, 17, 21, 33, 36);
Epsilon = PickFP(Sem, "9.765625e-4", "1.19209290e-7",
NormMax = PickFP(
Sem, "6.5504e+4", "3.38953138925153547590470800371487867e+38",
"3.40282347e+38", "1.7976931348623157e+308",
"1.18973149535723176502e+4932", "8.98846567431157953864652595394501e+307",
"1.18973149535723176508575932662800702e+4932");
DenormMin = PickFP(
Sem, "5.9604644775390625e-8", "9.18354961579912115600575419704879436e-41",
"1.40129846e-45", "4.9406564584124654e-324",
"3.64519953188247460253e-4951", "4.94065645841246544176568792868221e-324",
"6.47517511943802511092443895822764655e-4966");
int Digits = PickFP(Sem, 3, 2, 6, 15, 18, 31, 33);
int DecimalDigits = PickFP(Sem, 5, 4, 9, 17, 21, 33, 36);
Epsilon = PickFP(Sem, "9.765625e-4", "7.8125e-3", "1.19209290e-7",
"2.2204460492503131e-16", "1.08420217248550443401e-19",
"4.94065645841246544176568792868221e-324",
"1.92592994438723585305597794258492732e-34");
int MantissaDigits = PickFP(Sem, 11, 24, 53, 64, 106, 113);
int Min10Exp = PickFP(Sem, -4, -37, -307, -4931, -291, -4931);
int Max10Exp = PickFP(Sem, 4, 38, 308, 4932, 308, 4932);
int MinExp = PickFP(Sem, -13, -125, -1021, -16381, -968, -16381);
int MaxExp = PickFP(Sem, 16, 128, 1024, 16384, 1024, 16384);
Min = PickFP(Sem, "6.103515625e-5", "1.17549435e-38", "2.2250738585072014e-308",
"3.36210314311209350626e-4932",
int MantissaDigits = PickFP(Sem, 11, 8, 24, 53, 64, 106, 113);
int Min10Exp = PickFP(Sem, -4, -37, -37, -307, -4931, -291, -4931);
int Max10Exp = PickFP(Sem, 4, 38, 38, 308, 4932, 308, 4932);
int MinExp = PickFP(Sem, -13, -125, -125, -1021, -16381, -968, -16381);
int MaxExp = PickFP(Sem, 16, 128, 128, 1024, 16384, 1024, 16384);
Min = PickFP(Sem, "6.103515625e-5",
"1.17549435082228750796873653722224568e-38", "1.17549435e-38",
"2.2250738585072014e-308", "3.36210314311209350626e-4932",
"2.00416836000897277799610805135016e-292",
"3.36210314311209350626267781732175260e-4932");
Max = PickFP(Sem, "6.5504e+4", "3.40282347e+38", "1.7976931348623157e+308",
Max = PickFP(Sem, "6.5504e+4", "3.38953138925153547590470800371487867e+38",
"3.40282347e+38", "1.7976931348623157e+308",
"1.18973149535723176502e+4932",
"1.79769313486231580793728971405301e+308",
"1.18973149535723176508575932662800702e+4932");
Expand Down Expand Up @@ -1250,6 +1256,8 @@ static void InitializePredefinedMacros(const TargetInfo &TI,

if (TI.hasFloat16Type())
DefineFloatMacros(Builder, "FLT16", &TI.getHalfFormat(), "F16");
if (TI.hasBFloat16Type())
DefineFloatMacros(Builder, "BFLT16", &TI.getBFloat16Format(), "BF16");
DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat(), "F");
DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat(), "");
DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat(), "L");
Expand Down
20 changes: 20 additions & 0 deletions clang/lib/Lex/LiteralSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling,
isFloat = false;
isImaginary = false;
isFloat16 = false;
isBFloat16 = false;
isFloat128 = false;
MicrosoftInteger = 0;
isFract = false;
Expand Down Expand Up @@ -978,6 +979,24 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling,
// we break out of the loop.
for (; s != ThisTokEnd; ++s) {
switch (*s) {
case 'b': // FP Suffix for "__bf16"
case 'B':
if (!Target.hasBFloat16Type())
break;
if (!isFPConstant)
break; // Error for integer constant.
if (HasSize)
break;
HasSize = true;

if (s + 3 < ThisTokEnd &&
((s[0] == 'b' && s[1] == 'f') || (s[0] == 'B' && s[1] == 'F')) &&
s[2] == '1' && s[3] == '6') {
s += 3; // success, eat up 3 characters.
isBFloat16 = true;
continue;
}
break;
Comment on lines +982 to +999
Copy link
Contributor

Choose a reason for hiding this comment

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

This is true for C++23. Do we want extension warnings in other language modes @AaronBallman?

As an aside, did anyone benchmark that the pattern we use in this function is actually faster than string_view
comparisons?

Copy link
Collaborator

@AaronBallman AaronBallman Apr 7, 2025

Choose a reason for hiding this comment

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

Yes, we should have extension warnings in other language modes as well as the precompat warning for C++ before C++23.

case 'R':
case 'r':
if (!LangOpts.FixedPoint)
Expand Down Expand Up @@ -1183,6 +1202,7 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling,
isSizeT = false;
isFloat = false;
isFloat16 = false;
isBFloat16 = false;
isHalf = false;
isImaginary = false;
isBitInt = false;
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3878,6 +3878,8 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) {
Ty = !getLangOpts().HLSL ? Context.LongDoubleTy : Context.DoubleTy;
else if (Literal.isFloat16)
Ty = Context.Float16Ty;
else if (Literal.isBFloat16)
Ty = Context.BFloat16Ty;
else if (Literal.isFloat128)
Ty = Context.Float128Ty;
else if (getLangOpts().HLSL)
Expand Down
71 changes: 71 additions & 0 deletions clang/test/Lexer/bfloat-literal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -triple x86_64 -DSUPPORTED %s
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -triple armv7 %s
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -triple armv7 -target-feature +bf16 -DSUPPORTED %s

#ifdef SUPPORTED

__bf16 a = 1.b; // expected-error{{invalid suffix 'b' on floating constant}}
__bf16 b = 1.bf; // expected-error{{invalid suffix 'bf' on floating constant}}
__bf16 c = 1.bf166; // expected-error{{invalid suffix 'bf166' on floating constant}}
__bf16 d = 1.bf1; // expected-error{{invalid suffix 'bf1' on floating constant}}

__bf16 e = 1.B; // expected-error{{invalid suffix 'B' on floating constant}}
__bf16 f = 1.BF; // expected-error{{invalid suffix 'BF' on floating constant}}
__bf16 g = 1.BF166; // expected-error{{invalid suffix 'BF166' on floating constant}}
__bf16 h = 1.BF1; // expected-error{{invalid suffix 'BF1' on floating constant}}

__bf16 i = 1.bf16; // expect-success
__bf16 j = 1.BF16; // expect-success

__bf16 k = 1B; // expected-error{{invalid digit 'B' in decimal constant}}
__bf16 l = 1BF; // expected-error{{invalid digit 'B' in decimal constant}}
__bf16 m = 1BF166; // expected-error{{invalid digit 'B' in decimal constant}}
__bf16 n = 1BF1; // expected-error{{invalid digit 'B' in decimal constant}}

__bf16 o = 1b; // expected-error{{invalid digit 'b' in decimal constant}}
__bf16 p = 1bf; // expected-error{{invalid digit 'b' in decimal constant}}
__bf16 q = 1bf166; // expected-error{{invalid digit 'b' in decimal constant}}
__bf16 r = 1bf1; // expected-error{{invalid digit 'b' in decimal constant}}

__bf16 s = 1bf16; // expected-error{{invalid digit 'b' in decimal constant}}
__bf16 t = 1BF16; // expected-error{{invalid digit 'B' in decimal constant}}

__bf16 u = 1.bf16F16; // expected-error{{invalid suffix 'bf16F16' on floating constant}}
__bf16 v = 1.BF16f16; // expected-error{{invalid suffix 'BF16f16' on floating constant}}
__bf16 w = 1.F16bf16; // expected-error{{invalid suffix 'F16bf16' on floating constant}}

#endif

#ifndef SUPPORTED

__bf16 a = 1.b; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid suffix 'b' on floating constant}}
__bf16 b = 1.bf; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid suffix 'bf' on floating constant}}
__bf16 c = 1.bf166; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid suffix 'bf166' on floating constant}}
__bf16 d = 1.bf1; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid suffix 'bf1' on floating constant}}

__bf16 e = 1.B; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid suffix 'B' on floating constant}}
__bf16 f = 1.BF; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid suffix 'BF' on floating constant}}
__bf16 g = 1.BF166; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid suffix 'BF166' on floating constant}}
__bf16 h = 1.BF1; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid suffix 'BF1' on floating constant}}

__bf16 i = 1.bf16; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid suffix 'bf16' on floating constant}}
__bf16 j = 1.BF16; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid suffix 'BF16' on floating constant}}

__bf16 k = 1B; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid digit 'B' in decimal constant}}
__bf16 l = 1BF; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid digit 'B' in decimal constant}}
__bf16 m = 1BF166; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid digit 'B' in decimal constant}}
__bf16 n = 1BF1; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid digit 'B' in decimal constant}}

__bf16 o = 1b; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid digit 'b' in decimal constant}}
__bf16 p = 1bf; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid digit 'b' in decimal constant}}
__bf16 q = 1bf166; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid digit 'b' in decimal constant}}
__bf16 r = 1bf1; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid digit 'b' in decimal constant}}

__bf16 s = 1bf16; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid digit 'b' in decimal constant}}
__bf16 t = 1BF16; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid digit 'B' in decimal constant}}

__bf16 u = 1.bf16F16; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid suffix 'bf16F16' on floating constant}}
__bf16 v = 1.BF16f16; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid suffix 'BF16f16' on floating constant}}
__bf16 w = 1.F16bf16; // expected-error{{__bf16 is not supported on this target}} expected-error{{invalid suffix 'F16bf16' on floating constant}}

#endif
17 changes: 17 additions & 0 deletions clang/test/Preprocessor/bfloat16-macros.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %clang_cc1 -E -dM -ffreestanding < /dev/null | FileCheck -match-full-lines -check-prefix BFLT16 %s

// BFLT16: #define __BFLT16_DECIMAL_DIG__ 4
// BFLT16: #define __BFLT16_DENORM_MIN__ 9.18354961579912115600575419704879436e-41BF16
// BFLT16: #define __BFLT16_DIG__ 2
// BFLT16: #define __BFLT16_EPSILON__ 7.8125e-3BF16
// BFLT16: #define __BFLT16_HAS_DENORM__ 1
// BFLT16: #define __BFLT16_HAS_INFINITY__ 1
// BFLT16: #define __BFLT16_HAS_QUIET_NAN__ 1
// BFLT16: #define __BFLT16_MANT_DIG__ 8
// BFLT16: #define __BFLT16_MAX_10_EXP__ 38
// BFLT16: #define __BFLT16_MAX_EXP__ 128
// BFLT16: #define __BFLT16_MAX__ 3.38953138925153547590470800371487867e+38BF16
// BFLT16: #define __BFLT16_MIN_10_EXP__ (-37)
// BFLT16: #define __BFLT16_MIN_EXP__ (-125)
// BFLT16: #define __BFLT16_MIN__ 1.17549435082228750796873653722224568e-38BF16
// BFLT16: #define __BFLT16_NORM_MAX__ 3.38953138925153547590470800371487867e+38BF16