================
@@ -4672,39 +4672,69 @@ static bool
 buildCapturedStmtCaptureList(Sema &S, CapturedRegionScopeInfo *RSI,
                              SmallVectorImpl<CapturedStmt::Capture> &Captures,
                              SmallVectorImpl<Expr *> &CaptureInits) {
+  llvm::SmallPtrSet<VarDecl *, 4> CapturedDecomposed;
   for (const sema::Capture &Cap : RSI->Captures) {
     if (Cap.isInvalid())
       continue;
 
+    ValueDecl *CapVar = nullptr;
+    if (Cap.isVariableCapture()) {
+      CapVar = Cap.getVariable();
+      if (auto *BD = dyn_cast<BindingDecl>(CapVar)) {
+        VarDecl *DD = cast<VarDecl>(BD->getDecomposedDecl());
+        if (!CapturedDecomposed.insert(DD).second) {
+          continue; // Skip duplicate.
+        }
+        CapVar = DD;
+      }
+    }
+
     // Form the initializer for the capture.
     ExprResult Init = S.BuildCaptureInit(Cap, Cap.getLocation(),
                                          RSI->CapRegionKind == CR_OpenMP);
 
     // FIXME: Bail out now if the capture is not used and the initializer has
     // no side-effects.
 
-    // Create a field for this capture.
-    FieldDecl *Field = S.BuildCaptureField(RSI->TheRecordDecl, Cap);
+    // Build the capture field. For OpenMP BindingDecl captures redirected
+    // to their DecompositionDecl, the field type must use the
+    // DecompositionDecl's type (e.g. int[2]) not the BindingDecl's type (e.g.
+    // int).
+    FieldDecl *Field = nullptr;
+    if (RSI->CapRegionKind == CR_OpenMP && CapVar &&
+        CapVar != Cap.getVariable() && isa<DecompositionDecl>(CapVar)) {
+      // Manually build a reference field with the DecompositionDecl's type.
+      QualType DDType = cast<VarDecl>(CapVar)->getType();
+      QualType RefType = S.Context.getLValueReferenceType(DDType);
+      TypeSourceInfo *TSI =
+          S.Context.getTrivialTypeSourceInfo(RefType, Cap.getLocation());
+      Field = FieldDecl::Create(S.Context, RSI->TheRecordDecl,
+                                Cap.getLocation(), Cap.getLocation(),
+                                /*Id=*/nullptr, RefType, TSI,
+                                /*BW=*/nullptr, /*Mutable=*/false, 
ICIS_NoInit);
+      Field->setImplicit(true);
+      Field->setAccess(AS_private);
----------------
alexey-bataev wrote:

Hardcoded `getLValueReferenceType(DDType)` ignores `Cap.isReferenceCapture()`. 
For OpenMP today the captures end up by-ref by default, but when a binding ends 
up in a `firstprivate`/`task`/`shared` style capture path the field type and 
the capture kind (set further below) will disagree. At minimum add a comment 
that this is OpenMP-by-ref-only and an `assert(Cap.isReferenceCapture())` so 
the mismatch surfaces if the capture model is later extended.

https://github.com/llvm/llvm-project/pull/190832
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to