aaron.ballman created this revision.
aaron.ballman added reviewers: erichkeane, rsmith, rjmccall.
aaron.ballman requested review of this revision.
Herald added a project: clang.

I don't think the aggregate emitter needs to emit anything when handling a 
constant expression which has an unused result. The unused result means we have 
nowhere to store the value anyway during codegen, so this early returns in that 
case.

This addresses PR51484.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D111669

Files:
  clang/lib/CodeGen/CGExprAgg.cpp
  clang/test/CodeGenCXX/cxx20-consteval-crash.cpp


Index: clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 %s -emit-llvm 
-o - | FileCheck %s
+
+// Test case comes from PR51484, where this code previously caused a crash.
+struct X { int val; };
+consteval X g() { return {0}; }
+void f() { g(); }
+
+// CHECK: define dso_local void @_Z1fv() #0 {
+// CHECK: entry:
+// CHECK-NOT: call i32 @_Z1gv()
+// CHECK:  ret void
+// CHECK: }
Index: clang/lib/CodeGen/CGExprAgg.cpp
===================================================================
--- clang/lib/CodeGen/CGExprAgg.cpp
+++ clang/lib/CodeGen/CGExprAgg.cpp
@@ -127,6 +127,10 @@
   }
 
   void VisitConstantExpr(ConstantExpr *E) {
+    // If the result is unused, no need to emit anything for it.
+    if (IsResultUnused)
+      return;
+
     if (llvm::Value *Result = ConstantEmitter(CGF).tryEmitConstantExpr(E)) {
       CGF.EmitAggregateStore(Result, Dest.getAddress(),
                              E->getType().isVolatileQualified());


Index: clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 %s -emit-llvm -o - | FileCheck %s
+
+// Test case comes from PR51484, where this code previously caused a crash.
+struct X { int val; };
+consteval X g() { return {0}; }
+void f() { g(); }
+
+// CHECK: define dso_local void @_Z1fv() #0 {
+// CHECK: entry:
+// CHECK-NOT: call i32 @_Z1gv()
+// CHECK:  ret void
+// CHECK: }
Index: clang/lib/CodeGen/CGExprAgg.cpp
===================================================================
--- clang/lib/CodeGen/CGExprAgg.cpp
+++ clang/lib/CodeGen/CGExprAgg.cpp
@@ -127,6 +127,10 @@
   }
 
   void VisitConstantExpr(ConstantExpr *E) {
+    // If the result is unused, no need to emit anything for it.
+    if (IsResultUnused)
+      return;
+
     if (llvm::Value *Result = ConstantEmitter(CGF).tryEmitConstantExpr(E)) {
       CGF.EmitAggregateStore(Result, Dest.getAddress(),
                              E->getType().isVolatileQualified());
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to