Skip to content

Commit bddad03

Browse files
michaelmaitlandtopperckito-cheng
committed
[RISCV] Add VLOptimizer pass
The purpose of this optimization is to make the VL argument, for instructions that have a VL argument, as small as possible. This is implemented by visiting each instruction in reverse order and checking that if it has a VL argument, whether the VL can be reduced. This is done before vsetvli insertion to reduce the number of generated vsetvlis. It can also reduce the number of vsetvli instructions that toggle the VL (the vtype may still need to get set). The list of supported instructions is currently whitelisted for safety. In the future, we could add more instructions to isSupportedInstr to support even more VL optimization. Co-authored-by: Craig Topper <[email protected]> Co-authored-by: Kito Cheng <[email protected]>
1 parent b7e585b commit bddad03

29 files changed

+1960
-678
lines changed

llvm/lib/Target/RISCV/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ add_llvm_target(RISCVCodeGen
5959
RISCVTargetObjectFile.cpp
6060
RISCVTargetTransformInfo.cpp
6161
RISCVVectorPeephole.cpp
62+
RISCVVLOptimizer.cpp
6263
GISel/RISCVCallLowering.cpp
6364
GISel/RISCVInstructionSelector.cpp
6465
GISel/RISCVLegalizerInfo.cpp

llvm/lib/Target/RISCV/RISCV.h

+4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ void initializeRISCVO0PreLegalizerCombinerPass(PassRegistry &);
9999

100100
FunctionPass *createRISCVPreLegalizerCombiner();
101101
void initializeRISCVPreLegalizerCombinerPass(PassRegistry &);
102+
103+
FunctionPass *createRISCVVLOptimizerPass();
104+
void initializeRISCVVLOptimizerPass(PassRegistry &);
105+
102106
} // namespace llvm
103107

104108
#endif

llvm/lib/Target/RISCV/RISCVTargetMachine.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ static cl::opt<bool> EnableVSETVLIAfterRVVRegAlloc(
103103
cl::desc("Insert vsetvls after vector register allocation"),
104104
cl::init(true));
105105

106+
static cl::opt<bool> EnableVLOptimizer("riscv-enable-vloptimizer",
107+
cl::desc("Enable the VL Optimizer pass"),
108+
cl::init(true), cl::Hidden);
109+
106110
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget() {
107111
RegisterTargetMachine<RISCVTargetMachine> X(getTheRISCV32Target());
108112
RegisterTargetMachine<RISCVTargetMachine> Y(getTheRISCV64Target());
@@ -550,8 +554,11 @@ void RISCVPassConfig::addMachineSSAOptimization() {
550554

551555
void RISCVPassConfig::addPreRegAlloc() {
552556
addPass(createRISCVPreRAExpandPseudoPass());
553-
if (TM->getOptLevel() != CodeGenOptLevel::None)
557+
if (TM->getOptLevel() != CodeGenOptLevel::None) {
554558
addPass(createRISCVMergeBaseOffsetOptPass());
559+
if (EnableVLOptimizer)
560+
addPass(createRISCVVLOptimizerPass());
561+
}
555562

556563
addPass(createRISCVInsertReadWriteCSRPass());
557564
addPass(createRISCVInsertWriteVXRMPass());

0 commit comments

Comments
 (0)