[clang] [clang][bytecode] Fix 'if consteval' in non-constant contexts (PR #104707)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 18 07:05:55 PDT 2024
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/104707
>From 30ea12f3a0841a532471c750204f13e5850d714c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 18 Aug 2024 15:40:05 +0200
Subject: [PATCH] [clang][bytecode] Fix 'if consteval' in non-constant contexts
The previous code made this a compile-time decision but it's not.
---
clang/lib/AST/ByteCode/Compiler.cpp | 20 +++++++++++++-------
clang/lib/AST/ByteCode/Interp.h | 5 +++++
clang/lib/AST/ByteCode/Opcodes.td | 2 ++
clang/test/CodeGenCXX/cxx2b-consteval-if.cpp | 1 +
4 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 53144a50ccf4a9..6c76a88b85d8c8 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->emitInv(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 5aa3d24ebc7582..527e4e9e4129c7 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -3048,6 +3048,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 61319e1633d9ad..7374a441c8bb19 100644
--- a/clang/lib/AST/ByteCode/Opcodes.td
+++ b/clang/lib/AST/ByteCode/Opcodes.td
@@ -780,3 +780,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();
More information about the cfe-commits
mailing list