Skip to content

Commit 7c2ac13

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 1ebe16b commit 7c2ac13

31 files changed

+2098
-744
lines changed

llvm/lib/Target/RISCV/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ add_llvm_target(RISCVCodeGen
6161
RISCVTargetObjectFile.cpp
6262
RISCVTargetTransformInfo.cpp
6363
RISCVVectorPeephole.cpp
64+
RISCVVLOptimizer.cpp
6465
GISel/RISCVCallLowering.cpp
6566
GISel/RISCVInstructionSelector.cpp
6667
GISel/RISCVLegalizerInfo.cpp

llvm/lib/Target/RISCV/RISCV.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ void initializeRISCVPreLegalizerCombinerPass(PassRegistry &);
102102

103103
FunctionPass *createRISCVPostLegalizerLowering();
104104
void initializeRISCVPostLegalizerLoweringPass(PassRegistry &);
105+
106+
FunctionPass *createRISCVVLOptimizerPass();
107+
void initializeRISCVVLOptimizerPass(PassRegistry &);
105108
} // namespace llvm
106109

107110
#endif

llvm/lib/Target/RISCV/RISCVTargetMachine.cpp

Lines changed: 8 additions & 1 deletion
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());
@@ -552,8 +556,11 @@ void RISCVPassConfig::addMachineSSAOptimization() {
552556

553557
void RISCVPassConfig::addPreRegAlloc() {
554558
addPass(createRISCVPreRAExpandPseudoPass());
555-
if (TM->getOptLevel() != CodeGenOptLevel::None)
559+
if (TM->getOptLevel() != CodeGenOptLevel::None) {
556560
addPass(createRISCVMergeBaseOffsetOptPass());
561+
if (EnableVLOptimizer)
562+
addPass(createRISCVVLOptimizerPass());
563+
}
557564

558565
addPass(createRISCVInsertReadWriteCSRPass());
559566
addPass(createRISCVInsertWriteVXRMPass());

0 commit comments

Comments
 (0)