llvm.org GIT mirror llvm / 9a1f991
[ThinLTO] Import local variables from the same module as caller Summary: We can sometimes end up with multiple copies of a local variable that have the same GUID in the index. This happens when there are local variables with the same name that are in different source files having the same name/path at compile time (but compiled into different bitcode objects). In this case make sure we import the copy in the caller's module. This enables importing both of the variables having the same GUID (but which will have different promoted names since the module paths, and therefore the module hashes, will be distinct). Importing the wrong copy is particularly problematic for read only variables, since we must import them as a local copy whenever referenced. Otherwise we get undefs at link time. Note that the llvm-lto.cpp and ThinLTOCodeGenerator changes are needed for testing the distributed index case via clang, which will be sent as a separate clang-side patch shortly. We were previously not doing the dead code/read only computation before computing imports when testing distributed index generation (like it was for testing importing and other ThinLTO mechanisms alone). Reviewers: evgeny777 Subscribers: mehdi_amini, inglorion, eraman, steven_wu, dexonsmith, dang, llvm-commits Differential Revision: https://reviews.llvm.org/D55047 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@347886 91177308-0d34-0410-b5e6-96231b3b80d8 Teresa Johnson 9 months ago
10 changed file(s) with 123 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
272272 /**
273273 * Compute and emit the imported files for module at \p ModulePath.
274274 */
275 static void emitImports(StringRef ModulePath, StringRef OutputName,
276 ModuleSummaryIndex &Index);
275 void emitImports(Module &Module, StringRef OutputName,
276 ModuleSummaryIndex &Index);
277277
278278 /**
279279 * Perform cross-module importing for the module identified by
284284 /**
285285 * Compute the list of summaries needed for importing into module.
286286 */
287 static void gatherImportedSummariesForModule(
288 StringRef ModulePath, ModuleSummaryIndex &Index,
287 void gatherImportedSummariesForModule(
288 Module &Module, ModuleSummaryIndex &Index,
289289 std::map &ModuleToSummariesForIndex);
290290
291291 /**
660660 * Compute the list of summaries needed for importing into module.
661661 */
662662 void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
663 StringRef ModulePath, ModuleSummaryIndex &Index,
663 Module &TheModule, ModuleSummaryIndex &Index,
664664 std::map &ModuleToSummariesForIndex) {
665665 auto ModuleCount = Index.modulePaths().size();
666 auto ModuleIdentifier = TheModule.getModuleIdentifier();
666667
667668 // Collect for each module the list of function it defines (GUID -> Summary).
668669 StringMap ModuleToDefinedGVSummaries(ModuleCount);
669670 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
671
672 // Convert the preserved symbols set from string to GUID
673 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
674 PreservedSymbols, Triple(TheModule.getTargetTriple()));
675
676 // Compute "dead" symbols, we don't want to import/export these!
677 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
670678
671679 // Generate import/export list
672680 StringMap ImportLists(ModuleCount);
674682 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
675683 ExportLists);
676684
677 llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
678 ImportLists[ModulePath],
679 ModuleToSummariesForIndex);
685 llvm::gatherImportedSummariesForModule(
686 ModuleIdentifier, ModuleToDefinedGVSummaries,
687 ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
680688 }
681689
682690 /**
683691 * Emit the list of files needed for importing into module.
684692 */
685 void ThinLTOCodeGenerator::emitImports(StringRef ModulePath,
686 StringRef OutputName,
693 void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName,
687694 ModuleSummaryIndex &Index) {
688695 auto ModuleCount = Index.modulePaths().size();
696 auto ModuleIdentifier = TheModule.getModuleIdentifier();
689697
690698 // Collect for each module the list of function it defines (GUID -> Summary).
691699 StringMap ModuleToDefinedGVSummaries(ModuleCount);
692700 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
701
702 // Convert the preserved symbols set from string to GUID
703 auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
704 PreservedSymbols, Triple(TheModule.getTargetTriple()));
705
706 // Compute "dead" symbols, we don't want to import/export these!
707 computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
693708
694709 // Generate import/export list
695710 StringMap ImportLists(ModuleCount);
698713 ExportLists);
699714
700715 std::map ModuleToSummariesForIndex;
701 llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
702 ImportLists[ModulePath],
703 ModuleToSummariesForIndex);
716 llvm::gatherImportedSummariesForModule(
717 ModuleIdentifier, ModuleToDefinedGVSummaries,
718 ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
704719
705720 std::error_code EC;
706 if ((EC =
707 EmitImportsFiles(ModulePath, OutputName, ModuleToSummariesForIndex)))
721 if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName,
722 ModuleToSummariesForIndex)))
708723 report_fatal_error(Twine("Failed to open ") + OutputName +
709724 " to save imports lists\n");
710725 }
292292
293293 LLVM_DEBUG(dbgs() << " ref -> " << VI << "\n");
294294
295 // If this is a local variable, make sure we import the copy
296 // in the caller's module. The only time a local variable can
297 // share an entry in the index is if there is a local with the same name
298 // in another module that had the same source file name (in a different
299 // directory), where each was compiled in their own directory so there
300 // was not distinguishing path.
301 auto LocalNotInModule = [&](const GlobalValueSummary *RefSummary) -> bool {
302 return GlobalValue::isLocalLinkage(RefSummary->linkage()) &&
303 RefSummary->modulePath() != Summary.modulePath();
304 };
305
295306 for (auto &RefSummary : VI.getSummaryList())
296307 if (isa(RefSummary.get()) &&
297 canImportGlobalVar(RefSummary.get())) {
308 canImportGlobalVar(RefSummary.get()) &&
309 !LocalNotInModule(RefSummary.get())) {
298310 auto ILI = ImportList[RefSummary->modulePath()].insert(VI.getGUID());
299311 // Only update stat if we haven't already imported this variable.
300312 if (ILI.second)
11 source_filename = "local_name_conflict.c"
22 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
33 target triple = "x86_64-unknown-linux-gnu"
4
5 @baz = internal constant i32 10, align 4
46
57 ; Function Attrs: noinline nounwind uwtable
68 define i32 @a() {
1214 ; Function Attrs: noinline nounwind uwtable
1315 define internal i32 @foo() {
1416 entry:
15 ret i32 1
17 %0 = load i32, i32* @baz, align 4
18 ret i32 %0
1619 }
11 source_filename = "local_name_conflict.c"
22 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
33 target triple = "x86_64-unknown-linux-gnu"
4
5 @baz = internal constant i32 10, align 4
46
57 ; Function Attrs: noinline nounwind uwtable
68 define i32 @b() {
1214 ; Function Attrs: noinline nounwind uwtable
1315 define internal i32 @foo() {
1416 entry:
15 ret i32 2
17 %0 = load i32, i32* @baz, align 4
18 ret i32 %0
1619 }
0 ; ModuleID = 'local_name_conflict_var.o'
1 source_filename = "local_name_conflict_var.c"
2 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
3 target triple = "x86_64-unknown-linux-gnu"
4
5 @baz = internal global i32 10, align 4
6
7 ; Function Attrs: noinline nounwind uwtable
8 define i32 @a() {
9 entry:
10 %0 = load i32, i32* @baz, align 4
11 ret i32 %0
12 }
0 ; ModuleID = 'local_name_conflict_var.o'
1 source_filename = "local_name_conflict_var.c"
2 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
3 target triple = "x86_64-unknown-linux-gnu"
4
5 @baz = internal global i32 10, align 4
6
7 ; Function Attrs: noinline nounwind uwtable
8 define i32 @b() {
9 entry:
10 %0 = load i32, i32* @baz, align 4
11 ret i32 %0
12 }
0 ; Test handling when two files with the same source file name contain
1 ; static functions with the same name (which will have the same GUID
1 ; static functions/variables with the same name (which will have the same GUID
22 ; in the combined index.
33
44 ; Do setup work for all below tests: generate bitcode and combined index
77 ; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict2.ll -o %t3.bc
88 ; RUN: llvm-lto -thinlto-action=thinlink -o %t4.bc %t.bc %t2.bc %t3.bc
99
10 ; This module will import b() which should cause the copy of foo from
10 ; This module will import b() which should cause the copy of foo and baz from
1111 ; that module (%t3.bc) to be imported. Check that the imported reference's
1212 ; promoted name matches the imported copy.
1313 ; RUN: llvm-lto -thinlto-action=import %t.bc -thinlto-index=%t4.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=IMPORT
14 ; IMPORT: call i32 @foo.llvm.[[HASH:[0-9]+]]
14 ; IMPORT: @baz.llvm.[[HASH:[0-9]+]] = available_externally hidden constant i32 10, align 4
15 ; IMPORT: call i32 @foo.llvm.[[HASH]]
1516 ; IMPORT: define available_externally hidden i32 @foo.llvm.[[HASH]]()
1617
1718 ; The copy in %t2.bc should not be exported/promoted/renamed
1819 ; RUN: llvm-lto -thinlto-action=promote %t2.bc -thinlto-index=%t4.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=NOEXPORTSTATIC
20 ; NOEXPORTSTATIC: @baz = internal constant i32 10, align 4
1921 ; NOEXPORTSTATIC: define internal i32 @foo()
2022
2123 ; Make sure foo is promoted and renamed without complaint in %t3.bc.
2224 ; RUN: llvm-lto -thinlto-action=promote %t3.bc -thinlto-index=%t4.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=EXPORTSTATIC
25 ; EXPORTSTATIC: @baz.llvm.{{.*}} = hidden constant i32 10, align 4
2326 ; EXPORTSTATIC: define hidden i32 @foo.llvm.
2427
2528 ; ModuleID = 'local_name_conflict_main.o'
0 ; Test handling when two files with the same source file name contain
1 ; static read only variables with the same name (which will have the same GUID
2 ; in the combined index).
3
4 ; Do setup work for all below tests: generate bitcode and combined index
5 ; RUN: opt -module-summary -module-hash %s -o %t.bc
6 ; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var1.ll -o %t2.bc
7 ; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var2.ll -o %t3.bc
8 ; RUN: llvm-lto -thinlto-action=thinlink -o %t4.bc %t.bc %t2.bc %t3.bc
9
10 ; This module will import a() and b() which should cause the read only copy
11 ; of baz from each of those modules to be imported. Check that the both are
12 ; imported as local copies.
13 ; RUN: llvm-lto -thinlto-action=import -exported-symbol=main %t.bc -thinlto-index=%t4.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=IMPORT
14 ; IMPORT: @baz.llvm.{{.*}} = internal global i32 10
15 ; IMPORT: @baz.llvm.{{.*}} = internal global i32 10
16
17 ; ModuleID = 'local_name_conflict_var_main.o'
18 source_filename = "local_name_conflict_var_main.c"
19 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
20 target triple = "x86_64-unknown-linux-gnu"
21
22 ; Function Attrs: noinline nounwind uwtable
23 define i32 @main() {
24 entry:
25 %call1 = call i32 (...) @a()
26 %call2 = call i32 (...) @b()
27 ret i32 0
28 }
29
30 declare i32 @a(...)
31 declare i32 @b(...)
561561
562562 auto Index = loadCombinedIndex();
563563 for (auto &Filename : InputFilenames) {
564 LLVMContext Ctx;
565 auto TheModule = loadModule(Filename, Ctx);
566
564567 // Build a map of module to the GUIDs and summary objects that should
565568 // be written to its index.
566569 std::map ModuleToSummariesForIndex;
567 ThinLTOCodeGenerator::gatherImportedSummariesForModule(
568 Filename, *Index, ModuleToSummariesForIndex);
570 ThinGenerator.gatherImportedSummariesForModule(*TheModule, *Index,
571 ModuleToSummariesForIndex);
569572
570573 std::string OutputName = OutputFilename;
571574 if (OutputName.empty()) {
593596
594597 auto Index = loadCombinedIndex();
595598 for (auto &Filename : InputFilenames) {
599 LLVMContext Ctx;
600 auto TheModule = loadModule(Filename, Ctx);
596601 std::string OutputName = OutputFilename;
597602 if (OutputName.empty()) {
598603 OutputName = Filename + ".imports";
599604 }
600605 OutputName = getThinLTOOutputFile(OutputName, OldPrefix, NewPrefix);
601 ThinLTOCodeGenerator::emitImports(Filename, OutputName, *Index);
606 ThinGenerator.emitImports(*TheModule, OutputName, *Index);
602607 }
603608 }
604609