[clang] effbf0b - PR52183: Don't emit code for a void-typed constant expression.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 14 20:56:09 PDT 2021


Author: Richard Smith
Date: 2021-10-14T20:55:51-07:00
New Revision: effbf0bdd039237542ac5e9afe1f23c9386010e2

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

LOG: PR52183: Don't emit code for a void-typed constant expression.

This is unnecessary in general, and wrong when the expression invokes a
consteval function.

Added: 
    

Modified: 
    clang/lib/CodeGen/CGExprConstant.cpp
    clang/lib/CodeGen/CGExprScalar.cpp
    clang/test/CodeGenCXX/cxx2a-consteval.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index d9ecf57a9ab5..ff900ed077e6 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -1714,6 +1714,8 @@ llvm::Constant *ConstantEmitter::emitForMemory(CodeGenModule &CGM,
 
 llvm::Constant *ConstantEmitter::tryEmitPrivate(const Expr *E,
                                                 QualType destType) {
+  assert(!destType->isVoidType() && "can't emit a void constant");
+
   Expr::EvalResult Result;
 
   bool Success = false;

diff  --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 866da292819f..ae9434f96529 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -419,6 +419,11 @@ class ScalarExprEmitter
   Value *VisitExpr(Expr *S);
 
   Value *VisitConstantExpr(ConstantExpr *E) {
+    // A constant expression of type 'void' generates no code and produces no
+    // value.
+    if (E->getType()->isVoidType())
+      return nullptr;
+
     if (Value *Result = ConstantEmitter(CGF).tryEmitConstantExpr(E)) {
       if (E->isGLValue())
         return CGF.Builder.CreateLoad(Address(

diff  --git a/clang/test/CodeGenCXX/cxx2a-consteval.cpp b/clang/test/CodeGenCXX/cxx2a-consteval.cpp
index 7edddde693ca..b10ac8fa6825 100644
--- a/clang/test/CodeGenCXX/cxx2a-consteval.cpp
+++ b/clang/test/CodeGenCXX/cxx2a-consteval.cpp
@@ -2,7 +2,8 @@
 // RUN: %clang_cc1 -emit-llvm %s -std=c++2a -triple x86_64-unknown-linux-gnu -o %t.ll
 // RUN: FileCheck -check-prefix=EVAL -input-file=%t.ll %s
 // RUN: FileCheck -check-prefix=EVAL-STATIC -input-file=%t.ll %s
-// RUN: %clang_cc1 -emit-llvm %s -std=c++2a -triple x86_64-unknown-linux-gnu -o - | FileCheck -check-prefix=EVAL-FN %s
+// RUN: FileCheck -check-prefix=EVAL-FN -input-file=%t.ll %s
+//
 // RUN: %clang_cc1 -emit-llvm %s -Dconsteval="" -std=c++2a -triple x86_64-unknown-linux-gnu -o %t.ll
 // RUN: FileCheck -check-prefix=EXPR -input-file=%t.ll %s
 
@@ -243,3 +244,10 @@ consteval int test_UserConvOverload_helper_ceval(int a) { return a; }
 int test_UserConvOverload_ceval() {
   return test_UserConvOverload_helper_ceval(UserConv());
 }
+
+consteval void void_test() {}
+void void_call() { // EVAL-FN-LABEL: define {{.*}} @_Z9void_call
+  // EVAL-FN-NOT: call
+  void_test();
+  // EVAL-FN: {{^}}}
+}


        


More information about the cfe-commits mailing list