https://github.com/akash-manna-sky updated 
https://github.com/llvm/llvm-project/pull/244591

>From 94ca18af7aba88ac422337d78bdbd72d315b178a Mon Sep 17 00:00:00 2001
From: Akash Manna <[email protected]>
Date: Tue, 10 Nov 2026 17:52:44 +0530
Subject: [PATCH 1/2] [Clang][OpenMP] Allow array shaping as the base of array
 sections in target update

Fixes #136294

`#pragma omp target update from((([nx][ny+2])a)[0:nx][1])`, the array shaping 
example from the OpenMP Examples, is rejected with "array shaping operation is 
not allowed here". The shaped expression only had the placeholder type that 
keeps it out of other contexts, so `ActOnOMPArraySectionExpr` saw no array to 
section and refused the base.

The shaping expression now records the array type the spec gives it, and 
sections use it like the type of a declared array, so the existing contiguity 
checks and the non-contiguous codegen apply as they are. The example needed two 
more things: two sections of one variable in a motion clause no longer hit the 
OpenMP 4.5 map restriction, and `target update` treats a section it cannot 
prove contiguous as non-contiguous instead of copying whole rows.

LLM tools were used for this contribution.
---
 clang/docs/ReleaseNotes.md                    |   2 +
 clang/include/clang/AST/ExprOpenMP.h          |  19 +-
 clang/lib/AST/Expr.cpp                        |  28 +-
 clang/lib/AST/OpenMPClause.cpp                |   6 +-
 clang/lib/CodeGen/CGExpr.cpp                  |  16 +
 clang/lib/CodeGen/CGOpenMPRuntime.cpp         |  15 +-
 clang/lib/Sema/SemaExpr.cpp                   |   8 +
 clang/lib/Sema/SemaOpenMP.cpp                 | 126 +++++++-
 clang/lib/Serialization/ASTReaderStmt.cpp     |   1 +
 clang/lib/Serialization/ASTWriterStmt.cpp     |   1 +
 clang/test/OpenMP/GH136294.c                  | 279 ++++++++++++++++++
 clang/test/OpenMP/target_map_messages.cpp     |   8 +
 clang/test/OpenMP/target_update_ast_print.cpp |   9 +
 .../OpenMP/target_update_from_messages.cpp    |  49 ++-
 .../test/OpenMP/target_update_to_messages.cpp |  52 +++-
 15 files changed, 570 insertions(+), 49 deletions(-)
 create mode 100644 clang/test/OpenMP/GH136294.c

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6d1dbbbb6eb77b..69ef6a1dcd4345 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1491,6 +1491,8 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was 
renamed to `alpha.core.Use
 
 - Added parsing and semantic support for `dims` modifier in `num_teams`,
   `thread_limit` and `num_threads` clauses for OpenMP 6.1 or later.
+- Array shaping expressions can now be the base of array sections in the `to`
+  and `from` clauses of `target update`. (#GH136294)
 - Map-type-modifying modifiers applied to a list item with a user-defined 
mapper
   are now propagated onto the maps the mapper expands to.
 - Mapping of expressions with base-pointers through a user-defined mapper (e.g.
diff --git a/clang/include/clang/AST/ExprOpenMP.h 
b/clang/include/clang/AST/ExprOpenMP.h
index 4d3c5f54ad7dde..322f4d9e5572a4 100644
--- a/clang/include/clang/AST/ExprOpenMP.h
+++ b/clang/include/clang/AST/ExprOpenMP.h
@@ -29,10 +29,13 @@ class OMPArrayShapingExpr final
   SourceLocation LPLoc; /// The location of the left paren
   SourceLocation RPLoc; /// The location of the right paren
   unsigned NumDims = 0; /// Number of dimensions in the shaping expression.
+  /// The n-dimensional array type designated by the shaping operation.
+  QualType ShapedType;
 
   /// Construct full expression.
-  OMPArrayShapingExpr(QualType ExprTy, Expr *Op, SourceLocation L,
-                      SourceLocation R, ArrayRef<Expr *> Dims);
+  OMPArrayShapingExpr(QualType ExprTy, QualType ShapedTy, Expr *Op,
+                      SourceLocation L, SourceLocation R,
+                      ArrayRef<Expr *> Dims);
 
   /// Construct an empty expression.
   explicit OMPArrayShapingExpr(EmptyShell Shell, unsigned NumDims)
@@ -47,6 +50,9 @@ class OMPArrayShapingExpr final
   /// Sets source ranges for the brackets in the array shaping operation.
   void setBracketsRanges(ArrayRef<SourceRange> BR);
 
+  /// Sets the array type designated by the array shaping operation.
+  void setShapedType(QualType T) { ShapedType = T; }
+
   unsigned numTrailingObjects(OverloadToken<Expr *>) const {
     // Add an extra one for the base expression.
     return NumDims + 1;
@@ -58,8 +64,9 @@ class OMPArrayShapingExpr final
 
 public:
   static OMPArrayShapingExpr *Create(const ASTContext &Context, QualType T,
-                                     Expr *Op, SourceLocation L,
-                                     SourceLocation R, ArrayRef<Expr *> Dims,
+                                     QualType ShapedTy, Expr *Op,
+                                     SourceLocation L, SourceLocation R,
+                                     ArrayRef<Expr *> Dims,
                                      ArrayRef<SourceRange> BracketRanges);
 
   static OMPArrayShapingExpr *CreateEmpty(const ASTContext &Context,
@@ -90,6 +97,10 @@ class OMPArrayShapingExpr final
   Expr *getBase() { return getTrailingObjects<Expr *>()[NumDims]; }
   const Expr *getBase() const { return getTrailingObjects<Expr *>()[NumDims]; }
 
+  /// Returns the n-dimensional array type designated by the shaping operation
+  /// (OpenMP 5.0 [2.1.4]); array sections based on this expression index it.
+  QualType getShapedType() const { return ShapedType; }
+
   static bool classof(const Stmt *T) {
     return T->getStmtClass() == OMPArrayShapingExprClass;
   }
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 1ba2ccdf697f9d..984920e88262b1 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -5451,10 +5451,15 @@ QualType ArraySectionExpr::getBaseOriginalType(const 
Expr *Base) {
     ++ArraySectionCount;
   }
   Base = Base->IgnoreParenImpCasts();
-  auto OriginalTy = Base->getType();
-  if (auto *DRE = dyn_cast<DeclRefExpr>(Base))
-    if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
-      OriginalTy = PVD->getOriginalType().getNonReferenceType();
+  QualType OriginalTy;
+  if (const auto *OAShE = dyn_cast<OMPArrayShapingExpr>(Base)) {
+    OriginalTy = OAShE->getShapedType();
+  } else {
+    OriginalTy = Base->getType();
+    if (auto *DRE = dyn_cast<DeclRefExpr>(Base))
+      if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
+        OriginalTy = PVD->getOriginalType().getNonReferenceType();
+  }
 
   for (unsigned Cnt = 0; Cnt < ArraySectionCount; ++Cnt) {
     if (OriginalTy->isAnyPointerType())
@@ -5536,27 +5541,28 @@ void 
OMPArrayShapingExpr::setBracketsRanges(ArrayRef<SourceRange> BR) {
   llvm::copy(BR, getTrailingObjects<SourceRange>());
 }
 
-OMPArrayShapingExpr::OMPArrayShapingExpr(QualType ExprTy, Expr *Op,
-                                         SourceLocation L, SourceLocation R,
+OMPArrayShapingExpr::OMPArrayShapingExpr(QualType ExprTy, QualType ShapedTy,
+                                         Expr *Op, SourceLocation L,
+                                         SourceLocation R,
                                          ArrayRef<Expr *> Dims)
     : Expr(OMPArrayShapingExprClass, ExprTy, VK_LValue, OK_Ordinary), LPLoc(L),
-      RPLoc(R), NumDims(Dims.size()) {
+      RPLoc(R), NumDims(Dims.size()), ShapedType(ShapedTy) {
   setBase(Op);
   setDimensions(Dims);
   setDependence(computeDependence(this));
 }
 
 OMPArrayShapingExpr *
-OMPArrayShapingExpr::Create(const ASTContext &Context, QualType T, Expr *Op,
-                            SourceLocation L, SourceLocation R,
-                            ArrayRef<Expr *> Dims,
+OMPArrayShapingExpr::Create(const ASTContext &Context, QualType T,
+                            QualType ShapedTy, Expr *Op, SourceLocation L,
+                            SourceLocation R, ArrayRef<Expr *> Dims,
                             ArrayRef<SourceRange> BracketRanges) {
   assert(Dims.size() == BracketRanges.size() &&
          "Different number of dimensions and brackets ranges.");
   void *Mem = Context.Allocate(
       totalSizeToAlloc<Expr *, SourceRange>(Dims.size() + 1, Dims.size()),
       alignof(OMPArrayShapingExpr));
-  auto *E = new (Mem) OMPArrayShapingExpr(T, Op, L, R, Dims);
+  auto *E = new (Mem) OMPArrayShapingExpr(T, ShapedTy, Op, L, R, Dims);
   E->setBracketsRanges(BracketRanges);
   return E;
 }
diff --git a/clang/lib/AST/OpenMPClause.cpp b/clang/lib/AST/OpenMPClause.cpp
index f0102720f7d33b..b878f789154c83 100644
--- a/clang/lib/AST/OpenMPClause.cpp
+++ b/clang/lib/AST/OpenMPClause.cpp
@@ -1197,9 +1197,9 @@ OMPClauseMappableExprCommon::findAttachPtrExpr(
     if (!CurExpr)
       break;
 
-    // If CurExpr is something like `p + 10`, we need to ignore it, since
-    // we are looking for `p`.
-    if (isa<BinaryOperator>(CurExpr))
+    // If CurExpr is something like `p + 10` or `([n])p`, we need to ignore it,
+    // since we are looking for `p`.
+    if (isa<BinaryOperator, OMPArrayShapingExpr>(CurExpr))
       continue;
 
     // Keep going until we reach an Expr of pointer type.
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 98133ecf1b48fc..34210248367afd 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -5482,6 +5482,22 @@ static Address emitOMPArraySectionBase(CodeGenFunction 
&CGF, const Expr *Base,
     return Address(CGF.Builder.CreateLoad(BaseLVal.getAddress()),
                    CGF.ConvertTypeForMem(ElTy), Align);
   }
+  if (auto *OAShE =
+          dyn_cast<OMPArrayShapingExpr>(Base->IgnoreParenImpCasts())) {
+    // The shaped pointer is the address of the array; capture the variable
+    // dimensions of the shape first.
+    QualType ShapedTy = OAShE->getShapedType();
+    if (ShapedTy->isVariablyModifiedType())
+      CGF.EmitVariablyModifiedType(ShapedTy);
+    LValueBaseInfo TypeBaseInfo;
+    TBAAAccessInfo TypeTBAAInfo;
+    CharUnits Align =
+        CGF.CGM.getNaturalTypeAlignment(ElTy, &TypeBaseInfo, &TypeTBAAInfo);
+    BaseInfo.mergeForCast(TypeBaseInfo);
+    TBAAInfo = CGF.CGM.mergeTBAAInfoForCast(TBAAInfo, TypeTBAAInfo);
+    return Address(CGF.EmitScalarExpr(OAShE->getBase()),
+                   CGF.ConvertTypeForMem(ElTy), Align);
+  }
   return CGF.EmitPointerWithAlignment(Base, &BaseInfo, &TBAAInfo);
 }
 
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index d3a6ffee3ea748..4c5fe9c9192c6a 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -8430,8 +8430,11 @@ class MappableExprsHandler {
           dyn_cast<OMPArrayShapingExpr>(I->getAssociatedExpression());
       const auto *UO = dyn_cast<UnaryOperator>(I->getAssociatedExpression());
       const auto *BO = dyn_cast<BinaryOperator>(I->getAssociatedExpression());
+      // An array shaping expression maps the shaped storage only as the whole
+      // list item; as the base of array sections it just reinterprets the
+      // pointer.
       bool IsPointer =
-          OAShE ||
+          (OAShE && Next == CE) ||
           (OASE && ArraySectionExpr::getBaseOriginalType(OASE)
                        .getCanonicalType()
                        ->isAnyPointerType()) ||
@@ -8728,6 +8731,14 @@ class MappableExprsHandler {
     MapValuesArrayTy DimSizes{llvm::ConstantInt::get(CGF.CGM.Int64Ty, 1)};
     uint64_t ElementTypeSize;
 
+    // Only array sections and subscripts add a dimension.
+    unsigned NumArrayDims = llvm::count_if(
+        Components,
+        [](const OMPClauseMappableExprCommon::MappableComponent &Component) {
+          return isa<ArraySectionExpr, ArraySubscriptExpr>(
+              Component.getAssociatedExpression());
+        });
+
     // Collect Size information for each dimension and get the element size as
     // the first Stride. For example, for `int arr[10][10]`, the DimSizes
     // should be [10, 10] and the first stride is 4 btyes.
@@ -8787,7 +8798,7 @@ class MappableExprsHandler {
       }
       // Get dimension value except for the last dimension since we don't need
       // it.
-      if (DimSizes.size() < Components.size() - 1) {
+      if (DimSizes.size() < NumArrayDims) {
         if (CAT)
           DimSizes.push_back(
               llvm::ConstantInt::get(CGF.Int64Ty, CAT->getZExtSize()));
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index a14c3d50d12851..00c2e2fbacbf60 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -5124,6 +5124,14 @@ ExprResult Sema::ActOnArraySubscriptExpr(Scope *S, Expr 
*base,
                                            rbLoc);
   }
 
+  // A subscript of an OpenMP array shaping expression is a single-element
+  // array section.
+  if (base && !base->getType().isNull() && ArgExprs.size() == 1 &&
+      base->hasPlaceholderType(BuiltinType::OMPArrayShaping))
+    return OpenMP().ActOnOMPArraySectionExpr(
+        base, lbLoc, ArgExprs.front(), SourceLocation(), SourceLocation(),
+        /*Length=*/nullptr, /*Stride=*/nullptr, rbLoc);
+
   // Since this might be a postfix expression, get rid of ParenListExprs.
   if (isa<ParenListExpr>(base)) {
     ExprResult result = MaybeConvertParenListExprToParenExpr(S, base);
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 9acf3b3724527c..d56c86d90becd9 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -24216,6 +24216,53 @@ static bool 
checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
   return ConstLength.getSExtValue() != 1;
 }
 
+/// Return true if it can be proven that the provided array section refers to
+/// the whole dimension of the array whose base type is \a BaseQTy.
+static bool checkArrayExpressionRefersToWholeSize(Sema &SemaRef,
+                                                  const ArraySectionExpr *OASE,
+                                                  QualType BaseQTy) {
+  ASTContext &Ctx = SemaRef.getASTContext();
+  auto EvaluatesTo = [&Ctx](const Expr *E, int64_t Value) {
+    Expr::EvalResult Result;
+    return !E->isValueDependent() && E->EvaluateAsInt(Result, Ctx) &&
+           Result.Val.getInt().getSExtValue() == Value;
+  };
+
+  if (OASE->getColonLocFirst().isInvalid()) {
+    const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
+    return CATy && CATy->getSExtSize() == 1;
+  }
+
+  const Expr *Stride = OASE->getStride();
+  if (Stride && !EvaluatesTo(Stride, 1))
+    return false;
+  const Expr *LowerBound = OASE->getLowerBound();
+  if (LowerBound && !EvaluatesTo(LowerBound, 0))
+    return false;
+
+  if (BaseQTy->isPointerType())
+    return false;
+
+  // Without a length the section covers the rest of the dimension.
+  const Expr *Length = OASE->getLength();
+  if (!Length)
+    return true;
+
+  if (const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
+    return EvaluatesTo(Length, CATy->getSExtSize());
+
+  // For a variable length array the length has to be the size expression of
+  // the dimension, e.g. c[0:n] for double c[n][m].
+  const auto *VATy = dyn_cast<VariableArrayType>(BaseQTy.getTypePtr());
+  if (!VATy || !VATy->getSizeExpr() || Length->HasSideEffects(Ctx))
+    return false;
+  llvm::FoldingSetNodeID LengthID, SizeID;
+  Length->IgnoreParenImpCasts()->Profile(LengthID, Ctx, /*Canonical=*/true);
+  VATy->getSizeExpr()->IgnoreParenImpCasts()->Profile(SizeID, Ctx,
+                                                      /*Canonical=*/true);
+  return LengthID == SizeID;
+}
+
 // The base of elements of list in a map clause have to be either:
 //  - a reference to variable or field.
 //  - a member expression.
@@ -24513,14 +24560,21 @@ class MapBaseChecker final : public 
StmtVisitor<MapBaseChecker, bool> {
       // If this array section refers to the whole dimension we can still
       // accept other array sections before this one, except if the base is a
       // pointer. Otherwise, only unitary sections are accepted.
-      if (NotWhole || IsPointer)
+      //
+      // 'target update' transfers non-contiguous sections, so a section that
+      // is not provably whole (e.g. of a variable length array) is treated as
+      // partial there: a descriptor is correct either way, whole rows are not.
+      if (NotWhole || IsPointer ||
+          (DKind == OMPD_target_update && SemaRef.getLangOpts().OpenMP >= 50 &&
+           !checkArrayExpressionRefersToWholeSize(SemaRef, OASE, CurType)))
         AllowWholeSizeArraySection = false;
     } else if (DKind == OMPD_target_update &&
                SemaRef.getLangOpts().OpenMP >= 50) {
       if (IsPointer && !AllowAnotherPtr)
         SemaRef.Diag(ELoc, diag::err_omp_section_length_undefined)
             << /*array of unknown bound */ 1;
-      else
+      // A single element (no colon) keeps the storage contiguous.
+      else if (OASE->getColonLocFirst().isValid())
         IsNonContiguous = true;
     } else if (AllowUnitySizeArraySection && NotUnity) {
       // A unity or whole array section is not allowed and that is not
@@ -24567,6 +24621,15 @@ class MapBaseChecker final : public 
StmtVisitor<MapBaseChecker, bool> {
   bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) {
     Expr *Base = E->getBase();
 
+    // OpenMP 5.0 [2.1.4, Array Shaping, Restrictions]: the shape-operator may
+    // appear only in clauses that explicitly allow it. As the base of array
+    // sections only the motion clauses do (OpenMP 5.0 [2.12.6]).
+    if (!Components.empty() && CKind != OMPC_to && CKind != OMPC_from) {
+      if (!NoDiagnose)
+        SemaRef.Diag(E->getBeginLoc(), diag::err_omp_array_shaping_use);
+      return false;
+    }
+
     // Record the component - we don't have any declaration associated.
     Components.emplace_back(E, nullptr, IsNonContiguous);
 
@@ -24657,11 +24720,12 @@ static const Expr *checkMapClauseExpressionBase(
       auto CI = CurComponents.rbegin();
       auto CE = CurComponents.rend();
       for (; CI != CE; ++CI) {
-        const auto *OASE =
-            dyn_cast<ArraySectionExpr>(CI->getAssociatedExpression());
-        if (!OASE)
+        const Expr *E = CI->getAssociatedExpression();
+        if (!isa<ArraySectionExpr, ArraySubscriptExpr>(E))
           continue;
-        if (OASE && OASE->getLength())
+        // The outermost dimension is a single element or has a length.
+        const auto *OASE = dyn_cast<ArraySectionExpr>(E);
+        if (!OASE || OASE->getColonLocFirst().isInvalid() || OASE->getLength())
           break;
         SemaRef.Diag(ELoc, diag::err_array_section_does_not_specify_length)
             << ERange;
@@ -24726,7 +24790,8 @@ static bool checkMapConflicts(
           // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
           //  At most one list item can be an array item derived from a given
           //  variable in map clauses of the same construct.
-          if (CurrentRegionOnly &&
+          // Dropped in OpenMP 5.0.
+          if (CurrentRegionOnly && SemaRef.LangOpts.OpenMP < 50 &&
               (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
                isa<ArraySectionExpr>(CI->getAssociatedExpression()) ||
                isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) &&
@@ -25639,7 +25704,7 @@ static void checkMappableExpressionList(
     // Store the components in the stack so that they can be used to check
     // against other clauses later on.
     DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
-                                          /*WhereFoundClauseKind=*/OMPC_map);
+                                          /*WhereFoundClauseKind=*/CKind);
 
     // Save the components and declaration to create the clause. For purposes 
of
     // the clause creation, any component list that has base 'this' uses
@@ -28040,8 +28105,11 @@ ExprResult SemaOpenMP::ActOnOMPArraySectionExpr(
     SourceLocation ColonLocFirst, SourceLocation ColonLocSecond, Expr *Length,
     Expr *Stride, SourceLocation RBLoc) {
   ASTContext &Context = getASTContext();
+  // OpenMP 5.0 [2.1.4, Array Shaping]: the result of the shape-operator is an
+  // lvalue of n-dimensional array type, so it can be the base of a section.
+  bool IsShapedBase = Base->hasPlaceholderType(BuiltinType::OMPArrayShaping);
   if (Base->hasPlaceholderType() &&
-      !Base->hasPlaceholderType(BuiltinType::ArraySection)) {
+      !Base->hasPlaceholderType(BuiltinType::ArraySection) && !IsShapedBase) {
     ExprResult Result = SemaRef.CheckPlaceholderExpr(Base);
     if (Result.isInvalid())
       return ExprError();
@@ -28088,6 +28156,12 @@ ExprResult SemaOpenMP::ActOnOMPArraySectionExpr(
 
   // Perform default conversions.
   QualType OriginalTy = ArraySectionExpr::getBaseOriginalType(Base);
+  // The dimensions of the shape may depend on template parameters.
+  if (IsShapedBase && OriginalTy->isDependentType()) {
+    return new (Context) ArraySectionExpr(
+        Base, LowerBound, Length, Stride, Context.DependentTy, VK_LValue,
+        OK_Ordinary, ColonLocFirst, ColonLocSecond, RBLoc);
+  }
   QualType ResultTy;
   if (OriginalTy->isAnyPointerType()) {
     ResultTy = OriginalTy->getPointeeType();
@@ -28210,7 +28284,7 @@ ExprResult SemaOpenMP::ActOnOMPArraySectionExpr(
     }
   }
 
-  if (!Base->hasPlaceholderType(BuiltinType::ArraySection)) {
+  if (!Base->hasPlaceholderType(BuiltinType::ArraySection) && !IsShapedBase) {
     ExprResult Result = SemaRef.DefaultFunctionArrayLvalueConversion(Base);
     if (Result.isInvalid())
       return ExprError();
@@ -28238,8 +28312,9 @@ ExprResult SemaOpenMP::ActOnOMPArrayShapingExpr(
   // Delay analysis of the types/expressions if instantiation/specialization is
   // required.
   if (!BaseTy->isPointerType() && Base->isTypeDependent())
-    return OMPArrayShapingExpr::Create(Context, Context.DependentTy, Base,
-                                       LParenLoc, RParenLoc, Dims, Brackets);
+    return OMPArrayShapingExpr::Create(Context, Context.DependentTy,
+                                       Context.DependentTy, Base, LParenLoc,
+                                       RParenLoc, Dims, Brackets);
   if (!BaseTy->isPointerType() ||
       (!Base->isTypeDependent() &&
        BaseTy->getPointeeType()->isIncompleteType()))
@@ -28292,8 +28367,31 @@ ExprResult SemaOpenMP::ActOnOMPArrayShapingExpr(
   }
   if (ErrorFound)
     return ExprError();
-  return OMPArrayShapingExpr::Create(Context, Context.OMPArrayShapingTy, Base,
-                                     LParenLoc, RParenLoc, NewDims, Brackets);
+
+  // OpenMP 5.0 [2.1.4, Array Shaping]: the result has n-dimensional array type
+  // s1 x ... x sn of T; build it like the type of a declared array.
+  QualType ShapedTy = BaseTy->getPointeeType();
+  for (Expr *Dim : llvm::reverse(NewDims)) {
+    if (Dim->isTypeDependent() || Dim->isValueDependent()) {
+      ShapedTy = Context.getDependentSizedArrayType(
+          ShapedTy, Dim, ArraySizeModifier::Normal, /*IndexTypeQuals=*/0);
+      continue;
+    }
+    std::optional<llvm::APSInt> Size;
+    // An array of variable length arrays is itself a variable length array.
+    if (ShapedTy->isDependentType() || ShapedTy->isConstantSizeType())
+      Size = Dim->getIntegerConstantExpr(Context);
+    if (Size)
+      ShapedTy = Context.getConstantArrayType(ShapedTy, *Size, Dim,
+                                              ArraySizeModifier::Normal,
+                                              /*IndexTypeQuals=*/0);
+    else
+      ShapedTy = Context.getVariableArrayType(
+          ShapedTy, Dim, ArraySizeModifier::Normal, /*IndexTypeQuals=*/0);
+  }
+  return OMPArrayShapingExpr::Create(Context, Context.OMPArrayShapingTy,
+                                     ShapedTy, Base, LParenLoc, RParenLoc,
+                                     NewDims, Brackets);
 }
 
 ExprResult SemaOpenMP::ActOnOMPIteratorExpr(Scope *S,
diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp 
b/clang/lib/Serialization/ASTReaderStmt.cpp
index 26aee27c65e82f..8a3aa44a25e68a 100644
--- a/clang/lib/Serialization/ASTReaderStmt.cpp
+++ b/clang/lib/Serialization/ASTReaderStmt.cpp
@@ -1047,6 +1047,7 @@ void 
ASTStmtReader::VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) {
   E->setBracketsRanges(SRs);
   E->setLParenLoc(readSourceLocation());
   E->setRParenLoc(readSourceLocation());
+  E->setShapedType(Record.readType());
 }
 
 void ASTStmtReader::VisitOMPIteratorExpr(OMPIteratorExpr *E) {
diff --git a/clang/lib/Serialization/ASTWriterStmt.cpp 
b/clang/lib/Serialization/ASTWriterStmt.cpp
index c6e74fd19750a4..3746443756f0a5 100644
--- a/clang/lib/Serialization/ASTWriterStmt.cpp
+++ b/clang/lib/Serialization/ASTWriterStmt.cpp
@@ -983,6 +983,7 @@ void 
ASTStmtWriter::VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) {
     Record.AddSourceRange(SR);
   Record.AddSourceLocation(E->getLParenLoc());
   Record.AddSourceLocation(E->getRParenLoc());
+  Record.AddTypeRef(E->getShapedType());
   Code = serialization::EXPR_OMP_ARRAY_SHAPING;
 }
 
diff --git a/clang/test/OpenMP/GH136294.c b/clang/test/OpenMP/GH136294.c
new file mode 100644
index 00000000000000..e283a23574b6b1
--- /dev/null
+++ b/clang/test/OpenMP/GH136294.c
@@ -0,0 +1,279 @@
+// RUN: %clang_cc1 -verify -fopenmp -x c -triple x86_64-unknown-linux-gnu 
-fopenmp-targets=amdgpu-amd-amdhsa -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -fopenmp -x c -triple x86_64-unknown-linux-gnu 
-fopenmp-targets=amdgpu-amd-amdhsa -emit-pch -o %t %s
+// RUN: %clang_cc1 -fopenmp -x c -triple x86_64-unknown-linux-gnu 
-fopenmp-targets=amdgpu-amd-amdhsa -include-pch %t -verify %s -emit-llvm -o - | 
FileCheck %s
+
+// RUN: %clang_cc1 -verify -fopenmp-simd -x c -triple x86_64-unknown-linux-gnu 
-fopenmp-targets=amdgpu-amd-amdhsa -emit-llvm %s -o - | FileCheck 
--check-prefix SIMD-ONLY %s
+// SIMD-ONLY-NOT: {{__kmpc|__tgt}}
+
+// expected-no-diagnostics
+#ifndef HEADER
+#define HEADER
+
+// Array shaping expressions used as the base of array sections in the motion
+// clauses of 'target update' (OpenMP 5.0 [2.1.4, Array Shaping]). A column of
+// the shaped two-dimensional array is a non-contiguous section; it has to be
+// transferred through a descriptor with one entry per dimension, exactly like
+// the same section of a declared array.
+
+// FROM | NON_CONTIG, one entry per list item; the size slot holds the number
+// of dimensions of the descriptor (two array dimensions plus the element).
+// CHECK-DAG: [[FROM2_SIZES:@.+]] = private unnamed_addr constant [2 x i64] 
[i64 3, i64 3]
+// CHECK-DAG: [[FROM2_TYPES:@.+]] = private unnamed_addr constant [2 x i64] 
[i64 17592186044418, i64 17592186044418]
+// TO | NON_CONTIG
+// CHECK-DAG: [[TO2_SIZES:@.+]] = private unnamed_addr constant [2 x i64] [i64 
3, i64 3]
+// CHECK-DAG: [[TO2_TYPES:@.+]] = private unnamed_addr constant [2 x i64] [i64 
17592186044417, i64 17592186044417]
+// The declared variable length array control.
+// CHECK-DAG: [[VLA_SIZES:@.+]] = private unnamed_addr constant [1 x i64] [i64 
3]
+// CHECK-DAG: [[VLA_TYPES:@.+]] = private unnamed_addr constant [1 x i64] [i64 
17592186044418]
+// Constant dimensions.
+// CHECK-DAG: [[CONST_SIZES:@.+]] = private unnamed_addr constant [1 x i64] 
[i64 3]
+// CHECK-DAG: [[CONST_TYPES:@.+]] = private unnamed_addr constant [1 x i64] 
[i64 17592186044418]
+// A row of the shaped array is contiguous: 4 doubles, TO.
+// CHECK-DAG: [[ROW_SIZES:@.+]] = private unnamed_addr constant [1 x i64] [i64 
32]
+// CHECK-DAG: [[ROW_TYPES:@.+]] = private unnamed_addr constant [1 x i64] [i64 
1]
+
+void exch_data(double *a, int nx, int ny);
+
+// The example of the OpenMP Examples document: the boundary columns of a
+// pointer shaped as a (nx) x (ny+2) array.
+// CHECK-LABEL: define {{.*}}void @array_ing(
+// CHECK:       store ptr %a, ptr [[A_ADDR:%.+]],
+// CHECK-NEXT:  store i32 %nx, ptr [[NX_ADDR:%.+]],
+// CHECK-NEXT:  store i32 %ny, ptr [[NY_ADDR:%.+]],
+void array_ing(double *a, int nx, int ny) {
+#pragma omp target data map(a[0 : nx * (ny + 2)])
+  {
+    // CHECK:      call void @__tgt_target_data_begin_mapper(
+
+    // The base pointer is the value of 'a'; the dimensions of the shape are
+    // evaluated like the sizes of a variable length array.
+    // CHECK:      [[BP:%.+]] = load ptr, ptr [[A_ADDR]],
+    // CHECK-NEXT: [[NX:%.+]] = load i32, ptr [[NX_ADDR]],
+    // CHECK-NEXT: [[NX_64:%.+]] = zext i32 [[NX]] to i64
+    // CHECK-NEXT: [[NY:%.+]] = load i32, ptr [[NY_ADDR]],
+    // CHECK-NEXT: [[NY2:%.+]] = add nsw i32 [[NY]], 2
+    // CHECK-NEXT: [[NY2_64:%.+]] = zext i32 [[NY2]] to i64
+    // The section pointer is &a[0][1] of the shaped array.
+    // CHECK-NEXT: [[A:%.+]] = load ptr, ptr [[A_ADDR]],
+    // CHECK-NEXT: [[ROW0:%.+]] = mul nsw i64 0, [[NY2_64]]
+    // CHECK-NEXT: [[ROW0_PTR:%.+]] = getelementptr inbounds nuw double, ptr 
[[A]], i64 [[ROW0]]
+    // CHECK-NEXT: [[COL1_PTR:%.+]] = getelementptr inbounds nuw double, ptr 
[[ROW0_PTR]], i64 1
+    // Descriptor of the outer dimension: offset 0, count nx, stride (ny+2)*8.
+    // CHECK-NEXT: [[NY_B:%.+]] = load i32, ptr [[NY_ADDR]],
+    // CHECK-NEXT: [[NY2_B:%.+]] = add nsw i32 [[NY_B]], 2
+    // CHECK-NEXT: [[NY2_B64:%.+]] = zext i32 [[NY2_B]] to i64
+    // CHECK-NEXT: [[NX_B:%.+]] = load i32, ptr [[NX_ADDR]],
+    // CHECK-NEXT: [[COUNT0:%.+]] = zext i32 [[NX_B]] to i64
+    // CHECK-NEXT: [[STRIDE0:%.+]] = mul nuw i64 8, [[NY2_B64]]
+    // CHECK-NEXT: [[OFFSET0:%.+]] = mul nuw i64 [[STRIDE0]], 0
+
+    // The second list item, &a[0][ny] of the shaped array; its own shaping
+    // expression evaluates the dimensions again.
+    // CHECK-NEXT: [[BP2:%.+]] = load ptr, ptr [[A_ADDR]],
+    // CHECK-NEXT: [[NY_C:%.+]] = load i32, ptr [[NY_ADDR]],
+    // CHECK-NEXT: [[NY_IDX:%.+]] = sext i32 [[NY_C]] to i64
+    // CHECK-NEXT: [[NX_C:%.+]] = load i32, ptr [[NX_ADDR]],
+    // CHECK-NEXT: [[NX_C64:%.+]] = zext i32 [[NX_C]] to i64
+    // CHECK-NEXT: [[NY_D:%.+]] = load i32, ptr [[NY_ADDR]],
+    // CHECK-NEXT: [[NY2_D:%.+]] = add nsw i32 [[NY_D]], 2
+    // CHECK-NEXT: [[NY2_D64:%.+]] = zext i32 [[NY2_D]] to i64
+    // CHECK-NEXT: [[A2:%.+]] = load ptr, ptr [[A_ADDR]],
+    // CHECK-NEXT: [[ROW0_2:%.+]] = mul nsw i64 0, [[NY2_D64]]
+    // CHECK-NEXT: [[ROW0_PTR2:%.+]] = getelementptr inbounds nuw double, ptr 
[[A2]], i64 [[ROW0_2]]
+    // CHECK-NEXT: [[COLNY_PTR:%.+]] = getelementptr inbounds nuw double, ptr 
[[ROW0_PTR2]], i64 [[NY_IDX]]
+    // Its inner dimension starts at offset ny*8.
+    // CHECK-NEXT: [[NY_E:%.+]] = load i32, ptr [[NY_ADDR]],
+    // CHECK-NEXT: [[NY2_E:%.+]] = add nsw i32 [[NY_E]], 2
+    // CHECK-NEXT: [[NY2_E64:%.+]] = zext i32 [[NY2_E]] to i64
+    // CHECK-NEXT: [[NY_F:%.+]] = load i32, ptr [[NY_ADDR]],
+    // CHECK-NEXT: [[NY_F64:%.+]] = zext i32 [[NY_F]] to i64
+    // CHECK-NEXT: [[OFFSET1_2:%.+]] = mul nuw i64 8, [[NY_F64]]
+    // CHECK-NEXT: [[NX_G:%.+]] = load i32, ptr [[NX_ADDR]],
+    // CHECK-NEXT: [[COUNT0_2:%.+]] = zext i32 [[NX_G]] to i64
+    // CHECK-NEXT: [[STRIDE0_2:%.+]] = mul nuw i64 8, [[NY2_E64]]
+    // CHECK-NEXT: [[OFFSET0_2:%.+]] = mul nuw i64 [[STRIDE0_2]], 0
+
+    // CHECK:      store ptr [[BP]], ptr
+    // CHECK:      store ptr [[COL1_PTR]], ptr
+    // CHECK:      store ptr [[BP2]], ptr
+    // CHECK:      store ptr [[COLNY_PTR]], ptr
+
+    // Descriptor of the first list item: [0:nx] x [1] x element.
+    // CHECK:      [[DIM0:%.+]] = getelementptr inbounds [3 x 
[[DESC:%struct.descriptor_dim[.0-9]*]]], ptr [[DIMS:%.+]], i64 0, i64 0
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM0]], i32 0, i32 0
+    // CHECK-NEXT: store i64 [[OFFSET0]], ptr [[F]],
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM0]], i32 0, i32 1
+    // CHECK-NEXT: store i64 [[COUNT0]], ptr [[F]],
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM0]], i32 0, i32 2
+    // CHECK-NEXT: store i64 [[STRIDE0]], ptr [[F]],
+    // CHECK-NEXT: [[DIM1:%.+]] = getelementptr inbounds [3 x [[DESC]]], ptr 
[[DIMS]], i64 0, i64 1
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM1]], i32 0, i32 0
+    // CHECK-NEXT: store i64 8, ptr [[F]],
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM1]], i32 0, i32 1
+    // CHECK-NEXT: store i64 1, ptr [[F]],
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM1]], i32 0, i32 2
+    // CHECK-NEXT: store i64 8, ptr [[F]],
+    // CHECK-NEXT: [[DIM2:%.+]] = getelementptr inbounds [3 x [[DESC]]], ptr 
[[DIMS]], i64 0, i64 2
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM2]], i32 0, i32 0
+    // CHECK-NEXT: store i64 0, ptr [[F]],
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM2]], i32 0, i32 1
+    // CHECK-NEXT: store i64 8, ptr [[F]],
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM2]], i32 0, i32 2
+    // CHECK-NEXT: store i64 1, ptr [[F]],
+    // CHECK-NEXT: [[PTR0:%.+]] = getelementptr inbounds [2 x ptr], ptr 
[[PTRS:%.+]], i32 0, i32 0
+    // CHECK-NEXT: store ptr [[DIMS]], ptr [[PTR0]],
+
+    // Descriptor of the second list item: [0:nx] x [ny] x element.
+    // CHECK-NEXT: [[DIM0_2:%.+]] = getelementptr inbounds [3 x [[DESC]]], ptr 
[[DIMS2:%.+]], i64 0, i64 0
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM0_2]], i32 0, i32 0
+    // CHECK-NEXT: store i64 [[OFFSET0_2]], ptr [[F]],
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM0_2]], i32 0, i32 1
+    // CHECK-NEXT: store i64 [[COUNT0_2]], ptr [[F]],
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM0_2]], i32 0, i32 2
+    // CHECK-NEXT: store i64 [[STRIDE0_2]], ptr [[F]],
+    // CHECK-NEXT: [[DIM1_2:%.+]] = getelementptr inbounds [3 x [[DESC]]], ptr 
[[DIMS2]], i64 0, i64 1
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM1_2]], i32 0, i32 0
+    // CHECK-NEXT: store i64 [[OFFSET1_2]], ptr [[F]],
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM1_2]], i32 0, i32 1
+    // CHECK-NEXT: store i64 1, ptr [[F]],
+    // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[DESC]], ptr 
[[DIM1_2]], i32 0, i32 2
+    // CHECK-NEXT: store i64 8, ptr [[F]],
+    // CHECK:      [[PTR1:%.+]] = getelementptr inbounds [2 x ptr], ptr 
[[PTRS]], i32 0, i32 1
+    // CHECK-NEXT: store ptr [[DIMS2]], ptr [[PTR1]],
+    // CHECK:      call void @__tgt_target_data_update_mapper(ptr @{{.+}}, i64 
-1, i32 2, ptr %{{.+}}, ptr %{{.+}}, ptr [[FROM2_SIZES]], ptr [[FROM2_TYPES]], 
ptr null, ptr null)
+#pragma omp target update from((([nx][ny + 2])a)[0:nx][1], (([nx][ny + 
2])a)[0:nx][ny])
+
+    // CHECK:      call void @exch_data(
+    exch_data(a, nx, ny);
+
+    // CHECK:      call void @__tgt_target_data_update_mapper(ptr @{{.+}}, i64 
-1, i32 2, ptr %{{.+}}, ptr %{{.+}}, ptr [[TO2_SIZES]], ptr [[TO2_TYPES]], ptr 
null, ptr null)
+#pragma omp target update to((([nx][ny + 2])a)[0:nx][0], (([nx][ny + 
2])a)[0:nx][ny + 1])
+    // CHECK:      call void @__tgt_target_data_end_mapper(
+  }
+}
+
+// The same section of a declared variable length array: the dimension size is
+// not a constant, so contiguity cannot be decided at compile time and the
+// section is transferred through a descriptor as well (one entry, not whole
+// rows).
+// CHECK-LABEL: define {{.*}}void @vla_control(
+// CHECK:       store i32 %nx, ptr [[VNX_ADDR:%.+]],
+// CHECK-NEXT:  store i32 %ny, ptr [[VNY_ADDR:%.+]],
+void vla_control(int nx, int ny) {
+  double c[nx][ny + 2];
+  // CHECK:      [[VLA:%.+]] = alloca double, i64
+  // CHECK:      [[VROW0:%.+]] = mul nsw i64 0, [[VNY2_64:%.+]]
+  // CHECK-NEXT: [[VROW0_PTR:%.+]] = getelementptr inbounds nuw double, ptr 
[[VLA]], i64 [[VROW0]]
+  // CHECK-NEXT: [[VCOL1_PTR:%.+]] = getelementptr inbounds nuw double, ptr 
[[VROW0_PTR]], i64 1
+  // CHECK-NEXT: [[VNY_B:%.+]] = load i32, ptr [[VNY_ADDR]],
+  // CHECK-NEXT: [[VNY2_B:%.+]] = add nsw i32 [[VNY_B]], 2
+  // CHECK-NEXT: [[VNY2_B64:%.+]] = zext i32 [[VNY2_B]] to i64
+  // CHECK-NEXT: [[VNX_B:%.+]] = load i32, ptr [[VNX_ADDR]],
+  // CHECK-NEXT: [[VCOUNT0:%.+]] = zext i32 [[VNX_B]] to i64
+  // CHECK-NEXT: [[VSTRIDE0:%.+]] = mul nuw i64 8, [[VNY2_B64]]
+  // CHECK-NEXT: [[VOFFSET0:%.+]] = mul nuw i64 [[VSTRIDE0]], 0
+  // CHECK:      store ptr [[VLA]], ptr
+  // CHECK:      store ptr [[VCOL1_PTR]], ptr
+  // CHECK:      [[VDIM0:%.+]] = getelementptr inbounds [3 x 
[[VDESC:%struct.descriptor_dim[.0-9]*]]], ptr [[VDIMS:%.+]], i64 0, i64 0
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[VDESC]], ptr 
[[VDIM0]], i32 0, i32 0
+  // CHECK-NEXT: store i64 [[VOFFSET0]], ptr [[F]],
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[VDESC]], ptr 
[[VDIM0]], i32 0, i32 1
+  // CHECK-NEXT: store i64 [[VCOUNT0]], ptr [[F]],
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[VDESC]], ptr 
[[VDIM0]], i32 0, i32 2
+  // CHECK-NEXT: store i64 [[VSTRIDE0]], ptr [[F]],
+  // CHECK-NEXT: [[VDIM1:%.+]] = getelementptr inbounds [3 x [[VDESC]]], ptr 
[[VDIMS]], i64 0, i64 1
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[VDESC]], ptr 
[[VDIM1]], i32 0, i32 0
+  // CHECK-NEXT: store i64 8, ptr [[F]],
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[VDESC]], ptr 
[[VDIM1]], i32 0, i32 1
+  // CHECK-NEXT: store i64 1, ptr [[F]],
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[VDESC]], ptr 
[[VDIM1]], i32 0, i32 2
+  // CHECK-NEXT: store i64 8, ptr [[F]],
+  // CHECK:      store ptr [[VDIMS]], ptr
+  // CHECK:      call void @__tgt_target_data_update_mapper(ptr @{{.+}}, i64 
-1, i32 1, ptr %{{.+}}, ptr %{{.+}}, ptr [[VLA_SIZES]], ptr [[VLA_TYPES]], ptr 
null, ptr null)
+#pragma omp target update from(c[0:nx][1])
+}
+
+// Constant dimensions: the descriptors are compile-time constants.
+// CHECK-LABEL: define {{.*}}void @const_dims(
+// CHECK:       store ptr %p, ptr [[P_ADDR:%.+]],
+void const_dims(double *p) {
+  // &p[0][1] of the shaped double[3][4].
+  // CHECK:      [[CBP:%.+]] = load ptr, ptr [[P_ADDR]],
+  // CHECK-NEXT: [[CP:%.+]] = load ptr, ptr [[P_ADDR]],
+  // CHECK-NEXT: [[CROW0:%.+]] = getelementptr inbounds nuw [4 x double], ptr 
[[CP]], i64 0
+  // CHECK-NEXT: [[CDECAY:%.+]] = getelementptr inbounds [4 x double], ptr 
[[CROW0]], i64 0, i64 0
+  // CHECK-NEXT: [[CCOL1:%.+]] = getelementptr inbounds nuw double, ptr 
[[CDECAY]], i64 1
+  // CHECK:      store ptr [[CBP]], ptr
+  // CHECK:      store ptr [[CCOL1]], ptr
+  // [0:3]: offset 0, count 3, stride 4*8; [1]: offset 8, count 1, stride 8;
+  // element: offset 0, count 8 bytes, stride 1.
+  // CHECK:      [[CDIM0:%.+]] = getelementptr inbounds [3 x 
[[CDESC:%struct.descriptor_dim[.0-9]*]]], ptr [[CDIMS:%.+]], i64 0, i64 0
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[CDESC]], ptr 
[[CDIM0]], i32 0, i32 0
+  // CHECK-NEXT: store i64 0, ptr [[F]],
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[CDESC]], ptr 
[[CDIM0]], i32 0, i32 1
+  // CHECK-NEXT: store i64 3, ptr [[F]],
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[CDESC]], ptr 
[[CDIM0]], i32 0, i32 2
+  // CHECK-NEXT: store i64 32, ptr [[F]],
+  // CHECK-NEXT: [[CDIM1:%.+]] = getelementptr inbounds [3 x [[CDESC]]], ptr 
[[CDIMS]], i64 0, i64 1
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[CDESC]], ptr 
[[CDIM1]], i32 0, i32 0
+  // CHECK-NEXT: store i64 8, ptr [[F]],
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[CDESC]], ptr 
[[CDIM1]], i32 0, i32 1
+  // CHECK-NEXT: store i64 1, ptr [[F]],
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[CDESC]], ptr 
[[CDIM1]], i32 0, i32 2
+  // CHECK-NEXT: store i64 8, ptr [[F]],
+  // CHECK-NEXT: [[CDIM2:%.+]] = getelementptr inbounds [3 x [[CDESC]]], ptr 
[[CDIMS]], i64 0, i64 2
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[CDESC]], ptr 
[[CDIM2]], i32 0, i32 0
+  // CHECK-NEXT: store i64 0, ptr [[F]],
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[CDESC]], ptr 
[[CDIM2]], i32 0, i32 1
+  // CHECK-NEXT: store i64 8, ptr [[F]],
+  // CHECK-NEXT: [[F:%.+]] = getelementptr inbounds nuw [[CDESC]], ptr 
[[CDIM2]], i32 0, i32 2
+  // CHECK-NEXT: store i64 1, ptr [[F]],
+  // CHECK:      store ptr [[CDIMS]], ptr
+  // CHECK:      call void @__tgt_target_data_update_mapper(ptr @{{.+}}, i64 
-1, i32 1, ptr %{{.+}}, ptr %{{.+}}, ptr [[CONST_SIZES]], ptr [[CONST_TYPES]], 
ptr null, ptr null)
+#pragma omp target update from((([3][4])p)[0:3][1])
+
+  // A subscript of the shaped array selects one row, which is contiguous:
+  // &p[1][0], 4 doubles.
+  // CHECK:      [[RBP:%.+]] = load ptr, ptr [[P_ADDR]],
+  // CHECK-NEXT: [[RP:%.+]] = load ptr, ptr [[P_ADDR]],
+  // CHECK-NEXT: [[RROW1:%.+]] = getelementptr inbounds nuw [4 x double], ptr 
[[RP]], i64 1
+  // CHECK-NEXT: [[RDECAY:%.+]] = getelementptr inbounds [4 x double], ptr 
[[RROW1]], i64 0, i64 0
+  // CHECK-NEXT: [[RCOL0:%.+]] = getelementptr inbounds nuw double, ptr 
[[RDECAY]], i64 0
+  // CHECK:      store ptr [[RBP]], ptr
+  // CHECK:      store ptr [[RCOL0]], ptr
+  // CHECK:      call void @__tgt_target_data_update_mapper(ptr @{{.+}}, i64 
-1, i32 1, ptr %{{.+}}, ptr %{{.+}}, ptr [[ROW_SIZES]], ptr [[ROW_TYPES]], ptr 
null, ptr null)
+#pragma omp target update to((([3][4])p)[1][0:4])
+}
+
+struct S {
+  double *p;
+};
+double *g;
+
+// Other bases of the shaped pointer: a data member and a global variable; the
+// shaped pointer stays the base pointer of the entry.
+// CHECK-LABEL: define {{.*}}void @other_bases(
+void other_bases(struct S s, int n, int m) {
+  // CHECK:      call void @__tgt_target_data_update_mapper(ptr @{{.+}}, i64 
-1, i32 {{[0-9]+}}, ptr %{{.+}}, ptr %{{.+}}, ptr {{.+}}, ptr {{.+}}, ptr null, 
ptr null)
+#pragma omp target update to((([n])s.p)[1:n - 2])
+  // CHECK:      [[G:%.+]] = load ptr, ptr @g,
+  // CHECK:      store ptr [[G]], ptr
+  // CHECK:      call void @__tgt_target_data_update_mapper(ptr @{{.+}}, i64 
-1, i32 1, ptr %{{.+}}, ptr %{{.+}}, ptr @{{.+}}, ptr @{{.+}}, ptr null, ptr 
null)
+#pragma omp target update from((([n][m])g)[0:n][1])
+}
+
+// Other clauses of the directive, and the other clauses that allow array
+// shaping (depend, affinity) with a shaped base. With 'nowait' the update is
+// issued from an outlined target task.
+// CHECK-LABEL: define {{.*}}void @clauses(
+void clauses(double *p, int n, int m, int dev) {
+  // CHECK-DAG:  call ptr @__kmpc_omp_target_task_alloc(
+  // CHECK-DAG:  call i32 @__kmpc_omp_reg_task_with_affinity(
+  // CHECK-DAG:  call i32 @__kmpc_omp_task_with_deps(
+  // CHECK-DAG:  call void @__tgt_target_data_update_nowait_mapper(ptr 
@{{.+}}, i64 %{{.+}}, i32 1, ptr %{{.+}}, ptr %{{.+}}, ptr %{{.+}}, ptr 
@{{.+}}, ptr null, ptr null, i32 0, ptr null, i32 0, ptr null)
+#pragma omp target update to((([n][m])p)[0:n][1]) if(n > 2) device(dev) nowait
+#pragma omp task depend(in : (([n][m])p)[0:n][1]) affinity((([n][m])p)[0:n][1])
+  ;
+}
+
+#endif // HEADER
diff --git a/clang/test/OpenMP/target_map_messages.cpp 
b/clang/test/OpenMP/target_map_messages.cpp
index 0ee70bee9cc5d4..c7534b22fbf75a 100644
--- a/clang/test/OpenMP/target_map_messages.cpp
+++ b/clang/test/OpenMP/target_map_messages.cpp
@@ -47,6 +47,14 @@ void foo(int arg) {
   {}
   #pragma omp target map(marr[:][n:][:])
   {}
+
+  double *p = &marr[0][0][0];
+  // The shape-operator is allowed only in clauses that explicitly allow it;
+  // the map clause does not allow it as the base of an array section.
+  #pragma omp target map((([10])p)[0:10]) // expected-error {{OpenMP array 
shaping operation is not allowed here}}
+  {}
+  #pragma omp target map(to: (([2][10])p)[1][0:10]) // expected-error {{OpenMP 
array shaping operation is not allowed here}}
+  {}
 }
 #else
 
diff --git a/clang/test/OpenMP/target_update_ast_print.cpp 
b/clang/test/OpenMP/target_update_ast_print.cpp
index d4cc84bc8b73c0..de1849c91b7bc3 100644
--- a/clang/test/OpenMP/target_update_ast_print.cpp
+++ b/clang/test/OpenMP/target_update_ast_print.cpp
@@ -29,6 +29,8 @@ T foo(T targ, U uarg) {
 
 #pragma omp target update from(b, ([a][targ])p) if(l<5) device(l-1) nowait 
depend(inout:l)
 
+#pragma omp target update to((([a][targ])p)[0:l][1], (([a][targ])p)[0:l][l+1])
+
   U marr[10][10][10];
 #pragma omp target update to(marr[2][0:2][0:2])
 
@@ -76,16 +78,19 @@ T foo(T targ, U uarg) {
 // CHECK-NEXT: int l;
 // CHECK-NEXT: #pragma omp target update to(([a][targ])p,a) if(l > 5) 
device(l) nowait depend(inout : l){{$}}
 // CHECK-NEXT: #pragma omp target update from(b,([a][targ])p) if(l < 5) 
device(l - 1) nowait depend(inout : l)
+// CHECK-NEXT: #pragma omp target update 
to((([a][targ])p)[0:l][1],(([a][targ])p)[0:l][l + 1])
 // CHECK:      static int a, *p;
 // CHECK-NEXT: float b;
 // CHECK-NEXT: int l;
 // CHECK-NEXT: #pragma omp target update to(([a][targ])p,a) if(l > 5) 
device(l) nowait depend(inout : l)
 // CHECK-NEXT: #pragma omp target update from(b,([a][targ])p) if(l < 5) 
device(l - 1) nowait depend(inout : l)
+// CHECK-NEXT: #pragma omp target update 
to((([a][targ])p)[0:l][1],(([a][targ])p)[0:l][l + 1])
 // CHECK:      static char a, *p;
 // CHECK-NEXT: float b;
 // CHECK-NEXT: int l;
 // CHECK-NEXT: #pragma omp target update to(([a][targ])p,a) if(l > 5) 
device(l) nowait depend(inout : l)
 // CHECK-NEXT: #pragma omp target update from(b,([a][targ])p) if(l < 5) 
device(l - 1) nowait depend(inout : l)
+// CHECK-NEXT: #pragma omp target update 
to((([a][targ])p)[0:l][1],(([a][targ])p)[0:l][l + 1])
 // CHECK: marr[10][10][10];
 // CHECK-NEXT: #pragma omp target update to(marr[2][0:2][0:2])
 // CHECK-NEXT: #pragma omp target update from(marr[2][0:2][0:2])
@@ -123,6 +128,10 @@ int main(int argc, char **argv) {
 // CHECK-NEXT: #pragma omp target update to(argv[2][0:1:2])
 #pragma omp target update from(argv[2][0:1:2])
 // CHECK-NEXT: #pragma omp target update from(argv[2][0:1:2])
+#pragma omp target update to((([n][n])argv)[0:n][1])
+// CHECK-NEXT: #pragma omp target update to((([n][n])argv)[0:n][1])
+#pragma omp target update from((([n][n + 2])argv)[1][2:n:2])
+// CHECK-NEXT: #pragma omp target update from((([n][n + 2])argv)[1][2:n:2])
 #ifdef OMP51
 #pragma omp target update to(present: argv[2][0:1:2])
 // OMP5-NEXT: #pragma omp target update to(present: arr[2][0:1:2])
diff --git a/clang/test/OpenMP/target_update_from_messages.cpp 
b/clang/test/OpenMP/target_update_from_messages.cpp
index 02cd293c9a30a4..128f84f06b6128 100644
--- a/clang/test/OpenMP/target_update_from_messages.cpp
+++ b/clang/test/OpenMP/target_update_from_messages.cpp
@@ -1,9 +1,9 @@
-// RUN: %clang_cc1 -verify=expected,le51 -fopenmp -ferror-limit 100 %s 
-Wno-openmp-mapping -Wuninitialized
-// RUN: %clang_cc1 -verify=expected,le45 -fopenmp -fopenmp-version=40 
-ferror-limit 100 %s -Wno-openmp-mapping -Wuninitialized
-// RUN: %clang_cc1 -verify=expected,le45 -fopenmp -fopenmp-version=45 
-ferror-limit 100 %s -Wno-openmp-mapping -Wuninitialized
-// RUN: %clang_cc1 -verify=expected,le51 -fopenmp -fopenmp-version=50 
-ferror-limit 100 %s -Wno-openmp-mapping -Wuninitialized
+// RUN: %clang_cc1 -verify=expected,le51 -fopenmp -ferror-limit 300 %s 
-Wno-openmp-mapping -Wuninitialized
+// RUN: %clang_cc1 -verify=expected,le45 -fopenmp -fopenmp-version=40 
-ferror-limit 300 %s -Wno-openmp-mapping -Wuninitialized
+// RUN: %clang_cc1 -verify=expected,le45 -fopenmp -fopenmp-version=45 
-ferror-limit 300 %s -Wno-openmp-mapping -Wuninitialized
+// RUN: %clang_cc1 -verify=expected,le51 -fopenmp -fopenmp-version=50 
-ferror-limit 300 %s -Wno-openmp-mapping -Wuninitialized
 
-// RUN: %clang_cc1 -verify=expected,le51 -fopenmp-simd -ferror-limit 100 %s 
-Wno-openmp-mapping -Wuninitialized
+// RUN: %clang_cc1 -verify=expected,le51 -fopenmp-simd -ferror-limit 300 %s 
-Wno-openmp-mapping -Wuninitialized
 
 void foo() {
 }
@@ -119,7 +119,7 @@ T tmain(T argc) {
 #pragma omp target update from(ba)
 #pragma omp target update from(h) // expected-error {{threadprivate variables 
are not allowed in 'from' clause}} expected-error {{expected at least one 'to' 
clause or 'from' clause specified to '#pragma omp target update'}}
 #pragma omp target update from(k), to(k) // expected-error 2 {{variable can 
appear only once in OpenMP 'target update' construct}} expected-note 2 {{used 
here}}
-#pragma omp target update from(t), from(t[:5]) // expected-error 2 {{variable 
can appear only once in OpenMP 'target update' construct}} expected-note 2 
{{used here}}
+#pragma omp target update from(t), from(t[:5]) // le45-error 2 {{variable can 
appear only once in OpenMP 'target update' construct}} le45-note 2 {{used here}}
 #pragma omp target update from(da)
 #pragma omp target update from(da[:4])
 
@@ -138,6 +138,23 @@ T tmain(T argc) {
 #pragma omp target update from(s7.x)
   }
 
+// OpenMP 5.0 [2.1.4, Array Shaping]: the shaped pointer is the array of the
+// list item, so it can be the base of array sections and subscripts.
+#pragma omp target update from((([2][10])m)[0:2][1]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([I][10])m)[0:I][1], (([I][10])m)[0:I][5]) // 
le45-error 2 {{'I' in capture list does not name a variable}} le45-error 2 
{{expected body of lambda expression}} le45-error {{expected at least one 'to' 
clause or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][3][4])m)[0:2][1][0:4]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ')'}} le45-note {{to match this '('}} le45-error {{expected at least 
one 'to' clause or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[1][:]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[:2][2:]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[0:2][0:5:2]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ']'}} le45-note {{to match this '['}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[1]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[:][1]) // le51-error 2 {{array 
section does not specify length for outermost dimension}} le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2])i)[0:2]) // le51-error 2 {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} le45-error 
{{expected at least one 'to' clause or 'from' clause specified to '#pragma omp 
target update'}}
+#pragma omp target update from((([2])t)[0:2]) // le51-error 2 {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} le45-error 
{{expected at least one 'to' clause or 'from' clause specified to '#pragma omp 
target update'}}
+#pragma omp target update from((([2][10])m)[0:2][1]) to((([2][10])m)[0:2][1]) 
// le51-error 2 {{variable can appear only once in OpenMP 'target update' 
construct}} le51-note 2 {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update from(x, (([2][10])s7.p)[0:2][1]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ')'}} le45-note {{to match this '('}}
+#pragma omp target data map(from: (([10])m)[0:10]) // le51-error 2 {{OpenMP 
array shaping operation is not allowed here}} le45-error {{expected variable 
name or 'this' in lambda capture list}} le45-error {{expected ')'}} le45-note 
{{to match this '('}}
+  {}
+
   return 0;
 }
 
@@ -175,7 +192,7 @@ int main(int argc, char **argv) {
 #pragma omp target update from(ba)
 #pragma omp target update from(h) // expected-error {{threadprivate variables 
are not allowed in 'from' clause}} expected-error {{expected at least one 'to' 
clause or 'from' clause specified to '#pragma omp target update'}}
 #pragma omp target update from(k), to(k) // expected-error {{variable can 
appear only once in OpenMP 'target update' construct}} expected-note {{used 
here}}
-#pragma omp target update from(t), from(t[:5]) // expected-error {{variable 
can appear only once in OpenMP 'target update' construct}} expected-note {{used 
here}}
+#pragma omp target update from(t), from(t[:5]) // le45-error {{variable can 
appear only once in OpenMP 'target update' construct}} le45-note {{used here}}
 #pragma omp target update from(da)
 #pragma omp target update from(da[:4])
 
@@ -205,6 +222,24 @@ int main(int argc, char **argv) {
 #pragma omp target update from(s7.x)
   }
 
+// OpenMP 5.0 [2.1.4, Array Shaping]: the shaped pointer is the array of the
+// list item, so it can be the base of array sections and subscripts.
+#pragma omp target update from((([2][10])m)[0:2][1]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([argc][argc + 2])m)[0:argc][1], (([argc][argc 
+ 2])m)[0:argc][argc + 1]) // le45-error 2 {{expected body of lambda 
expression}} le45-error {{expected at least one 'to' clause or 'from' clause 
specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][3][4])m)[0:2][1][0:4]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ')'}} le45-note {{to match this '('}} le45-error {{expected at least 
one 'to' clause or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[1][:]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[:2][2:]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[0:2][0:5:2]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ']'}} le45-note {{to match this '['}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[1]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[:][1]) // le51-error {{array 
section does not specify length for outermost dimension}} le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2])i)[0:2]) // le51-error {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
+#pragma omp target update from((([2])t)[0:2]) // le51-error {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[0:2][1]) to((([2][10])m)[0:2][1]) 
// le51-error {{variable can appear only once in OpenMP 'target update' 
construct}} le51-note {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update from(x, (([2][10])s7.p)[0:2][1]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ')'}} le45-note {{to match this '('}}
+#pragma omp target update from((([2][10])BB)[0:2][1]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target data map(from: (([10])m)[0:10]) // le51-error {{OpenMP 
array shaping operation is not allowed here}} le45-error {{expected variable 
name or 'this' in lambda capture list}} le45-error {{expected ')'}} le45-note 
{{to match this '('}}
+  {}
+
   return tmain<int, 3>(argc)+tmain<to, 4>(argc); // expected-note {{in 
instantiation of function template specialization 'tmain<int, 3>' requested 
here}} expected-note {{in instantiation of function template specialization 
'tmain<int, 4>' requested here}}
 }
 
diff --git a/clang/test/OpenMP/target_update_to_messages.cpp 
b/clang/test/OpenMP/target_update_to_messages.cpp
index 33aaf3bb9028b5..49de404c459343 100644
--- a/clang/test/OpenMP/target_update_to_messages.cpp
+++ b/clang/test/OpenMP/target_update_to_messages.cpp
@@ -1,10 +1,10 @@
-// RUN: %clang_cc1 -verify=expected,le51 -fopenmp -ferror-limit 100 %s 
-Wno-openmp-mapping -Wuninitialized
-// RUN: %clang_cc1 -verify=expected,le45 -fopenmp -fopenmp-version=40 
-ferror-limit 100 %s -Wno-openmp-mapping -Wuninitialized
-// RUN: %clang_cc1 -verify=expected,le45 -fopenmp -fopenmp-version=45 
-ferror-limit 100 %s -Wno-openmp-mapping -Wuninitialized
+// RUN: %clang_cc1 -verify=expected,le51 -fopenmp -ferror-limit 300 %s 
-Wno-openmp-mapping -Wuninitialized
+// RUN: %clang_cc1 -verify=expected,le45 -fopenmp -fopenmp-version=40 
-ferror-limit 300 %s -Wno-openmp-mapping -Wuninitialized
+// RUN: %clang_cc1 -verify=expected,le45 -fopenmp -fopenmp-version=45 
-ferror-limit 300 %s -Wno-openmp-mapping -Wuninitialized
 
-// RUN: %clang_cc1 -verify=expected,le51 -fopenmp-simd -ferror-limit 100 %s 
-Wno-openmp-mapping -Wuninitialized
-// RUN: %clang_cc1 -verify=expected,le45 -fopenmp-simd -fopenmp-version=40 
-ferror-limit 100 %s -Wno-openmp-mapping -Wuninitialized
-// RUN: %clang_cc1 -verify=expected,le45 -fopenmp-simd -fopenmp-version=45 
-ferror-limit 100 %s -Wno-openmp-mapping -Wuninitialized
+// RUN: %clang_cc1 -verify=expected,le51 -fopenmp-simd -ferror-limit 300 %s 
-Wno-openmp-mapping -Wuninitialized
+// RUN: %clang_cc1 -verify=expected,le45 -fopenmp-simd -fopenmp-version=40 
-ferror-limit 300 %s -Wno-openmp-mapping -Wuninitialized
+// RUN: %clang_cc1 -verify=expected,le45 -fopenmp-simd -fopenmp-version=45 
-ferror-limit 300 %s -Wno-openmp-mapping -Wuninitialized
 
 void foo() {
 }
@@ -124,7 +124,7 @@ T tmain(T argc) {
 #pragma omp target update to(ba)
 #pragma omp target update to(h) // expected-error {{threadprivate variables 
are not allowed in 'to' clause}} expected-error {{expected at least one 'to' 
clause or 'from' clause specified to '#pragma omp target update'}}
 #pragma omp target update to(k), from(k) // expected-error 2 {{variable can 
appear only once in OpenMP 'target update' construct}} expected-note 2 {{used 
here}}
-#pragma omp target update to(t), to(t[:5]) // expected-error 2 {{variable can 
appear only once in OpenMP 'target update' construct}} expected-note 2 {{used 
here}}
+#pragma omp target update to(t), to(t[:5]) // le45-error 2 {{variable can 
appear only once in OpenMP 'target update' construct}} le45-note 2 {{used here}}
 #pragma omp target update to(da)
 #pragma omp target update to(da[:4])
 
@@ -142,6 +142,23 @@ T tmain(T argc) {
   {
 #pragma omp target update to(s7.x)
   }
+
+// OpenMP 5.0 [2.1.4, Array Shaping]: the shaped pointer is the array of the
+// list item, so it can be the base of array sections and subscripts.
+#pragma omp target update to((([2][10])m)[0:2][1]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([I][10])m)[0:I][1], (([I][10])m)[0:I][5]) // 
le45-error 2 {{'I' in capture list does not name a variable}} le45-error 2 
{{expected body of lambda expression}} le45-error {{expected at least one 'to' 
clause or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][3][4])m)[0:2][1][0:4]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ')'}} le45-note {{to match this '('}} le45-error {{expected at least 
one 'to' clause or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[1][:]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[:2][2:]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[0:2][0:5:2]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ']'}} le45-note {{to match this '['}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[1]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[:][1]) // le51-error 2 {{array 
section does not specify length for outermost dimension}} le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2])i)[0:2]) // le51-error 2 {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} le45-error 
{{expected at least one 'to' clause or 'from' clause specified to '#pragma omp 
target update'}}
+#pragma omp target update to((([2])t)[0:2]) // le51-error 2 {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} le45-error 
{{expected at least one 'to' clause or 'from' clause specified to '#pragma omp 
target update'}}
+#pragma omp target update to((([2][10])m)[0:2][1]) from((([2][10])m)[0:2][1]) 
// le51-error 2 {{variable can appear only once in OpenMP 'target update' 
construct}} le51-note 2 {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update to(x, (([2][10])s7.p)[0:2][1]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ')'}} le45-note {{to match this '('}}
+#pragma omp target data map(to: (([10])m)[0:10]) // le51-error 2 {{OpenMP 
array shaping operation is not allowed here}} le45-error {{expected variable 
name or 'this' in lambda capture list}} le45-error {{expected ')'}} le45-note 
{{to match this '('}}
+  {}
   return 0;
 }
 
@@ -179,7 +196,7 @@ int main(int argc, char **argv) {
 #pragma omp target update to(ba)
 #pragma omp target update to(h) // expected-error {{threadprivate variables 
are not allowed in 'to' clause}} expected-error {{expected at least one 'to' 
clause or 'from' clause specified to '#pragma omp target update'}}
 #pragma omp target update to(k), from(k) // expected-error {{variable can 
appear only once in OpenMP 'target update' construct}} expected-note {{used 
here}}
-#pragma omp target update to(t), to(t[:5]) // expected-error {{variable can 
appear only once in OpenMP 'target update' construct}} expected-note {{used 
here}}
+#pragma omp target update to(t), to(t[:5]) // le45-error {{variable can appear 
only once in OpenMP 'target update' construct}} le45-note {{used here}}
 #pragma omp target update to(da)
 #pragma omp target update to(da[:4])
 
@@ -210,6 +227,25 @@ int main(int argc, char **argv) {
 #pragma omp target update to(*(m+(m))) // expected-error {{invalid operands to 
binary expression ('int *' and 'int *')}}
 #pragma omp target update to(*(1+y+y)) // expected-error {{indirection 
requires pointer operand ('int' invalid)}}
   {}
+
+// OpenMP 5.0 [2.1.4, Array Shaping]: the shaped pointer is the array of the
+// list item, so it can be the base of array sections and subscripts.
+#pragma omp target update to((([2][10])m)[0:2][1]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([argc][argc + 2])m)[0:argc][1], (([argc][argc + 
2])m)[0:argc][argc + 1]) // le45-error 2 {{expected body of lambda expression}} 
le45-error {{expected at least one 'to' clause or 'from' clause specified to 
'#pragma omp target update'}}
+#pragma omp target update to((([2][3][4])m)[0:2][1][0:4]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ')'}} le45-note {{to match this '('}} le45-error {{expected at least 
one 'to' clause or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[1][:]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[:2][2:]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[0:2][0:5:2]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ']'}} le45-note {{to match this '['}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[1]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[:][1]) // le51-error {{array section 
does not specify length for outermost dimension}} le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2])i)[0:2]) // le51-error {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
+#pragma omp target update to((([2])t)[0:2]) // le51-error {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[0:2][1]) from((([2][10])m)[0:2][1]) 
// le51-error {{variable can appear only once in OpenMP 'target update' 
construct}} le51-note {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update to(x, (([2][10])s7.p)[0:2][1]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ')'}} le45-note {{to match this '('}}
+#pragma omp target update to((([2][10])BB)[0:2][1]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target update to(present: (([2][10])m)[0:2][1]) // le45-error 
{{use of undeclared identifier 'present'}} le45-error {{expected at least one 
'to' clause or 'from' clause specified to '#pragma omp target update'}}
+#pragma omp target data map(to: (([10])m)[0:10]) // le51-error {{OpenMP array 
shaping operation is not allowed here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}}
+  {}
   return tmain<int, 3>(argc)+tmain<from, 4>(argc); // expected-note {{in 
instantiation of function template specialization 'tmain<int, 3>' requested 
here}} expected-note {{in instantiation of function template specialization 
'tmain<int, 4>' requested here}}
 }
 

>From 1352c61eea29b5c7a33698d4241bd20c2917d855 Mon Sep 17 00:00:00 2001
From: Akash Manna <[email protected]>
Date: Sun, 22 Nov 2026 09:59:29 +0530
Subject: [PATCH 2/2] [Clang][OpenMP] Address review comments on shaped array
 sections

Shaping expressions no longer take part in the conflict redundancy walk,
and a matching array component without a declaration is not dereferenced.
A variable length array with an explicit length is never a whole
dimension. A non-contiguous section of a shaped pointer uses the pointer
value as its base and only its own sections as descriptor dimensions.
Oversized constant dimensions and function pointees are rejected, and the
whole-size checks use APSInt.
---
 clang/lib/CodeGen/CGOpenMPRuntime.cpp         | 29 ++++++++-
 clang/lib/Sema/SemaOpenMP.cpp                 | 60 +++++++++----------
 clang/test/OpenMP/GH136294.c                  | 42 ++++++++++---
 .../OpenMP/target_update_from_messages.cpp    |  7 +++
 .../test/OpenMP/target_update_to_messages.cpp |  7 +++
 5 files changed, 104 insertions(+), 41 deletions(-)

diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 0dfc276158c256..f3ed789516cc68 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -8456,6 +8456,20 @@ class MappableExprsHandler {
           dyn_cast<OMPArrayShapingExpr>(I->getAssociatedExpression());
       const auto *UO = dyn_cast<UnaryOperator>(I->getAssociatedExpression());
       const auto *BO = dyn_cast<BinaryOperator>(I->getAssociatedExpression());
+      // A non-contiguous section of a shaped array is transferred on its own:
+      // the shaped pointer is its base pointer, whatever expression (member,
+      // subscript, global) yields it.
+      if (OAShE && Next != CE && IsNonContiguous) {
+        BP = Address(
+            CGF.EmitScalarExpr(OAShE->getBase()),
+            CGF.ConvertTypeForMem(
+                OAShE->getBase()->getType()->getPointeeType()),
+            CGF.getContext().getTypeAlignInChars(OAShE->getBase()->getType()));
+        EncounteredME = nullptr;
+        ShouldBeMemberOf = false;
+        FirstPointerInComplexData = false;
+        continue;
+      }
       // An array shaping expression maps the shaped storage only as the whole
       // list item; as the base of array sections it just reinterprets the
       // pointer.
@@ -8757,9 +8771,18 @@ class MappableExprsHandler {
     MapValuesArrayTy DimSizes{llvm::ConstantInt::get(CGF.CGM.Int64Ty, 1)};
     uint64_t ElementTypeSize;
 
+    // Components past an array shaping expression belong to the shaped pointer
+    // base, not to dimensions of the shaped array.
+    auto ArrayDimsEnd = llvm::find_if(
+        Components,
+        [](const OMPClauseMappableExprCommon::MappableComponent &Component) {
+          return isa<OMPArrayShapingExpr>(Component.getAssociatedExpression());
+        });
+    auto ArrayDimComponents =
+        llvm::make_range(Components.begin(), ArrayDimsEnd);
     // Only array sections and subscripts add a dimension.
     unsigned NumArrayDims = llvm::count_if(
-        Components,
+        ArrayDimComponents,
         [](const OMPClauseMappableExprCommon::MappableComponent &Component) {
           return isa<ArraySectionExpr, ArraySubscriptExpr>(
               Component.getAssociatedExpression());
@@ -8769,7 +8792,7 @@ class MappableExprsHandler {
     // the first Stride. For example, for `int arr[10][10]`, the DimSizes
     // should be [10, 10] and the first stride is 4 btyes.
     for (const OMPClauseMappableExprCommon::MappableComponent &Component :
-         Components) {
+         ArrayDimComponents) {
       const Expr *AssocExpr = Component.getAssociatedExpression();
       const auto *OASE = dyn_cast<ArraySectionExpr>(AssocExpr);
 
@@ -8849,7 +8872,7 @@ class MappableExprsHandler {
     // count, and stride are the same as the number of non-contiguous
     // declaration in target update to/from clause.
     for (const OMPClauseMappableExprCommon::MappableComponent &Component :
-         Components) {
+         ArrayDimComponents) {
       const Expr *AssocExpr = Component.getAssociatedExpression();
 
       if (const auto *AE = dyn_cast<ArraySubscriptExpr>(AssocExpr)) {
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 9a722df5d424de..51604969e8688c 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -24508,25 +24508,25 @@ static bool 
checkArrayExpressionRefersToWholeSize(Sema &SemaRef,
                                                   const ArraySectionExpr *OASE,
                                                   QualType BaseQTy) {
   ASTContext &Ctx = SemaRef.getASTContext();
-  auto EvaluatesTo = [&Ctx](const Expr *E, int64_t Value) {
+  auto EvaluatesTo = [&Ctx](const Expr *E, const llvm::APSInt &Value) {
     Expr::EvalResult Result;
     return !E->isValueDependent() && E->EvaluateAsInt(Result, Ctx) &&
-           Result.Val.getInt().getSExtValue() == Value;
+           llvm::APSInt::isSameValue(Result.Val.getInt(), Value);
   };
 
   if (OASE->getColonLocFirst().isInvalid()) {
     const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
-    return CATy && CATy->getSExtSize() == 1;
+    return CATy && CATy->getSize().isOne();
   }
 
   const Expr *Stride = OASE->getStride();
-  if (Stride && !EvaluatesTo(Stride, 1))
+  if (Stride && !EvaluatesTo(Stride, llvm::APSInt::get(1)))
     return false;
   const Expr *LowerBound = OASE->getLowerBound();
-  if (LowerBound && !EvaluatesTo(LowerBound, 0))
+  if (LowerBound && !EvaluatesTo(LowerBound, llvm::APSInt::get(0)))
     return false;
 
-  if (BaseQTy->isPointerType())
+  if (!BaseQTy->isConstantArrayType() && !BaseQTy->isVariableArrayType())
     return false;
 
   // Without a length the section covers the rest of the dimension.
@@ -24534,19 +24534,11 @@ static bool 
checkArrayExpressionRefersToWholeSize(Sema &SemaRef,
   if (!Length)
     return true;
 
-  if (const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
-    return EvaluatesTo(Length, CATy->getSExtSize());
-
-  // For a variable length array the length has to be the size expression of
-  // the dimension, e.g. c[0:n] for double c[n][m].
-  const auto *VATy = dyn_cast<VariableArrayType>(BaseQTy.getTypePtr());
-  if (!VATy || !VATy->getSizeExpr() || Length->HasSideEffects(Ctx))
-    return false;
-  llvm::FoldingSetNodeID LengthID, SizeID;
-  Length->IgnoreParenImpCasts()->Profile(LengthID, Ctx, /*Canonical=*/true);
-  VATy->getSizeExpr()->IgnoreParenImpCasts()->Profile(SizeID, Ctx,
-                                                      /*Canonical=*/true);
-  return LengthID == SizeID;
+  // The size expression of a variable length array may have changed since the
+  // declaration, so only a constant size can be compared with the length.
+  const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
+  return CATy && EvaluatesTo(Length, llvm::APSInt(CATy->getSize(),
+                                                  /*isUnsigned=*/true));
 }
 
 // The base of elements of list in a map clause have to be either:
@@ -25114,9 +25106,6 @@ static bool checkMapConflicts(
                          SI->getAssociatedExpression())) {
             const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
             Type = ArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
-          } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>(
-                         SI->getAssociatedExpression())) {
-            Type = OASE->getBase()->getType()->getPointeeType();
           }
           if (Type.isNull() || Type->isAnyPointerType() ||
               checkArrayExpressionDoesNotReferToWholeSize(
@@ -25149,15 +25138,20 @@ static bool checkMapConflicts(
           return false;
         }
 
-        QualType DerivedType =
-            std::prev(CI)->getAssociatedDeclaration()->getType();
+        // The last matching component has no declaration when both
+        // expressions section the same array (OpenMP 5.0 motion clauses).
+        QualType DerivedType;
+        if (const ValueDecl *DerivedDecl =
+                std::prev(CI)->getAssociatedDeclaration())
+          DerivedType = DerivedDecl->getType();
         SourceLocation DerivedLoc =
             std::prev(CI)->getAssociatedExpression()->getExprLoc();
 
         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
         //  If the type of a list item is a reference to a type T then the type
         //  will be considered to be T for all purposes of this clause.
-        DerivedType = DerivedType.getNonReferenceType();
+        if (!DerivedType.isNull())
+          DerivedType = DerivedType.getNonReferenceType();
 
         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
         //  A variable for which the type is pointer and an array section
@@ -25170,7 +25164,7 @@ static bool checkMapConflicts(
         //  storage in the device data environment, all of the original storage
         //  must have corresponding storage in the device data environment.
         //
-        if (DerivedType->isAnyPointerType()) {
+        if (!DerivedType.isNull() && DerivedType->isAnyPointerType()) {
           if (CI == CE || SI == SE) {
             SemaRef.Diag(
                 DerivedLoc,
@@ -28603,7 +28597,8 @@ ExprResult SemaOpenMP::ActOnOMPArrayShapingExpr(
                                        RParenLoc, Dims, Brackets);
   if (!BaseTy->isPointerType() ||
       (!Base->isTypeDependent() &&
-       BaseTy->getPointeeType()->isIncompleteType()))
+       (BaseTy->getPointeeType()->isIncompleteType() ||
+        BaseTy->getPointeeType()->isFunctionType())))
     return ExprError(Diag(Base->getExprLoc(),
                           diag::err_omp_non_pointer_type_array_shaping_base)
                      << Base->getSourceRange());
@@ -28657,7 +28652,8 @@ ExprResult SemaOpenMP::ActOnOMPArrayShapingExpr(
   // OpenMP 5.0 [2.1.4, Array Shaping]: the result has n-dimensional array type
   // s1 x ... x sn of T; build it like the type of a declared array.
   QualType ShapedTy = BaseTy->getPointeeType();
-  for (Expr *Dim : llvm::reverse(NewDims)) {
+  for (size_t I = NewDims.size(); I > 0; --I) {
+    Expr *Dim = NewDims[I - 1];
     if (Dim->isTypeDependent() || Dim->isValueDependent()) {
       ShapedTy = Context.getDependentSizedArrayType(
           ShapedTy, Dim, ArraySizeModifier::Normal, /*IndexTypeQuals=*/0);
@@ -28667,13 +28663,17 @@ ExprResult SemaOpenMP::ActOnOMPArrayShapingExpr(
     // An array of variable length arrays is itself a variable length array.
     if (ShapedTy->isDependentType() || ShapedTy->isConstantSizeType())
       Size = Dim->getIntegerConstantExpr(Context);
-    if (Size)
+    if (Size) {
+      if (SemaRef.checkArrayTooLarge(ShapedTy, *Size, Dim->getExprLoc(),
+                                     Brackets[I - 1]))
+        return ExprError();
       ShapedTy = Context.getConstantArrayType(ShapedTy, *Size, Dim,
                                               ArraySizeModifier::Normal,
                                               /*IndexTypeQuals=*/0);
-    else
+    } else {
       ShapedTy = Context.getVariableArrayType(
           ShapedTy, Dim, ArraySizeModifier::Normal, /*IndexTypeQuals=*/0);
+    }
   }
   return OMPArrayShapingExpr::Create(Context, Context.OMPArrayShapingTy,
                                      ShapedTy, Base, LParenLoc, RParenLoc,
diff --git a/clang/test/OpenMP/GH136294.c b/clang/test/OpenMP/GH136294.c
index e283a23574b6b1..68e8883d16c0ed 100644
--- a/clang/test/OpenMP/GH136294.c
+++ b/clang/test/OpenMP/GH136294.c
@@ -31,6 +31,14 @@
 // A row of the shaped array is contiguous: 4 doubles, TO.
 // CHECK-DAG: [[ROW_SIZES:@.+]] = private unnamed_addr constant [1 x i64] [i64 
32]
 // CHECK-DAG: [[ROW_TYPES:@.+]] = private unnamed_addr constant [1 x i64] [i64 
1]
+// Shaped pointers that are a member, a global and an array element: one entry
+// with the three descriptor dimensions each.
+// CHECK-DAG: [[MEMBER_SIZES:@.+]] = private unnamed_addr constant [1 x i64] 
[i64 3]
+// CHECK-DAG: [[MEMBER_TYPES:@.+]] = private unnamed_addr constant [1 x i64] 
[i64 17592186044418]
+// CHECK-DAG: [[GLOBAL_SIZES:@.+]] = private unnamed_addr constant [1 x i64] 
[i64 3]
+// CHECK-DAG: [[GLOBAL_TYPES:@.+]] = private unnamed_addr constant [1 x i64] 
[i64 17592186044418]
+// CHECK-DAG: [[ELEMENT_SIZES:@.+]] = private unnamed_addr constant [1 x i64] 
[i64 3]
+// CHECK-DAG: [[ELEMENT_TYPES:@.+]] = private unnamed_addr constant [1 x i64] 
[i64 17592186044417]
 
 void exch_data(double *a, int nx, int ny);
 
@@ -47,7 +55,8 @@ void array_ing(double *a, int nx, int ny) {
 
     // The base pointer is the value of 'a'; the dimensions of the shape are
     // evaluated like the sizes of a variable length array.
-    // CHECK:      [[BP:%.+]] = load ptr, ptr [[A_ADDR]],
+    // CHECK:      load ptr, ptr [[A_ADDR]],
+    // CHECK-NEXT: [[BP:%.+]] = load ptr, ptr [[A_ADDR]],
     // CHECK-NEXT: [[NX:%.+]] = load i32, ptr [[NX_ADDR]],
     // CHECK-NEXT: [[NX_64:%.+]] = zext i32 [[NX]] to i64
     // CHECK-NEXT: [[NY:%.+]] = load i32, ptr [[NY_ADDR]],
@@ -69,6 +78,7 @@ void array_ing(double *a, int nx, int ny) {
 
     // The second list item, &a[0][ny] of the shaped array; its own shaping
     // expression evaluates the dimensions again.
+    // CHECK-NEXT: load ptr, ptr [[A_ADDR]],
     // CHECK-NEXT: [[BP2:%.+]] = load ptr, ptr [[A_ADDR]],
     // CHECK-NEXT: [[NY_C:%.+]] = load i32, ptr [[NY_ADDR]],
     // CHECK-NEXT: [[NY_IDX:%.+]] = sext i32 [[NY_C]] to i64
@@ -198,7 +208,8 @@ void vla_control(int nx, int ny) {
 // CHECK:       store ptr %p, ptr [[P_ADDR:%.+]],
 void const_dims(double *p) {
   // &p[0][1] of the shaped double[3][4].
-  // CHECK:      [[CBP:%.+]] = load ptr, ptr [[P_ADDR]],
+  // CHECK:      load ptr, ptr [[P_ADDR]],
+  // CHECK-NEXT: [[CBP:%.+]] = load ptr, ptr [[P_ADDR]],
   // CHECK-NEXT: [[CP:%.+]] = load ptr, ptr [[P_ADDR]],
   // CHECK-NEXT: [[CROW0:%.+]] = getelementptr inbounds nuw [4 x double], ptr 
[[CP]], i64 0
   // CHECK-NEXT: [[CDECAY:%.+]] = getelementptr inbounds [4 x double], ptr 
[[CROW0]], i64 0, i64 0
@@ -250,16 +261,31 @@ struct S {
 };
 double *g;
 
-// Other bases of the shaped pointer: a data member and a global variable; the
-// shaped pointer stays the base pointer of the entry.
+// Other bases of the shaped pointer: a data member, a global variable and an
+// element of an array of pointers. The shaped pointer value is the base
+// pointer of the entry, and only the sections of the shaped array are
+// dimensions of the descriptor.
 // CHECK-LABEL: define {{.*}}void @other_bases(
-void other_bases(struct S s, int n, int m) {
-  // CHECK:      call void @__tgt_target_data_update_mapper(ptr @{{.+}}, i64 
-1, i32 {{[0-9]+}}, ptr %{{.+}}, ptr %{{.+}}, ptr {{.+}}, ptr {{.+}}, ptr null, 
ptr null)
-#pragma omp target update to((([n])s.p)[1:n - 2])
+void other_bases(struct S s, double **pp, int n, int m) {
+  // CHECK:      [[SP:%.+]] = load ptr, ptr [[SP_ADDR:%.+]],
+  // CHECK:      store ptr [[SP]], ptr
+  // CHECK:      getelementptr inbounds [3 x %struct.descriptor_dim{{.*}}], 
ptr %{{.+}}, i64 0, i64 0
+  // CHECK:      call void @__tgt_target_data_update_mapper(ptr @{{.+}}, i64 
-1, i32 1, ptr %{{.+}}, ptr %{{.+}}, ptr [[MEMBER_SIZES]], ptr 
[[MEMBER_TYPES]], ptr null, ptr null)
+#pragma omp target update from((([n][m])s.p)[0:n][1])
   // CHECK:      [[G:%.+]] = load ptr, ptr @g,
   // CHECK:      store ptr [[G]], ptr
-  // CHECK:      call void @__tgt_target_data_update_mapper(ptr @{{.+}}, i64 
-1, i32 1, ptr %{{.+}}, ptr %{{.+}}, ptr @{{.+}}, ptr @{{.+}}, ptr null, ptr 
null)
+  // CHECK:      call void @__tgt_target_data_update_mapper(ptr @{{.+}}, i64 
-1, i32 1, ptr %{{.+}}, ptr %{{.+}}, ptr [[GLOBAL_SIZES]], ptr 
[[GLOBAL_TYPES]], ptr null, ptr null)
 #pragma omp target update from((([n][m])g)[0:n][1])
+  // CHECK:      [[PP3_ADDR:%.+]] = getelementptr inbounds ptr, ptr %{{.+}}, 
i64 3
+  // CHECK-NEXT: [[PP3:%.+]] = load ptr, ptr [[PP3_ADDR]],
+  // CHECK:      store ptr [[PP3]], ptr
+  // CHECK:      getelementptr inbounds [3 x %struct.descriptor_dim{{.*}}], 
ptr %{{.+}}, i64 0, i64 0
+  // CHECK:      call void @__tgt_target_data_update_mapper(ptr @{{.+}}, i64 
-1, i32 1, ptr %{{.+}}, ptr %{{.+}}, ptr [[ELEMENT_SIZES]], ptr 
[[ELEMENT_TYPES]], ptr null, ptr null)
+#pragma omp target update to((([n][m])pp[3])[0:n][1])
+  // A contiguous section keeps the pointer-attachment path: the member is the
+  // base pointer there as well.
+  // CHECK:      call void @__tgt_target_data_update_mapper(ptr @{{.+}}, i64 
-1, i32 1, ptr %{{.+}}, ptr %{{.+}}, ptr %{{.+}}, ptr @{{.+}}, ptr null, ptr 
null)
+#pragma omp target update to((([n])s.p)[1:n - 2])
 }
 
 // Other clauses of the directive, and the other clauses that allow array
diff --git a/clang/test/OpenMP/target_update_from_messages.cpp 
b/clang/test/OpenMP/target_update_from_messages.cpp
index 128f84f06b6128..3b7e4c58de2fb4 100644
--- a/clang/test/OpenMP/target_update_from_messages.cpp
+++ b/clang/test/OpenMP/target_update_from_messages.cpp
@@ -151,6 +151,8 @@ T tmain(T argc) {
 #pragma omp target update from((([2])i)[0:2]) // le51-error 2 {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} le45-error 
{{expected at least one 'to' clause or 'from' clause specified to '#pragma omp 
target update'}}
 #pragma omp target update from((([2])t)[0:2]) // le51-error 2 {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} le45-error 
{{expected at least one 'to' clause or 'from' clause specified to '#pragma omp 
target update'}}
 #pragma omp target update from((([2][10])m)[0:2][1]) to((([2][10])m)[0:2][1]) 
// le51-error 2 {{variable can appear only once in OpenMP 'target update' 
construct}} le51-note 2 {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[0:2]) to((([2][10])m)[0:2][1]) // 
le51-error 2 {{variable can appear only once in OpenMP 'target update' 
construct}} le51-note 2 {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[0:2][1]) to(m[0]) // le51-error 2 
{{same pointer dereferenced in multiple different ways in map clause 
expressions}} le51-note 2 {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
 #pragma omp target update from(x, (([2][10])s7.p)[0:2][1]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ')'}} le45-note {{to match this '('}}
 #pragma omp target data map(from: (([10])m)[0:10]) // le51-error 2 {{OpenMP 
array shaping operation is not allowed here}} le45-error {{expected variable 
name or 'this' in lambda capture list}} le45-error {{expected ')'}} le45-note 
{{to match this '('}}
   {}
@@ -174,6 +176,7 @@ int main(int argc, char **argv) {
   S7 s7;
   int **BB, *offset;
   int ***BBB;
+  void (*fp)(void);
 
 #pragma omp target update from // expected-error {{expected '(' after 'from'}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
 #pragma omp target update from( // expected-error {{expected ')'}} 
expected-note {{to match this '('}} expected-error {{expected expression}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
@@ -235,6 +238,10 @@ int main(int argc, char **argv) {
 #pragma omp target update from((([2])i)[0:2]) // le51-error {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
 #pragma omp target update from((([2])t)[0:2]) // le51-error {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
 #pragma omp target update from((([2][10])m)[0:2][1]) to((([2][10])m)[0:2][1]) 
// le51-error {{variable can appear only once in OpenMP 'target update' 
construct}} le51-note {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[0:2]) to((([2][10])m)[0:2][1]) // 
le51-error {{variable can appear only once in OpenMP 'target update' 
construct}} le51-note {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([2][10])m)[0:2][1]) to(m[0]) // le51-error 
{{same pointer dereferenced in multiple different ways in map clause 
expressions}} le51-note {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update from((([1ULL << 62])m)[0:1]) // le51-error {{array 
is too large}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
+#pragma omp target update from((([2])fp)[0:1]) // le51-error {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
 #pragma omp target update from(x, (([2][10])s7.p)[0:2][1]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ')'}} le45-note {{to match this '('}}
 #pragma omp target update from((([2][10])BB)[0:2][1]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
 #pragma omp target data map(from: (([10])m)[0:10]) // le51-error {{OpenMP 
array shaping operation is not allowed here}} le45-error {{expected variable 
name or 'this' in lambda capture list}} le45-error {{expected ')'}} le45-note 
{{to match this '('}}
diff --git a/clang/test/OpenMP/target_update_to_messages.cpp 
b/clang/test/OpenMP/target_update_to_messages.cpp
index 82277d3ed1cd9f..ee6c5104cd6c9a 100644
--- a/clang/test/OpenMP/target_update_to_messages.cpp
+++ b/clang/test/OpenMP/target_update_to_messages.cpp
@@ -156,6 +156,8 @@ T tmain(T argc) {
 #pragma omp target update to((([2])i)[0:2]) // le51-error 2 {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} le45-error 
{{expected at least one 'to' clause or 'from' clause specified to '#pragma omp 
target update'}}
 #pragma omp target update to((([2])t)[0:2]) // le51-error 2 {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} le45-error 
{{expected at least one 'to' clause or 'from' clause specified to '#pragma omp 
target update'}}
 #pragma omp target update to((([2][10])m)[0:2][1]) from((([2][10])m)[0:2][1]) 
// le51-error 2 {{variable can appear only once in OpenMP 'target update' 
construct}} le51-note 2 {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[0:2]) from((([2][10])m)[0:2][1]) // 
le51-error 2 {{variable can appear only once in OpenMP 'target update' 
construct}} le51-note 2 {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[0:2][1]) from(m[0]) // le51-error 2 
{{same pointer dereferenced in multiple different ways in map clause 
expressions}} le51-note 2 {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
 #pragma omp target update to(x, (([2][10])s7.p)[0:2][1]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ')'}} le45-note {{to match this '('}}
 #pragma omp target data map(to: (([10])m)[0:10]) // le51-error 2 {{OpenMP 
array shaping operation is not allowed here}} le45-error {{expected variable 
name or 'this' in lambda capture list}} le45-error {{expected ')'}} le45-note 
{{to match this '('}}
   {}
@@ -178,6 +180,7 @@ int main(int argc, char **argv) {
   int *m;
   int **BB, *offset;
   int ***BBB;
+  void (*fp)(void);
   S8 s8v, *s8p = &s8v;
   int *S8::*s8mp = &S8::ptr;
 
@@ -245,6 +248,10 @@ int main(int argc, char **argv) {
 #pragma omp target update to((([2])i)[0:2]) // le51-error {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
 #pragma omp target update to((([2])t)[0:2]) // le51-error {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
 #pragma omp target update to((([2][10])m)[0:2][1]) from((([2][10])m)[0:2][1]) 
// le51-error {{variable can appear only once in OpenMP 'target update' 
construct}} le51-note {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[0:2]) from((([2][10])m)[0:2][1]) // 
le51-error {{variable can appear only once in OpenMP 'target update' 
construct}} le51-note {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([2][10])m)[0:2][1]) from(m[0]) // le51-error 
{{same pointer dereferenced in multiple different ways in map clause 
expressions}} le51-note {{used here}} le45-error {{expected variable name or 
'this' in lambda capture list}} le45-error {{expected ')'}} le45-note {{to 
match this '('}} le45-error {{expected at least one 'to' clause or 'from' 
clause specified to '#pragma omp target update'}}
+#pragma omp target update to((([1ULL << 62])m)[0:1]) // le51-error {{array is 
too large}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
+#pragma omp target update to((([2])fp)[0:1]) // le51-error {{expected 
expression with a pointer to a complete type as a base of an array shaping 
operation}} le45-error {{expected variable name or 'this' in lambda capture 
list}} le45-error {{expected ')'}} le45-note {{to match this '('}} 
expected-error {{expected at least one 'to' clause or 'from' clause specified 
to '#pragma omp target update'}}
 #pragma omp target update to(x, (([2][10])s7.p)[0:2][1]) // le45-error 
{{expected variable name or 'this' in lambda capture list}} le45-error 
{{expected ')'}} le45-note {{to match this '('}}
 #pragma omp target update to((([2][10])BB)[0:2][1]) // le45-error {{expected 
variable name or 'this' in lambda capture list}} le45-error {{expected ')'}} 
le45-note {{to match this '('}} le45-error {{expected at least one 'to' clause 
or 'from' clause specified to '#pragma omp target update'}}
 #pragma omp target update to(present: (([2][10])m)[0:2][1]) // le45-error 
{{use of undeclared identifier 'present'}} le45-error {{expected at least one 
'to' clause or 'from' clause specified to '#pragma omp target update'}}

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

Reply via email to