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 3 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
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
29 changes: 19 additions & 10 deletions clang/lib/Frontend/InitPreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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;
}
Expand All @@ -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);
Min = PickFP(Sem, "6.103515625e-5", "1.17549435e-38", "2.2250738585072014e-308",
"3.36210314311209350626e-4932",
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;
Expand Down Expand Up @@ -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");
Expand Down
20 changes: 19 additions & 1 deletion 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 @@ -973,11 +974,23 @@ 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)
Expand Down Expand Up @@ -1022,7 +1035,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;
}

Expand Down Expand Up @@ -1183,6 +1200,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
Loading