[clang] [clang][bytecode] Fix 'if consteval' in non-constant contexts (PR #104707)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 18 06:57:27 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
The previous code made this a compile-time decision but it's not.
---
Full diff: https://github.com/llvm/llvm-project/pull/104707.diff
4 Files Affected:
- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+13-7)
- (modified) clang/lib/AST/ByteCode/Interp.h (+5)
- (modified) clang/lib/AST/ByteCode/Opcodes.td (+2)
- (modified) clang/test/CodeGenCXX/cxx2b-consteval-if.cpp (+1)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index c3ecaa40b86f2a..06e66f8d9ef2fa 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -4355,11 +4355,6 @@ bool Compiler<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
}
template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
- if (IS->isNonNegatedConsteval())
- return visitStmt(IS->getThen());
- if (IS->isNegatedConsteval())
- return IS->getElse() ? visitStmt(IS->getElse()) : true;
-
if (auto *CondInit = IS->getInit())
if (!visitStmt(CondInit))
return false;
@@ -4368,8 +4363,19 @@ template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
if (!visitDeclStmt(CondDecl))
return false;
- if (!this->visitBool(IS->getCond()))
- return false;
+ // Compile condition.
+ if (IS->isNonNegatedConsteval()) {
+ if (!this->emitIsConstantContext(IS))
+ return false;
+ } else if (IS->isNegatedConsteval()) {
+ if (!this->emitIsConstantContext(IS))
+ return false;
+ if (!this->emitInvBool(IS))
+ return false;
+ } else {
+ if (!this->visitBool(IS->getCond()))
+ return false;
+ }
if (const Stmt *Else = IS->getElse()) {
LabelTy LabelElse = this->getLabel();
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 6cb7a42482ab25..02dff753062dc8 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -3054,6 +3054,11 @@ static inline bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm) {
BlockDesc, Source);
}
+static inline bool IsConstantContext(InterpState &S, CodePtr OpPC) {
+ S.Stk.push<Boolean>(Boolean::from(S.inConstantContext()));
+ return true;
+}
+
inline bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T) {
assert(T);
assert(!S.getLangOpts().CPlusPlus23);
diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td
index 3478eb174e518e..21bc31bfc5706e 100644
--- a/clang/lib/AST/ByteCode/Opcodes.td
+++ b/clang/lib/AST/ByteCode/Opcodes.td
@@ -787,3 +787,5 @@ def AllocCN : Opcode {
def Free : Opcode {
let Args = [ArgBool];
}
+
+def IsConstantContext: Opcode;
diff --git a/clang/test/CodeGenCXX/cxx2b-consteval-if.cpp b/clang/test/CodeGenCXX/cxx2b-consteval-if.cpp
index 343b6a0bbd8a6b..a6aa862b975ebe 100644
--- a/clang/test/CodeGenCXX/cxx2b-consteval-if.cpp
+++ b/clang/test/CodeGenCXX/cxx2b-consteval-if.cpp
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -std=c++23 %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -std=c++23 %s -emit-llvm -o - -fexperimental-new-constant-interpreter | FileCheck %s
void should_be_used_1();
void should_be_used_2();
``````````
</details>
https://github.com/llvm/llvm-project/pull/104707
More information about the cfe-commits
mailing list