https://github.com/bassiounix created 
https://github.com/llvm/llvm-project/pull/134214

closes #133660.

I added the suffix support to this pr as the generated macros uses the `BF16` 
suffix.

The only line I found in GCC which we don't seem to support as a macro is 
```c
#define __BFLT16_IS_IEC_60559__ 0
```

we don't have `__*_IS_IEC_60559__` in our macro builder so I skipped it.

cc @AaronBallman  @lntue @jhuber6 

>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 1/2] 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 2/2] 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");

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to