Skip to content

Commit 1869085

Browse files
optimisanDanielCChen
authored andcommitted
[NewPM][CodeGen] Port StackColoring to NPM (llvm#111812)
1 parent e477f95 commit 1869085

File tree

13 files changed

+75
-24
lines changed

13 files changed

+75
-24
lines changed

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ namespace llvm {
285285

286286
/// StackSlotColoring - This pass performs stack coloring and merging.
287287
/// It merges disjoint allocas to reduce the stack size.
288-
extern char &StackColoringID;
288+
extern char &StackColoringLegacyID;
289289

290290
/// StackFramePrinter - This pass prints the stack frame layout and variable
291291
/// mappings.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===- llvm/CodeGen/StackColoring.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_STACKCOLORINGPASS_H
10+
#define LLVM_CODEGEN_STACKCOLORINGPASS_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
16+
class StackColoringPass : public PassInfoMixin<StackColoringPass> {
17+
public:
18+
PreservedAnalyses run(MachineFunction &MF,
19+
MachineFunctionAnalysisManager &MFAM);
20+
};
21+
22+
} // namespace llvm
23+
24+
#endif // LLVM_CODEGEN_STACKCOLORINGPASS_H

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void initializeSjLjEHPreparePass(PassRegistry &);
288288
void initializeSlotIndexesWrapperPassPass(PassRegistry &);
289289
void initializeSpeculativeExecutionLegacyPassPass(PassRegistry &);
290290
void initializeSpillPlacementPass(PassRegistry &);
291-
void initializeStackColoringPass(PassRegistry &);
291+
void initializeStackColoringLegacyPass(PassRegistry &);
292292
void initializeStackFrameLayoutAnalysisPassPass(PassRegistry &);
293293
void initializeStackMapLivenessPass(PassRegistry &);
294294
void initializeStackProtectorPass(PassRegistry &);

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "llvm/CodeGen/SelectOptimize.h"
5757
#include "llvm/CodeGen/ShadowStackGCLowering.h"
5858
#include "llvm/CodeGen/SjLjEHPrepare.h"
59+
#include "llvm/CodeGen/StackColoring.h"
5960
#include "llvm/CodeGen/StackProtector.h"
6061
#include "llvm/CodeGen/TargetPassConfig.h"
6162
#include "llvm/CodeGen/TwoAddressInstructionPass.h"

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ MACHINE_FUNCTION_PASS("print<machine-post-dom-tree>",
152152
MACHINE_FUNCTION_PASS("print<slot-indexes>", SlotIndexesPrinterPass(dbgs()))
153153
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
154154
RequireAllMachineFunctionPropertiesPass())
155+
MACHINE_FUNCTION_PASS("stack-coloring", StackColoringPass())
155156
MACHINE_FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
156157
MACHINE_FUNCTION_PASS("two-address-instruction", TwoAddressInstructionPass())
157158
MACHINE_FUNCTION_PASS("verify", MachineVerifierPass())
@@ -255,7 +256,6 @@ DUMMY_MACHINE_FUNCTION_PASS("rename-independent-subregs", RenameIndependentSubre
255256
DUMMY_MACHINE_FUNCTION_PASS("reset-machine-function", ResetMachineFunctionPass)
256257
DUMMY_MACHINE_FUNCTION_PASS("shrink-wrap", ShrinkWrapPass)
257258
DUMMY_MACHINE_FUNCTION_PASS("simple-register-coalescing", RegisterCoalescerPass)
258-
DUMMY_MACHINE_FUNCTION_PASS("stack-coloring", StackColoringPass)
259259
DUMMY_MACHINE_FUNCTION_PASS("stack-frame-layout", StackFrameLayoutAnalysisPass)
260260
DUMMY_MACHINE_FUNCTION_PASS("stack-slot-coloring", StackSlotColoringPass)
261261
DUMMY_MACHINE_FUNCTION_PASS("stackmap-liveness", StackMapLivenessPass)

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
125125
initializeShrinkWrapPass(Registry);
126126
initializeSjLjEHPreparePass(Registry);
127127
initializeSlotIndexesWrapperPassPass(Registry);
128-
initializeStackColoringPass(Registry);
128+
initializeStackColoringLegacyPass(Registry);
129129
initializeStackFrameLayoutAnalysisPassPass(Registry);
130130
initializeStackMapLivenessPass(Registry);
131131
initializeStackProtectorPass(Registry);

llvm/lib/CodeGen/StackColoring.cpp

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
//
2121
//===----------------------------------------------------------------------===//
2222

23+
#include "llvm/CodeGen/StackColoring.h"
2324
#include "llvm/ADT/BitVector.h"
2425
#include "llvm/ADT/DenseMap.h"
2526
#include "llvm/ADT/DepthFirstIterator.h"
@@ -376,7 +377,7 @@ namespace {
376377

377378
/// StackColoring - A machine pass for merging disjoint stack allocations,
378379
/// marked by the LIFETIME_START and LIFETIME_END pseudo instructions.
379-
class StackColoring : public MachineFunctionPass {
380+
class StackColoring {
380381
MachineFrameInfo *MFI = nullptr;
381382
MachineFunction *MF = nullptr;
382383

@@ -436,14 +437,8 @@ class StackColoring : public MachineFunctionPass {
436437
unsigned NumIterations;
437438

438439
public:
439-
static char ID;
440-
441-
StackColoring() : MachineFunctionPass(ID) {
442-
initializeStackColoringPass(*PassRegistry::getPassRegistry());
443-
}
444-
445-
void getAnalysisUsage(AnalysisUsage &AU) const override;
446-
bool runOnMachineFunction(MachineFunction &Func) override;
440+
StackColoring(SlotIndexes *Indexes) : Indexes(Indexes) {}
441+
bool run(MachineFunction &Func);
447442

448443
private:
449444
/// Used in collectMarkers
@@ -509,19 +504,29 @@ class StackColoring : public MachineFunctionPass {
509504
void expungeSlotMap(DenseMap<int, int> &SlotRemap, unsigned NumSlots);
510505
};
511506

507+
class StackColoringLegacy : public MachineFunctionPass {
508+
public:
509+
static char ID;
510+
511+
StackColoringLegacy() : MachineFunctionPass(ID) {}
512+
513+
void getAnalysisUsage(AnalysisUsage &AU) const override;
514+
bool runOnMachineFunction(MachineFunction &Func) override;
515+
};
516+
512517
} // end anonymous namespace
513518

514-
char StackColoring::ID = 0;
519+
char StackColoringLegacy::ID = 0;
515520

516-
char &llvm::StackColoringID = StackColoring::ID;
521+
char &llvm::StackColoringLegacyID = StackColoringLegacy::ID;
517522

518-
INITIALIZE_PASS_BEGIN(StackColoring, DEBUG_TYPE,
523+
INITIALIZE_PASS_BEGIN(StackColoringLegacy, DEBUG_TYPE,
519524
"Merge disjoint stack slots", false, false)
520525
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
521-
INITIALIZE_PASS_END(StackColoring, DEBUG_TYPE,
526+
INITIALIZE_PASS_END(StackColoringLegacy, DEBUG_TYPE,
522527
"Merge disjoint stack slots", false, false)
523528

524-
void StackColoring::getAnalysisUsage(AnalysisUsage &AU) const {
529+
void StackColoringLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
525530
AU.addRequired<SlotIndexesWrapperPass>();
526531
MachineFunctionPass::getAnalysisUsage(AU);
527532
}
@@ -1178,12 +1183,30 @@ void StackColoring::expungeSlotMap(DenseMap<int, int> &SlotRemap,
11781183
}
11791184
}
11801185

1181-
bool StackColoring::runOnMachineFunction(MachineFunction &Func) {
1186+
bool StackColoringLegacy::runOnMachineFunction(MachineFunction &MF) {
1187+
if (skipFunction(MF.getFunction()))
1188+
return false;
1189+
1190+
StackColoring SC(&getAnalysis<SlotIndexesWrapperPass>().getSI());
1191+
return SC.run(MF);
1192+
}
1193+
1194+
PreservedAnalyses StackColoringPass::run(MachineFunction &MF,
1195+
MachineFunctionAnalysisManager &MFAM) {
1196+
if (MF.getFunction().hasOptNone())
1197+
return PreservedAnalyses::all();
1198+
1199+
StackColoring SC(&MFAM.getResult<SlotIndexesAnalysis>(MF));
1200+
if (SC.run(MF))
1201+
return PreservedAnalyses::none();
1202+
return PreservedAnalyses::all();
1203+
}
1204+
1205+
bool StackColoring::run(MachineFunction &Func) {
11821206
LLVM_DEBUG(dbgs() << "********** Stack Coloring **********\n"
11831207
<< "********** Function: " << Func.getName() << '\n');
11841208
MF = &Func;
11851209
MFI = &MF->getFrameInfo();
1186-
Indexes = &getAnalysis<SlotIndexesWrapperPass>().getSI();
11871210
BlockLiveness.clear();
11881211
BasicBlocks.clear();
11891212
BasicBlockNumbering.clear();
@@ -1220,8 +1243,7 @@ bool StackColoring::runOnMachineFunction(MachineFunction &Func) {
12201243

12211244
// Don't continue because there are not enough lifetime markers, or the
12221245
// stack is too small, or we are told not to optimize the slots.
1223-
if (NumMarkers < 2 || TotalSize < 16 || DisableColoring ||
1224-
skipFunction(Func.getFunction())) {
1246+
if (NumMarkers < 2 || TotalSize < 16 || DisableColoring) {
12251247
LLVM_DEBUG(dbgs() << "Will not try to merge slots.\n");
12261248
return removeAllMarkers();
12271249
}

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ void TargetPassConfig::addMachineSSAOptimization() {
12871287

12881288
// This pass merges large allocas. StackSlotColoring is a different pass
12891289
// which merges spill slots.
1290-
addPass(&StackColoringID);
1290+
addPass(&StackColoringLegacyID);
12911291

12921292
// If the target requests it, assign local variables to stack slots relative
12931293
// to one another and simplify frame index references where possible.

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
#include "llvm/CodeGen/ShadowStackGCLowering.h"
119119
#include "llvm/CodeGen/SjLjEHPrepare.h"
120120
#include "llvm/CodeGen/SlotIndexes.h"
121+
#include "llvm/CodeGen/StackColoring.h"
121122
#include "llvm/CodeGen/StackProtector.h"
122123
#include "llvm/CodeGen/TargetPassConfig.h"
123124
#include "llvm/CodeGen/TwoAddressInstructionPass.h"

llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ void NVPTXPassConfig::addMachineSSAOptimization() {
445445

446446
// This pass merges large allocas. StackSlotColoring is a different pass
447447
// which merges spill slots.
448-
addPass(&StackColoringID);
448+
addPass(&StackColoringLegacyID);
449449

450450
// If the target requests it, assign local variables to stack slots relative
451451
// to one another and simplify frame index references where possible.

llvm/test/CodeGen/X86/PR37310.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -mtriple=x86_64-unknown-linux-gnu -mcpu=corei7 -no-stack-coloring=false -run-pass stack-coloring -o - %s
2+
# RUN: llc -mtriple=x86_64-unknown-linux-gnu -mcpu=corei7 -no-stack-coloring=false -passes stack-coloring -o - %s
23

34
# Test to insure that the liveness analysis in the StackColoring
45
# pass gracefully handles statically unreachable blocks. See PR 37310.

llvm/test/CodeGen/X86/StackColoring-dbg-invariance.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
22
# RUN: llc -mcpu=corei7 -no-stack-coloring=false -run-pass=stack-coloring -o - %s | FileCheck %s
3+
# RUN: llc -mcpu=corei7 -no-stack-coloring=false -passes=stack-coloring -o - %s | FileCheck %s
34

45
# Difference between test_1 and test_2 is that there is a DBG_VALUE in test_1.
56
# If transformation is debug invariant the resulting LEA instruction should be

llvm/test/CodeGen/X86/pr48064.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -mtriple="i386-pc-windows-msvc" -run-pass=stack-coloring %s -o - | FileCheck %s
2+
# RUN: llc -mtriple="i386-pc-windows-msvc" -passes=stack-coloring %s -o - | FileCheck %s
23

34
# There is a problem with the exception handler, we found in windows, when set
45
# LifetimeStartOnFirstUse=true for stack-coloring in default. Take the following

0 commit comments

Comments
 (0)