Skip to content

Commit 85aa697

Browse files
committed
ThinLTO: Add flag to print uselistorder in bitcode writer pass
Use it in llvm-reduce to avoid perturbing the use order in the intermediate step. Fixes #63621
1 parent 8abca17 commit 85aa697

File tree

5 files changed

+43
-13
lines changed

5 files changed

+43
-13
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: opt --thinlto-bc --thinlto-split-lto-unit %s -o %t.0
2+
; RUN: llvm-reduce -write-tmp-files-as-bitcode --delta-passes=instructions %t.0 -o %t.1 \
3+
; RUN: --test %python --test-arg %p/Inputs/llvm-dis-and-filecheck.py --test-arg llvm-dis --test-arg FileCheck --test-arg --check-prefix=INTERESTING --test-arg %s
4+
; RUN: llvm-dis --preserve-ll-uselistorder %t.1 -o %t.2
5+
; RUN: FileCheck --check-prefix=RESULT %s < %t.2
6+
7+
define i32 @func(i32 %arg0, i32 %arg1) {
8+
entry:
9+
%add0 = add i32 %arg0, 0
10+
%add1 = add i32 %add0, 0
11+
%add2 = add i32 %add1, 0
12+
%add3 = add i32 %arg1, 0
13+
%add4 = add i32 %add2, %add3
14+
ret i32 %add4
15+
}
16+
17+
; INTERESTING: uselistorder i32 0
18+
; RESULT: uselistorder i32 0, { 0, 2, 1 }
19+
uselistorder i32 0, { 3, 2, 1, 0 }

llvm/tools/llvm-reduce/ReducerWorkItem.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ void ReducerWorkItem::readBitcode(MemoryBufferRef Data, LLVMContext &Ctx,
769769
}
770770

771771
void ReducerWorkItem::writeBitcode(raw_ostream &OutStream) const {
772+
const bool ShouldPreserveUseListOrder = true;
773+
772774
if (LTOInfo && LTOInfo->IsThinLTO && LTOInfo->EnableSplitLTOUnit) {
773775
PassBuilder PB;
774776
LoopAnalysisManager LAM;
@@ -781,7 +783,8 @@ void ReducerWorkItem::writeBitcode(raw_ostream &OutStream) const {
781783
PB.registerLoopAnalyses(LAM);
782784
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
783785
ModulePassManager MPM;
784-
MPM.addPass(ThinLTOBitcodeWriterPass(OutStream, nullptr));
786+
MPM.addPass(ThinLTOBitcodeWriterPass(OutStream, nullptr,
787+
ShouldPreserveUseListOrder));
785788
MPM.run(*M, MAM);
786789
} else {
787790
std::unique_ptr<ModuleSummaryIndex> Index;
@@ -790,8 +793,8 @@ void ReducerWorkItem::writeBitcode(raw_ostream &OutStream) const {
790793
Index = std::make_unique<ModuleSummaryIndex>(
791794
buildModuleSummaryIndex(*M, nullptr, &PSI));
792795
}
793-
WriteBitcodeToFile(getModule(), OutStream,
794-
/*ShouldPreserveUseListOrder=*/true, Index.get());
796+
WriteBitcodeToFile(getModule(), OutStream, ShouldPreserveUseListOrder,
797+
Index.get());
795798
}
796799
}
797800

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)