-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[C++23] [CLANG] Adding C++23 constexpr math functions: fmin, fmax and frexp. #88978
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
base: main
Are you sure you want to change the base?
Changes from all commits
3acc848
d7050b5
625028a
319fdd0
da06dba
1793cf0
42d7d4c
9f4d632
efeec9b
e384a05
5f67645
49186b9
cf2814d
1c2912c
d32737e
bd536f5
32b788a
501e39a
0cc61eb
c615b22
5eec2db
299df9a
1507a33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -253,8 +253,9 @@ def FmodF16F128 : F16F128MathTemplate, Builtin { | |||||||
|
||||||||
def FrexpF16F128 : F16F128MathTemplate, Builtin { | ||||||||
let Spellings = ["__builtin_frexp"]; | ||||||||
let Attributes = [FunctionWithBuiltinPrefix, NoThrow]; | ||||||||
let Attributes = [FunctionWithBuiltinPrefix, NoThrow, Constexpr]; | ||||||||
let Prototype = "T(T, int*)"; | ||||||||
let BuiltinPrefixedAliasIsConstexpr = 1; | ||||||||
} | ||||||||
|
||||||||
def HugeVal : Builtin, FPMathWithF128Template { | ||||||||
|
@@ -3463,15 +3464,15 @@ def Copysign : FPMathTemplate, LibBuiltin<"math.h"> { | |||||||
let Attributes = [NoThrow, Const]; | ||||||||
let Prototype = "T(T, T)"; | ||||||||
let AddBuiltinPrefixedAlias = 1; | ||||||||
let OnlyBuiltinPrefixedAliasIsConstexpr = 1; | ||||||||
let BuiltinPrefixedAliasIsConstexpr = 1; | ||||||||
} | ||||||||
|
||||||||
def Fabs : FPMathTemplate, LibBuiltin<"math.h"> { | ||||||||
let Spellings = ["fabs"]; | ||||||||
let Attributes = [NoThrow, Const]; | ||||||||
let Prototype = "T(T)"; | ||||||||
let AddBuiltinPrefixedAlias = 1; | ||||||||
let OnlyBuiltinPrefixedAliasIsConstexpr = 1; | ||||||||
let BuiltinPrefixedAliasIsConstexpr = 1; | ||||||||
} | ||||||||
|
||||||||
def Finite : FPMathTemplate, GNULibBuiltin<"math.h"> { | ||||||||
|
@@ -3496,9 +3497,10 @@ def Fmod : FPMathTemplate, LibBuiltin<"math.h"> { | |||||||
|
||||||||
def Frexp : FPMathTemplate, LibBuiltin<"math.h"> { | ||||||||
let Spellings = ["frexp"]; | ||||||||
let Attributes = [NoThrow]; | ||||||||
let Attributes = [NoThrow, Constexpr]; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The For reference: llvm-project/clang/utils/TableGen/ClangBuiltinsEmitter.cpp Lines 225 to 226 in 702198f
We may need a new builtin attribute that expresses @philnik777, thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also influences There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@philnik777, are you envisioning something where
I think the existing For the selectively- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||||
let Prototype = "T(T, int*)"; | ||||||||
let AddBuiltinPrefixedAlias = 1; | ||||||||
zahiraam marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
let BuiltinPrefixedAliasIsConstexpr = 1; | ||||||||
} | ||||||||
|
||||||||
def Ldexp : FPMathTemplate, LibBuiltin<"math.h"> { | ||||||||
|
@@ -3520,7 +3522,7 @@ def Nan : FPMathTemplate, LibBuiltin<"math.h"> { | |||||||
let Attributes = [Pure, NoThrow]; | ||||||||
let Prototype = "T(char const*)"; | ||||||||
let AddBuiltinPrefixedAlias = 1; | ||||||||
let OnlyBuiltinPrefixedAliasIsConstexpr = 1; | ||||||||
let BuiltinPrefixedAliasIsConstexpr = 1; | ||||||||
} | ||||||||
|
||||||||
def Pow : FPMathTemplate, LibBuiltin<"math.h"> { | ||||||||
|
@@ -3666,18 +3668,18 @@ def Fma : FPMathTemplate, LibBuiltin<"math.h"> { | |||||||
|
||||||||
def Fmax : FPMathTemplate, LibBuiltin<"math.h"> { | ||||||||
let Spellings = ["fmax"]; | ||||||||
let Attributes = [NoThrow, Const]; | ||||||||
let Attributes = [NoThrow, Const, Constexpr]; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment re: making the non-prefixed versions |
||||||||
let Prototype = "T(T, T)"; | ||||||||
let AddBuiltinPrefixedAlias = 1; | ||||||||
let OnlyBuiltinPrefixedAliasIsConstexpr = 1; | ||||||||
let BuiltinPrefixedAliasIsConstexpr = 1; | ||||||||
} | ||||||||
|
||||||||
def Fmin : FPMathTemplate, LibBuiltin<"math.h"> { | ||||||||
let Spellings = ["fmin"]; | ||||||||
let Attributes = [NoThrow, Const]; | ||||||||
let Attributes = [NoThrow, Const, Constexpr]; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment re: making the non-prefixed versions |
||||||||
let Prototype = "T(T, T)"; | ||||||||
let AddBuiltinPrefixedAlias = 1; | ||||||||
let OnlyBuiltinPrefixedAliasIsConstexpr = 1; | ||||||||
let BuiltinPrefixedAliasIsConstexpr = 1; | ||||||||
} | ||||||||
|
||||||||
def Hypot : FPMathTemplate, LibBuiltin<"math.h"> { | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14942,6 +14942,16 @@ static bool TryEvaluateBuiltinNaN(const ASTContext &Context, | |
|
||
return true; | ||
} | ||
// Checks that the value x is in the range (-1;-0.5], [0.5; 1) | ||
static bool isInFrexpResultRange(const llvm::APFloat &x) { | ||
llvm::APFloat AbsX = abs(x); | ||
|
||
llvm::APFloat half(x.getSemantics(), "0.5"); | ||
llvm::APFloat one(x.getSemantics(), "1.0"); | ||
|
||
return (AbsX.compare(half) != llvm::APFloat::cmpLessThan && | ||
AbsX.compare(one) == llvm::APFloat::cmpLessThan); | ||
} | ||
|
||
bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { | ||
if (!IsConstantEvaluatedBuiltinCall(E)) | ||
|
@@ -14951,6 +14961,36 @@ bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { | |
default: | ||
return false; | ||
|
||
case Builtin::BIfrexp: | ||
case Builtin::BIfrexpf: | ||
case Builtin::BIfrexpl: | ||
case Builtin::BI__builtin_frexp: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The non- Some more thought is needed on how to handle the non- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the non-`_builtin prefixed cases. |
||
case Builtin::BI__builtin_frexpf: | ||
tbaederr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case Builtin::BI__builtin_frexpl: | ||
case Builtin::BI__builtin_frexpf16: | ||
case Builtin::BI__builtin_frexpf128: { | ||
const auto *FDecl = E->getDirectCallee(); | ||
clang::LangStandard::Kind ConstExprSinceVersion = | ||
FDecl->getConstexprBuiltinSinceVersion(); | ||
if (ConstExprSinceVersion > Info.Ctx.getLangOpts().LangStd) | ||
return false; | ||
LValue Pointer; | ||
if (!EvaluateFloat(E->getArg(0), Result, Info) || | ||
!EvaluatePointer(E->getArg(1), Pointer, Info)) | ||
Comment on lines
+14972
to
+14979
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the builtins should be unconditionally constexpr in Clang. And we should certainly not store additional info for that in Instead, the builtin should become constexpr, and libc++ can decide not to make the wrapper constexpr in older language modes, or to have some availability warning in older language modes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cor3ntin, you mean the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hubert-reinterpretcast I think if we wanted to do something about the non-prefixed builtin, I would prefer
|
||
return false; | ||
int FrexpExp; | ||
llvm::RoundingMode RM = getActiveRoundingMode(Info, E); | ||
Result = llvm::frexp(Result, FrexpExp, RM); | ||
assert((Result.isZero() || Result.isNaN() || Result.isInfinity() || | ||
isInFrexpResultRange(Result)) && | ||
"The value is not in the expected range for frexp."); | ||
APValue ExpVal{Result}; | ||
if (!handleAssignment(Info, E, Pointer, | ||
E->getArg(1)->getType()->getPointeeType(), ExpVal)) | ||
return false; | ||
return true; | ||
} | ||
|
||
case Builtin::BI__builtin_huge_val: | ||
case Builtin::BI__builtin_huge_valf: | ||
case Builtin::BI__builtin_huge_vall: | ||
|
@@ -15024,12 +15064,20 @@ bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { | |
return true; | ||
} | ||
|
||
case Builtin::BIfmax: | ||
case Builtin::BIfmaxf: | ||
case Builtin::BIfmaxl: | ||
case Builtin::BI__builtin_fmax: | ||
case Builtin::BI__builtin_fmaxf: | ||
case Builtin::BI__builtin_fmaxl: | ||
case Builtin::BI__builtin_fmaxf16: | ||
case Builtin::BI__builtin_fmaxf128: { | ||
// TODO: Handle sNaN. | ||
const auto *FDecl = E->getDirectCallee(); | ||
clang::LangStandard::Kind ConstExprSinceVersion = | ||
FDecl->getConstexprBuiltinSinceVersion(); | ||
if (ConstExprSinceVersion > Info.Ctx.getLangOpts().LangStd) | ||
return false; | ||
APFloat RHS(0.); | ||
if (!EvaluateFloat(E->getArg(0), Result, Info) || | ||
!EvaluateFloat(E->getArg(1), RHS, Info)) | ||
|
@@ -15042,12 +15090,20 @@ bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { | |
return true; | ||
} | ||
|
||
case Builtin::BIfmin: | ||
case Builtin::BIfminf: | ||
Comment on lines
+15093
to
+15094
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't make these evaluate in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These have to be made
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we'll be missing at least "pedantic" diagnostics if we don't restrict the The interaction with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the Clang C/C++ Language Workgroup call on 2024-05-15, it was agreed that For me, the first expectation for this PR (on that subject) is an update to the documentation of @AaronBallman @ldionne, I am no sure how we want to handle this w.r.t. documenting the C++23 implementation status. Does it go in both the Clang documentation (for the functions shared with C) and the libc++ documentation (for the overloads added by C++)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think anything should happen to the libc++ documentation in this patch. We can update the implementation to behave the same across the different overloads in a follow-up and update our documentation then. |
||
case Builtin::BIfminl: | ||
case Builtin::BI__builtin_fmin: | ||
case Builtin::BI__builtin_fminf: | ||
case Builtin::BI__builtin_fminl: | ||
case Builtin::BI__builtin_fminf16: | ||
case Builtin::BI__builtin_fminf128: { | ||
// TODO: Handle sNaN. | ||
const auto *FDecl = E->getDirectCallee(); | ||
clang::LangStandard::Kind ConstExprSinceVersion = | ||
FDecl->getConstexprBuiltinSinceVersion(); | ||
if (ConstExprSinceVersion > Info.Ctx.getLangOpts().LangStd) | ||
return false; | ||
APFloat RHS(0.); | ||
if (!EvaluateFloat(E->getArg(0), Result, Info) || | ||
!EvaluateFloat(E->getArg(1), RHS, Info)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a separate
FrexpF16F128
that should probably also be updated.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added
UnprefixedOnlyConstexprSince
to the classLIBBUILTIN
so it will be unknow if I add it to this def. I will wait to see if the changes I made were correct and update this rule accordingly.