Skip to content

[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

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3acc848
Adding C23 constexpr math functions fmin and frexp.
zahiraam Apr 16, 2024
d7050b5
Addressed review comments.
zahiraam Apr 22, 2024
625028a
Responded to review comments.
zahiraam May 1, 2024
319fdd0
Added code to store the exponent in Pointer. Edited LIT tests
zahiraam May 8, 2024
da06dba
Added fmax function and function IsinRange function.
zahiraam May 9, 2024
1793cf0
Addressed review comments.
zahiraam May 10, 2024
42d7d4c
Added a fix for failing LIT test.
zahiraam May 15, 2024
9f4d632
Fixed aix-builtin-mapping.c test.
zahiraam May 17, 2024
efeec9b
Merge remote-tracking branch 'origin/main' into C23ConstExpr
zahiraam May 17, 2024
e384a05
Addressed review comments.
zahiraam Jun 11, 2024
5f67645
Put back CodeGen/aix-builtin-mapping.c how it was originally written.
zahiraam Jun 11, 2024
49186b9
Fix build issue in flang.
zahiraam Jul 26, 2024
cf2814d
Merge remote-tracking branch 'origin/main' into C23ConstExpr
zahiraam Jul 26, 2024
1c2912c
Merge remote-tracking branch 'origin/main' into C23ConstExpr
zahiraam Jul 26, 2024
d32737e
Rewrote isInFrexpResultRange according to review comments and fixed
zahiraam Jul 26, 2024
bd536f5
Fix format.
zahiraam Jul 26, 2024
32b788a
Addressed missed review comments.
zahiraam Jul 29, 2024
501e39a
Addressed review comments. Remove StoreExponent function and fixed LIT
zahiraam Aug 5, 2024
0cc61eb
Replaced ConstexpSince by Constexpr for FrexpF16F128.
zahiraam Aug 13, 2024
c615b22
Removed builtin attribute ConstExprSince and replaced it with a member
zahiraam Aug 22, 2024
5eec2db
Fix format.
zahiraam Aug 22, 2024
299df9a
Fix LIT failures.
zahiraam Aug 23, 2024
1507a33
Merge remote-tracking branch 'origin/main' into C23ConstExpr
zahiraam Aug 23, 2024
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
18 changes: 18 additions & 0 deletions clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2036,6 +2036,10 @@ class FunctionDecl : public DeclaratorDecl,
/// the DeclaratorDecl base class.
DeclarationNameLoc DNLoc;

/// Represents the language version since this function has been declared
/// constexpr.
clang::LangStandard::Kind ConstexprSinceVersion;

/// Specify that this function declaration is actually a function
/// template specialization.
///
Expand Down Expand Up @@ -2151,6 +2155,20 @@ class FunctionDecl : public DeclaratorDecl,

void setDeclarationNameLoc(DeclarationNameLoc L) { DNLoc = L; }

/// Set the language standard version at which this builtin became constexpr.
void setConstexprBuiltinSinceVersion(IdentifierInfo *builtinIdentifier);

/// Get the language standard version at which this builtin became constexpr.
clang::LangStandard::Kind getConstexprBuiltinSinceVersion() const {
return ConstexprSinceVersion;
}

/// Set the version of the language standard at which this declaration became
/// constexpr.
void setConstexprSinceVersion(clang::LangStandard::Kind LangVersion) {
ConstexprSinceVersion = LangVersion;
}

/// Returns the location of the ellipsis of a variadic function.
SourceLocation getEllipsisLoc() const {
const auto *FPT = getType()->getAs<FunctionProtoType>();
Expand Down
11 changes: 10 additions & 1 deletion clang/include/clang/Basic/Builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef LLVM_CLANG_BASIC_BUILTINS_H
#define LLVM_CLANG_BASIC_BUILTINS_H

#include "LangStandard.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
Expand Down Expand Up @@ -74,6 +75,7 @@ struct Info {
const char *Features;
HeaderDesc Header;
LanguageID Langs;
LangStandard UnprefixedOnlyConstexprSince;
};

/// Holds information about both target-independent and
Expand Down Expand Up @@ -161,6 +163,12 @@ class Context {
return strchr(getRecord(ID).Attributes, 'f') != nullptr;
}

clang::LangStandard::Kind isBuiltinConstant(unsigned ID) const {
return strchr(getRecord(ID).Attributes, 'O') != nullptr
? LangStandard::lang_cxx23
: LangStandard::lang_cxx20; // Not constexpr
}

/// Returns true if this builtin requires appropriate header in other
/// compilers. In Clang it will work even without including it, but we can emit
/// a warning about missing header.
Expand Down Expand Up @@ -277,7 +285,8 @@ class Context {

/// Return true if this function can be constant evaluated by Clang frontend.
bool isConstantEvaluated(unsigned ID) const {
return strchr(getRecord(ID).Attributes, 'E') != nullptr;
return (strchr(getRecord(ID).Attributes, 'E') != nullptr ||
strchr(getRecord(ID).Attributes, 'O') != nullptr);
}

/// Returns true if this is an immediate (consteval) function
Expand Down
20 changes: 11 additions & 9 deletions clang/include/clang/Basic/Builtins.td
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"> {
Expand All @@ -3496,9 +3497,10 @@ def Fmod : FPMathTemplate, LibBuiltin<"math.h"> {

def Frexp : FPMathTemplate, LibBuiltin<"math.h"> {
Copy link
Collaborator

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.

Copy link
Contributor Author

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 class LIBBUILTIN 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.

let Spellings = ["frexp"];
let Attributes = [NoThrow];
let Attributes = [NoThrow, Constexpr];
Copy link
Collaborator

Choose a reason for hiding this comment

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

The Constexpr here makes the non-__builtin_-prefixed version of the function constexpr under all language levels.
I checked the code: The OnlyBuiltinPrefixedAliasIsConstexpr flag can add constexpr-ness; it does not restrict it.

For reference:

if (Builtin->getValueAsBit("OnlyBuiltinPrefixedAliasIsConstexpr"))
OS << 'E';

We may need a new builtin attribute that expresses constexpr for the non-prefixed functions only for C++23 and up (but we might end up with C++26-and-up, etc. in the future). However, it seems making the non-prefixed functions actually non-constexpr (as opposed to simply failing to evaluate) is only a QoI concern (see #88978 (comment) for more context).

@philnik777, thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

This also influences __has_constexpr_builtin(fmin), which would return true but then fail to actually evaluate during a constant expression, which doesn't seem that great. IMO we'd probably want to introduce the concept of "constexpr since C++xy".

Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO we'd probably want to introduce the concept of "constexpr since C++xy".

@philnik777, are you envisioning something where LangOptions are passed into Builtin::Context::isConstantEvaluated?

bool isConstantEvaluated(unsigned ID) const {

I think the existing Constexpr/"E" attribute can be left alone for use in "always constexpr" cases.

For the selectively-constexpr cases, LibBuiltin in clang/include/clang/Basic/BuiltinsBase.td should have OnlyBuiltinPrefixedAliasIsConstexpr renamed to BuiltinPrefixedAliasIsConstexpr. Then adding a new string property for UnprefixedOnlyConstexprSince makes sense to me. It can be encoded into Builtin::Info (and I think using the LangFeatures enumeration from "clang/Basic/LangStandard.h" is not terribly wrong).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

let Prototype = "T(T, int*)";
let AddBuiltinPrefixedAlias = 1;
let BuiltinPrefixedAliasIsConstexpr = 1;
}

def Ldexp : FPMathTemplate, LibBuiltin<"math.h"> {
Expand All @@ -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"> {
Expand Down Expand Up @@ -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];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment re: making the non-prefixed versions constexpr only for C++23 and up.

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];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment re: making the non-prefixed versions constexpr only for C++23 and up.

let Prototype = "T(T, T)";
let AddBuiltinPrefixedAlias = 1;
let OnlyBuiltinPrefixedAliasIsConstexpr = 1;
let BuiltinPrefixedAliasIsConstexpr = 1;
}

def Hypot : FPMathTemplate, LibBuiltin<"math.h"> {
Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/Basic/BuiltinsBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Builtin {
// On some platforms, some functions are actually macros. In that case we need
// to #undef them.
bit RequiresUndef = 0;
bit BuiltinPrefixedAliasIsConstexpr = 0;
}

class CustomEntry {
Expand All @@ -99,7 +100,7 @@ class LibBuiltin<string header, string languages = "ALL_LANGUAGES"> : Builtin {
string Header = header;
string Languages = languages;
bit AddBuiltinPrefixedAlias = 0;
bit OnlyBuiltinPrefixedAliasIsConstexpr = 0;
bit BuiltinPrefixedAliasIsConstexpr = 0;
}

class MSLibBuiltin<string header> : LibBuiltin<header, "ALL_MS_LANGUAGES">;
Expand Down
20 changes: 20 additions & 0 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_set>

using namespace clang;

Expand Down Expand Up @@ -3066,6 +3067,25 @@ FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC,
setTrailingRequiresClause(TrailingRequiresClause);
}

void FunctionDecl::setConstexprBuiltinSinceVersion(IdentifierInfo *I) {
const std::unordered_set<std::string> ConstexprFunctions = {
"__builtin_fmax", "__builtin_fmaxf", "__builtin_fmaxl",
"__builtin_fmin", "__builtin_fminf", "__builtin_fminl"};
const std::unordered_set<std::string> ConstexprFunctionsFrexp = {
"__builtin_frexp", "__builtin_frexpf", "__builtin_frexpl",
"__builtin_frexpf16", "__builtin_frexpf128"};
std::string BuiltinName = I->getName().str();
if (ConstexprFunctions.count(BuiltinName)) {
setConstexprSinceVersion(getLangOpts().CPlusPlus23
? LangStandard::lang_cxx23
: getLangOpts().LangStd);
} else if (ConstexprFunctionsFrexp.count(BuiltinName)) {
setConstexprSinceVersion(LangStandard::lang_cxx23);
} else {
setConstexprSinceVersion(getLangOpts().LangStd);
}
}

void FunctionDecl::getNameForDiagnostic(
raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Expand Down
56 changes: 56 additions & 0 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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:
Copy link
Collaborator

Choose a reason for hiding this comment

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

The non-__builtin_-prefixed cases need to be added in as per @philnik777's comment (#88978 (comment)). Same for the other two function families.

Some more thought is needed on how to handle the non-__builtin_-prefixed cases under non-C++23-or-higher language modes. The specific implications of those functions being non-constexpr under said modes (as required, for C++, by https://wg21.link/constexpr.functions) may determine the solution to apply in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the non-`_builtin prefixed cases.

case Builtin::BI__builtin_frexpf:
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
Copy link
Contributor

@cor3ntin cor3ntin May 13, 2025

Choose a reason for hiding this comment

The 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 FunctionDecl, which has a huge cost in terms of AST size.

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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@cor3ntin, you mean the __builtin_-prefixed form, right?

Copy link
Contributor

Choose a reason for hiding this comment

The 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

  • that we table-gen the information
  • that the version is encoded as an attribute (it might just be an implementation detail attribute) - so as to not bloat the AST
  • that we make it a diagnostic that users can turn into an error or not (and then we can discuss what the default should be)

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:
Expand Down Expand Up @@ -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))
Expand All @@ -15042,12 +15090,20 @@ bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
return true;
}

case Builtin::BIfmin:
case Builtin::BIfminf:
Comment on lines +15093 to +15094
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't make these evaluate in constexpr. Not only does this extend C (not a stated intent of the patch), it will cause accidental dependencies that break when -fno-builtin is used.

Copy link
Contributor

Choose a reason for hiding this comment

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

These have to be made constexpr, at least in C++23. C++ stdlibs can't override them to make them call the __builtin_ versions, so Clang has to handle that.

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 constexpr-ness to C++23 and up. So we do need that restriction.

The interaction with -fno-builtin[-*] may need more discussion. At least the documentation for that option would need to be updated if the C++23 constexpr math would break with it.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 -fno-builtin[-*] should remain capable of disabling built-in treatment even when that treatment would be required for conformance w.r.t. constexpr behaviour of functions shared with C.

For me, the first expectation for this PR (on that subject) is an update to the documentation of -fno-builtin[-*].

@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++)?

Copy link
Contributor

Choose a reason for hiding this comment

The 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))
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,7 @@ FunctionDecl *Sema::CreateBuiltin(IdentifierInfo *II, QualType Type,
Type->isFunctionProtoType(), ConstexprKind);
New->setImplicit();
New->addAttr(BuiltinAttr::CreateImplicit(Context, ID));
New->setConstexprBuiltinSinceVersion(II);

// Create Decl objects for each parameter, adding them to the
// FunctionDecl.
Expand Down
Loading