[PATCH] D111669: No longer crash when a consteval function returns a structure

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 12 12:20:00 PDT 2021


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());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111669.379134.patch
Type: text/x-patch
Size: 1155 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211012/3ee19999/attachment.bin>


More information about the cfe-commits mailing list