Skip to content

Commit b9c4c4c

Browse files
authored
[clang][bytecode] Fix 'if consteval' in non-constant contexts (llvm#104707)
The previous code made this a compile-time decision but it's not.
1 parent bc860b4 commit b9c4c4c

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4385,11 +4385,6 @@ bool Compiler<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
43854385
}
43864386

43874387
template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
4388-
if (IS->isNonNegatedConsteval())
4389-
return visitStmt(IS->getThen());
4390-
if (IS->isNegatedConsteval())
4391-
return IS->getElse() ? visitStmt(IS->getElse()) : true;
4392-
43934388
if (auto *CondInit = IS->getInit())
43944389
if (!visitStmt(CondInit))
43954390
return false;
@@ -4398,8 +4393,19 @@ template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
43984393
if (!visitDeclStmt(CondDecl))
43994394
return false;
44004395

4401-
if (!this->visitBool(IS->getCond()))
4402-
return false;
4396+
// Compile condition.
4397+
if (IS->isNonNegatedConsteval()) {
4398+
if (!this->emitIsConstantContext(IS))
4399+
return false;
4400+
} else if (IS->isNegatedConsteval()) {
4401+
if (!this->emitIsConstantContext(IS))
4402+
return false;
4403+
if (!this->emitInv(IS))
4404+
return false;
4405+
} else {
4406+
if (!this->visitBool(IS->getCond()))
4407+
return false;
4408+
}
44034409

44044410
if (const Stmt *Else = IS->getElse()) {
44054411
LabelTy LabelElse = this->getLabel();

clang/lib/AST/ByteCode/Interp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3076,6 +3076,11 @@ static inline bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm) {
30763076
BlockDesc, Source);
30773077
}
30783078

3079+
static inline bool IsConstantContext(InterpState &S, CodePtr OpPC) {
3080+
S.Stk.push<Boolean>(Boolean::from(S.inConstantContext()));
3081+
return true;
3082+
}
3083+
30793084
inline bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T) {
30803085
assert(T);
30813086
assert(!S.getLangOpts().CPlusPlus23);

clang/lib/AST/ByteCode/Opcodes.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,5 @@ def AllocCN : Opcode {
780780
def Free : Opcode {
781781
let Args = [ArgBool];
782782
}
783+
784+
def IsConstantContext: Opcode;

clang/test/CodeGenCXX/cxx2b-consteval-if.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -std=c++23 %s -emit-llvm -o - | FileCheck %s
2+
// RUN: %clang_cc1 -std=c++23 %s -emit-llvm -o - -fexperimental-new-constant-interpreter | FileCheck %s
23

34
void should_be_used_1();
45
void should_be_used_2();

0 commit comments

Comments
 (0)