https://github.com/bassiounix updated https://github.com/llvm/llvm-project/pull/134214
>From 620bdbed2b372b97b66147684d4ded5b666c7786 Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Thu, 3 Apr 2025 10:08:59 +0200 Subject: [PATCH 01/17] add `bf16`/`BF16` suffix support --- clang/include/clang/Lex/LiteralSupport.h | 1 + clang/lib/Lex/LiteralSupport.cpp | 17 ++++++++++++++++- clang/lib/Sema/SemaExpr.cpp | 2 ++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Lex/LiteralSupport.h b/clang/include/clang/Lex/LiteralSupport.h index ea5f63bc20399..1907cfc365d97 100644 --- a/clang/include/clang/Lex/LiteralSupport.h +++ b/clang/include/clang/Lex/LiteralSupport.h @@ -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 diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp index 20933cc8dee69..ab0d301a70fbd 100644 --- a/clang/lib/Lex/LiteralSupport.cpp +++ b/clang/lib/Lex/LiteralSupport.cpp @@ -917,6 +917,7 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, isFloat = false; isImaginary = false; isFloat16 = false; + isBFloat16 = false; isFloat128 = false; MicrosoftInteger = 0; isFract = false; @@ -973,11 +974,20 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, bool isFPConstant = isFloatingLiteral(); bool HasSize = false; bool DoubleUnderscore = false; + bool isBF16 = false; // Loop over all of the characters of the suffix. If we see something bad, // we break out of the loop. for (; s != ThisTokEnd; ++s) { switch (*s) { + case 'b': + case 'B': + if (isBFloat16) break; + if (isBF16) break; + if (HasSize) break; + + isBF16 = true; + continue; case 'R': case 'r': if (!LangOpts.FixedPoint) @@ -1022,7 +1032,11 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, (LangOpts.OpenMPIsTargetDevice && Target.getTriple().isNVPTX())) && s + 2 < ThisTokEnd && s[1] == '1' && s[2] == '6') { s += 2; // success, eat up 2 characters. - isFloat16 = true; + if (isBF16) { + isBFloat16 = true; + } else { + isFloat16 = true; + } continue; } @@ -1183,6 +1197,7 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, isSizeT = false; isFloat = false; isFloat16 = false; + isBFloat16 = false; isHalf = false; isImaginary = false; isBitInt = false; diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 3af6d6c23438f..b4210fc22ae52 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -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) >From d55e0ca50eb50ec4a2fdb0e7ba9e3c11be70065d Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Thu, 3 Apr 2025 10:09:22 +0200 Subject: [PATCH 02/17] add `__bf16` macros --- clang/lib/Frontend/InitPreprocessor.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index 0b54665501c76..9bca74a8b4bd6 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -96,7 +96,7 @@ static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP, template <typename T> static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal, T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, - T IEEEQuadVal) { + T BFloatVal, T IEEEQuadVal) { if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEhalf()) return IEEEHalfVal; if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle()) @@ -107,6 +107,8 @@ static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal, return X87DoubleExtendedVal; if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble()) return PPCDoubleDoubleVal; + if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::BFloat()) + return BFloatVal; assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad()); return IEEEQuadVal; } @@ -117,29 +119,34 @@ static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, NormMax = PickFP(Sem, "6.5504e+4", "3.40282347e+38", "1.7976931348623157e+308", "1.18973149535723176502e+4932", "8.98846567431157953864652595394501e+307", + "3.38953138925153547590470800371487867e+38", "1.18973149535723176508575932662800702e+4932"); DenormMin = PickFP(Sem, "5.9604644775390625e-8", "1.40129846e-45", "4.9406564584124654e-324", "3.64519953188247460253e-4951", "4.94065645841246544176568792868221e-324", + "9.18354961579912115600575419704879436e-41", "6.47517511943802511092443895822764655e-4966"); - int Digits = PickFP(Sem, 3, 6, 15, 18, 31, 33); - int DecimalDigits = PickFP(Sem, 5, 9, 17, 21, 33, 36); + int Digits = PickFP(Sem, 3, 6, 15, 18, 31, 2, 33); + int DecimalDigits = PickFP(Sem, 5, 9, 17, 21, 33, 4, 36); Epsilon = PickFP(Sem, "9.765625e-4", "1.19209290e-7", "2.2204460492503131e-16", "1.08420217248550443401e-19", "4.94065645841246544176568792868221e-324", + "7.81250000000000000000000000000000000e-3", "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); + int MantissaDigits = PickFP(Sem, 11, 24, 53, 64, 106, 8, 113); + int Min10Exp = PickFP(Sem, -4, -37, -307, -4931, -291, -37, -4931); + int Max10Exp = PickFP(Sem, 4, 38, 308, 4932, 308, 38, 4932); + int MinExp = PickFP(Sem, -13, -125, -1021, -16381, -968, -125, -16381); + int MaxExp = PickFP(Sem, 16, 128, 1024, 16384, 1024, 128, 16384); Min = PickFP(Sem, "6.103515625e-5", "1.17549435e-38", "2.2250738585072014e-308", "3.36210314311209350626e-4932", "2.00416836000897277799610805135016e-292", + "1.17549435082228750796873653722224568e-38", "3.36210314311209350626267781732175260e-4932"); Max = PickFP(Sem, "6.5504e+4", "3.40282347e+38", "1.7976931348623157e+308", "1.18973149535723176502e+4932", "1.79769313486231580793728971405301e+308", + "3.38953138925153547590470800371487867e+38", "1.18973149535723176508575932662800702e+4932"); SmallString<32> DefPrefix; @@ -1250,6 +1257,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"); >From d0b9ecd3f3a5347eba484485705ed01a96bbf18e Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Thu, 3 Apr 2025 10:17:59 +0200 Subject: [PATCH 03/17] fix formatting --- clang/lib/Frontend/InitPreprocessor.cpp | 6 +++--- clang/lib/Lex/LiteralSupport.cpp | 9 ++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index 9bca74a8b4bd6..8bb91a8654836 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -107,7 +107,7 @@ static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal, return X87DoubleExtendedVal; if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble()) return PPCDoubleDoubleVal; - if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::BFloat()) + if (Sem == (const llvm::fltSemantics *)&llvm::APFloat::BFloat()) return BFloatVal; assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad()); return IEEEQuadVal; @@ -138,8 +138,8 @@ static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, int Max10Exp = PickFP(Sem, 4, 38, 308, 4932, 308, 38, 4932); int MinExp = PickFP(Sem, -13, -125, -1021, -16381, -968, -125, -16381); int MaxExp = PickFP(Sem, 16, 128, 1024, 16384, 1024, 128, 16384); - Min = PickFP(Sem, "6.103515625e-5", "1.17549435e-38", "2.2250738585072014e-308", - "3.36210314311209350626e-4932", + Min = PickFP(Sem, "6.103515625e-5", "1.17549435e-38", + "2.2250738585072014e-308", "3.36210314311209350626e-4932", "2.00416836000897277799610805135016e-292", "1.17549435082228750796873653722224568e-38", "3.36210314311209350626267781732175260e-4932"); diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp index ab0d301a70fbd..bdfb7575b96cb 100644 --- a/clang/lib/Lex/LiteralSupport.cpp +++ b/clang/lib/Lex/LiteralSupport.cpp @@ -982,9 +982,12 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, switch (*s) { case 'b': case 'B': - if (isBFloat16) break; - if (isBF16) break; - if (HasSize) break; + if (isBFloat16) + break; + if (isBF16) + break; + if (HasSize) + break; isBF16 = true; continue; >From 94244f4a0945b9b20d4b6aad15ff9011549477b3 Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Thu, 3 Apr 2025 22:38:53 +0200 Subject: [PATCH 04/17] chore(style): fix redundant style --- clang/lib/Frontend/InitPreprocessor.cpp | 3 +-- clang/lib/Lex/LiteralSupport.cpp | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index 8bb91a8654836..8d8c115297eab 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -130,8 +130,7 @@ static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, int DecimalDigits = PickFP(Sem, 5, 9, 17, 21, 33, 4, 36); Epsilon = PickFP(Sem, "9.765625e-4", "1.19209290e-7", "2.2204460492503131e-16", "1.08420217248550443401e-19", - "4.94065645841246544176568792868221e-324", - "7.81250000000000000000000000000000000e-3", + "4.94065645841246544176568792868221e-324", "7.8125e-3", "1.92592994438723585305597794258492732e-34"); int MantissaDigits = PickFP(Sem, 11, 24, 53, 64, 106, 8, 113); int Min10Exp = PickFP(Sem, -4, -37, -307, -4931, -291, -37, -4931); diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp index bdfb7575b96cb..7620a1fc3cfd8 100644 --- a/clang/lib/Lex/LiteralSupport.cpp +++ b/clang/lib/Lex/LiteralSupport.cpp @@ -1035,11 +1035,10 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, (LangOpts.OpenMPIsTargetDevice && Target.getTriple().isNVPTX())) && s + 2 < ThisTokEnd && s[1] == '1' && s[2] == '6') { s += 2; // success, eat up 2 characters. - if (isBF16) { + if (isBF16) isBFloat16 = true; - } else { + else isFloat16 = true; - } continue; } >From 5b2589826087b8ff3e483dd82af07cc2039c0c88 Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Thu, 3 Apr 2025 22:57:58 +0200 Subject: [PATCH 05/17] chore: reorder `PickFP` arguments --- clang/lib/Frontend/InitPreprocessor.cpp | 48 +++++++++++++------------ 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index 8d8c115297eab..25199cb605558 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -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 BFloatVal, 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()) @@ -107,8 +109,6 @@ static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal, return X87DoubleExtendedVal; if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble()) return PPCDoubleDoubleVal; - if (Sem == (const llvm::fltSemantics *)&llvm::APFloat::BFloat()) - return BFloatVal; assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad()); return IEEEQuadVal; } @@ -116,36 +116,40 @@ 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", + NormMax = PickFP(Sem, "6.5504e+4", + "3.38953138925153547590470800371487867e+38", + "3.40282347e+38", "1.7976931348623157e+308", "1.18973149535723176502e+4932", "8.98846567431157953864652595394501e+307", - "3.38953138925153547590470800371487867e+38", "1.18973149535723176508575932662800702e+4932"); - DenormMin = PickFP(Sem, "5.9604644775390625e-8", "1.40129846e-45", + DenormMin = PickFP(Sem, "5.9604644775390625e-8", + "9.18354961579912115600575419704879436e-41", + "1.40129846e-45", "4.9406564584124654e-324", "3.64519953188247460253e-4951", "4.94065645841246544176568792868221e-324", - "9.18354961579912115600575419704879436e-41", "6.47517511943802511092443895822764655e-4966"); - int Digits = PickFP(Sem, 3, 6, 15, 18, 31, 2, 33); - int DecimalDigits = PickFP(Sem, 5, 9, 17, 21, 33, 4, 36); - Epsilon = PickFP(Sem, "9.765625e-4", "1.19209290e-7", + 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", "7.8125e-3", + "4.94065645841246544176568792868221e-324", "1.92592994438723585305597794258492732e-34"); - int MantissaDigits = PickFP(Sem, 11, 24, 53, 64, 106, 8, 113); - int Min10Exp = PickFP(Sem, -4, -37, -307, -4931, -291, -37, -4931); - int Max10Exp = PickFP(Sem, 4, 38, 308, 4932, 308, 38, 4932); - int MinExp = PickFP(Sem, -13, -125, -1021, -16381, -968, -125, -16381); - int MaxExp = PickFP(Sem, 16, 128, 1024, 16384, 1024, 128, 16384); - Min = PickFP(Sem, "6.103515625e-5", "1.17549435e-38", + 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", - "1.17549435082228750796873653722224568e-38", "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", - "3.38953138925153547590470800371487867e+38", "1.18973149535723176508575932662800702e+4932"); SmallString<32> DefPrefix; >From 2c62638f56cfc1dd629797025382ee2fdc77eabc Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Thu, 3 Apr 2025 23:32:16 +0200 Subject: [PATCH 06/17] fix(bf suffix): fix float literal suffix support --- clang/lib/Lex/LiteralSupport.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp index 7620a1fc3cfd8..543fe26f486a4 100644 --- a/clang/lib/Lex/LiteralSupport.cpp +++ b/clang/lib/Lex/LiteralSupport.cpp @@ -974,7 +974,6 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, bool isFPConstant = isFloatingLiteral(); bool HasSize = false; bool DoubleUnderscore = false; - bool isBF16 = false; // Loop over all of the characters of the suffix. If we see something bad, // we break out of the loop. @@ -982,15 +981,25 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, switch (*s) { case 'b': case 'B': + if (!isFPConstant) break; // Error for integer constant. if (isBFloat16) break; - if (isBF16) + if (!Target.hasBFloat16Type()) break; if (HasSize) break; + HasSize = true; - isBF16 = true; - continue; + if ((Target.hasBFloat16Type() || LangOpts.CUDA || + (LangOpts.OpenMPIsTargetDevice && Target.getTriple().isNVPTX())) && + s + 2 < 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; case 'R': case 'r': if (!LangOpts.FixedPoint) @@ -1035,10 +1044,7 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, (LangOpts.OpenMPIsTargetDevice && Target.getTriple().isNVPTX())) && s + 2 < ThisTokEnd && s[1] == '1' && s[2] == '6') { s += 2; // success, eat up 2 characters. - if (isBF16) - isBFloat16 = true; - else - isFloat16 = true; + isFloat16 = true; continue; } >From 3abe5e751729d1f2863049036d9ec0fab8cacfb2 Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Fri, 4 Apr 2025 01:46:37 +0200 Subject: [PATCH 07/17] chore(docs): add release notes for the macros and bfloat literal suffix --- clang/docs/ReleaseNotes.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 04ec2cfef679c..da04e34d8eeca 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -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 ---------------------------- >From 9e48a497812bc3d825ed0371cd54711bf6c4728f Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Fri, 4 Apr 2025 02:18:36 +0200 Subject: [PATCH 08/17] chore(test): add test for bfloat literal suffix support --- clang/test/Lexer/bfloat-literal.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 clang/test/Lexer/bfloat-literal.cpp diff --git a/clang/test/Lexer/bfloat-literal.cpp b/clang/test/Lexer/bfloat-literal.cpp new file mode 100644 index 0000000000000..f37b055e2a96b --- /dev/null +++ b/clang/test/Lexer/bfloat-literal.cpp @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s +__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 >From 2680e738d0a13cf2bd2083f006ffac63747a69c7 Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Fri, 4 Apr 2025 04:54:39 +0200 Subject: [PATCH 09/17] chore(test): add test for `__bf16`/`BFLT16` predefined macros --- clang/test/Preprocessor/bfloat16-macros.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 clang/test/Preprocessor/bfloat16-macros.c diff --git a/clang/test/Preprocessor/bfloat16-macros.c b/clang/test/Preprocessor/bfloat16-macros.c new file mode 100644 index 0000000000000..9604275b78d33 --- /dev/null +++ b/clang/test/Preprocessor/bfloat16-macros.c @@ -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 >From 3b793b2ceaf0ae8e147158d04120292eafece874 Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Fri, 4 Apr 2025 10:18:59 +0200 Subject: [PATCH 10/17] fix(TargetInfo): save state of the type format after copy --- clang/lib/Basic/Targets/AMDGPU.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clang/lib/Basic/Targets/AMDGPU.cpp b/clang/lib/Basic/Targets/AMDGPU.cpp index a42b4589fb5ac..5b2982ecf47aa 100644 --- a/clang/lib/Basic/Targets/AMDGPU.cpp +++ b/clang/lib/Basic/Targets/AMDGPU.cpp @@ -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; >From ae0cf842f3a8bd06bb174b12f21dbc1dc0af223c Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Fri, 4 Apr 2025 10:26:05 +0200 Subject: [PATCH 11/17] chore(style): fix code style --- clang/lib/Frontend/InitPreprocessor.cpp | 32 +++++++++++-------------- clang/lib/Lex/LiteralSupport.cpp | 3 ++- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index 25199cb605558..1a4885dd17ca0 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -95,8 +95,8 @@ static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP, /// specified FP model. template <typename T> static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T BFloatVal, - T IEEESingleVal, T IEEEDoubleVal, T X87DoubleExtendedVal, - T PPCDoubleDoubleVal, T IEEEQuadVal) { + 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()) @@ -116,18 +116,16 @@ static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T BFloatVal, 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.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"); + 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", @@ -140,13 +138,11 @@ static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, 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", + "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.38953138925153547590470800371487867e+38", + Max = PickFP(Sem, "6.5504e+4", "3.38953138925153547590470800371487867e+38", "3.40282347e+38", "1.7976931348623157e+308", "1.18973149535723176502e+4932", "1.79769313486231580793728971405301e+308", diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp index 543fe26f486a4..a12d7442b33e7 100644 --- a/clang/lib/Lex/LiteralSupport.cpp +++ b/clang/lib/Lex/LiteralSupport.cpp @@ -981,7 +981,8 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, switch (*s) { case 'b': case 'B': - if (!isFPConstant) break; // Error for integer constant. + if (!isFPConstant) + break; // Error for integer constant. if (isBFloat16) break; if (!Target.hasBFloat16Type()) >From 621d5a323646b2840b08de80a054f70b7da2c209 Mon Sep 17 00:00:00 2001 From: Muhammad Bassiouni <60100307+bassiou...@users.noreply.github.com> Date: Fri, 4 Apr 2025 11:09:56 +0200 Subject: [PATCH 12/17] Update clang/lib/Lex/LiteralSupport.cpp Co-authored-by: OverMighty <its.overmig...@gmail.com> --- clang/lib/Lex/LiteralSupport.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp index a12d7442b33e7..9ade1e21a11bd 100644 --- a/clang/lib/Lex/LiteralSupport.cpp +++ b/clang/lib/Lex/LiteralSupport.cpp @@ -993,7 +993,7 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, if ((Target.hasBFloat16Type() || LangOpts.CUDA || (LangOpts.OpenMPIsTargetDevice && Target.getTriple().isNVPTX())) && - s + 2 < ThisTokEnd && + 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. >From f6a05c3b544e3d9b3dcdea70fd2432a0da08cde2 Mon Sep 17 00:00:00 2001 From: Muhammad Bassiouni <60100307+bassiou...@users.noreply.github.com> Date: Fri, 4 Apr 2025 11:10:14 +0200 Subject: [PATCH 13/17] Update clang/lib/Lex/LiteralSupport.cpp Co-authored-by: OverMighty <its.overmig...@gmail.com> --- clang/lib/Lex/LiteralSupport.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp index 9ade1e21a11bd..9d41494fe18ac 100644 --- a/clang/lib/Lex/LiteralSupport.cpp +++ b/clang/lib/Lex/LiteralSupport.cpp @@ -979,7 +979,7 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, // we break out of the loop. for (; s != ThisTokEnd; ++s) { switch (*s) { - case 'b': + case 'b': // FP Suffix for "__bf16" case 'B': if (!isFPConstant) break; // Error for integer constant. >From c272219e2934e20d309901c06fb1a54edbf40da4 Mon Sep 17 00:00:00 2001 From: Muhammad Bassiouni <60100307+bassiou...@users.noreply.github.com> Date: Fri, 4 Apr 2025 11:12:30 +0200 Subject: [PATCH 14/17] Update clang/lib/Lex/LiteralSupport.cpp Co-authored-by: OverMighty <its.overmig...@gmail.com> --- clang/lib/Lex/LiteralSupport.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp index 9d41494fe18ac..e17e1fde36203 100644 --- a/clang/lib/Lex/LiteralSupport.cpp +++ b/clang/lib/Lex/LiteralSupport.cpp @@ -981,12 +981,10 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, switch (*s) { case 'b': // FP Suffix for "__bf16" case 'B': - if (!isFPConstant) - break; // Error for integer constant. - if (isBFloat16) - break; if (!Target.hasBFloat16Type()) break; + if (!isFPConstant) + break; // Error for integer constant. if (HasSize) break; HasSize = true; >From 33d9adbfdedfe13ac302e5fd26520281954117cf Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Fri, 4 Apr 2025 11:18:17 +0200 Subject: [PATCH 15/17] remove reduntant checks --- clang/lib/Lex/LiteralSupport.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp index e17e1fde36203..a9822f646da23 100644 --- a/clang/lib/Lex/LiteralSupport.cpp +++ b/clang/lib/Lex/LiteralSupport.cpp @@ -989,9 +989,7 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, break; HasSize = true; - if ((Target.hasBFloat16Type() || LangOpts.CUDA || - (LangOpts.OpenMPIsTargetDevice && Target.getTriple().isNVPTX())) && - s + 3 < ThisTokEnd && + if ((Target.hasBFloat16Type()) && 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. >From 3cc63e96810707ec6778b889f29afde9ac36e9f2 Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Fri, 4 Apr 2025 11:36:16 +0200 Subject: [PATCH 16/17] add more bf16 literal suffix tests --- clang/test/Lexer/bfloat-literal.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/clang/test/Lexer/bfloat-literal.cpp b/clang/test/Lexer/bfloat-literal.cpp index f37b055e2a96b..5585b906c5461 100644 --- a/clang/test/Lexer/bfloat-literal.cpp +++ b/clang/test/Lexer/bfloat-literal.cpp @@ -11,3 +11,20 @@ __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}} >From e88f537398f1167080d4b45bb6b0d5539c0d78bb Mon Sep 17 00:00:00 2001 From: bassiounix <muhammad.m.bassio...@gmail.com> Date: Fri, 4 Apr 2025 11:46:51 +0200 Subject: [PATCH 17/17] remove redundant check --- clang/lib/Lex/LiteralSupport.cpp | 2 +- clang/test/Lexer/bfloat-literal.cpp | 43 ++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp index a9822f646da23..d4d9c0755b4bc 100644 --- a/clang/lib/Lex/LiteralSupport.cpp +++ b/clang/lib/Lex/LiteralSupport.cpp @@ -989,7 +989,7 @@ NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, break; HasSize = true; - if ((Target.hasBFloat16Type()) && s + 3 < ThisTokEnd && + 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. diff --git a/clang/test/Lexer/bfloat-literal.cpp b/clang/test/Lexer/bfloat-literal.cpp index 5585b906c5461..41a9dedbef382 100644 --- a/clang/test/Lexer/bfloat-literal.cpp +++ b/clang/test/Lexer/bfloat-literal.cpp @@ -1,4 +1,9 @@ -// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s +// 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}} @@ -28,3 +33,39 @@ __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}} +__bf16 b = 1.bf; // expected-error{{__bf16 is not supported on this target}} +__bf16 c = 1.bf166; // expected-error{{__bf16 is not supported on this target}} +__bf16 d = 1.bf1; // expected-error{{__bf16 is not supported on this target}} + +__bf16 e = 1.B; // expected-error{{__bf16 is not supported on this target}} +__bf16 f = 1.BF; // expected-error{{__bf16 is not supported on this target}} +__bf16 g = 1.BF166; // expected-error{{__bf16 is not supported on this target}} +__bf16 h = 1.BF1; // expected-error{{__bf16 is not supported on this target}} + +__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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits