Skip to content

Commit aff6ab9

Browse files
authored
[clang][bytecode] Surround bcp condition with Start/EndSpeculation (#130427)
This is similar to what the current interpreter is doing - the FoldConstant RAII object surrounds the entire HandleConditionalOperator call, which means the condition and both TrueExpr or FalseExpr.
1 parent bac490b commit aff6ab9

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,29 +2309,36 @@ bool Compiler<Emitter>::VisitAbstractConditionalOperator(
23092309
return visitChildExpr(FalseExpr);
23102310
}
23112311

2312+
bool IsBcpCall = false;
2313+
if (const auto *CE = dyn_cast<CallExpr>(Condition->IgnoreParenCasts());
2314+
CE && CE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) {
2315+
IsBcpCall = true;
2316+
}
2317+
23122318
LabelTy LabelEnd = this->getLabel(); // Label after the operator.
23132319
LabelTy LabelFalse = this->getLabel(); // Label for the false expr.
23142320

2321+
if (IsBcpCall) {
2322+
if (!this->emitStartSpeculation(E))
2323+
return false;
2324+
}
2325+
23152326
if (!this->visitBool(Condition))
23162327
return false;
2317-
23182328
if (!this->jumpFalse(LabelFalse))
23192329
return false;
2320-
23212330
if (!visitChildExpr(TrueExpr))
23222331
return false;
2323-
23242332
if (!this->jump(LabelEnd))
23252333
return false;
2326-
23272334
this->emitLabel(LabelFalse);
2328-
23292335
if (!visitChildExpr(FalseExpr))
23302336
return false;
2331-
23322337
this->fallthrough(LabelEnd);
23332338
this->emitLabel(LabelEnd);
23342339

2340+
if (IsBcpCall)
2341+
return this->emitEndSpeculation(E);
23352342
return true;
23362343
}
23372344

clang/test/AST/ByteCode/builtin-constant-p.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,10 @@ template<typename T> constexpr bool bcp(T t) {
5959
}
6060

6161
constexpr intptr_t ptr_to_int(const void *p) {
62-
return __builtin_constant_p(1) ? (intptr_t)p : (intptr_t)p; // expected-note {{cast that performs the conversions of a reinterpret_cast}}
62+
return __builtin_constant_p(1) ? (intptr_t)p : (intptr_t)p;
6363
}
6464

65-
/// This is from test/SemaCXX/builtin-constant-p.cpp, but it makes no sense.
66-
/// ptr_to_int is called before bcp(), so it fails. GCC does not accept this either.
67-
static_assert(bcp(ptr_to_int("foo"))); // expected-error {{not an integral constant expression}} \
68-
// expected-note {{in call to}}
65+
static_assert(bcp(ptr_to_int("foo")));
6966

7067
constexpr bool AndFold(const int &a, const int &b) {
7168
return __builtin_constant_p(a && b);

0 commit comments

Comments
 (0)