Skip to content

Commit

Permalink
[clang][Interp] Implement __builtin_isnan()
Browse files Browse the repository at this point in the history
The previous version was using llvm::reverse(CallExpr::arguments()),
which causes problems when clang is compiled with GCC.

Differential Revision: https://reviews.llvm.org/D155369
  • Loading branch information
tbaederr committed Jul 29, 2023
1 parent 7dbc7b1 commit ff80fc0
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
9 changes: 8 additions & 1 deletion clang/lib/AST/Interp/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
//===----------------------------------------------------------------------===//

#include "Function.h"
#include "Program.h"
#include "Opcode.h"
#include "Program.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/Basic/Builtins.h"

using namespace clang;
using namespace clang::interp;
Expand Down Expand Up @@ -47,3 +48,9 @@ bool Function::isVirtual() const {
return M->isVirtual();
return false;
}

bool Function::needsRuntimeArgPop(const ASTContext &Ctx) const {
if (!isBuiltin())
return false;
return Ctx.BuiltinInfo.hasCustomTypechecking(getBuiltinID());
}
6 changes: 6 additions & 0 deletions clang/lib/AST/Interp/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ class Function final {

unsigned getBuiltinID() const { return F->getBuiltinID(); }

bool isBuiltin() const { return F->getBuiltinID() != 0; }

/// Does this function need its arguments to be classified at runtime
/// rather than at bytecode-compile-time?
bool needsRuntimeArgPop(const ASTContext &Ctx) const;

unsigned getNumParams() const { return ParamTypes.size(); }

unsigned getParamOffset(unsigned ParamIndex) const {
Expand Down
13 changes: 13 additions & 0 deletions clang/lib/AST/Interp/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ static bool CheckGlobal(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
namespace clang {
namespace interp {

bool popBuiltinArgs(InterpState &S, CodePtr OpPC) {
assert(S.Current && S.Current->getFunction()->needsRuntimeArgPop(S.getCtx()));
const Expr *E = S.Current->getExpr(OpPC);
assert(isa<CallExpr>(E));
const CallExpr *CE = cast<CallExpr>(E);
for (int32_t I = CE->getNumArgs() - 1; I >= 0; --I) {
const Expr *A = CE->getArg(I);
PrimType Ty = S.getContext().classify(A->getType()).value_or(PT_Ptr);
TYPE_SWITCH(Ty, S.Stk.discard<T>());
}
return true;
}

bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
if (!Ptr.isExtern())
return true;
Expand Down
15 changes: 13 additions & 2 deletions clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ enum class ArithOp { Add, Sub };
// Returning values
//===----------------------------------------------------------------------===//

/// Pop arguments of builtins defined as func-name(...).
bool popBuiltinArgs(InterpState &S, CodePtr OpPC);

template <PrimType Name, class T = typename PrimConv<Name>::T>
bool Ret(InterpState &S, CodePtr &PC, APValue &Result) {
const T &Ret = S.Stk.pop<T>();
Expand All @@ -197,8 +200,16 @@ bool Ret(InterpState &S, CodePtr &PC, APValue &Result) {
}

assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
if (!S.checkingPotentialConstantExpression() || S.Current->Caller)
S.Current->popArgs();
if (!S.checkingPotentialConstantExpression() || S.Current->Caller) {
// Certain builtin functions are declared as func-name(...), so the
// parameters are checked in Sema and only available through the CallExpr.
// The interp::Function we create for them has 0 parameters, so we need to
// remove them from the stack by checking the CallExpr.
if (S.Current && S.Current->getFunction()->needsRuntimeArgPop(S.getCtx()))
popBuiltinArgs(S, PC);
else
S.Current->popArgs();
}

if (InterpFrame *Caller = S.Current->Caller) {
PC = S.Current->getRetPC();
Expand Down
16 changes: 16 additions & 0 deletions clang/lib/AST/Interp/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ static bool interp__builtin_fmin(InterpState &S, CodePtr OpPC,
return true;
}

/// Defined as __builtin_isnan(...), to accommodate the fact that it can
/// take a float, double, long double, etc.
/// But for us, that's all a Floating anyway.
static bool interp__builtin_isnan(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame, const Function *F) {
const Floating &Arg = S.Stk.peek<Floating>();

S.Stk.push<Integral<32, true>>(Integral<32, true>::from(Arg.isNan()));
return true;
}

bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F) {
InterpFrame *Frame = S.Current;
APValue Dummy;
Expand Down Expand Up @@ -223,6 +234,11 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F) {
return Ret<PT_Float>(S, OpPC, Dummy);
break;

case Builtin::BI__builtin_isnan:
if (interp__builtin_isnan(S, OpPC, Frame, F))
return Ret<PT_Sint32>(S, OpPC, Dummy);
break;

default:
return false;
}
Expand Down
1 change: 1 addition & 0 deletions clang/test/Sema/constant-builtins-fmin.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s
// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s
// expected-no-diagnostics

constexpr double NaN = __builtin_nan("");
Expand Down

0 comments on commit ff80fc0

Please sign in to comment.