Skip to content

Commit c5ac63e

Browse files
authored
ThinLTO: Add flag to print uselistorder in bitcode writer pass (#133230)
This is needed in llvm-reduce to avoid perturbing the uselistorder in intermediate steps. Really llvm-reduce wants pure serialization with no dependency on the pass manager. There are other optimizations mixed in to the serialization here depending on metadata in the module, which is also bad. Part of #63621
1 parent c766d0c commit c5ac63e

File tree

6 files changed

+79
-15
lines changed

6 files changed

+79
-15
lines changed

llvm/include/llvm/Transforms/IPO/ThinLTOBitcodeWriter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ class ThinLTOBitcodeWriterPass
2626
: public PassInfoMixin<ThinLTOBitcodeWriterPass> {
2727
raw_ostream &OS;
2828
raw_ostream *ThinLinkOS;
29+
const bool ShouldPreserveUseListOrder;
2930

3031
public:
3132
// Writes bitcode to OS. Also write thin link file to ThinLinkOS, if
3233
// it's not nullptr.
33-
ThinLTOBitcodeWriterPass(raw_ostream &OS, raw_ostream *ThinLinkOS)
34-
: OS(OS), ThinLinkOS(ThinLinkOS) {}
34+
ThinLTOBitcodeWriterPass(raw_ostream &OS, raw_ostream *ThinLinkOS,
35+
bool ShouldPreserveUseListOrder = false)
36+
: OS(OS), ThinLinkOS(ThinLinkOS),
37+
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
3538

3639
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
3740

llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ static bool enableUnifiedLTO(Module &M) {
274274
// regular LTO bitcode file to OS.
275275
void splitAndWriteThinLTOBitcode(
276276
raw_ostream &OS, raw_ostream *ThinLinkOS,
277-
function_ref<AAResults &(Function &)> AARGetter, Module &M) {
277+
function_ref<AAResults &(Function &)> AARGetter, Module &M,
278+
const bool ShouldPreserveUseListOrder) {
278279
std::string ModuleId = getUniqueModuleId(&M);
279280
if (ModuleId.empty()) {
280281
assert(!enableUnifiedLTO(M));
@@ -283,14 +284,14 @@ void splitAndWriteThinLTOBitcode(
283284
ProfileSummaryInfo PSI(M);
284285
M.addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
285286
ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI);
286-
WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false, &Index,
287+
WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, &Index,
287288
/*UnifiedLTO=*/false);
288289

289290
if (ThinLinkOS)
290291
// We don't have a ThinLTO part, but still write the module to the
291292
// ThinLinkOS if requested so that the expected output file is produced.
292-
WriteBitcodeToFile(M, *ThinLinkOS, /*ShouldPreserveUseListOrder=*/false,
293-
&Index, /*UnifiedLTO=*/false);
293+
WriteBitcodeToFile(M, *ThinLinkOS, ShouldPreserveUseListOrder, &Index,
294+
/*UnifiedLTO=*/false);
294295

295296
return;
296297
}
@@ -487,9 +488,9 @@ void splitAndWriteThinLTOBitcode(
487488
// be used in the backends, and use that in the minimized bitcode
488489
// produced for the full link.
489490
ModuleHash ModHash = {{0}};
490-
W.writeModule(M, /*ShouldPreserveUseListOrder=*/false, &Index,
491+
W.writeModule(M, ShouldPreserveUseListOrder, &Index,
491492
/*GenerateHash=*/true, &ModHash);
492-
W.writeModule(*MergedM, /*ShouldPreserveUseListOrder=*/false, &MergedMIndex);
493+
W.writeModule(*MergedM, ShouldPreserveUseListOrder, &MergedMIndex);
493494
W.writeSymtab();
494495
W.writeStrtab();
495496
OS << Buffer;
@@ -530,13 +531,15 @@ bool hasTypeMetadata(Module &M) {
530531

531532
bool writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS,
532533
function_ref<AAResults &(Function &)> AARGetter,
533-
Module &M, const ModuleSummaryIndex *Index) {
534+
Module &M, const ModuleSummaryIndex *Index,
535+
const bool ShouldPreserveUseListOrder) {
534536
std::unique_ptr<ModuleSummaryIndex> NewIndex = nullptr;
535537
// See if this module has any type metadata. If so, we try to split it
536538
// or at least promote type ids to enable WPD.
537539
if (hasTypeMetadata(M)) {
538540
if (enableSplitLTOUnit(M)) {
539-
splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M);
541+
splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M,
542+
ShouldPreserveUseListOrder);
540543
return true;
541544
}
542545
// Promote type ids as needed for index-based WPD.
@@ -564,7 +567,7 @@ bool writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS,
564567
// be used in the backends, and use that in the minimized bitcode
565568
// produced for the full link.
566569
ModuleHash ModHash = {{0}};
567-
WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false, Index,
570+
WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index,
568571
/*GenerateHash=*/true, &ModHash);
569572
// If a minimized bitcode module was requested for the thin link, only
570573
// the information that is needed by thin link will be written in the
@@ -590,7 +593,8 @@ llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
590593
[&FAM](Function &F) -> AAResults & {
591594
return FAM.getResult<AAManager>(F);
592595
},
593-
M, &AM.getResult<ModuleSummaryIndexAnalysis>(M));
596+
M, &AM.getResult<ModuleSummaryIndexAnalysis>(M),
597+
ShouldPreserveUseListOrder);
594598

595599
return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
596600
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; Check that thin lto bitcode respects preserve-bc-uselistorder
2+
3+
; RUN: opt --preserve-bc-uselistorder --thinlto-bc --thinlto-split-lto-unit < %s | llvm-dis --preserve-ll-uselistorder | FileCheck %s
4+
5+
; CHECK: uselistorder ptr @g, { 3, 2, 1, 0 }
6+
7+
@g = external global i32
8+
9+
define void @func1() {
10+
load i32, ptr @g
11+
load i32, ptr @g
12+
ret void
13+
}
14+
15+
define void @func2() {
16+
load i32, ptr @g
17+
load i32, ptr @g
18+
ret void
19+
}
20+
21+
uselistorder ptr @g, { 3, 2, 1, 0 }

llvm/test/Transforms/ThinLTOBitcodeWriter/split.ll

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
; RUN: llvm-modextract -b -n 0 -o %t0.thinlink.bc %t2
77
; RUN: llvm-modextract -b -n 1 -o %t1.thinlink.bc %t2
88
; RUN: not llvm-modextract -b -n 2 -o - %t 2>&1 | FileCheck --check-prefix=ERROR %s
9-
; RUN: llvm-dis -o - %t0.bc | FileCheck --check-prefix=M0 %s
10-
; RUN: llvm-dis -o - %t1.bc | FileCheck --check-prefix=M1 %s
9+
; RUN: llvm-dis -preserve-ll-uselistorder -o - %t0.bc | FileCheck --check-prefix=M0 %s
10+
; RUN: llvm-dis -preserve-ll-uselistorder -o - %t1.bc | FileCheck --check-prefix=M1 %s
1111
; RUN: llvm-bcanalyzer -dump %t0.bc | FileCheck --check-prefix=BCA0 %s
1212
; RUN: llvm-bcanalyzer -dump %t1.bc | FileCheck --check-prefix=BCA1 %s
1313

@@ -34,11 +34,31 @@ $g = comdat any
3434
; M1: @g = global i8 42, comdat, !type !0
3535
@g = global i8 42, comdat, !type !0
3636

37+
; M0: @g1 = external global i8{{$}}
38+
; M1: @g1 = global i8 43, !type !0
39+
@g1 = global i8 43, !type !0
40+
3741
; M0: define ptr @f()
3842
; M1-NOT: @f()
3943
define ptr @f() {
4044
ret ptr @g
4145
}
4246

47+
; M0: define void @h(ptr %ptr)
48+
; M1-NOT: @h(
49+
50+
define void @h(ptr %ptr) {
51+
store ptr @g1, ptr %ptr
52+
store ptr @g1, ptr %ptr
53+
store ptr @g1, ptr %ptr
54+
store ptr @g1, ptr %ptr
55+
ret void
56+
}
57+
58+
; M0: uselistorder ptr @g1, { 3, 2, 0, 1 }
59+
; M1-NOT: uselistorder
60+
61+
uselistorder ptr @g1, { 3, 2, 0, 1 }
62+
4363
; M1: !0 = !{i32 0, !"typeid"}
4464
!0 = !{i32 0, !"typeid"}

llvm/test/Transforms/ThinLTOBitcodeWriter/unsplittable.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
; copy of the regular module.
66
; RUN: diff %t %t2
77

8+
9+
; Do it again, with preserved uselistorder
10+
; RUN: opt --preserve-bc-uselistorder -thinlto-bc -thin-link-bitcode-file=%t2 -thinlto-split-lto-unit -o %t %s
11+
; RUN: llvm-dis --preserve-ll-uselistorder -o - %t | FileCheck -check-prefixes=CHECK,USELISTORDER %s
12+
; RUN: llvm-bcanalyzer -dump %t | FileCheck --check-prefix=BCA %s
13+
; When not splitting the module, the thin link bitcode file should simply be a
14+
; copy of the regular module.
15+
; RUN: diff %t %t2
16+
17+
818
; BCA: <FULL_LTO_GLOBALVAL_SUMMARY_BLOCK
919
; BCA-NOT: <GLOBALVAL_SUMMARY_BLOCK
1020

@@ -18,6 +28,8 @@ declare void @sink(ptr)
1828

1929
; CHECK: define internal void @f()
2030
define internal void @f() {
31+
call void @sink(ptr @g)
32+
call void @sink(ptr @g)
2133
call void @sink(ptr @g)
2234
ret void
2335
}
@@ -28,6 +40,9 @@ define void @h() comdat {
2840
ret void
2941
}
3042

43+
uselistorder ptr @g, { 2, 1, 0}
44+
; USELISTORDER: uselistorder ptr @g, { 2, 1, 0 }
45+
3146
; CHECK: !llvm.module.flags = !{![[FLAG1:[0-9]+]], ![[FLAG2:[0-9]+]]}
3247
; CHECK: ![[FLAG1]] = !{i32 1, !"EnableSplitLTOUnit", i32 1}
3348
; CHECK: ![[FLAG2]] = !{i32 1, !"ThinLTO", i32 0}

llvm/tools/opt/NewPMDriver.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,8 @@ bool llvm::runPassPipeline(
519519
break;
520520
case OK_OutputThinLTOBitcode:
521521
MPM.addPass(ThinLTOBitcodeWriterPass(
522-
Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
522+
Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr,
523+
ShouldPreserveBitcodeUseListOrder));
523524
break;
524525
}
525526

0 commit comments

Comments
 (0)