1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
//===- FunctionImport.cpp - ThinLTO Summary-based Function Import ---------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements Function import based on summaries.
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/IPO/FunctionImport.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/IR/AutoUpgrade.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Linker/Linker.h"
#include "llvm/Object/FunctionIndexObjectFile.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/SourceMgr.h"
#include <map>
using namespace llvm;
#define DEBUG_TYPE "function-import"
/// Limit on instruction count of imported functions.
static cl::opt<unsigned> ImportInstrLimit(
"import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
cl::desc("Only import functions with less than N instructions"));
// Load lazily a module from \p FileName in \p Context.
static std::unique_ptr<Module> loadFile(const std::string &FileName,
LLVMContext &Context) {
SMDiagnostic Err;
DEBUG(dbgs() << "Loading '" << FileName << "'\n");
std::unique_ptr<Module> Result = getLazyIRFileModule(FileName, Err, Context);
if (!Result) {
Err.print("function-import", errs());
return nullptr;
}
Result->materializeMetadata();
UpgradeDebugInfo(*Result);
return Result;
}
namespace {
/// Helper to load on demand a Module from file and cache it for subsequent
/// queries. It can be used with the FunctionImporter.
class ModuleLazyLoaderCache {
/// Cache of lazily loaded module for import.
StringMap<std::unique_ptr<Module>> ModuleMap;
/// Retrieve a Module from the cache or lazily load it on demand.
std::function<std::unique_ptr<Module>(StringRef FileName)> createLazyModule;
public:
/// Create the loader, Module will be initialized in \p Context.
ModuleLazyLoaderCache(std::function<
std::unique_ptr<Module>(StringRef FileName)> createLazyModule)
: createLazyModule(createLazyModule) {}
/// Retrieve a Module from the cache or lazily load it on demand.
Module &operator()(StringRef FileName);
std::unique_ptr<Module> takeModule(StringRef FileName) {
auto I = ModuleMap.find(FileName);
assert(I != ModuleMap.end());
std::unique_ptr<Module> Ret = std::move(I->second);
ModuleMap.erase(I);
return Ret;
}
};
// Get a Module for \p FileName from the cache, or load it lazily.
Module &ModuleLazyLoaderCache::operator()(StringRef Identifier) {
auto &Module = ModuleMap[Identifier];
if (!Module)
Module = createLazyModule(Identifier);
return *Module;
}
} // anonymous namespace
/// Walk through the instructions in \p F looking for external
/// calls not already in the \p CalledFunctions set. If any are
/// found they are added to the \p Worklist for importing.
static void findExternalCalls(const Module &DestModule, Function &F,
const FunctionInfoIndex &Index,
StringSet<> &CalledFunctions,
SmallVector<StringRef, 64> &Worklist) {
// We need to suffix internal function calls imported from other modules,
// prepare the suffix ahead of time.
std::string Suffix;
if (F.getParent() != &DestModule)
Suffix =
(Twine(".llvm.") +
Twine(Index.getModuleId(F.getParent()->getModuleIdentifier()))).str();
for (auto &BB : F) {
for (auto &I : BB) {
if (isa<CallInst>(I)) {
auto CalledFunction = cast<CallInst>(I).getCalledFunction();
// Insert any new external calls that have not already been
// added to set/worklist.
if (!CalledFunction || !CalledFunction->hasName())
continue;
// Ignore intrinsics early
if (CalledFunction->isIntrinsic()) {
assert(CalledFunction->getIntrinsicID() != 0);
continue;
}
auto ImportedName = CalledFunction->getName();
auto Renamed = (ImportedName + Suffix).str();
// Rename internal functions
if (CalledFunction->hasInternalLinkage()) {
ImportedName = Renamed;
}
auto It = CalledFunctions.insert(ImportedName);
if (!It.second) {
// This is a call to a function we already considered, skip.
continue;
}
// Ignore functions already present in the destination module
auto *SrcGV = DestModule.getNamedValue(ImportedName);
if (SrcGV) {
assert(isa<Function>(SrcGV) && "Name collision during import");
if (!cast<Function>(SrcGV)->isDeclaration()) {
DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Ignoring "
<< ImportedName << " already in DestinationModule\n");
continue;
}
}
Worklist.push_back(It.first->getKey());
DEBUG(dbgs() << DestModule.getModuleIdentifier()
<< ": Adding callee for : " << ImportedName << " : "
<< F.getName() << "\n");
}
}
}
}
// Helper function: given a worklist and an index, will process all the worklist
// and decide what to import based on the summary information.
//
// Nothing is actually imported, functions are materialized in their source
// module and analyzed there.
//
// \p ModuleToFunctionsToImportMap is filled with the set of Function to import
// per Module.
static void GetImportList(Module &DestModule,
SmallVector<StringRef, 64> &Worklist,
StringSet<> &CalledFunctions,
std::map<StringRef, DenseSet<const GlobalValue *>>
&ModuleToFunctionsToImportMap,
const FunctionInfoIndex &Index,
ModuleLazyLoaderCache &ModuleLoaderCache) {
while (!Worklist.empty()) {
auto CalledFunctionName = Worklist.pop_back_val();
DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Process import for "
<< CalledFunctionName << "\n");
// Try to get a summary for this function call.
auto InfoList = Index.findFunctionInfoList(CalledFunctionName);
if (InfoList == Index.end()) {
DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": No summary for "
<< CalledFunctionName << " Ignoring.\n");
continue;
}
assert(!InfoList->second.empty() && "No summary, error at import?");
// Comdat can have multiple entries, FIXME: what do we do with them?
auto &Info = InfoList->second[0];
assert(Info && "Nullptr in list, error importing summaries?\n");
auto *Summary = Info->functionSummary();
if (!Summary) {
// FIXME: in case we are lazyloading summaries, we can do it now.
DEBUG(dbgs() << DestModule.getModuleIdentifier()
<< ": Missing summary for " << CalledFunctionName
<< ", error at import?\n");
llvm_unreachable("Missing summary");
}
if (Summary->instCount() > ImportInstrLimit) {
DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Skip import of "
<< CalledFunctionName << " with " << Summary->instCount()
<< " instructions (limit " << ImportInstrLimit << ")\n");
continue;
}
// Get the module path from the summary.
auto ModuleIdentifier = Summary->modulePath();
DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Importing "
<< CalledFunctionName << " from " << ModuleIdentifier << "\n");
auto &SrcModule = ModuleLoaderCache(ModuleIdentifier);
// The function that we will import!
GlobalValue *SGV = SrcModule.getNamedValue(CalledFunctionName);
if (!SGV) {
// The destination module is referencing function using their renamed name
// when importing a function that was originally local in the source
// module. The source module we have might not have been renamed so we try
// to remove the suffix added during the renaming to recover the original
// name in the source module.
std::pair<StringRef, StringRef> Split =
CalledFunctionName.split(".llvm.");
SGV = SrcModule.getNamedValue(Split.first);
assert(SGV && "Can't find function to import in source module");
}
if (!SGV) {
report_fatal_error(Twine("Can't load function '") + CalledFunctionName +
"' in Module '" + SrcModule.getModuleIdentifier() +
"', error in the summary?\n");
}
Function *F = dyn_cast<Function>(SGV);
if (!F && isa<GlobalAlias>(SGV)) {
auto *SGA = dyn_cast<GlobalAlias>(SGV);
F = dyn_cast<Function>(SGA->getBaseObject());
CalledFunctionName = F->getName();
}
assert(F && "Imported Function is ... not a Function");
// We cannot import weak_any functions/aliases without possibly affecting
// the order they are seen and selected by the linker, changing program
// semantics.
if (SGV->hasWeakAnyLinkage()) {
DEBUG(dbgs() << DestModule.getModuleIdentifier()
<< ": Ignoring import request for weak-any "
<< (isa<Function>(SGV) ? "function " : "alias ")
<< CalledFunctionName << " from "
<< SrcModule.getModuleIdentifier() << "\n");
continue;
}
// Add the function to the import list
auto &Entry = ModuleToFunctionsToImportMap[SrcModule.getModuleIdentifier()];
Entry.insert(F);
// Process the newly imported functions and add callees to the worklist.
F->materialize();
findExternalCalls(DestModule, *F, Index, CalledFunctions, Worklist);
}
}
// Automatically import functions in Module \p DestModule based on the summaries
// index.
//
// The current implementation imports every called functions that exists in the
// summaries index.
bool FunctionImporter::importFunctions(Module &DestModule) {
DEBUG(dbgs() << "Starting import for Module "
<< DestModule.getModuleIdentifier() << "\n");
unsigned ImportedCount = 0;
/// First step is collecting the called external functions.
StringSet<> CalledFunctions;
SmallVector<StringRef, 64> Worklist;
for (auto &F : DestModule) {
if (F.isDeclaration() || F.hasFnAttribute(Attribute::OptimizeNone))
continue;
findExternalCalls(DestModule, F, Index, CalledFunctions, Worklist);
}
if (Worklist.empty())
return false;
/// Second step: for every call to an external function, try to import it.
// Linker that will be used for importing function
Linker TheLinker(DestModule);
// Map of Module -> List of Function to import from the Module
std::map<StringRef, DenseSet<const GlobalValue *>>
ModuleToFunctionsToImportMap;
// Analyze the summaries and get the list of functions to import by
// populating ModuleToFunctionsToImportMap
ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
GetImportList(DestModule, Worklist, CalledFunctions,
ModuleToFunctionsToImportMap, Index, ModuleLoaderCache);
assert(Worklist.empty() && "Worklist hasn't been flushed in GetImportList");
StringMap<std::unique_ptr<DenseMap<unsigned, MDNode *>>>
ModuleToTempMDValsMap;
// Do the actual import of functions now, one Module at a time
for (auto &FunctionsToImportPerModule : ModuleToFunctionsToImportMap) {
// Get the module for the import
auto &FunctionsToImport = FunctionsToImportPerModule.second;
std::unique_ptr<Module> SrcModule =
ModuleLoaderCache.takeModule(FunctionsToImportPerModule.first);
assert(&DestModule.getContext() == &SrcModule->getContext() &&
"Context mismatch");
// Save the mapping of value ids to temporary metadata created when
// importing this function. If we have already imported from this module,
// add new temporary metadata to the existing mapping.
auto &TempMDVals = ModuleToTempMDValsMap[SrcModule->getModuleIdentifier()];
if (!TempMDVals)
TempMDVals = llvm::make_unique<DenseMap<unsigned, MDNode *>>();
// Link in the specified functions.
if (TheLinker.linkInModule(std::move(SrcModule), Linker::Flags::None,
&Index, &FunctionsToImport, TempMDVals.get()))
report_fatal_error("Function Import: link error");
ImportedCount += FunctionsToImport.size();
}
// Now link in metadata for all modules from which we imported functions.
for (StringMapEntry<std::unique_ptr<DenseMap<unsigned, MDNode *>>> &SME :
ModuleToTempMDValsMap) {
// Load the specified source module.
auto &SrcModule = ModuleLoaderCache(SME.getKey());
// Link in all necessary metadata from this module.
if (TheLinker.linkInMetadata(SrcModule, SME.getValue().get()))
return false;
}
DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module "
<< DestModule.getModuleIdentifier() << "\n");
return ImportedCount;
}
/// Summary file to use for function importing when using -function-import from
/// the command line.
static cl::opt<std::string>
SummaryFile("summary-file",
cl::desc("The summary file to use for function importing."));
static void diagnosticHandler(const DiagnosticInfo &DI) {
raw_ostream &OS = errs();
DiagnosticPrinterRawOStream DP(OS);
DI.print(DP);
OS << '\n';
}
/// Parse the function index out of an IR file and return the function
/// index object if found, or nullptr if not.
static std::unique_ptr<FunctionInfoIndex>
getFunctionIndexForFile(StringRef Path, std::string &Error,
DiagnosticHandlerFunction DiagnosticHandler) {
std::unique_ptr<MemoryBuffer> Buffer;
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
MemoryBuffer::getFile(Path);
if (std::error_code EC = BufferOrErr.getError()) {
Error = EC.message();
return nullptr;
}
Buffer = std::move(BufferOrErr.get());
ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr =
object::FunctionIndexObjectFile::create(Buffer->getMemBufferRef(),
DiagnosticHandler);
if (std::error_code EC = ObjOrErr.getError()) {
Error = EC.message();
return nullptr;
}
return (*ObjOrErr)->takeIndex();
}
namespace {
/// Pass that performs cross-module function import provided a summary file.
class FunctionImportPass : public ModulePass {
/// Optional function summary index to use for importing, otherwise
/// the summary-file option must be specified.
const FunctionInfoIndex *Index;
public:
/// Pass identification, replacement for typeid
static char ID;
/// Specify pass name for debug output
const char *getPassName() const override {
return "Function Importing";
}
explicit FunctionImportPass(const FunctionInfoIndex *Index = nullptr)
: ModulePass(ID), Index(Index) {}
bool runOnModule(Module &M) override {
if (SummaryFile.empty() && !Index)
report_fatal_error("error: -function-import requires -summary-file or "
"file from frontend\n");
std::unique_ptr<FunctionInfoIndex> IndexPtr;
if (!SummaryFile.empty()) {
if (Index)
report_fatal_error("error: -summary-file and index from frontend\n");
std::string Error;
IndexPtr = getFunctionIndexForFile(SummaryFile, Error, diagnosticHandler);
if (!IndexPtr) {
errs() << "Error loading file '" << SummaryFile << "': " << Error
<< "\n";
return false;
}
Index = IndexPtr.get();
}
// Perform the import now.
auto ModuleLoader = [&M](StringRef Identifier) {
return loadFile(Identifier, M.getContext());
};
FunctionImporter Importer(*Index, ModuleLoader);
return Importer.importFunctions(M);
return false;
}
};
} // anonymous namespace
char FunctionImportPass::ID = 0;
INITIALIZE_PASS_BEGIN(FunctionImportPass, "function-import",
"Summary Based Function Import", false, false)
INITIALIZE_PASS_END(FunctionImportPass, "function-import",
"Summary Based Function Import", false, false)
namespace llvm {
Pass *createFunctionImportPass(const FunctionInfoIndex *Index = nullptr) {
return new FunctionImportPass(Index);
}
}
|