Skip to content

Commit d8e4163

Browse files
committed
[NewPM][CodeGen] Port localstackalloc to new pass manager
1 parent 9b0e1c2 commit d8e4163

File tree

7 files changed

+55
-10
lines changed

7 files changed

+55
-10
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===- LocalStackSlotAllocation.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_LOCALSTACKSLOTALLOCATION_H
10+
#define LLVM_CODEGEN_LOCALSTACKSLOTALLOCATION_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
16+
class LocalStackSlotAllocationPass
17+
: public PassInfoMixin<LocalStackSlotAllocationPass> {
18+
public:
19+
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &);
20+
};
21+
22+
} // namespace llvm
23+
#endif // LLVM_CODEGEN_LOCALSTACKSLOTALLOCATION_H

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/CodeGen/InterleavedAccess.h"
3737
#include "llvm/CodeGen/InterleavedLoadCombine.h"
3838
#include "llvm/CodeGen/JMCInstrumenter.h"
39+
#include "llvm/CodeGen/LocalStackSlotAllocation.h"
3940
#include "llvm/CodeGen/LowerEmuTLS.h"
4041
#include "llvm/CodeGen/MIRPrinter.h"
4142
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
@@ -881,7 +882,7 @@ Error CodeGenPassBuilder<Derived, TargetMachineT>::addMachinePasses(
881882
} else {
882883
// If the target requests it, assign local variables to stack slots relative
883884
// to one another and simplify frame index references where possible.
884-
addPass(LocalStackSlotPass());
885+
addPass(LocalStackSlotAllocationPass());
885886
}
886887

887888
if (TM.Options.EnableIPRA)
@@ -995,7 +996,7 @@ void CodeGenPassBuilder<Derived, TargetMachineT>::addMachineSSAOptimization(
995996

996997
// If the target requests it, assign local variables to stack slots relative
997998
// to one another and simplify frame index references where possible.
998-
addPass(LocalStackSlotPass());
999+
addPass(LocalStackSlotAllocationPass());
9991000

10001001
// With optimization, dead code should already be eliminated. However
10011002
// there is one known exception: lowered code for arguments that are only

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PI
125125
#endif
126126
MACHINE_FUNCTION_PASS("dead-mi-elimination", DeadMachineInstructionElimPass())
127127
MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass())
128+
MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotAllocationPass())
128129
MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass())
129130
MACHINE_FUNCTION_PASS("print", PrintMIRPass())
130131
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
@@ -182,7 +183,6 @@ DUMMY_MACHINE_FUNCTION_PASS("kcfi", MachineKCFIPass)
182183
DUMMY_MACHINE_FUNCTION_PASS("legalizer", LegalizerPass)
183184
DUMMY_MACHINE_FUNCTION_PASS("livedebugvalues", LiveDebugValuesPass)
184185
DUMMY_MACHINE_FUNCTION_PASS("liveintervals", LiveIntervalsPass)
185-
DUMMY_MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotPass)
186186
DUMMY_MACHINE_FUNCTION_PASS("lrshrink", LiveRangeShrinkPass)
187187
DUMMY_MACHINE_FUNCTION_PASS("machine-combiner", MachineCombinerPass)
188188
DUMMY_MACHINE_FUNCTION_PASS("machine-cp", MachineCopyPropagationPass)

llvm/lib/CodeGen/LocalStackSlotAllocation.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//
1414
//===----------------------------------------------------------------------===//
1515

16+
#include "llvm/CodeGen/LocalStackSlotAllocation.h"
1617
#include "llvm/ADT/SetVector.h"
1718
#include "llvm/ADT/SmallSet.h"
1819
#include "llvm/ADT/SmallVector.h"
@@ -71,7 +72,7 @@ namespace {
7172
int getFrameIndex() const { return FrameIdx; }
7273
};
7374

74-
class LocalStackSlotPass: public MachineFunctionPass {
75+
class LocalStackSlotImpl {
7576
SmallVector<int64_t, 16> LocalOffsets;
7677

7778
/// StackObjSet - A set of stack object indexes
@@ -86,14 +87,21 @@ namespace {
8687
void calculateFrameObjectOffsets(MachineFunction &Fn);
8788
bool insertFrameReferenceRegisters(MachineFunction &Fn);
8889

90+
public:
91+
bool runOnMachineFunction(MachineFunction &MF);
92+
};
93+
94+
class LocalStackSlotPass : public MachineFunctionPass {
8995
public:
9096
static char ID; // Pass identification, replacement for typeid
9197

9298
explicit LocalStackSlotPass() : MachineFunctionPass(ID) {
9399
initializeLocalStackSlotPassPass(*PassRegistry::getPassRegistry());
94100
}
95101

96-
bool runOnMachineFunction(MachineFunction &MF) override;
102+
bool runOnMachineFunction(MachineFunction &MF) override {
103+
return LocalStackSlotImpl().runOnMachineFunction(MF);
104+
}
97105

98106
void getAnalysisUsage(AnalysisUsage &AU) const override {
99107
AU.setPreservesCFG();
@@ -103,13 +111,23 @@ namespace {
103111

104112
} // end anonymous namespace
105113

114+
PreservedAnalyses
115+
LocalStackSlotAllocationPass::run(MachineFunction &MF,
116+
MachineFunctionAnalysisManager &) {
117+
bool Changed = LocalStackSlotImpl().runOnMachineFunction(MF);
118+
if (!Changed)
119+
return PreservedAnalyses::all();
120+
auto PA = getMachineFunctionPassPreservedAnalyses();
121+
PA.preserveSet<CFGAnalyses>();
122+
}
123+
106124
char LocalStackSlotPass::ID = 0;
107125

108126
char &llvm::LocalStackSlotAllocationID = LocalStackSlotPass::ID;
109127
INITIALIZE_PASS(LocalStackSlotPass, DEBUG_TYPE,
110128
"Local Stack Slot Allocation", false, false)
111129

112-
bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) {
130+
bool LocalStackSlotImpl::runOnMachineFunction(MachineFunction &MF) {
113131
MachineFrameInfo &MFI = MF.getFrameInfo();
114132
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
115133
unsigned LocalObjectCount = MFI.getObjectIndexEnd();
@@ -139,7 +157,7 @@ bool LocalStackSlotPass::runOnMachineFunction(MachineFunction &MF) {
139157
}
140158

141159
/// AdjustStackOffset - Helper function used to adjust the stack frame offset.
142-
void LocalStackSlotPass::AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
160+
void LocalStackSlotImpl::AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
143161
int64_t &Offset, bool StackGrowsDown,
144162
Align &MaxAlign) {
145163
// If the stack grows down, add the object size to find the lowest address.
@@ -171,7 +189,7 @@ void LocalStackSlotPass::AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
171189

172190
/// AssignProtectedObjSet - Helper function to assign large stack objects (i.e.,
173191
/// those required to be close to the Stack Protector) to stack offsets.
174-
void LocalStackSlotPass::AssignProtectedObjSet(
192+
void LocalStackSlotImpl::AssignProtectedObjSet(
175193
const StackObjSet &UnassignedObjs, SmallSet<int, 16> &ProtectedObjs,
176194
MachineFrameInfo &MFI, bool StackGrowsDown, int64_t &Offset,
177195
Align &MaxAlign) {
@@ -183,7 +201,7 @@ void LocalStackSlotPass::AssignProtectedObjSet(
183201

184202
/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
185203
/// abstract stack objects.
186-
void LocalStackSlotPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
204+
void LocalStackSlotImpl::calculateFrameObjectOffsets(MachineFunction &Fn) {
187205
// Loop over all of the stack objects, assigning sequential addresses...
188206
MachineFrameInfo &MFI = Fn.getFrameInfo();
189207
const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
@@ -281,7 +299,7 @@ lookupCandidateBaseReg(unsigned BaseReg,
281299
return TRI->isFrameOffsetLegal(&MI, BaseReg, Offset);
282300
}
283301

284-
bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) {
302+
bool LocalStackSlotImpl::insertFrameReferenceRegisters(MachineFunction &Fn) {
285303
// Scan the function's instructions looking for frame index references.
286304
// For each, ask the target if it wants a virtual base register for it
287305
// based on what we can tell it about where the local will end up in the

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
#include "llvm/CodeGen/InterleavedAccess.h"
9191
#include "llvm/CodeGen/InterleavedLoadCombine.h"
9292
#include "llvm/CodeGen/JMCInstrumenter.h"
93+
#include "llvm/CodeGen/LocalStackSlotAllocation.h"
9394
#include "llvm/CodeGen/LowerEmuTLS.h"
9495
#include "llvm/CodeGen/MIRPrinter.h"
9596
#include "llvm/CodeGen/MachineFunctionAnalysis.h"

llvm/test/CodeGen/AArch64/aarch64st1.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Check that it doesn't crash with unhandled opcode error, see pr52249
22
# RUN: llc -mtriple=aarch64-none-linux-gnu -run-pass localstackalloc -o - %s | FileCheck %s
3+
# RUN: llc -mtriple=aarch64-none-linux-gnu -passes=localstackalloc -o - %s | FileCheck %s
34
--- |
45

56
define void @test_st1_to_sp(<2 x i32> %a, <4 x i16> %b, <8 x i8> %c, <2 x i64> %d) gc "statepoint-example" { entry: ret void }

llvm/test/CodeGen/AArch64/sve-localstackalloc.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -mtriple=aarch64--linux-gnu -mattr=+sve -run-pass=localstackalloc -o - %s | FileCheck %s
2+
# RUN: llc -mtriple=aarch64--linux-gnu -mattr=+sve -passes=localstackalloc -o - %s | FileCheck %s
23

34
--- |
45
; ModuleID = '<stdin>'

0 commit comments

Comments
 (0)