Skip to content

Commit 53a8b89

Browse files
authored
[CodeGen][NewPM] Port "ShrinkWrap" pass to NPM (llvm#129880)
1 parent 55d0610 commit 53a8b89

File tree

11 files changed

+107
-28
lines changed

11 files changed

+107
-28
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===- llvm/CodeGen/ShrinkWrap.h --------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CODEGEN_SHRINKWRAP_H
10+
#define LLVM_CODEGEN_SHRINKWRAP_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
16+
class ShrinkWrapPass : public PassInfoMixin<ShrinkWrapPass> {
17+
public:
18+
PreservedAnalyses run(MachineFunction &MF,
19+
MachineFunctionAnalysisManager &MFAM);
20+
21+
MachineFunctionProperties getRequiredProperties() const {
22+
return MachineFunctionProperties().set(
23+
MachineFunctionProperties::Property::NoVRegs);
24+
}
25+
};
26+
27+
} // namespace llvm
28+
29+
#endif // LLVM_CODEGEN_SHRINKWRAP_H

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ void initializeScavengerTestPass(PassRegistry &);
285285
void initializeScopedNoAliasAAWrapperPassPass(PassRegistry &);
286286
void initializeSeparateConstOffsetFromGEPLegacyPassPass(PassRegistry &);
287287
void initializeShadowStackGCLoweringPass(PassRegistry &);
288-
void initializeShrinkWrapPass(PassRegistry &);
288+
void initializeShrinkWrapLegacyPass(PassRegistry &);
289289
void initializeSingleLoopExtractorPass(PassRegistry &);
290290
void initializeSinkingLegacyPassPass(PassRegistry &);
291291
void initializeSjLjEHPreparePass(PassRegistry &);

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
#include "llvm/CodeGen/SanitizerBinaryMetadata.h"
8585
#include "llvm/CodeGen/SelectOptimize.h"
8686
#include "llvm/CodeGen/ShadowStackGCLowering.h"
87+
#include "llvm/CodeGen/ShrinkWrap.h"
8788
#include "llvm/CodeGen/SjLjEHPrepare.h"
8889
#include "llvm/CodeGen/StackColoring.h"
8990
#include "llvm/CodeGen/StackFrameLayoutAnalysisPass.h"

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ MACHINE_FUNCTION_PASS("remove-loads-into-fake-uses", RemoveLoadsIntoFakeUsesPass
192192
MACHINE_FUNCTION_PASS("remove-redundant-debug-values", RemoveRedundantDebugValuesPass())
193193
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
194194
RequireAllMachineFunctionPropertiesPass())
195+
MACHINE_FUNCTION_PASS("shrink-wrap", ShrinkWrapPass())
195196
MACHINE_FUNCTION_PASS("stack-coloring", StackColoringPass())
196197
MACHINE_FUNCTION_PASS("stack-frame-layout", StackFrameLayoutAnalysisPass())
197198
MACHINE_FUNCTION_PASS("stack-slot-coloring", StackSlotColoringPass())
@@ -317,7 +318,6 @@ DUMMY_MACHINE_FUNCTION_PASS("regalloc", RegAllocPass)
317318
DUMMY_MACHINE_FUNCTION_PASS("regallocscoringpass", RegAllocScoringPass)
318319
DUMMY_MACHINE_FUNCTION_PASS("regbankselect", RegBankSelectPass)
319320
DUMMY_MACHINE_FUNCTION_PASS("reset-machine-function", ResetMachineFunctionPass)
320-
DUMMY_MACHINE_FUNCTION_PASS("shrink-wrap", ShrinkWrapPass)
321321
DUMMY_MACHINE_FUNCTION_PASS("stackmap-liveness", StackMapLivenessPass)
322322
DUMMY_MACHINE_FUNCTION_PASS("unpack-mi-bundles", UnpackMachineBundlesPass)
323323
DUMMY_MACHINE_FUNCTION_PASS("virtregrewriter", VirtRegRewriterPass)

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
123123
initializeSafeStackLegacyPassPass(Registry);
124124
initializeSelectOptimizePass(Registry);
125125
initializeShadowStackGCLoweringPass(Registry);
126-
initializeShrinkWrapPass(Registry);
126+
initializeShrinkWrapLegacyPass(Registry);
127127
initializeSjLjEHPreparePass(Registry);
128128
initializeSlotIndexesWrapperPassPass(Registry);
129129
initializeStackColoringLegacyPass(Registry);

llvm/lib/CodeGen/ShrinkWrap.cpp

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
//
4848
//===----------------------------------------------------------------------===//
4949

50+
#include "llvm/CodeGen/ShrinkWrap.h"
5051
#include "llvm/ADT/BitVector.h"
5152
#include "llvm/ADT/PostOrderIterator.h"
5253
#include "llvm/ADT/SetVector.h"
@@ -110,7 +111,7 @@ namespace {
110111
/// does not rely on expensive data-flow analysis. Instead we use the
111112
/// dominance properties and loop information to decide which point
112113
/// are safe for such insertion.
113-
class ShrinkWrap : public MachineFunctionPass {
114+
class ShrinkWrapImpl {
114115
/// Hold callee-saved information.
115116
RegisterClassInfo RCI;
116117
MachineDominatorTree *MDT = nullptr;
@@ -224,13 +225,8 @@ class ShrinkWrap : public MachineFunctionPass {
224225
/// Initialize the pass for \p MF.
225226
void init(MachineFunction &MF) {
226227
RCI.runOnMachineFunction(MF);
227-
MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
228-
MPDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();
229228
Save = nullptr;
230229
Restore = nullptr;
231-
MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
232-
MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
233-
ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
234230
EntryFreq = MBFI->getEntryFreq();
235231
const TargetSubtargetInfo &Subtarget = MF.getSubtarget();
236232
const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
@@ -248,14 +244,24 @@ class ShrinkWrap : public MachineFunctionPass {
248244
/// shrink-wrapping.
249245
bool ArePointsInteresting() const { return Save != Entry && Save && Restore; }
250246

247+
public:
248+
ShrinkWrapImpl(MachineDominatorTree *MDT, MachinePostDominatorTree *MPDT,
249+
MachineBlockFrequencyInfo *MBFI, MachineLoopInfo *MLI,
250+
MachineOptimizationRemarkEmitter *ORE)
251+
: MDT(MDT), MPDT(MPDT), MBFI(MBFI), MLI(MLI), ORE(ORE) {}
252+
251253
/// Check if shrink wrapping is enabled for this target and function.
252254
static bool isShrinkWrapEnabled(const MachineFunction &MF);
253255

256+
bool run(MachineFunction &MF);
257+
};
258+
259+
class ShrinkWrapLegacy : public MachineFunctionPass {
254260
public:
255261
static char ID;
256262

257-
ShrinkWrap() : MachineFunctionPass(ID) {
258-
initializeShrinkWrapPass(*PassRegistry::getPassRegistry());
263+
ShrinkWrapLegacy() : MachineFunctionPass(ID) {
264+
initializeShrinkWrapLegacyPass(*PassRegistry::getPassRegistry());
259265
}
260266

261267
void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -282,20 +288,22 @@ class ShrinkWrap : public MachineFunctionPass {
282288

283289
} // end anonymous namespace
284290

285-
char ShrinkWrap::ID = 0;
291+
char ShrinkWrapLegacy::ID = 0;
286292

287-
char &llvm::ShrinkWrapID = ShrinkWrap::ID;
293+
char &llvm::ShrinkWrapID = ShrinkWrapLegacy::ID;
288294

289-
INITIALIZE_PASS_BEGIN(ShrinkWrap, DEBUG_TYPE, "Shrink Wrap Pass", false, false)
295+
INITIALIZE_PASS_BEGIN(ShrinkWrapLegacy, DEBUG_TYPE, "Shrink Wrap Pass", false,
296+
false)
290297
INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfoWrapperPass)
291298
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
292299
INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTreeWrapperPass)
293300
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
294301
INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
295-
INITIALIZE_PASS_END(ShrinkWrap, DEBUG_TYPE, "Shrink Wrap Pass", false, false)
302+
INITIALIZE_PASS_END(ShrinkWrapLegacy, DEBUG_TYPE, "Shrink Wrap Pass", false,
303+
false)
296304

297-
bool ShrinkWrap::useOrDefCSROrFI(const MachineInstr &MI, RegScavenger *RS,
298-
bool StackAddressUsed) const {
305+
bool ShrinkWrapImpl::useOrDefCSROrFI(const MachineInstr &MI, RegScavenger *RS,
306+
bool StackAddressUsed) const {
299307
/// Check if \p Op is known to access an address not on the function's stack .
300308
/// At the moment, accesses where the underlying object is a global, function
301309
/// argument, or jump table are considered non-stack accesses. Note that the
@@ -549,7 +557,7 @@ static void rollbackRestoreSplit(MachineFunction &MF, MachineBasicBlock *NMBB,
549557
// A block is deemed fit for restore point split iff there exist
550558
// 1. DirtyPreds - preds of CurRestore reachable from use or def of CSR/FI
551559
// 2. CleanPreds - preds of CurRestore that arent DirtyPreds
552-
bool ShrinkWrap::checkIfRestoreSplittable(
560+
bool ShrinkWrapImpl::checkIfRestoreSplittable(
553561
const MachineBasicBlock *CurRestore,
554562
const DenseSet<const MachineBasicBlock *> &ReachableByDirty,
555563
SmallVectorImpl<MachineBasicBlock *> &DirtyPreds,
@@ -572,8 +580,8 @@ bool ShrinkWrap::checkIfRestoreSplittable(
572580
return !(CleanPreds.empty() || DirtyPreds.empty());
573581
}
574582

575-
bool ShrinkWrap::postShrinkWrapping(bool HasCandidate, MachineFunction &MF,
576-
RegScavenger *RS) {
583+
bool ShrinkWrapImpl::postShrinkWrapping(bool HasCandidate, MachineFunction &MF,
584+
RegScavenger *RS) {
577585
if (!EnablePostShrinkWrapOpt)
578586
return false;
579587

@@ -679,8 +687,8 @@ bool ShrinkWrap::postShrinkWrapping(bool HasCandidate, MachineFunction &MF,
679687
return true;
680688
}
681689

682-
void ShrinkWrap::updateSaveRestorePoints(MachineBasicBlock &MBB,
683-
RegScavenger *RS) {
690+
void ShrinkWrapImpl::updateSaveRestorePoints(MachineBasicBlock &MBB,
691+
RegScavenger *RS) {
684692
// Get rid of the easy cases first.
685693
if (!Save)
686694
Save = &MBB;
@@ -810,7 +818,7 @@ static bool giveUpWithRemarks(MachineOptimizationRemarkEmitter *ORE,
810818
return false;
811819
}
812820

813-
bool ShrinkWrap::performShrinkWrapping(
821+
bool ShrinkWrapImpl::performShrinkWrapping(
814822
const ReversePostOrderTraversal<MachineBasicBlock *> &RPOT,
815823
RegScavenger *RS) {
816824
for (MachineBasicBlock *MBB : RPOT) {
@@ -919,10 +927,7 @@ bool ShrinkWrap::performShrinkWrapping(
919927
return true;
920928
}
921929

922-
bool ShrinkWrap::runOnMachineFunction(MachineFunction &MF) {
923-
if (skipFunction(MF.getFunction()) || MF.empty() || !isShrinkWrapEnabled(MF))
924-
return false;
925-
930+
bool ShrinkWrapImpl::run(MachineFunction &MF) {
926931
LLVM_DEBUG(dbgs() << "**** Analysing " << MF.getName() << '\n');
927932

928933
init(MF);
@@ -969,7 +974,44 @@ bool ShrinkWrap::runOnMachineFunction(MachineFunction &MF) {
969974
return Changed;
970975
}
971976

972-
bool ShrinkWrap::isShrinkWrapEnabled(const MachineFunction &MF) {
977+
bool ShrinkWrapLegacy::runOnMachineFunction(MachineFunction &MF) {
978+
if (skipFunction(MF.getFunction()) || MF.empty() ||
979+
!ShrinkWrapImpl::isShrinkWrapEnabled(MF))
980+
return false;
981+
982+
MachineDominatorTree *MDT =
983+
&getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
984+
MachinePostDominatorTree *MPDT =
985+
&getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();
986+
MachineBlockFrequencyInfo *MBFI =
987+
&getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
988+
MachineLoopInfo *MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
989+
MachineOptimizationRemarkEmitter *ORE =
990+
&getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
991+
992+
return ShrinkWrapImpl(MDT, MPDT, MBFI, MLI, ORE).run(MF);
993+
}
994+
995+
PreservedAnalyses ShrinkWrapPass::run(MachineFunction &MF,
996+
MachineFunctionAnalysisManager &MFAM) {
997+
MFPropsModifier _(*this, MF);
998+
if (MF.empty() || !ShrinkWrapImpl::isShrinkWrapEnabled(MF))
999+
return PreservedAnalyses::all();
1000+
1001+
MachineDominatorTree &MDT = MFAM.getResult<MachineDominatorTreeAnalysis>(MF);
1002+
MachinePostDominatorTree &MPDT =
1003+
MFAM.getResult<MachinePostDominatorTreeAnalysis>(MF);
1004+
MachineBlockFrequencyInfo &MBFI =
1005+
MFAM.getResult<MachineBlockFrequencyAnalysis>(MF);
1006+
MachineLoopInfo &MLI = MFAM.getResult<MachineLoopAnalysis>(MF);
1007+
MachineOptimizationRemarkEmitter &ORE =
1008+
MFAM.getResult<MachineOptimizationRemarkEmitterAnalysis>(MF);
1009+
1010+
ShrinkWrapImpl(&MDT, &MPDT, &MBFI, &MLI, &ORE).run(MF);
1011+
return PreservedAnalyses::all();
1012+
}
1013+
1014+
bool ShrinkWrapImpl::isShrinkWrapEnabled(const MachineFunction &MF) {
9731015
const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
9741016

9751017
switch (EnableShrinkWrapOpt) {

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
#include "llvm/CodeGen/SanitizerBinaryMetadata.h"
159159
#include "llvm/CodeGen/SelectOptimize.h"
160160
#include "llvm/CodeGen/ShadowStackGCLowering.h"
161+
#include "llvm/CodeGen/ShrinkWrap.h"
161162
#include "llvm/CodeGen/SjLjEHPrepare.h"
162163
#include "llvm/CodeGen/SlotIndexes.h"
163164
#include "llvm/CodeGen/SpillPlacement.h"

llvm/test/CodeGen/AArch64/dont-shrink-wrap-stack-mayloadorstore.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
; to a position where the stack might still be accessed by a load or store
55

66
; RUN: llc -x=mir -simplify-mir -run-pass=shrink-wrap -o - %s | FileCheck %s
7+
; RUN: llc -x=mir -simplify-mir -passes='shrink-wrap' -o - %s | FileCheck %s
78
; CHECK: name: compiler_pop_stack
89
; CHECK: frameInfo:
910
; CHECK: savePoint: '%bb.1'

llvm/test/CodeGen/AArch64/shrinkwrap-split-restore-point.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 2
22
# RUN: llc -mtriple=aarch64 -run-pass=shrink-wrap -o - %s | FileCheck %s
3+
# RUN: llc -mtriple=aarch64 -passes='shrink-wrap' -o - %s | FileCheck %s
34

45
--- |
56
define void @shrink_test1(i32 %a) {

llvm/test/CodeGen/PowerPC/shrink-wrap.mir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 2
22
# RUN: llc -verify-machineinstrs -mcpu=pwr9 -mtriple powerpc64le-unknown-linux-gnu \
33
# RUN: -run-pass=shrink-wrap -o - %s | FileCheck %s
4+
# RUN: llc -mcpu=pwr9 -mtriple powerpc64le-unknown-linux-gnu -passes='shrink-wrap' -o - %s | FileCheck %s
45
# RUN: llc -verify-machineinstrs -mcpu=pwr9 -mtriple powerpc-ibm-aix-xcoff \
56
# RUN: -run-pass=shrink-wrap -mattr=-altivec -o - %s | FileCheck %s
7+
# RUN: llc -mcpu=pwr9 -mtriple powerpc-ibm-aix-xcoff -passes='shrink-wrap' -mattr=-altivec -o - %s | FileCheck %s
68
# RUN: llc -verify-machineinstrs -mcpu=pwr9 -mtriple powerpc64-ibm-aix-xcoff \
79
# RUN: -run-pass=shrink-wrap -mattr=-altivec -o - %s | FileCheck %s
10+
# RUN: llc -mcpu=pwr9 -mtriple powerpc64-ibm-aix-xcoff -passes='shrink-wrap' -mattr=-altivec -o - %s | FileCheck %s
811
--- |
912
; ModuleID = 'test.ll'
1013
source_filename = "test.ll"

llvm/test/CodeGen/X86/shrink_wrap_dbg_value.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -o - %s -run-pass=shrink-wrap | FileCheck %s
2+
# RUN: llc -o - %s -passes='shrink-wrap' | FileCheck %s
23
--- |
34
; ModuleID = '<stdin>'
45
source_filename = "t.c"

0 commit comments

Comments
 (0)