2828#include "clang/Basic/SourceManager.h"
2929#include "clang/CodeGen/ConstantInitBuilder.h"
3030#include "llvm/ADT/ArrayRef.h"
31+ #include "llvm/ADT/SmallSet.h"
3132#include "llvm/ADT/SmallVector.h"
3233#include "llvm/ADT/StringExtras.h"
3334#include "llvm/Bitcode/BitcodeReader.h"
@@ -7211,6 +7212,9 @@ class MappableExprsHandler {
72117212 /// firstprivate, false otherwise.
72127213 llvm::DenseMap<CanonicalDeclPtr<const VarDecl>, bool> FirstPrivateDecls;
72137214
7215+ /// Set of defaultmap clause kinds that use firstprivate behavior.
7216+ llvm::SmallSet<OpenMPDefaultmapClauseKind, 4> DefaultmapFirstprivateKinds;
7217+
72147218 /// Map between device pointer declarations and their expression components.
72157219 /// The key value for declarations in 'this' is null.
72167220 llvm::DenseMap<
@@ -8989,6 +8993,10 @@ class MappableExprsHandler {
89898993 FirstPrivateDecls.try_emplace(VD, /*Implicit=*/true);
89908994 }
89918995 }
8996+ // Extract defaultmap clause information.
8997+ for (const auto *C : Dir.getClausesOfKind<OMPDefaultmapClause>())
8998+ if (C->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_firstprivate)
8999+ DefaultmapFirstprivateKinds.insert(C->getDefaultmapKind());
89929000 // Extract device pointer clause information.
89939001 for (const auto *C : Dir.getClausesOfKind<OMPIsDevicePtrClause>())
89949002 for (auto L : C->component_lists())
@@ -9566,6 +9574,37 @@ class MappableExprsHandler {
95669574 }
95679575 }
95689576
9577+ /// Check if a variable should be treated as firstprivate literal.
9578+ /// Returns true ONLY for explicit firstprivate clauses, not for implicit
9579+ /// captures via defaultmap(firstprivate:pointer). Implicitly captured
9580+ /// pointers need runtime lookup to get their device addresses.
9581+ bool isEffectivelyFirstprivate(const VarDecl *VD, QualType Type) const {
9582+ // Check explicit firstprivate clauses (not implicit from defaultmap)
9583+ auto I = FirstPrivateDecls.find(VD);
9584+ if (I != FirstPrivateDecls.end() && !I->getSecond())
9585+ return true; // Explicit firstprivate only
9586+
9587+ // For non-pointer types, defaultmap(firstprivate:...) should also
9588+ // be treated as firstprivate literals since they're passed by value
9589+ if (Type->isAnyPointerType())
9590+ return false; // Pointers from defaultmap need runtime lookup
9591+
9592+ // Check defaultmap(firstprivate:scalar) for scalar types
9593+ if (DefaultmapFirstprivateKinds.count(OMPC_DEFAULTMAP_scalar)) {
9594+ if (Type->isScalarType())
9595+ return true;
9596+ }
9597+
9598+ // Check defaultmap(firstprivate:aggregate) for aggregate types
9599+ if (DefaultmapFirstprivateKinds.count(OMPC_DEFAULTMAP_aggregate)) {
9600+ if (Type->isAggregateType())
9601+ return true;
9602+ }
9603+
9604+ // Check defaultmap(firstprivate:all) for all types
9605+ return DefaultmapFirstprivateKinds.count(OMPC_DEFAULTMAP_all);
9606+ }
9607+
95699608 /// Generate the default map information for a given capture \a CI,
95709609 /// record field declaration \a RI and captured value \a CV.
95719610 void generateDefaultMapInfo(const CapturedStmt::Capture &CI,
@@ -9593,13 +9632,23 @@ class MappableExprsHandler {
95939632 CombinedInfo.DevicePtrDecls.push_back(nullptr);
95949633 CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
95959634 CombinedInfo.Pointers.push_back(CV);
9635+ bool IsFirstprivate =
9636+ isEffectivelyFirstprivate(VD, RI.getType().getNonReferenceType());
9637+
95969638 if (!RI.getType()->isAnyPointerType()) {
95979639 // We have to signal to the runtime captures passed by value that are
95989640 // not pointers.
95999641 CombinedInfo.Types.push_back(
96009642 OpenMPOffloadMappingFlags::OMP_MAP_LITERAL);
96019643 CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
96029644 CGF.getTypeSize(RI.getType()), CGF.Int64Ty, /*isSigned=*/true));
9645+ } else if (IsFirstprivate) {
9646+ // Firstprivate pointers should be passed by value (as literals)
9647+ // without performing a present table lookup at runtime.
9648+ CombinedInfo.Types.push_back(
9649+ OpenMPOffloadMappingFlags::OMP_MAP_LITERAL);
9650+ // Use zero size for pointer literals (just passing the pointer value)
9651+ CombinedInfo.Sizes.push_back(llvm::Constant::getNullValue(CGF.Int64Ty));
96039652 } else {
96049653 // Pointers are implicitly mapped with a zero size and no flags
96059654 // (other than first map that is added for all implicit maps).
@@ -9613,26 +9662,31 @@ class MappableExprsHandler {
96139662 assert(CI.capturesVariable() && "Expected captured reference.");
96149663 const auto *PtrTy = cast<ReferenceType>(RI.getType().getTypePtr());
96159664 QualType ElementType = PtrTy->getPointeeType();
9616- CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
9617- CGF.getTypeSize(ElementType), CGF.Int64Ty, /*isSigned=*/true));
9618- // The default map type for a scalar/complex type is 'to' because by
9619- // default the value doesn't have to be retrieved. For an aggregate
9620- // type, the default is 'tofrom'.
9621- CombinedInfo.Types.push_back(getMapModifiersForPrivateClauses(CI));
96229665 const VarDecl *VD = CI.getCapturedVar();
9623- auto I = FirstPrivateDecls.find (VD);
9666+ bool IsFirstprivate = isEffectivelyFirstprivate (VD, ElementType );
96249667 CombinedInfo.Exprs.push_back(VD->getCanonicalDecl());
96259668 CombinedInfo.BasePointers.push_back(CV);
96269669 CombinedInfo.DevicePtrDecls.push_back(nullptr);
96279670 CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
9628- if (I != FirstPrivateDecls.end() && ElementType->isAnyPointerType()) {
9629- Address PtrAddr = CGF.EmitLoadOfReference(CGF.MakeAddrLValue(
9630- CV, ElementType, CGF.getContext().getDeclAlign(VD),
9631- AlignmentSource::Decl));
9632- CombinedInfo.Pointers.push_back(PtrAddr.emitRawPointer(CGF));
9671+
9672+ // For firstprivate pointers, pass by value instead of dereferencing
9673+ if (IsFirstprivate && ElementType->isAnyPointerType()) {
9674+ // Treat as a literal value (pass the pointer value itself)
9675+ CombinedInfo.Pointers.push_back(CV);
9676+ // Use zero size for pointer literals
9677+ CombinedInfo.Sizes.push_back(llvm::Constant::getNullValue(CGF.Int64Ty));
9678+ CombinedInfo.Types.push_back(
9679+ OpenMPOffloadMappingFlags::OMP_MAP_LITERAL);
96339680 } else {
9681+ CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
9682+ CGF.getTypeSize(ElementType), CGF.Int64Ty, /*isSigned=*/true));
9683+ // The default map type for a scalar/complex type is 'to' because by
9684+ // default the value doesn't have to be retrieved. For an aggregate
9685+ // type, the default is 'tofrom'.
9686+ CombinedInfo.Types.push_back(getMapModifiersForPrivateClauses(CI));
96349687 CombinedInfo.Pointers.push_back(CV);
96359688 }
9689+ auto I = FirstPrivateDecls.find(VD);
96369690 if (I != FirstPrivateDecls.end())
96379691 IsImplicit = I->getSecond();
96389692 }
0 commit comments