[PATCH] D136457: [clang][Interp] Fix discarding non-primitive function call return values

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 21 08:35:15 PDT 2022


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, tahonermann, shafik.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

As briefly discussed in https://reviews.llvm.org/D136013

This creates a temporary local variable and puts the RVO pointer on the stack for the function to write the result into.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136457

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/test/AST/Interp/records.cpp


Index: clang/test/AST/Interp/records.cpp
===================================================================
--- clang/test/AST/Interp/records.cpp
+++ clang/test/AST/Interp/records.cpp
@@ -214,6 +214,10 @@
     this->a; // expected-warning {{expression result unused}} \
              // ref-warning {{expression result unused}}
     get5();
+#if __cplusplus >= 201703L
+    // FIXME: Enable once we support MaterializeConstantExpr properly.
+    getInts();
+#endif
   }
 
   constexpr int m() const {
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1152,6 +1152,19 @@
     if (Func->isFullyCompiled() && !Func->isConstexpr())
       return false;
 
+    QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
+    Optional<PrimType> T = classify(ReturnType);
+
+    if (DiscardResult && !T) {
+      // If we need to discard the return value but the function returns its
+      // value via an RVO pointer, we need to create one such pointer just
+      // for this call.
+      if (Optional<unsigned> LocalIndex = allocateLocal(E)) {
+        if (!this->emitGetPtrLocal(*LocalIndex, E))
+          return false;
+      }
+    }
+
     // Put arguments on the stack.
     for (const auto *Arg : E->arguments()) {
       if (!this->visit(Arg))
@@ -1164,13 +1177,8 @@
     if (!this->emitCall(Func, E))
       return false;
 
-    QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
-    if (DiscardResult && !ReturnType->isVoidType()) {
-      Optional<PrimType> T = classify(ReturnType);
-      if (T)
-        return this->emitPop(*T, E);
-      // TODO: This is a RVO function and we need to ignore the return value.
-    }
+    if (DiscardResult && !ReturnType->isVoidType() && T)
+      return this->emitPop(*T, E);
 
     return true;
   } else {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136457.469628.patch
Type: text/x-patch
Size: 1938 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221021/7732ffaa/attachment.bin>


More information about the cfe-commits mailing list