Skip to content

Commit 185b4ab

Browse files
committed
[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 llvm-svn: 288446
1 parent c47701c commit 185b4ab

File tree

8 files changed

+64
-31
lines changed

8 files changed

+64
-31
lines changed

llvm/lib/Transforms/IPO/FunctionImport.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,6 @@ static void exportGlobalInModule(const ModuleSummaryIndex &Index,
268268
auto GVS = dyn_cast<GlobalVarSummary>(Summary);
269269
if (!GVS)
270270
return;
271-
// FunctionImportGlobalProcessing::doPromoteLocalToGlobal() will always
272-
// trigger importing the initializer for `constant unnamed addr` globals that
273-
// are referenced. We conservatively export all the referenced symbols for
274-
// every global to workaround this, so that the ExportList is accurate.
275-
// FIXME: with a "isConstant" flag in the summary we could be more targetted.
276-
for (auto &Ref : GVS->refs()) {
277-
auto GUID = Ref.getGUID();
278-
auto *RefSummary = FindGlobalSummaryInModule(GUID);
279-
if (RefSummary)
280-
// Found a ref in the current module, mark it as exported
281-
ExportList.insert(GUID);
282-
}
283271
}
284272

285273
using EdgeInfo = std::pair<const FunctionSummary *, unsigned /* Threshold */>;

llvm/lib/Transforms/Utils/FunctionImportUtils.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,6 @@ bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
5656
if (!isPerformingImport() && !isModuleExporting())
5757
return false;
5858

59-
// Local const variables never need to be promoted unless they are address
60-
// taken. The imported uses can simply use the clone created in this module.
61-
// For now we are conservative in determining which variables are not
62-
// address taken by checking the unnamed addr flag. To be more aggressive,
63-
// the address taken information must be checked earlier during parsing
64-
// of the module and recorded in the summary index for use when importing
65-
// from that module.
66-
auto *GVar = dyn_cast<GlobalVariable>(SGV);
67-
if (GVar && GVar->isConstant() && GVar->hasGlobalUnnamedAddr())
68-
return false;
69-
7059
// If we are exporting, we need to see whether this value is marked
7160
// as NoRename in the summary. If we are importing, we may not have
7261
// a summary in the distributed backend case (only summaries for values

llvm/test/Linker/funcimport.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
; constant variable need promotion).
1515
; RUN: llvm-link %t.bc -summary-index=%t3.thinlto.bc -S | FileCheck %s --check-prefix=EXPORTSTATIC
1616
; EXPORTSTATIC-DAG: @staticvar.llvm.{{.*}} = hidden global
17-
; EXPORTSTATIC-DAG: @staticconstvar = internal unnamed_addr constant
17+
; Eventually @staticconstvar can be exported as a copy and not promoted
18+
; EXPORTSTATIC-DAG: @staticconstvar.llvm.0 = hidden unnamed_addr constant
1819
; EXPORTSTATIC-DAG: @P.llvm.{{.*}} = hidden global void ()* null
1920
; EXPORTSTATIC-DAG: define hidden i32 @staticfunc.llvm.
2021
; EXPORTSTATIC-DAG: define hidden void @staticfunc2.llvm.
@@ -68,7 +69,8 @@
6869
; promoted and renamed (including static constant variable).
6970
; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=referencestatics:%t.bc -S | FileCheck %s --check-prefix=IMPORTSTATIC
7071
; IMPORTSTATIC-DAG: @staticvar.llvm.{{.*}} = external hidden global
71-
; IMPORTSTATIC-DAG: @staticconstvar.llvm.{{.*}} = internal unnamed_addr constant
72+
; Eventually @staticconstvar can be imported as a copy
73+
; IMPORTSTATIC-DAG: @staticconstvar.llvm.{{.*}} = external hidden unnamed_addr constant
7274
; IMPORTSTATIC-DAG: define available_externally i32 @referencestatics
7375
; IMPORTSTATIC-DAG: %call = call i32 @staticfunc.llvm.
7476
; IMPORTSTATIC-DAG: %0 = load i32, i32* @staticvar.llvm.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
2+
target triple = "x86_64-apple-macosx10.11.0"
3+
4+
declare i8 **@foo()
5+
define i32 @main() {
6+
call i8 **@foo()
7+
ret i32 0
8+
}

llvm/test/ThinLTO/X86/Inputs/referenced_by_constant.ll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ define void @referencedbyglobal() {
66
ret void
77
}
88

9+
define internal void @localreferencedbyglobal() {
10+
ret void
11+
}
12+
913
@someglobal = internal unnamed_addr constant i8* bitcast (void ()* @referencedbyglobal to i8*)
14+
@someglobal2 = internal unnamed_addr constant i8* bitcast (void ()* @localreferencedbyglobal to i8*)
1015
@ptr = global i8** null
16+
@ptr2 = global i8** null
1117

1218
define void @bar() #0 align 2 {
1319
store i8** getelementptr inbounds (i8*, i8** @someglobal, i64 0) , i8*** @ptr, align 8
20+
store i8** getelementptr inbounds (i8*, i8** @someglobal2, i64 0) , i8*** @ptr2, align 8
1421
ret void
1522
}
16-

llvm/test/ThinLTO/X86/funcimport.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
; constant variable need promotion).
1111
; RUN: llvm-lto -thinlto-action=promote %t.bc -thinlto-index=%t3.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=EXPORTSTATIC
1212
; EXPORTSTATIC-DAG: @staticvar.llvm.0 = hidden global
13-
; EXPORTSTATIC-DAG: @staticconstvar = internal unnamed_addr constant
13+
; Eventually @staticconstvar can be exported as a copy and not promoted
14+
; EXPORTSTATIC-DAG: @staticconstvar.llvm.0 = hidden unnamed_addr constant
1415
; EXPORTSTATIC-DAG: @P.llvm.0 = hidden global void ()* null
1516
; EXPORTSTATIC-DAG: define hidden i32 @staticfunc.llvm.0
1617
; EXPORTSTATIC-DAG: define hidden void @staticfunc2.llvm.0
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; RUN: opt -module-summary %s -o %t1.bc
2+
; RUN: opt -module-summary %p/Inputs/reference_non_importable.ll -o %t2.bc
3+
4+
; RUN: llvm-lto2 %t1.bc %t2.bc -o %t.o -save-temps \
5+
; RUN: -r=%t1.bc,_foo,pxl \
6+
; RUN: -r=%t1.bc,_b,pxl \
7+
; RUN: -r=%t2.bc,_main,pxl \
8+
; RUN: -r=%t2.bc,_foo,xl
9+
10+
11+
12+
13+
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
14+
target triple = "x86_64-apple-macosx10.11.0"
15+
16+
; We shouldn't promote the private because it has a section
17+
; RUN: llvm-dis < %t.o.0.2.internalize.bc | FileCheck %s --check-prefix=PROMOTE
18+
; PROMOTE: @a = private global i8 0, section "__TEXT,__cstring,cstring_literals"
19+
@a = private global i8 0, section "__TEXT,__cstring,cstring_literals"
20+
@b = global i8 *@a
21+
22+
23+
; We want foo to be imported in the main module!
24+
; RUN: llvm-dis < %t.o.1.3.import.bc | FileCheck %s --check-prefix=IMPORT
25+
; IMPORT: define available_externally i8** @foo()
26+
define i8 **@foo() {
27+
ret i8 **@b
28+
}

llvm/test/ThinLTO/X86/referenced_by_constant.ll

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@
33
; RUN: opt -module-summary %p/Inputs/referenced_by_constant.ll -o %t2.bc
44
; RUN: llvm-lto -thinlto-action=thinlink -o %t3.bc %t.bc %t2.bc
55

6-
; Check the import side: we import bar() and @someglobal, but not @referencedbyglobal()
6+
; Check the import side: we currently only import bar() (with a future
7+
; enhancement to identify constants in the summary, we should mark
8+
; @someglobal/@someglobal2 for import as a local copy, which would
9+
; cause @referencedbyglobal and @localreferencedbyglobal to be exported
10+
; and promoted).
711
; RUN: llvm-lto -thinlto-action=import %t.bc -thinlto-index=%t3.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=IMPORT
8-
; IMPORT: @someglobal.llvm.0 =
12+
; IMPORT: @someglobal.llvm.0 = external hidden unnamed_addr constant
13+
; IMPORT: @someglobal2.llvm.0 = external hidden unnamed_addr constant
914
; IMPORT: define available_externally void @bar()
10-
; IMPORT: declare void @referencedbyglobal()
15+
16+
; Check the export side: we currently only export bar(), which causes
17+
; @someglobal and @someglobal2 to be promoted (see above).
18+
; RUN: llvm-lto -thinlto-action=promote %t2.bc -thinlto-index=%t3.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=EXPORT
19+
; EXPORT: @someglobal.llvm.0 = hidden unnamed_addr constant
20+
; EXPORT: @someglobal2.llvm.0 = hidden unnamed_addr constant
21+
; EXPORT: define void @referencedbyglobal()
22+
; EXPORT: define internal void @localreferencedbyglobal()
1123

1224
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
1325
target triple = "x86_64-apple-macosx10.11.0"
@@ -18,4 +30,3 @@ define void @foo() {
1830
call void @bar()
1931
ret void
2032
}
21-

0 commit comments

Comments
 (0)