[PATCH] D135730: Make inalloca lambda parameters work (for 32-bit windows).

Amy Huang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 11 16:55:46 PDT 2022


akhuang created this revision.
Herald added a project: All.
akhuang requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This fixes a bug where inalloca arguments can't be used as parameters in
non-capturing lambdas.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135730

Files:
  clang/lib/CodeGen/CGClass.cpp
  clang/test/CodeGenCXX/inalloca-lambda.cpp


Index: clang/test/CodeGenCXX/inalloca-lambda.cpp
===================================================================
--- clang/test/CodeGenCXX/inalloca-lambda.cpp
+++ clang/test/CodeGenCXX/inalloca-lambda.cpp
@@ -1,7 +1,4 @@
-// RUN: not %clang_cc1 -triple i686-windows-msvc -emit-llvm -o /dev/null %s  2>&1 | FileCheck %s
-
-// PR28299
-// CHECK: error: cannot compile this forwarded non-trivially copyable parameter yet
+// RUN: %clang_cc1 -triple i686-windows-msvc -emit-llvm -o - %s  2>&1 | FileCheck %s
 
 class A {
   A(const A &);
@@ -9,3 +6,14 @@
 typedef void (*fptr_t)(A);
 fptr_t fn1() { return [](A) {}; }
 
+// CHECK-LABEL: define dso_local noundef ptr @"?fn1@@YAP6AXVA@@@ZXZ"
+// CHECK-LABEL: define internal x86_thiscallcc noundef ptr @"??B<lambda_0>@?0??fn1@@YAP6AXVA@@@ZXZ at QBEP6A?A?<auto>@@0 at ZXZ"
+// CHECK-LABEL: define internal void @"?__invoke@<lambda_0>@?0??fn1@@YAP6AXVA@@@ZXZ at CA?A?<auto>@@0 at Z"
+// CHECK: %argmem = alloca inalloca <{ %class.A, [3 x i8] }>, align 4
+// CHECK: %[[V1:[0-9]+]] = getelementptr inbounds
+// CHECK: %[[V2:[0-9]+]] = load %class.A, ptr %[[V1]], align 4
+// CHECK: %[[V3:[0-9]+]] = getelementptr inbounds
+// CHECK: store %class.A %[[V2]], ptr %[[V3]], align 4
+// CHECK: call x86_thiscallcc void @"??R<lambda_0>@?0??fn1@@YAP6AXVA@@@ZXZ at QBE?A?<auto>@@0 at Z"
+// CHECK-SAME: ptr noundef %unused.capture, ptr inalloca(<{ %class.A, [3 x i8] }>) %argmem
+// CHECK-LABEL: define internal x86_thiscallcc void @"??R<lambda_0>@?0??fn1@@YAP6AXVA@@@ZXZ at QBE?A?<auto>@@0 at Z"
Index: clang/lib/CodeGen/CGClass.cpp
===================================================================
--- clang/lib/CodeGen/CGClass.cpp
+++ clang/lib/CodeGen/CGClass.cpp
@@ -3015,8 +3015,24 @@
   CallArgs.add(RValue::get(ThisPtr.getPointer()), ThisType);
 
   // Add the rest of the parameters.
-  for (auto *Param : MD->parameters())
-    EmitDelegateCallArg(CallArgs, Param, Param->getBeginLoc());
+  for (auto *Param : MD->parameters()) {
+    // If there's an inalloca param type, use a reference to the inalloca type
+    // in the forwarding call instead.
+    const CXXRecordDecl *RD = Param->getType()->getAsCXXRecordDecl();
+    if (RD && CGM.getCXXABI().getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory) {
+      QualType ParamAsRefType = 
+        getContext().getLValueReferenceType(Param->getType());
+      ParmVarDecl *ParamAsRef = ParmVarDecl::Create(
+          Param->getASTContext(), Param->getDeclContext(),
+          Param->getBeginLoc(), Param->getEndLoc(), Param->getIdentifier(),
+          ParamAsRefType, Param->getTypeSourceInfo(),
+          Param->getStorageClass(), Param->getDefaultArg());
+      setAddrOfLocalVar(ParamAsRef, GetAddrOfLocalVar(Param));
+      EmitDelegateCallArg(CallArgs, ParamAsRef, Param->getBeginLoc());
+    } else {
+      EmitDelegateCallArg(CallArgs, Param, Param->getBeginLoc());
+    }
+  }
 
   const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator();
   // For a generic lambda, find the corresponding call operator specialization


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135730.466978.patch
Type: text/x-patch
Size: 3014 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221011/4c83b5f3/attachment.bin>


More information about the cfe-commits mailing list