clang 24.0.0git
ModuleLinker.cpp
Go to the documentation of this file.
1//===--- ModuleLinker.cpp - Shared bitcode link helpers -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
14#include "llvm/ADT/StringSet.h"
15#include "llvm/Bitcode/BitcodeReader.h"
16#include "llvm/IR/Module.h"
17#include "llvm/Linker/Linker.h"
18#include "llvm/Transforms/IPO/Internalize.h"
19
20using namespace clang;
21
22bool clang::loadLinkModules(CompilerInstance &CI, llvm::LLVMContext &Ctx,
24 bool OnlyInternalized) {
25 if (!LinkModules.empty())
26 return false;
27
30 if (OnlyInternalized && !F.Internalize)
31 continue;
32 auto BCBuf = CI.getFileManager().getBufferForFile(F.Filename);
33 if (!BCBuf) {
34 CI.getDiagnostics().Report(diag::err_cannot_open_file)
35 << F.Filename << BCBuf.getError().message();
36 LinkModules.clear();
37 return true;
38 }
39
41 llvm::getOwningLazyBitcodeModule(std::move(*BCBuf), Ctx);
42 if (!ModuleOrErr) {
43 llvm::handleAllErrors(
44 ModuleOrErr.takeError(), [&](llvm::ErrorInfoBase &EIB) {
45 CI.getDiagnostics().Report(diag::err_cannot_open_file)
46 << F.Filename << EIB.message();
47 });
48 LinkModules.clear();
49 return true;
50 }
51
52 LinkModules.push_back({std::move(ModuleOrErr.get()), F.PropagateAttrs,
53 F.Internalize, F.LinkFlags});
54 }
55 return false;
56}
57
58bool clang::linkInModules(llvm::Module &M,
60 const CodeGenOptions &CodeGenOpts,
61 const LangOptions &LangOpts,
62 const TargetOptions &TargetOpts,
63 BackendDiagnosticConsumer &DiagConsumer) {
64 for (LinkModule &LM : LinkModules) {
65 assert(LM.Module && "LinkModule does not actually have a module");
66
67 if (LM.PropagateAttrs)
68 for (llvm::Function &F : *LM.Module) {
69 // Skip intrinsics. Keep consistent with how intrinsics are created
70 // in LLVM IR.
71 if (F.isIntrinsic())
72 continue;
74 F, CodeGenOpts, LangOpts, TargetOpts, LM.Internalize);
75 }
76
77 DiagConsumer.setCurLinkModule(LM.Module.get());
78 bool Err;
79
80 if (LM.Internalize) {
81 Err = llvm::Linker::linkModules(
82 M, std::move(LM.Module), LM.LinkFlags,
83 [](llvm::Module &M, const llvm::StringSet<> &GVS) {
84 llvm::internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) {
85 return !GV.hasName() || (GVS.count(GV.getName()) == 0);
86 });
87 });
88 } else
89 Err = llvm::Linker::linkModules(M, std::move(LM.Module), LM.LinkFlags);
90
91 if (Err)
92 return true;
93 }
94
95 LinkModules.clear();
96 return false; // success
97}
Owns the state needed to translate LLVM backend diagnostics into clang diagnostics,...
void setCurLinkModule(llvm::Module *M)
Set the module currently being linked in, used to name DK_Linker diagnostics.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
std::vector< BitcodeFileToLink > LinkBitcodeFiles
The files specified here are linked in to the module before optimizations.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
FileManager & getFileManager() const
Return the current file manager to the caller.
CodeGenOptions & getCodeGenOpts()
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(FileEntryRef Entry, bool isVolatile=false, bool RequiresNullTerminator=true, std::optional< int64_t > MaybeLimit=std::nullopt, bool IsText=true)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Options for controlling the target.
void mergeDefaultFunctionDefinitionAttributes(llvm::Function &F, const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts, const TargetOptions &TargetOpts, bool WillInternalize)
Adds attributes to F according to our CodeGenOpts and LangOpts, as though we had emitted it ourselves...
Definition CGCall.cpp:2528
Top level wrappers for InstallAPI frontend operations.
bool loadLinkModules(CompilerInstance &CI, llvm::LLVMContext &Ctx, llvm::SmallVectorImpl< LinkModule > &LinkModules, bool OnlyInternalized)
Load every bitcode file listed in CodeGenOpts.LinkBitcodeFiles into LinkModules.
bool linkInModules(llvm::Module &M, llvm::SmallVectorImpl< LinkModule > &LinkModules, const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts, const TargetOptions &TargetOpts, BackendDiagnosticConsumer &DiagConsumer)
Link every module in LinkModules into M, propagating default function attributes and internalizing as...
Info about a module to link into the module currently being generated.