llvm.org GIT mirror llvm / b6c8e5a
Merging r352770: ------------------------------------------------------------------------ r352770 | tejohnson | 2019-01-31 18:18:11 +0100 (Thu, 31 Jan 2019) | 3 lines Recommit "[ThinLTO] Rename COMDATs for COFF when promoting/renaming COMDAT leader" Recommit of r352763 with fix for use after free. ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_80@352856 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 1 year, 9 months ago
4 changed file(s) with 65 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
4242 /// Set of llvm.*used values, in order to validate that we don't try
4343 /// to promote any non-renamable values.
4444 SmallPtrSet Used;
45
46 /// Keep track of any COMDATs that require renaming (because COMDAT
47 /// leader was promoted and renamed). Maps from original COMDAT to one
48 /// with new name.
49 DenseMap RenamedComdats;
4550
4651 /// Check if we should promote the given local value to global scope.
4752 bool shouldPromoteLocalToGlobal(const GlobalValue *SGV);
248248 bool DoPromote = false;
249249 if (GV.hasLocalLinkage() &&
250250 ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) {
251 // Save the original name string before we rename GV below.
252 auto Name = GV.getName().str();
251253 // Once we change the name or linkage it is difficult to determine
252254 // again whether we should promote since shouldPromoteLocalToGlobal needs
253255 // to locate the summary (based on GUID from name and linkage). Therefore,
256258 GV.setLinkage(getLinkage(&GV, DoPromote));
257259 if (!GV.hasLocalLinkage())
258260 GV.setVisibility(GlobalValue::HiddenVisibility);
261
262 // If we are renaming a COMDAT leader, ensure that we record the COMDAT
263 // for later renaming as well. This is required for COFF.
264 if (const auto *C = GV.getComdat())
265 if (C->getName() == Name)
266 RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName()));
259267 } else
260268 GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
261269
280288 processGlobalForThinLTO(SF);
281289 for (GlobalAlias &GA : M.aliases())
282290 processGlobalForThinLTO(GA);
291
292 // Replace any COMDATS that required renaming (because the COMDAT leader was
293 // promoted and renamed).
294 if (!RenamedComdats.empty())
295 for (auto &GO : M.global_objects())
296 if (auto *C = GO.getComdat()) {
297 auto Replacement = RenamedComdats.find(C);
298 if (Replacement != RenamedComdats.end())
299 GO.setComdat(Replacement->second);
300 }
283301 }
284302
285303 bool FunctionImportGlobalProcessing::run() {
0 target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
1 target triple = "x86_64-pc-windows-msvc19.0.24215"
2
3 define void @main() {
4 entry:
5 call i8* @lwt_fun()
6 ret void
7 }
8
9 declare i8* @lwt_fun()
0 ; Test to ensure that comdat is renamed consistently when comdat leader is
1 ; promoted and renamed due to an import. Required by COFF.
2
3 ; REQUIRES: x86-registered-target
4
5 ; RUN: opt -thinlto-bc -o %t1.bc %s
6 ; RUN: opt -thinlto-bc -o %t2.bc %S/Inputs/comdat.ll
7 ; RUN: llvm-lto2 run -save-temps -o %t3 %t1.bc %t2.bc \
8 ; RUN: -r %t1.bc,lwt_fun,plx \
9 ; RUN: -r %t2.bc,main,plx \
10 ; RUN: -r %t2.bc,lwt_fun,
11 ; RUN: llvm-dis -o - %t3.1.3.import.bc | FileCheck %s
12
13 target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
14 target triple = "x86_64-pc-windows-msvc19.0.24215"
15
16 ; CHECK: $lwt.llvm.[[HASH:[0-9]+]] = comdat any
17 $lwt = comdat any
18
19 ; CHECK: @lwt_aliasee = private unnamed_addr global {{.*}}, comdat($lwt.llvm.[[HASH]])
20 @lwt_aliasee = private unnamed_addr global [1 x i8*] [i8* null], comdat($lwt)
21
22 ; CHECK: @lwt.llvm.[[HASH]] = hidden unnamed_addr alias
23 @lwt = internal unnamed_addr alias [1 x i8*], [1 x i8*]* @lwt_aliasee
24
25 ; Below function should get imported into other module, resulting in @lwt being
26 ; promoted and renamed.
27 define i8* @lwt_fun() {
28 %1 = getelementptr inbounds [1 x i8*], [1 x i8*]* @lwt, i32 0, i32 0
29 %2 = load i8*, i8** %1
30 ret i8* %2
31 }