Skip to content

Commit cb93d27

Browse files
author
Vasileios Porpodas
committed
[CodeGen][Spill2Reg] Initial patch
This is the first commit for the Spill2Reg optimization pass. The goal of this pass is to selectively replace spills to the stack with spills to vector registers. This can help remove back-end stalls in x86. Old code review: https://reviews.llvm.org/D118298 RFC: https://lists.llvm.org/pipermail/llvm-dev/2022-January/154782.html https://discourse.llvm.org/t/rfc-spill2reg-selectively-replace-spills-to-stack-with-spills-to-vector-registers/59630
1 parent 245f26a commit cb93d27

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,9 @@ namespace llvm {
608608

609609
/// Lowers KCFI operand bundles for indirect calls.
610610
FunctionPass *createKCFIPass();
611+
612+
/// This pass replaces spills to stack with spills to registers.
613+
extern char &Spill2RegID;
611614
} // End llvm namespace
612615

613616
#endif

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ void initializeWasmEHPreparePass(PassRegistry &);
321321
void initializeWinEHPreparePass(PassRegistry &);
322322
void initializeWriteBitcodePassPass(PassRegistry &);
323323
void initializeXRayInstrumentationPass(PassRegistry &);
324+
void initializeSpill2RegPass(PassRegistry &);
324325

325326
} // end namespace llvm
326327

llvm/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ add_llvm_component_library(LLVMCodeGen
219219
SjLjEHPrepare.cpp
220220
SlotIndexes.cpp
221221
SpillPlacement.cpp
222+
Spill2Reg.cpp
222223
SplitKit.cpp
223224
StackColoring.cpp
224225
StackFrameLayoutAnalysisPass.cpp

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,5 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
143143
initializeWasmEHPreparePass(Registry);
144144
initializeWinEHPreparePass(Registry);
145145
initializeXRayInstrumentationPass(Registry);
146+
initializeSpill2RegPass(Registry);
146147
}

llvm/lib/CodeGen/Spill2Reg.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===- Spill2Reg.cpp - Spill To Register Optimization ---------------------===//
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+
//
10+
/// \file This file implements Spill2Reg, an optimization which selectively
11+
/// replaces spills/reloads to/from the stack with register copies to/from the
12+
/// vector register file. This works even on targets where load/stores have
13+
/// similar latency to register copies because it can free up memory units which
14+
/// helps avoid back-end stalls.
15+
///
16+
//===----------------------------------------------------------------------===//
17+
18+
#include "llvm/CodeGen/MachineFunctionPass.h"
19+
#include "llvm/CodeGen/Passes.h"
20+
#include "llvm/InitializePasses.h"
21+
#include "llvm/Support/CommandLine.h"
22+
23+
using namespace llvm;
24+
25+
namespace {
26+
27+
class Spill2Reg : public MachineFunctionPass {
28+
public:
29+
static char ID;
30+
Spill2Reg() : MachineFunctionPass(ID) {
31+
initializeSpill2RegPass(*PassRegistry::getPassRegistry());
32+
}
33+
void getAnalysisUsage(AnalysisUsage &AU) const override;
34+
void releaseMemory() override;
35+
bool runOnMachineFunction(MachineFunction &) override;
36+
};
37+
38+
} // namespace
39+
40+
void Spill2Reg::getAnalysisUsage(AnalysisUsage &AU) const {
41+
AU.setPreservesCFG();
42+
MachineFunctionPass::getAnalysisUsage(AU);
43+
}
44+
45+
void Spill2Reg::releaseMemory() {}
46+
47+
bool Spill2Reg::runOnMachineFunction(MachineFunction &MFn) {
48+
llvm_unreachable("Unimplemented");
49+
}
50+
51+
char Spill2Reg::ID = 0;
52+
53+
char &llvm::Spill2RegID = Spill2Reg::ID;
54+
55+
INITIALIZE_PASS_BEGIN(Spill2Reg, "spill2reg", "Spill2Reg", false, false)
56+
INITIALIZE_PASS_END(Spill2Reg, "spill2reg", "Spill2Reg", false, false)

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ static cl::opt<bool> DisableReplaceWithVecLib(
214214
"disable-replace-with-vec-lib", cl::Hidden,
215215
cl::desc("Disable replace with vector math call pass"));
216216

217+
// Enable the Spill2Reg pass.
218+
static cl::opt<bool> EnableSpill2Reg("enable-spill2reg", cl::Hidden,
219+
cl::init(false),
220+
cl::desc("Enable Spill2Reg pass"));
221+
217222
/// Option names for limiting the codegen pipeline.
218223
/// Those are used in error reporting and we didn't want
219224
/// to duplicate their names all over the place.
@@ -1415,6 +1420,10 @@ bool TargetPassConfig::addRegAssignAndRewriteOptimized() {
14151420
// Finally rewrite virtual registers.
14161421
addPass(&VirtRegRewriterID);
14171422

1423+
// Replace spills to stack with spills to registers.
1424+
if (EnableSpill2Reg)
1425+
addPass(&Spill2RegID);
1426+
14181427
// Regalloc scoring for ML-driven eviction - noop except when learning a new
14191428
// eviction policy.
14201429
addPass(createRegAllocScoringPass());

0 commit comments

Comments
 (0)