Skip to content

Commit 2df298a

Browse files
committed
ThinLTO: Add flag to print uselistorder in bitcode writer pass
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 8742022 commit 2df298a

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
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: 11 additions & 7 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));
@@ -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
@@ -591,7 +594,8 @@ llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
591594
[&FAM](Function &F) -> AAResults & {
592595
return FAM.getResult<AAManager>(F);
593596
},
594-
M, &AM.getResult<ModuleSummaryIndexAnalysis>(M));
597+
M, &AM.getResult<ModuleSummaryIndexAnalysis>(M),
598+
ShouldPreserveUseListOrder);
595599

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

llvm/tools/opt/NewPMDriver.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,8 @@ bool llvm::runPassPipeline(
517517
break;
518518
case OK_OutputThinLTOBitcode:
519519
MPM.addPass(ThinLTOBitcodeWriterPass(
520-
Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr));
520+
Out->os(), ThinLTOLinkOut ? &ThinLTOLinkOut->os() : nullptr,
521+
ShouldPreserveBitcodeUseListOrder));
521522
break;
522523
}
523524

0 commit comments

Comments
 (0)