diff options
Diffstat (limited to 'clang/lib/CodeGen')
| -rw-r--r-- | clang/lib/CodeGen/CGDeclCXX.cpp | 2 | ||||
| -rw-r--r-- | clang/lib/CodeGen/CGExpr.cpp | 19 | ||||
| -rw-r--r-- | clang/lib/CodeGen/CGOpenMPRuntime.cpp | 73 | ||||
| -rw-r--r-- | clang/lib/CodeGen/CGOpenMPRuntime.h | 4 | ||||
| -rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 14 |
5 files changed, 76 insertions, 36 deletions
diff --git a/clang/lib/CodeGen/CGDeclCXX.cpp b/clang/lib/CodeGen/CGDeclCXX.cpp index 199e0ac99e8..b03e3faeb33 100644 --- a/clang/lib/CodeGen/CGDeclCXX.cpp +++ b/clang/lib/CodeGen/CGDeclCXX.cpp @@ -74,7 +74,7 @@ static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D, // bails even if the attribute is not present. if (D.isNoDestroy(CGF.getContext())) return; - + CodeGenModule &CGM = CGF.CGM; // FIXME: __attribute__((cleanup)) ? diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index f0d8619faa3..a2a4bca2a5c 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -2295,15 +2295,22 @@ static LValue EmitThreadPrivateVarDeclLValue( return CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl); } -static Address emitDeclTargetLinkVarDeclLValue(CodeGenFunction &CGF, - const VarDecl *VD, QualType T) { +static Address emitDeclTargetVarDeclLValue(CodeGenFunction &CGF, + const VarDecl *VD, QualType T) { llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); - if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_To) + // Return an invalid address if variable is MT_To and unified + // memory is not enabled. For all other cases: MT_Link and + // MT_To with unified memory, return a valid address. + if (!Res || (*Res == OMPDeclareTargetDeclAttr::MT_To && + !CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())) return Address::invalid(); - assert(*Res == OMPDeclareTargetDeclAttr::MT_Link && "Expected link clause"); + assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) || + (*Res == OMPDeclareTargetDeclAttr::MT_To && + CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())) && + "Expected link clause OR to clause with unified memory enabled."); QualType PtrTy = CGF.getContext().getPointerType(VD->getType()); - Address Addr = CGF.CGM.getOpenMPRuntime().getAddrOfDeclareTargetLink(VD); + Address Addr = CGF.CGM.getOpenMPRuntime().getAddrOfDeclareTargetVar(VD); return CGF.EmitLoadOfPointer(Addr, PtrTy->castAs<PointerType>()); } @@ -2359,7 +2366,7 @@ static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, // Check if the variable is marked as declare target with link clause in // device codegen. if (CGF.getLangOpts().OpenMPIsDevice) { - Address Addr = emitDeclTargetLinkVarDeclLValue(CGF, VD, T); + Address Addr = emitDeclTargetVarDeclLValue(CGF, VD, T); if (Addr.isValid()) return CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl); } diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp index 1a1e0b0af0b..602c9a238d4 100644 --- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -2552,16 +2552,18 @@ CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize, bool IVSigned) { return CGM.CreateRuntimeFunction(FnTy, Name); } -Address CGOpenMPRuntime::getAddrOfDeclareTargetLink(const VarDecl *VD) { +Address CGOpenMPRuntime::getAddrOfDeclareTargetVar(const VarDecl *VD) { if (CGM.getLangOpts().OpenMPSimd) return Address::invalid(); llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); - if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) { + if (Res && (*Res == OMPDeclareTargetDeclAttr::MT_Link || + (*Res == OMPDeclareTargetDeclAttr::MT_To && + HasRequiresUnifiedSharedMemory))) { SmallString<64> PtrName; { llvm::raw_svector_ostream OS(PtrName); - OS << CGM.getMangledName(GlobalDecl(VD)) << "_decl_tgt_link_ptr"; + OS << CGM.getMangledName(GlobalDecl(VD)) << "_decl_tgt_ref_ptr"; } llvm::Value *Ptr = CGM.getModule().getNamedValue(PtrName); if (!Ptr) { @@ -2778,7 +2780,9 @@ bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD, bool PerformInit) { Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); - if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link) + if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link || + (*Res == OMPDeclareTargetDeclAttr::MT_To && + HasRequiresUnifiedSharedMemory)) return CGM.getLangOpts().OpenMPIsDevice; VD = VD->getDefinition(CGM.getContext()); if (VD && !DeclareTargetWithDefinition.insert(CGM.getMangledName(VD)).second) @@ -4194,6 +4198,9 @@ void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() { CE->getFlags()); switch (Flags) { case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo: { + if (CGM.getLangOpts().OpenMPIsDevice && + CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory()) + continue; if (!CE->getAddress()) { unsigned DiagID = CGM.getDiags().getCustomDiagID( DiagnosticsEngine::Error, @@ -7452,7 +7459,10 @@ private: // Track if the map information being generated is the first for a capture. bool IsCaptureFirstInfo = IsFirstComponentList; - bool IsLink = false; // Is this variable a "declare target link"? + // When the variable is on a declare target link or in a to clause with + // unified memory, a reference is needed to hold the host/device address + // of the variable. + bool RequiresReference = false; // Scan the components from the base to the complete expression. auto CI = Components.rbegin(); @@ -7482,11 +7492,14 @@ private: if (const auto *VD = dyn_cast_or_null<VarDecl>(I->getAssociatedDeclaration())) { if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = - OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) - if (*Res == OMPDeclareTargetDeclAttr::MT_Link) { - IsLink = true; - BP = CGF.CGM.getOpenMPRuntime().getAddrOfDeclareTargetLink(VD); + OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { + if ((*Res == OMPDeclareTargetDeclAttr::MT_Link) || + (*Res == OMPDeclareTargetDeclAttr::MT_To && + CGF.CGM.getOpenMPRuntime().hasRequiresUnifiedSharedMemory())) { + RequiresReference = true; + BP = CGF.CGM.getOpenMPRuntime().getAddrOfDeclareTargetVar(VD); } + } } // If the variable is a pointer and is being dereferenced (i.e. is not @@ -7652,7 +7665,8 @@ private: // (there is a set of entries for each capture). OpenMPOffloadMappingFlags Flags = getMapTypeBits( MapType, MapModifiers, IsImplicit, - !IsExpressionFirstInfo || IsLink, IsCaptureFirstInfo && !IsLink); + !IsExpressionFirstInfo || RequiresReference, + IsCaptureFirstInfo && !RequiresReference); if (!IsExpressionFirstInfo) { // If we have a PTR_AND_OBJ pair where the OBJ is a pointer as well, @@ -9124,7 +9138,9 @@ bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) { llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration( cast<VarDecl>(GD.getDecl())); - if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link) { + if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link || + (*Res == OMPDeclareTargetDeclAttr::MT_To && + HasRequiresUnifiedSharedMemory)) { DeferredGlobalVariables.insert(cast<VarDecl>(GD.getDecl())); return true; } @@ -9183,8 +9199,9 @@ void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD, StringRef VarName; CharUnits VarSize; llvm::GlobalValue::LinkageTypes Linkage; - switch (*Res) { - case OMPDeclareTargetDeclAttr::MT_To: + + if (*Res == OMPDeclareTargetDeclAttr::MT_To && + !HasRequiresUnifiedSharedMemory) { Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo; VarName = CGM.getMangledName(VD); if (VD->hasDefinition(CGM.getContext()) != VarDecl::DeclarationOnly) { @@ -9207,20 +9224,27 @@ void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD, CGM.addCompilerUsedGlobal(GVAddrRef); } } - break; - case OMPDeclareTargetDeclAttr::MT_Link: - Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink; + } else { + assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) || + (*Res == OMPDeclareTargetDeclAttr::MT_To && + HasRequiresUnifiedSharedMemory)) && + "Declare target attribute must link or to with unified memory."); + if (*Res == OMPDeclareTargetDeclAttr::MT_Link) + Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink; + else + Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo; + if (CGM.getLangOpts().OpenMPIsDevice) { VarName = Addr->getName(); Addr = nullptr; } else { - VarName = getAddrOfDeclareTargetLink(VD).getName(); - Addr = cast<llvm::Constant>(getAddrOfDeclareTargetLink(VD).getPointer()); + VarName = getAddrOfDeclareTargetVar(VD).getName(); + Addr = cast<llvm::Constant>(getAddrOfDeclareTargetVar(VD).getPointer()); } VarSize = CGM.getPointerSize(); Linkage = llvm::GlobalValue::WeakAnyLinkage; - break; } + OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo( VarName, Addr, VarSize, Flags, Linkage); } @@ -9239,12 +9263,15 @@ void CGOpenMPRuntime::emitDeferredTargetDecls() const { OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD); if (!Res) continue; - if (*Res == OMPDeclareTargetDeclAttr::MT_To) { + if (*Res == OMPDeclareTargetDeclAttr::MT_To && + !HasRequiresUnifiedSharedMemory) { CGM.EmitGlobal(VD); } else { - assert(*Res == OMPDeclareTargetDeclAttr::MT_Link && - "Expected to or link clauses."); - (void)CGM.getOpenMPRuntime().getAddrOfDeclareTargetLink(VD); + assert((*Res == OMPDeclareTargetDeclAttr::MT_Link || + (*Res == OMPDeclareTargetDeclAttr::MT_To && + HasRequiresUnifiedSharedMemory)) && + "Expected link clause or to clause with unified memory."); + (void)CGM.getOpenMPRuntime().getAddrOfDeclareTargetVar(VD); } } } diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.h b/clang/lib/CodeGen/CGOpenMPRuntime.h index 5be322de239..3f842ce9640 100644 --- a/clang/lib/CodeGen/CGOpenMPRuntime.h +++ b/clang/lib/CodeGen/CGOpenMPRuntime.h @@ -1121,8 +1121,8 @@ public: SourceLocation Loc); /// Returns the address of the variable marked as declare target with link - /// clause. - virtual Address getAddrOfDeclareTargetLink(const VarDecl *VD); + /// clause OR as declare target with to clause and unified memory. + virtual Address getAddrOfDeclareTargetVar(const VarDecl *VD); /// Emit a code for initialization of threadprivate variable. It emits /// a call to runtime library which adds initial value to the newly created diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 559f3a73d38..d0f62beed96 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -2475,13 +2475,19 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) { // Emit declaration of the must-be-emitted declare target variable. if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { - if (*Res == OMPDeclareTargetDeclAttr::MT_To) { + bool UnifiedMemoryEnabled = + getOpenMPRuntime().hasRequiresUnifiedSharedMemory(); + if (*Res == OMPDeclareTargetDeclAttr::MT_To && + !UnifiedMemoryEnabled) { (void)GetAddrOfGlobalVar(VD); } else { - assert(*Res == OMPDeclareTargetDeclAttr::MT_Link && - "link claue expected."); - (void)getOpenMPRuntime().getAddrOfDeclareTargetLink(VD); + assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) || + (*Res == OMPDeclareTargetDeclAttr::MT_To && + UnifiedMemoryEnabled)) && + "Link clause or to clause with unified memory expected."); + (void)getOpenMPRuntime().getAddrOfDeclareTargetVar(VD); } + return; } } |

