llvm.org GIT mirror llvm / 7d2e3aa
[ThinLTO] Stop importing constant global vars as copies in the backend Summary: We were doing an optimization in the ThinLTO backends of importing constant unnamed_addr globals unconditionally as a local copy (regardless of whether the thin link decided to import them). This should be done in the thin link instead, so that resulting exported references are marked and promoted appropriately, but will need a summary enhancement to mark these variables as constant unnamed_addr. The function import logic during the thin link was trying to handle this proactively, by conservatively marking all values referenced in the initializer lists of exported global variables as also exported. However, this only handled values referenced directly from the initializer list of an exported global variable. If the value is itself a constant unnamed_addr variable, we could end up exporting its references as well. This caused multiple issues. The first is that the transitively exported references weren't promoted. Secondly, some could not be promoted/renamed (e.g. they had a section or other constraint). recursively, instead of just adding the first level of initializer list references to the ExportList directly. Remove this optimization and the associated handling in the function import backend. SPEC measurements indicate we weren't getting much from it in any case. Fixes PR31052. Reviewers: mehdi_amini Subscribers: krasin, llvm-commits Differential Revision: https://reviews.llvm.org/D26880 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288446 91177308-0d34-0410-b5e6-96231b3b80d8 Teresa Johnson 3 years ago
8 changed file(s) with 64 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
267267 auto GVS = dyn_cast(Summary);
268268 if (!GVS)
269269 return;
270 // FunctionImportGlobalProcessing::doPromoteLocalToGlobal() will always
271 // trigger importing the initializer for `constant unnamed addr` globals that
272 // are referenced. We conservatively export all the referenced symbols for
273 // every global to workaround this, so that the ExportList is accurate.
274 // FIXME: with a "isConstant" flag in the summary we could be more targetted.
275 for (auto &Ref : GVS->refs()) {
276 auto GUID = Ref.getGUID();
277 auto *RefSummary = FindGlobalSummaryInModule(GUID);
278 if (RefSummary)
279 // Found a ref in the current module, mark it as exported
280 ExportList.insert(GUID);
281 }
282270 }
283271
284272 using EdgeInfo = std::pair;
5353 // Both the imported references and the original local variable must
5454 // be promoted.
5555 if (!isPerformingImport() && !isModuleExporting())
56 return false;
57
58 // Local const variables never need to be promoted unless they are address
59 // taken. The imported uses can simply use the clone created in this module.
60 // For now we are conservative in determining which variables are not
61 // address taken by checking the unnamed addr flag. To be more aggressive,
62 // the address taken information must be checked earlier during parsing
63 // of the module and recorded in the summary index for use when importing
64 // from that module.
65 auto *GVar = dyn_cast(SGV);
66 if (GVar && GVar->isConstant() && GVar->hasGlobalUnnamedAddr())
6756 return false;
6857
6958 // If we are exporting, we need to see whether this value is marked
1313 ; constant variable need promotion).
1414 ; RUN: llvm-link %t.bc -summary-index=%t3.thinlto.bc -S | FileCheck %s --check-prefix=EXPORTSTATIC
1515 ; EXPORTSTATIC-DAG: @staticvar.llvm.{{.*}} = hidden global
16 ; EXPORTSTATIC-DAG: @staticconstvar = internal unnamed_addr constant
16 ; Eventually @staticconstvar can be exported as a copy and not promoted
17 ; EXPORTSTATIC-DAG: @staticconstvar.llvm.0 = hidden unnamed_addr constant
1718 ; EXPORTSTATIC-DAG: @P.llvm.{{.*}} = hidden global void ()* null
1819 ; EXPORTSTATIC-DAG: define hidden i32 @staticfunc.llvm.
1920 ; EXPORTSTATIC-DAG: define hidden void @staticfunc2.llvm.
6768 ; promoted and renamed (including static constant variable).
6869 ; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=referencestatics:%t.bc -S | FileCheck %s --check-prefix=IMPORTSTATIC
6970 ; IMPORTSTATIC-DAG: @staticvar.llvm.{{.*}} = external hidden global
70 ; IMPORTSTATIC-DAG: @staticconstvar.llvm.{{.*}} = internal unnamed_addr constant
71 ; Eventually @staticconstvar can be imported as a copy
72 ; IMPORTSTATIC-DAG: @staticconstvar.llvm.{{.*}} = external hidden unnamed_addr constant
7173 ; IMPORTSTATIC-DAG: define available_externally i32 @referencestatics
7274 ; IMPORTSTATIC-DAG: %call = call i32 @staticfunc.llvm.
7375 ; IMPORTSTATIC-DAG: %0 = load i32, i32* @staticvar.llvm.
0 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
1 target triple = "x86_64-apple-macosx10.11.0"
2
3 declare i8 **@foo()
4 define i32 @main() {
5 call i8 **@foo()
6 ret i32 0
7 }
55 ret void
66 }
77
8 define internal void @localreferencedbyglobal() {
9 ret void
10 }
11
812 @someglobal = internal unnamed_addr constant i8* bitcast (void ()* @referencedbyglobal to i8*)
13 @someglobal2 = internal unnamed_addr constant i8* bitcast (void ()* @localreferencedbyglobal to i8*)
914 @ptr = global i8** null
15 @ptr2 = global i8** null
1016
1117 define void @bar() #0 align 2 {
1218 store i8** getelementptr inbounds (i8*, i8** @someglobal, i64 0) , i8*** @ptr, align 8
19 store i8** getelementptr inbounds (i8*, i8** @someglobal2, i64 0) , i8*** @ptr2, align 8
1320 ret void
1421 }
15
99 ; constant variable need promotion).
1010 ; RUN: llvm-lto -thinlto-action=promote %t.bc -thinlto-index=%t3.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=EXPORTSTATIC
1111 ; EXPORTSTATIC-DAG: @staticvar.llvm.0 = hidden global
12 ; EXPORTSTATIC-DAG: @staticconstvar = internal unnamed_addr constant
12 ; Eventually @staticconstvar can be exported as a copy and not promoted
13 ; EXPORTSTATIC-DAG: @staticconstvar.llvm.0 = hidden unnamed_addr constant
1314 ; EXPORTSTATIC-DAG: @P.llvm.0 = hidden global void ()* null
1415 ; EXPORTSTATIC-DAG: define hidden i32 @staticfunc.llvm.0
1516 ; EXPORTSTATIC-DAG: define hidden void @staticfunc2.llvm.0
0 ; RUN: opt -module-summary %s -o %t1.bc
1 ; RUN: opt -module-summary %p/Inputs/reference_non_importable.ll -o %t2.bc
2
3 ; RUN: llvm-lto2 %t1.bc %t2.bc -o %t.o -save-temps \
4 ; RUN: -r=%t1.bc,_foo,pxl \
5 ; RUN: -r=%t1.bc,_b,pxl \
6 ; RUN: -r=%t2.bc,_main,pxl \
7 ; RUN: -r=%t2.bc,_foo,xl
8
9
10
11
12 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
13 target triple = "x86_64-apple-macosx10.11.0"
14
15 ; We shouldn't promote the private because it has a section
16 ; RUN: llvm-dis < %t.o.0.2.internalize.bc | FileCheck %s --check-prefix=PROMOTE
17 ; PROMOTE: @a = private global i8 0, section "__TEXT,__cstring,cstring_literals"
18 @a = private global i8 0, section "__TEXT,__cstring,cstring_literals"
19 @b = global i8 *@a
20
21
22 ; We want foo to be imported in the main module!
23 ; RUN: llvm-dis < %t.o.1.3.import.bc | FileCheck %s --check-prefix=IMPORT
24 ; IMPORT: define available_externally i8** @foo()
25 define i8 **@foo() {
26 ret i8 **@b
27 }
22 ; RUN: opt -module-summary %p/Inputs/referenced_by_constant.ll -o %t2.bc
33 ; RUN: llvm-lto -thinlto-action=thinlink -o %t3.bc %t.bc %t2.bc
44
5 ; Check the import side: we import bar() and @someglobal, but not @referencedbyglobal()
5 ; Check the import side: we currently only import bar() (with a future
6 ; enhancement to identify constants in the summary, we should mark
7 ; @someglobal/@someglobal2 for import as a local copy, which would
8 ; cause @referencedbyglobal and @localreferencedbyglobal to be exported
9 ; and promoted).
610 ; RUN: llvm-lto -thinlto-action=import %t.bc -thinlto-index=%t3.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=IMPORT
7 ; IMPORT: @someglobal.llvm.0 =
11 ; IMPORT: @someglobal.llvm.0 = external hidden unnamed_addr constant
12 ; IMPORT: @someglobal2.llvm.0 = external hidden unnamed_addr constant
813 ; IMPORT: define available_externally void @bar()
9 ; IMPORT: declare void @referencedbyglobal()
14
15 ; Check the export side: we currently only export bar(), which causes
16 ; @someglobal and @someglobal2 to be promoted (see above).
17 ; RUN: llvm-lto -thinlto-action=promote %t2.bc -thinlto-index=%t3.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=EXPORT
18 ; EXPORT: @someglobal.llvm.0 = hidden unnamed_addr constant
19 ; EXPORT: @someglobal2.llvm.0 = hidden unnamed_addr constant
20 ; EXPORT: define void @referencedbyglobal()
21 ; EXPORT: define internal void @localreferencedbyglobal()
1022
1123 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
1224 target triple = "x86_64-apple-macosx10.11.0"
1729 call void @bar()
1830 ret void
1931 }
20