Index: include/clang/AST/ASTContext.h =================================================================== --- include/clang/AST/ASTContext.h +++ include/clang/AST/ASTContext.h @@ -74,6 +74,8 @@ namespace llvm { +class LLVMContext; +class Module; struct fltSemantics; } // namespace llvm @@ -536,6 +538,10 @@ /// should be imbued with the XRay "always" or "never" attributes. std::unique_ptr XRayFilter; + std::unique_ptr OpenMPHostIRContext; + std::unique_ptr OpenMPHostIR; + void readOpenMPHostIR(); + /// The allocator used to create AST objects. /// /// AST objects are never destructed; rather, all memory associated with the @@ -698,6 +704,8 @@ const TargetInfo &getTargetInfo() const { return *Target; } const TargetInfo *getAuxTargetInfo() const { return AuxTarget; } + const llvm::Module *getOpenMPHostIR() const { return OpenMPHostIR.get(); } + /// getIntTypeForBitwidth - /// sets integer QualTy according to specified details: /// bitwidth, signed/unsigned. Index: lib/AST/ASTContext.cpp =================================================================== --- lib/AST/ASTContext.cpp +++ lib/AST/ASTContext.cpp @@ -76,6 +76,7 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Triple.h" +#include "llvm/Bitcode/BitcodeReader.h" #include "llvm/Support/Capacity.h" #include "llvm/Support/Casting.h" #include "llvm/Support/Compiler.h" @@ -746,6 +747,38 @@ llvm_unreachable("Invalid CXXABI type!"); } +void ASTContext::readOpenMPHostIR() { + if (!LangOpts.OpenMPIsDevice) + return; + + if (LangOpts.OMPHostIRFile.empty()) + return; + + auto Buf = llvm::MemoryBuffer::getFile(LangOpts.OMPHostIRFile); + if (auto EC = Buf.getError()) { + getDiagnostics().Report(diag::err_cannot_open_file) + << LangOpts.OMPHostIRFile << EC.message(); + return; + } + + std::unique_ptr C(new llvm::LLVMContext()); + auto ME = expectedToErrorOrAndEmitErrors( + *C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), *C)); + + if (auto EC = ME.getError()) { + unsigned DiagID = getDiagnostics().getCustomDiagID( + DiagnosticsEngine::Error, "Unable to parse host IR file '%0':'%1'"); + getDiagnostics().Report(DiagID) + << LangOpts.OMPHostIRFile << EC.message(); + return; + } + + // Save generated objects. + OpenMPHostIRContext = std::move(C); + OpenMPHostIR = std::move(ME.get()); + OpenMPHostIR->setOwnedMemoryBuffer(std::move(Buf.get())); +} + static const LangASMap *getAddressSpaceMap(const TargetInfo &T, const LangOptions &LOpts) { if (LOpts.FakeAddressSpaceMap) { @@ -1099,6 +1132,7 @@ this->AuxTarget = AuxTarget; ABI.reset(createCXXABI(Target)); + readOpenMPHostIR(); AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); AddrSpaceMapMangling = isAddrSpaceMapManglingEnabled(Target, LangOpts); Index: lib/AST/CMakeLists.txt =================================================================== --- lib/AST/CMakeLists.txt +++ lib/AST/CMakeLists.txt @@ -1,5 +1,7 @@ set(LLVM_LINK_COMPONENTS BinaryFormat + BitReader + Core Support ) Index: lib/CodeGen/CGOpenMPRuntime.cpp =================================================================== --- lib/CodeGen/CGOpenMPRuntime.cpp +++ lib/CodeGen/CGOpenMPRuntime.cpp @@ -21,7 +21,6 @@ #include "clang/AST/StmtOpenMP.h" #include "clang/Basic/BitmaskEnum.h" #include "llvm/ADT/ArrayRef.h" -#include "llvm/Bitcode/BitcodeReader.h" #include "llvm/IR/CallSite.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/GlobalValue.h" @@ -4024,29 +4023,11 @@ if (!CGM.getLangOpts().OpenMPIsDevice) return; - if (CGM.getLangOpts().OMPHostIRFile.empty()) + const llvm::Module *OpenMPHostIR = CGM.getContext().getOpenMPHostIR(); + if (!OpenMPHostIR) return; - auto Buf = llvm::MemoryBuffer::getFile(CGM.getLangOpts().OMPHostIRFile); - if (auto EC = Buf.getError()) { - CGM.getDiags().Report(diag::err_cannot_open_file) - << CGM.getLangOpts().OMPHostIRFile << EC.message(); - return; - } - - llvm::LLVMContext C; - auto ME = expectedToErrorOrAndEmitErrors( - C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C)); - - if (auto EC = ME.getError()) { - unsigned DiagID = CGM.getDiags().getCustomDiagID( - DiagnosticsEngine::Error, "Unable to parse host IR file '%0':'%1'"); - CGM.getDiags().Report(DiagID) - << CGM.getLangOpts().OMPHostIRFile << EC.message(); - return; - } - - llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info"); + llvm::NamedMDNode *MD = OpenMPHostIR->getNamedMetadata("omp_offload.info"); if (!MD) return;