diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.h b/clang/lib/CodeGen/CGOpenMPRuntime.h --- a/clang/lib/CodeGen/CGOpenMPRuntime.h +++ b/clang/lib/CodeGen/CGOpenMPRuntime.h @@ -2488,6 +2488,20 @@ } }; +/// Encapsulates helper methods to be used by target specific specializations +/// of CGOpenMPRuntimeGPU. +class CodeGenUtil { +public: + static FieldDecl *addFieldToRecordDecl(ASTContext &C, DeclContext *DC, + QualType FieldTy); + + template + static llvm::GlobalVariable * + createGlobalStruct(CodeGenModule &CGM, QualType Ty, bool IsConstant, + ArrayRef Data, const Twine &Name, + As &&... Args); +}; + } // namespace CodeGen } // namespace clang diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp --- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -1048,8 +1048,9 @@ AlignmentSource::Decl); } -static FieldDecl *addFieldToRecordDecl(ASTContext &C, DeclContext *DC, - QualType FieldTy) { +FieldDecl *clang::CodeGen::CodeGenUtil::addFieldToRecordDecl(ASTContext &C, + DeclContext *DC, + QualType FieldTy) { auto *Field = FieldDecl::Create( C, DC, SourceLocation(), SourceLocation(), /*Id=*/nullptr, FieldTy, C.getTrivialTypeSourceInfo(FieldTy, SourceLocation()), @@ -1353,10 +1354,10 @@ } template -static llvm::GlobalVariable * -createGlobalStruct(CodeGenModule &CGM, QualType Ty, bool IsConstant, - ArrayRef Data, const Twine &Name, - As &&... Args) { +llvm::GlobalVariable *clang::CodeGen::CodeGenUtil::createGlobalStruct( + CodeGenModule &CGM, QualType Ty, bool IsConstant, + ArrayRef Data, const Twine &Name, As &&... Args) { + const auto *RD = cast(Ty->getAsTagDecl()); const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD); ConstantInitBuilder CIBuilder(CGM); @@ -3082,7 +3083,7 @@ llvm::ConstantInt::get(CGM.Int32Ty, Flags), llvm::ConstantInt::get(CGM.Int32Ty, 0)}; std::string EntryName = getName({"omp_offloading", "entry", ""}); - llvm::GlobalVariable *Entry = createGlobalStruct( + llvm::GlobalVariable *Entry = CodeGenUtil::createGlobalStruct( CGM, getTgtOffloadEntryQTy(), /*IsConstant=*/true, Data, Twine(EntryName).concat(Name), llvm::GlobalValue::WeakAnyLinkage); @@ -3360,12 +3361,12 @@ ASTContext &C = CGM.getContext(); RecordDecl *RD = C.buildImplicitRecord("__tgt_offload_entry"); RD->startDefinition(); - addFieldToRecordDecl(C, RD, C.VoidPtrTy); - addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy)); - addFieldToRecordDecl(C, RD, C.getSizeType()); - addFieldToRecordDecl( + CodeGenUtil::addFieldToRecordDecl(C, RD, C.VoidPtrTy); + CodeGenUtil::addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy)); + CodeGenUtil::addFieldToRecordDecl(C, RD, C.getSizeType()); + CodeGenUtil::addFieldToRecordDecl( C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); - addFieldToRecordDecl( + CodeGenUtil::addFieldToRecordDecl( C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); RD->completeDefinition(); RD->addAttr(PackedAttr::CreateImplicit(C)); @@ -3423,7 +3424,7 @@ if (isAllocatableDecl(VD)) Type = C.getPointerType(Type); } - FieldDecl *FD = addFieldToRecordDecl(C, RD, Type); + FieldDecl *FD = CodeGenUtil::addFieldToRecordDecl(C, RD, Type); if (VD->hasAttrs()) { for (specific_attr_iterator I(VD->getAttrs().begin()), E(VD->getAttrs().end()); @@ -3457,27 +3458,27 @@ // }; RecordDecl *UD = C.buildImplicitRecord("kmp_cmplrdata_t", TTK_Union); UD->startDefinition(); - addFieldToRecordDecl(C, UD, KmpInt32Ty); - addFieldToRecordDecl(C, UD, KmpRoutineEntryPointerQTy); + CodeGenUtil::addFieldToRecordDecl(C, UD, KmpInt32Ty); + CodeGenUtil::addFieldToRecordDecl(C, UD, KmpRoutineEntryPointerQTy); UD->completeDefinition(); QualType KmpCmplrdataTy = C.getRecordType(UD); RecordDecl *RD = C.buildImplicitRecord("kmp_task_t"); RD->startDefinition(); - addFieldToRecordDecl(C, RD, C.VoidPtrTy); - addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy); - addFieldToRecordDecl(C, RD, KmpInt32Ty); - addFieldToRecordDecl(C, RD, KmpCmplrdataTy); - addFieldToRecordDecl(C, RD, KmpCmplrdataTy); + CodeGenUtil::addFieldToRecordDecl(C, RD, C.VoidPtrTy); + CodeGenUtil::addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy); + CodeGenUtil::addFieldToRecordDecl(C, RD, KmpInt32Ty); + CodeGenUtil::addFieldToRecordDecl(C, RD, KmpCmplrdataTy); + CodeGenUtil::addFieldToRecordDecl(C, RD, KmpCmplrdataTy); if (isOpenMPTaskLoopDirective(Kind)) { QualType KmpUInt64Ty = CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0); QualType KmpInt64Ty = CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1); - addFieldToRecordDecl(C, RD, KmpUInt64Ty); - addFieldToRecordDecl(C, RD, KmpUInt64Ty); - addFieldToRecordDecl(C, RD, KmpInt64Ty); - addFieldToRecordDecl(C, RD, KmpInt32Ty); - addFieldToRecordDecl(C, RD, C.VoidPtrTy); + CodeGenUtil::addFieldToRecordDecl(C, RD, KmpUInt64Ty); + CodeGenUtil::addFieldToRecordDecl(C, RD, KmpUInt64Ty); + CodeGenUtil::addFieldToRecordDecl(C, RD, KmpInt64Ty); + CodeGenUtil::addFieldToRecordDecl(C, RD, KmpInt32Ty); + CodeGenUtil::addFieldToRecordDecl(C, RD, C.VoidPtrTy); } RD->completeDefinition(); return RD; @@ -3493,9 +3494,9 @@ // }; RecordDecl *RD = C.buildImplicitRecord("kmp_task_t_with_privates"); RD->startDefinition(); - addFieldToRecordDecl(C, RD, KmpTaskTQTy); + CodeGenUtil::addFieldToRecordDecl(C, RD, KmpTaskTQTy); if (const RecordDecl *PrivateRD = createPrivatesRecordDecl(CGM, Privates)) - addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD)); + CodeGenUtil::addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD)); RD->completeDefinition(); return RD; } @@ -4128,9 +4129,9 @@ RecordDecl *KmpAffinityInfoRD = C.buildImplicitRecord("kmp_task_affinity_info_t"); KmpAffinityInfoRD->startDefinition(); - addFieldToRecordDecl(C, KmpAffinityInfoRD, C.getIntPtrType()); - addFieldToRecordDecl(C, KmpAffinityInfoRD, C.getSizeType()); - addFieldToRecordDecl(C, KmpAffinityInfoRD, FlagsTy); + CodeGenUtil::addFieldToRecordDecl(C, KmpAffinityInfoRD, C.getIntPtrType()); + CodeGenUtil::addFieldToRecordDecl(C, KmpAffinityInfoRD, C.getSizeType()); + CodeGenUtil::addFieldToRecordDecl(C, KmpAffinityInfoRD, FlagsTy); KmpAffinityInfoRD->completeDefinition(); KmpTaskAffinityInfoTy = C.getRecordType(KmpAffinityInfoRD); } @@ -4568,9 +4569,9 @@ if (KmpDependInfoTy.isNull()) { RecordDecl *KmpDependInfoRD = C.buildImplicitRecord("kmp_depend_info"); KmpDependInfoRD->startDefinition(); - addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType()); - addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType()); - addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy); + CodeGenUtil::addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType()); + CodeGenUtil::addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType()); + CodeGenUtil::addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy); KmpDependInfoRD->completeDefinition(); KmpDependInfoTy = C.getRecordType(KmpDependInfoRD); } @@ -6017,13 +6018,19 @@ ASTContext &C = CGM.getContext(); RecordDecl *RD = C.buildImplicitRecord("kmp_taskred_input_t"); RD->startDefinition(); - const FieldDecl *SharedFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); - const FieldDecl *OrigFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); - const FieldDecl *SizeFD = addFieldToRecordDecl(C, RD, C.getSizeType()); - const FieldDecl *InitFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); - const FieldDecl *FiniFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); - const FieldDecl *CombFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy); - const FieldDecl *FlagsFD = addFieldToRecordDecl( + const FieldDecl *SharedFD = + CodeGenUtil::addFieldToRecordDecl(C, RD, C.VoidPtrTy); + const FieldDecl *OrigFD = + CodeGenUtil::addFieldToRecordDecl(C, RD, C.VoidPtrTy); + const FieldDecl *SizeFD = + CodeGenUtil::addFieldToRecordDecl(C, RD, C.getSizeType()); + const FieldDecl *InitFD = + CodeGenUtil::addFieldToRecordDecl(C, RD, C.VoidPtrTy); + const FieldDecl *FiniFD = + CodeGenUtil::addFieldToRecordDecl(C, RD, C.VoidPtrTy); + const FieldDecl *CombFD = + CodeGenUtil::addFieldToRecordDecl(C, RD, C.VoidPtrTy); + const FieldDecl *FlagsFD = CodeGenUtil::addFieldToRecordDecl( C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false)); RD->completeDefinition(); QualType RDType = C.getRecordType(RD); @@ -9058,9 +9065,9 @@ RecordDecl *RD; RD = C.buildImplicitRecord("descriptor_dim"); RD->startDefinition(); - addFieldToRecordDecl(C, RD, Int64Ty); - addFieldToRecordDecl(C, RD, Int64Ty); - addFieldToRecordDecl(C, RD, Int64Ty); + CodeGenUtil::addFieldToRecordDecl(C, RD, Int64Ty); + CodeGenUtil::addFieldToRecordDecl(C, RD, Int64Ty); + CodeGenUtil::addFieldToRecordDecl(C, RD, Int64Ty); RD->completeDefinition(); QualType DimTy = C.getRecordType(RD); @@ -11669,9 +11676,9 @@ // }; RD = C.buildImplicitRecord("kmp_dim"); RD->startDefinition(); - addFieldToRecordDecl(C, RD, Int64Ty); - addFieldToRecordDecl(C, RD, Int64Ty); - addFieldToRecordDecl(C, RD, Int64Ty); + CodeGenUtil::addFieldToRecordDecl(C, RD, Int64Ty); + CodeGenUtil::addFieldToRecordDecl(C, RD, Int64Ty); + CodeGenUtil::addFieldToRecordDecl(C, RD, Int64Ty); RD->completeDefinition(); KmpDimTy = C.getRecordType(RD); } else { @@ -12124,8 +12131,9 @@ if (VI == I->getSecond().end()) { RecordDecl *RD = C.buildImplicitRecord("lasprivate.conditional"); RD->startDefinition(); - VDField = addFieldToRecordDecl(C, RD, VD->getType().getNonReferenceType()); - FiredField = addFieldToRecordDecl(C, RD, C.CharTy); + VDField = CodeGenUtil::addFieldToRecordDecl( + C, RD, VD->getType().getNonReferenceType()); + FiredField = CodeGenUtil::addFieldToRecordDecl(C, RD, C.CharTy); RD->completeDefinition(); NewType = C.getRecordType(RD); Address Addr = CGF.CreateMemTemp(NewType, C.getDeclAlign(VD), VD->getName());