Skip to content

Commit 82ed9c0

Browse files
authored
[clang][bytecode][NFC] Remove APValue Result argument where unnecessary (#118199)
This is unneeded in almost all circumstances. We only return an APValue back to clang when the evaluation is finished, and that is always done by an EvalEmitter - which has its own implementation of the Ret instructions.
1 parent 4a07433 commit 82ed9c0

File tree

6 files changed

+17
-30
lines changed

6 files changed

+17
-30
lines changed

clang/lib/AST/ByteCode/Context.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {
3434
if (!Func)
3535
return false;
3636

37-
APValue DummyResult;
38-
if (!Run(Parent, Func, DummyResult))
37+
if (!Run(Parent, Func))
3938
return false;
4039

4140
return Func->isConstexpr();
@@ -213,13 +212,13 @@ const llvm::fltSemantics &Context::getFloatSemantics(QualType T) const {
213212
return Ctx.getFloatTypeSemantics(T);
214213
}
215214

216-
bool Context::Run(State &Parent, const Function *Func, APValue &Result) {
215+
bool Context::Run(State &Parent, const Function *Func) {
217216

218217
{
219218
InterpState State(Parent, *P, Stk, *this);
220219
State.Current = new InterpFrame(State, Func, /*Caller=*/nullptr, CodePtr(),
221220
Func->getArgSize());
222-
if (Interpret(State, Result)) {
221+
if (Interpret(State)) {
223222
assert(Stk.empty());
224223
return true;
225224
}

clang/lib/AST/ByteCode/Context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class Context final {
114114

115115
private:
116116
/// Runs a function.
117-
bool Run(State &Parent, const Function *Func, APValue &Result);
117+
bool Run(State &Parent, const Function *Func);
118118

119119
/// Current compilation context.
120120
ASTContext &Ctx;

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
using namespace clang;
2828
using namespace clang::interp;
2929

30-
static bool RetValue(InterpState &S, CodePtr &Pt, APValue &Result) {
30+
static bool RetValue(InterpState &S, CodePtr &Pt) {
3131
llvm::report_fatal_error("Interpreter cannot return values");
3232
}
3333

@@ -1205,11 +1205,10 @@ bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func,
12051205
InterpFrame *FrameBefore = S.Current;
12061206
S.Current = NewFrame.get();
12071207

1208-
APValue CallResult;
12091208
// Note that we cannot assert(CallResult.hasValue()) here since
12101209
// Ret() above only sets the APValue if the curent frame doesn't
12111210
// have a caller set.
1212-
if (Interpret(S, CallResult)) {
1211+
if (Interpret(S)) {
12131212
NewFrame.release(); // Frame was delete'd already.
12141213
assert(S.Current == FrameBefore);
12151214
return true;
@@ -1270,11 +1269,10 @@ bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
12701269
S.Current = NewFrame.get();
12711270

12721271
InterpStateCCOverride CCOverride(S, Func->getDecl()->isImmediateFunction());
1273-
APValue CallResult;
12741272
// Note that we cannot assert(CallResult.hasValue()) here since
12751273
// Ret() above only sets the APValue if the curent frame doesn't
12761274
// have a caller set.
1277-
if (Interpret(S, CallResult)) {
1275+
if (Interpret(S)) {
12781276
NewFrame.release(); // Frame was delete'd already.
12791277
assert(S.Current == FrameBefore);
12801278
return true;
@@ -1598,7 +1596,7 @@ bool CheckBitCast(InterpState &S, CodePtr OpPC, bool HasIndeterminateBits,
15981596
#if defined(_MSC_VER) && !defined(__clang__) && !defined(NDEBUG)
15991597
#pragma optimize("", off)
16001598
#endif
1601-
bool Interpret(InterpState &S, APValue &Result) {
1599+
bool Interpret(InterpState &S) {
16021600
// The current stack frame when we started Interpret().
16031601
// This is being used by the ops to determine wheter
16041602
// to return from this function and thus terminate

clang/lib/AST/ByteCode/Interp.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ namespace interp {
4141
using APSInt = llvm::APSInt;
4242
using FixedPointSemantics = llvm::FixedPointSemantics;
4343

44-
/// Convert a value to an APValue.
45-
template <typename T>
46-
bool ReturnValue(const InterpState &S, const T &V, APValue &R) {
47-
R = V.toAPValue(S.getASTContext());
48-
return true;
49-
}
50-
5144
/// Checks if the variable has externally defined storage.
5245
bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
5346

@@ -299,7 +292,7 @@ bool CheckFloatResult(InterpState &S, CodePtr OpPC, const Floating &Result,
299292
bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR);
300293

301294
/// Interpreter entry point.
302-
bool Interpret(InterpState &S, APValue &Result);
295+
bool Interpret(InterpState &S);
303296

304297
/// Interpret a builtin function.
305298
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
@@ -321,7 +314,7 @@ void cleanupAfterFunctionCall(InterpState &S, CodePtr OpPC,
321314
const Function *Func);
322315

323316
template <PrimType Name, class T = typename PrimConv<Name>::T>
324-
bool Ret(InterpState &S, CodePtr &PC, APValue &Result) {
317+
bool Ret(InterpState &S, CodePtr &PC) {
325318
const T &Ret = S.Stk.pop<T>();
326319

327320
// Make sure returned pointers are live. We might be trying to return a
@@ -349,13 +342,13 @@ bool Ret(InterpState &S, CodePtr &PC, APValue &Result) {
349342
} else {
350343
delete S.Current;
351344
S.Current = nullptr;
352-
if (!ReturnValue<T>(S, Ret, Result))
353-
return false;
345+
// The topmost frame should come from an EvalEmitter,
346+
// which has its own implementation of the Ret<> instruction.
354347
}
355348
return true;
356349
}
357350

358-
inline bool RetVoid(InterpState &S, CodePtr &PC, APValue &Result) {
351+
inline bool RetVoid(InterpState &S, CodePtr &PC) {
359352
assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
360353

361354
if (!S.checkingPotentialConstantExpression() || S.Current->Caller)

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ static void assignInteger(Pointer &Dest, PrimType ValueT, const APSInt &Value) {
116116
ValueT, { Dest.deref<T>() = T::from(static_cast<T>(Value)); });
117117
}
118118

119-
static bool retPrimValue(InterpState &S, CodePtr OpPC, APValue &Result,
119+
static bool retPrimValue(InterpState &S, CodePtr OpPC,
120120
std::optional<PrimType> &T) {
121121
if (!T)
122-
return RetVoid(S, OpPC, Result);
122+
return RetVoid(S, OpPC);
123123

124124
#define RET_CASE(X) \
125125
case X: \
126-
return Ret<X>(S, OpPC, Result);
126+
return Ret<X>(S, OpPC);
127127
switch (*T) {
128128
RET_CASE(PT_Ptr);
129129
RET_CASE(PT_FnPtr);
@@ -1687,7 +1687,6 @@ static bool interp__builtin_arithmetic_fence(InterpState &S, CodePtr OpPC,
16871687
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
16881688
const CallExpr *Call, uint32_t BuiltinID) {
16891689
const InterpFrame *Frame = S.Current;
1690-
APValue Dummy;
16911690

16921691
std::optional<PrimType> ReturnT = S.getContext().classify(Call);
16931692

@@ -2138,7 +2137,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
21382137
return false;
21392138
}
21402139

2141-
return retPrimValue(S, OpPC, Dummy, ReturnT);
2140+
return retPrimValue(S, OpPC, ReturnT);
21422141
}
21432142

21442143
bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E,

clang/utils/TableGen/ClangOpcodesEmitter.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ void ClangOpcodesEmitter::EmitInterp(raw_ostream &OS, StringRef N,
146146
OS << ", PC";
147147
else
148148
OS << ", OpPC";
149-
if (CanReturn)
150-
OS << ", Result";
151149
for (size_t I = 0, N = Args.size(); I < N; ++I)
152150
OS << ", V" << I;
153151
OS << "))\n";

0 commit comments

Comments
 (0)