diff options
author | Fady Ghanim <fghanim84@gmail.com> | 2020-06-04 15:55:17 -0400 |
---|---|---|
committer | Fady Ghanim <fghanim84@gmail.com> | 2020-06-28 19:04:20 -0400 |
commit | 80e15b4574f4a11792472249f64bac790145cc56 (patch) | |
tree | b03807d5d9cde888a2e4dbc1553d479d4f68f344 /clang/lib/CodeGen/CodeGenFunction.h | |
parent | 1becd298b82ed2f1a8ba5e61c5ad2ce7fe32d812 (diff) | |
download | llvm-80e15b4574f4a11792472249f64bac790145cc56.zip llvm-80e15b4574f4a11792472249f64bac790145cc56.tar.gz llvm-80e15b4574f4a11792472249f64bac790145cc56.tar.bz2 |
[Clang][OpenMP][OMPBuilder] Moving OMP allocation and cache creation code to OMPBuilderCBHelpers
Summary:
Modified the OMPBuilderCBHelpers in the following ways:
- Moved location of class definition and deleted all constructors
- Moved OpenMP-specific address allocation of local variables
- Moved threadprivate variable creation for the current thread
Reviewers: jdoerfert
Subscribers: yaxunl, guansong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79676
Diffstat (limited to 'clang/lib/CodeGen/CodeGenFunction.h')
-rw-r--r-- | clang/lib/CodeGen/CodeGenFunction.h | 272 |
1 files changed, 164 insertions, 108 deletions
diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index 37a3225..6b2538a 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -26,6 +26,7 @@ #include "clang/AST/ExprCXX.h" #include "clang/AST/ExprObjC.h" #include "clang/AST/ExprOpenMP.h" +#include "clang/AST/StmtOpenMP.h" #include "clang/AST/Type.h" #include "clang/Basic/ABI.h" #include "clang/Basic/CapturedStmt.h" @@ -80,6 +81,7 @@ class OMPUseDevicePtrClause; class OMPUseDeviceAddrClause; class ReturnsNonNullAttr; class SVETypeFlags; +class OMPExecutableDirective; namespace analyze_os_log { class OSLogBufferLayout; @@ -259,114 +261,6 @@ public: unsigned Index; }; - // Helper class for the OpenMP IR Builder. Allows reusability of code used for - // region body, and finalization codegen callbacks. This will class will also - // contain privatization functions used by the privatization call backs - struct OMPBuilderCBHelpers { - - using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; - - /// Emit the Finalization for an OMP region - /// \param CGF The Codegen function this belongs to - /// \param IP Insertion point for generating the finalization code. - static void FinalizeOMPRegion(CodeGenFunction &CGF, InsertPointTy IP) { - CGBuilderTy::InsertPointGuard IPG(CGF.Builder); - assert(IP.getBlock()->end() != IP.getPoint() && - "OpenMP IR Builder should cause terminated block!"); - - llvm::BasicBlock *IPBB = IP.getBlock(); - llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor(); - assert(DestBB && "Finalization block should have one successor!"); - - // erase and replace with cleanup branch. - IPBB->getTerminator()->eraseFromParent(); - CGF.Builder.SetInsertPoint(IPBB); - CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB); - CGF.EmitBranchThroughCleanup(Dest); - } - - /// Emit the body of an OMP region - /// \param CGF The Codegen function this belongs to - /// \param RegionBodyStmt The body statement for the OpenMP region being - /// generated - /// \param CodeGenIP Insertion point for generating the body code. - /// \param FiniBB The finalization basic block - static void EmitOMPRegionBody(CodeGenFunction &CGF, - const Stmt *RegionBodyStmt, - InsertPointTy CodeGenIP, - llvm::BasicBlock &FiniBB) { - llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock(); - if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator()) - CodeGenIPBBTI->eraseFromParent(); - - CGF.Builder.SetInsertPoint(CodeGenIPBB); - - CGF.EmitStmt(RegionBodyStmt); - - if (CGF.Builder.saveIP().isSet()) - CGF.Builder.CreateBr(&FiniBB); - } - - /// RAII for preserving necessary info during Outlined region body codegen. - class OutlinedRegionBodyRAII { - - llvm::AssertingVH<llvm::Instruction> OldAllocaIP; - CodeGenFunction::JumpDest OldReturnBlock; - CodeGenFunction &CGF; - - public: - OutlinedRegionBodyRAII(CodeGenFunction &cgf, InsertPointTy &AllocaIP, - llvm::BasicBlock &RetBB) - : CGF(cgf) { - assert(AllocaIP.isSet() && - "Must specify Insertion point for allocas of outlined function"); - OldAllocaIP = CGF.AllocaInsertPt; - CGF.AllocaInsertPt = &*AllocaIP.getPoint(); - - OldReturnBlock = CGF.ReturnBlock; - CGF.ReturnBlock = CGF.getJumpDestInCurrentScope(&RetBB); - } - - ~OutlinedRegionBodyRAII() { - CGF.AllocaInsertPt = OldAllocaIP; - CGF.ReturnBlock = OldReturnBlock; - } - }; - - /// RAII for preserving necessary info during inlined region body codegen. - class InlinedRegionBodyRAII { - - llvm::AssertingVH<llvm::Instruction> OldAllocaIP; - CodeGenFunction &CGF; - - public: - InlinedRegionBodyRAII(CodeGenFunction &cgf, InsertPointTy &AllocaIP, - llvm::BasicBlock &FiniBB) - : CGF(cgf) { - // Alloca insertion block should be in the entry block of the containing - // function so it expects an empty AllocaIP in which case will reuse the - // old alloca insertion point, or a new AllocaIP in the same block as - // the old one - assert((!AllocaIP.isSet() || - CGF.AllocaInsertPt->getParent() == AllocaIP.getBlock()) && - "Insertion point should be in the entry block of containing " - "function!"); - OldAllocaIP = CGF.AllocaInsertPt; - if (AllocaIP.isSet()) - CGF.AllocaInsertPt = &*AllocaIP.getPoint(); - - // TODO: Remove the call, after making sure the counter is not used by - // the EHStack. - // Since this is an inlined region, it should not modify the - // ReturnBlock, and should reuse the one for the enclosing outlined - // region. So, the JumpDest being return by the function is discarded - (void)CGF.getJumpDestInCurrentScope(&FiniBB); - } - - ~InlinedRegionBodyRAII() { CGF.AllocaInsertPt = OldAllocaIP; } - }; - }; - CodeGenModule &CGM; // Per-module state. const TargetInfo &Target; @@ -1695,6 +1589,168 @@ public: CallArgList OldCXXInheritedCtorInitExprArgs; }; + // Helper class for the OpenMP IR Builder. Allows reusability of code used for + // region body, and finalization codegen callbacks. This will class will also + // contain privatization functions used by the privatization call backs + // + // TODO: this is temporary class for things that are being moved out of + // CGOpenMPRuntime, new versions of current CodeGenFunction methods, or + // utility function for use with the OMPBuilder. Once that move to use the + // OMPBuilder is done, everything here will either become part of CodeGenFunc. + // directly, or a new helper class that will contain functions used by both + // this and the OMPBuilder + + struct OMPBuilderCBHelpers { + + OMPBuilderCBHelpers() = delete; + OMPBuilderCBHelpers(const OMPBuilderCBHelpers &) = delete; + OMPBuilderCBHelpers &operator=(const OMPBuilderCBHelpers &) = delete; + + using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; + + /// Cleanup action for allocate support. + class OMPAllocateCleanupTy final : public EHScopeStack::Cleanup { + + private: + llvm::CallInst *RTLFnCI; + + public: + OMPAllocateCleanupTy(llvm::CallInst *RLFnCI) : RTLFnCI(RLFnCI) { + RLFnCI->removeFromParent(); + } + + void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { + if (!CGF.HaveInsertPoint()) + return; + CGF.Builder.Insert(RTLFnCI); + } + }; + + /// Returns address of the threadprivate variable for the current + /// thread. This Also create any necessary OMP runtime calls. + /// + /// \param VD VarDecl for Threadprivate variable. + /// \param VDAddr Address of the Vardecl + /// \param Loc The location where the barrier directive was encountered + static Address getAddrOfThreadPrivate(CodeGenFunction &CGF, + const VarDecl *VD, Address VDAddr, + SourceLocation Loc); + + /// Gets the OpenMP-specific address of the local variable /p VD. + static Address getAddressOfLocalVariable(CodeGenFunction &CGF, + const VarDecl *VD); + /// Get the platform-specific name separator. + /// \param Parts different parts of the final name that needs separation + /// \param FirstSeparator First separator used between the initial two + /// parts of the name. + /// \param Separator separator used between all of the rest consecutinve + /// parts of the name + static std::string getNameWithSeparators(ArrayRef<StringRef> Parts, + StringRef FirstSeparator = ".", + StringRef Separator = "."); + /// Emit the Finalization for an OMP region + /// \param CGF The Codegen function this belongs to + /// \param IP Insertion point for generating the finalization code. + static void FinalizeOMPRegion(CodeGenFunction &CGF, InsertPointTy IP) { + CGBuilderTy::InsertPointGuard IPG(CGF.Builder); + assert(IP.getBlock()->end() != IP.getPoint() && + "OpenMP IR Builder should cause terminated block!"); + + llvm::BasicBlock *IPBB = IP.getBlock(); + llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor(); + assert(DestBB && "Finalization block should have one successor!"); + + // erase and replace with cleanup branch. + IPBB->getTerminator()->eraseFromParent(); + CGF.Builder.SetInsertPoint(IPBB); + CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB); + CGF.EmitBranchThroughCleanup(Dest); + } + + /// Emit the body of an OMP region + /// \param CGF The Codegen function this belongs to + /// \param RegionBodyStmt The body statement for the OpenMP region being + /// generated + /// \param CodeGenIP Insertion point for generating the body code. + /// \param FiniBB The finalization basic block + static void EmitOMPRegionBody(CodeGenFunction &CGF, + const Stmt *RegionBodyStmt, + InsertPointTy CodeGenIP, + llvm::BasicBlock &FiniBB) { + llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock(); + if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator()) + CodeGenIPBBTI->eraseFromParent(); + + CGF.Builder.SetInsertPoint(CodeGenIPBB); + + CGF.EmitStmt(RegionBodyStmt); + + if (CGF.Builder.saveIP().isSet()) + CGF.Builder.CreateBr(&FiniBB); + } + + /// RAII for preserving necessary info during Outlined region body codegen. + class OutlinedRegionBodyRAII { + + llvm::AssertingVH<llvm::Instruction> OldAllocaIP; + CodeGenFunction::JumpDest OldReturnBlock; + CGBuilderTy::InsertPoint IP; + CodeGenFunction &CGF; + + public: + OutlinedRegionBodyRAII(CodeGenFunction &cgf, InsertPointTy &AllocaIP, + llvm::BasicBlock &RetBB) + : CGF(cgf) { + assert(AllocaIP.isSet() && + "Must specify Insertion point for allocas of outlined function"); + OldAllocaIP = CGF.AllocaInsertPt; + CGF.AllocaInsertPt = &*AllocaIP.getPoint(); + IP = CGF.Builder.saveIP(); + + OldReturnBlock = CGF.ReturnBlock; + CGF.ReturnBlock = CGF.getJumpDestInCurrentScope(&RetBB); + } + + ~OutlinedRegionBodyRAII() { + CGF.AllocaInsertPt = OldAllocaIP; + CGF.ReturnBlock = OldReturnBlock; + CGF.Builder.restoreIP(IP); + } + }; + + /// RAII for preserving necessary info during inlined region body codegen. + class InlinedRegionBodyRAII { + + llvm::AssertingVH<llvm::Instruction> OldAllocaIP; + CodeGenFunction &CGF; + + public: + InlinedRegionBodyRAII(CodeGenFunction &cgf, InsertPointTy &AllocaIP, + llvm::BasicBlock &FiniBB) + : CGF(cgf) { + // Alloca insertion block should be in the entry block of the containing + // function so it expects an empty AllocaIP in which case will reuse the + // old alloca insertion point, or a new AllocaIP in the same block as + // the old one + assert((!AllocaIP.isSet() || + CGF.AllocaInsertPt->getParent() == AllocaIP.getBlock()) && + "Insertion point should be in the entry block of containing " + "function!"); + OldAllocaIP = CGF.AllocaInsertPt; + if (AllocaIP.isSet()) + CGF.AllocaInsertPt = &*AllocaIP.getPoint(); + + // TODO: Remove the call, after making sure the counter is not used by + // the EHStack. + // Since this is an inlined region, it should not modify the + // ReturnBlock, and should reuse the one for the enclosing outlined + // region. So, the JumpDest being return by the function is discarded + (void)CGF.getJumpDestInCurrentScope(&FiniBB); + } + + ~InlinedRegionBodyRAII() { CGF.AllocaInsertPt = OldAllocaIP; } + }; + }; private: /// CXXThisDecl - When generating code for a C++ member function, /// this will hold the implicit 'this' declaration. |