[clang] b9c4c4c - [clang][bytecode] Fix 'if consteval' in non-constant contexts (#104707)

via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 22 10:06:12 PDT 2024


Author: Timm Baeder
Date: 2024-08-22T19:06:09+02:00
New Revision: b9c4c4ccf921c0481d51d4e0c9e862aa9ea3fcf3

URL: https://github.com/llvm/llvm-project/commit/b9c4c4ccf921c0481d51d4e0c9e862aa9ea3fcf3
DIFF: https://github.com/llvm/llvm-project/commit/b9c4c4ccf921c0481d51d4e0c9e862aa9ea3fcf3.diff

LOG: [clang][bytecode] Fix 'if consteval' in non-constant contexts (#104707)

The previous code made this a compile-time decision but it's not.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Compiler.cpp
    clang/lib/AST/ByteCode/Interp.h
    clang/lib/AST/ByteCode/Opcodes.td
    clang/test/CodeGenCXX/cxx2b-consteval-if.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 3a3927a9671345..655983a1ca0494 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -4385,11 +4385,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;
@@ -4398,8 +4393,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 fd4406c0db2b88..7ba51f737db491 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -3076,6 +3076,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