Skip to content

Commit cc0b964

Browse files
committed
[LLD][ThinLTO] Handle GUID collision in import global processing
Summary: If there are a GUID collision between two globals checking the summarylist from the import index to make assumption can be dangerous. Do not assume that a GlobalValue that has a GlobalVarSummary actually is a GlobalVariable as it can be another GlobalValue with the same GUID that the summary is connected to. Patch by Joel Klinghed ([email protected]) Reviewers: evgeny777, tejohnson Reviewed By: tejohnson Subscribers: tejohnson, dblaikie, MaskRay, mehdi_amini, inglorion, hiraditya, steven_wu, dexonsmith, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D67322
1 parent a4783ef commit cc0b964

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

llvm/lib/Transforms/Utils/FunctionImportUtils.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,17 @@ void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
239239
// propagateConstants hasn't been run. We can't internalize GV
240240
// in such case.
241241
if (!GV.isDeclaration() && VI && ImportIndex.withGlobalValueDeadStripping()) {
242-
const auto &SL = VI.getSummaryList();
243-
auto *GVS = SL.empty() ? nullptr : dyn_cast<GlobalVarSummary>(SL[0].get());
244-
// At this stage "maybe" is "definitely"
245-
if (GVS && (GVS->maybeReadOnly() || GVS->maybeWriteOnly()))
246-
cast<GlobalVariable>(&GV)->addAttribute("thinlto-internalize");
242+
if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
243+
// We can have more than one local with the same GUID, in the case of
244+
// same-named locals in different but same-named source files that were
245+
// compiled in their respective directories (so the source file name
246+
// and resulting GUID is the same). Find the one in this module.
247+
auto* GVS = dyn_cast<GlobalVarSummary>(
248+
ImportIndex.findSummaryInModule(VI, M.getModuleIdentifier()));
249+
// At this stage "maybe" is "definitely"
250+
if (GVS && (GVS->maybeReadOnly() || GVS->maybeWriteOnly()))
251+
V->addAttribute("thinlto-internalize");
252+
}
247253
}
248254

249255
bool DoPromote = false;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
2+
target triple = "x86_64-pc-linux-gnu"
3+
4+
; The source for the GUID for this symbol will be -:F
5+
source_filename = "-"
6+
define internal fastcc i64 @F() {
7+
ret i64 0
8+
}
9+
10+
; Needed to give llvm-lto2 something to do
11+
@dummy2 = global i32 0
12+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; Make sure LTO succeeds even if %t.bc contains a GlobalVariable F and
2+
; %t2.bc cointains a Function F with the same GUID.
3+
;
4+
; RUN: opt -module-summary %s -o %t.bc
5+
; RUN: opt -module-summary %p/Inputs/guid_collision.ll -o %t2.bc
6+
; RUN: llvm-lto2 run %t.bc %t2.bc -o %t.out \
7+
; RUN: -r=%t.bc,dummy,px -r=%t2.bc,dummy2,px
8+
9+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
10+
target triple = "x86_64-pc-linux-gnu"
11+
12+
; The source for the GUID for this symbol will be -:F
13+
source_filename = "-"
14+
@F = internal constant i8 0
15+
16+
; Needed to give llvm-lto2 something to do
17+
@dummy = global i32 0

0 commit comments

Comments
 (0)