Skip to content

Commit 7573853

Browse files
author
Yeting Kuo
committed
[RISCV] Recommit "Expand vp.stride.load to splat of a scalar load."
This is recommit of #98140. It should be based on #98205 which changes the feature of hardware zero stride optimization. It's a similar patch as a214c52 for vp.stride.load. Some targets prefer pattern (vmv.v.x (load)) instead of vlse with zero stride.
1 parent cb3bc5b commit 7573853

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

llvm/lib/Target/RISCV/RISCVCodeGenPrepare.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
#include "llvm/ADT/Statistic.h"
1919
#include "llvm/Analysis/ValueTracking.h"
2020
#include "llvm/CodeGen/TargetPassConfig.h"
21+
#include "llvm/IR/Dominators.h"
2122
#include "llvm/IR/IRBuilder.h"
2223
#include "llvm/IR/InstVisitor.h"
2324
#include "llvm/IR/Intrinsics.h"
25+
#include "llvm/IR/IntrinsicsRISCV.h"
2426
#include "llvm/IR/PatternMatch.h"
2527
#include "llvm/InitializePasses.h"
2628
#include "llvm/Pass.h"
@@ -35,6 +37,7 @@ namespace {
3537
class RISCVCodeGenPrepare : public FunctionPass,
3638
public InstVisitor<RISCVCodeGenPrepare, bool> {
3739
const DataLayout *DL;
40+
const DominatorTree *DT;
3841
const RISCVSubtarget *ST;
3942

4043
public:
@@ -48,12 +51,14 @@ class RISCVCodeGenPrepare : public FunctionPass,
4851

4952
void getAnalysisUsage(AnalysisUsage &AU) const override {
5053
AU.setPreservesCFG();
54+
AU.addRequired<DominatorTreeWrapperPass>();
5155
AU.addRequired<TargetPassConfig>();
5256
}
5357

5458
bool visitInstruction(Instruction &I) { return false; }
5559
bool visitAnd(BinaryOperator &BO);
5660
bool visitIntrinsicInst(IntrinsicInst &I);
61+
bool expandVPStrideLoad(IntrinsicInst &I);
5762
};
5863

5964
} // end anonymous namespace
@@ -128,6 +133,9 @@ bool RISCVCodeGenPrepare::visitAnd(BinaryOperator &BO) {
128133
// Which eliminates the scalar -> vector -> scalar crossing during instruction
129134
// selection.
130135
bool RISCVCodeGenPrepare::visitIntrinsicInst(IntrinsicInst &I) {
136+
if (expandVPStrideLoad(I))
137+
return true;
138+
131139
if (I.getIntrinsicID() != Intrinsic::vector_reduce_fadd)
132140
return false;
133141

@@ -155,6 +163,45 @@ bool RISCVCodeGenPrepare::visitIntrinsicInst(IntrinsicInst &I) {
155163
return true;
156164
}
157165

166+
bool RISCVCodeGenPrepare::expandVPStrideLoad(IntrinsicInst &II) {
167+
Value *BasePtr, *VL;
168+
169+
using namespace PatternMatch;
170+
if (!match(&II, m_Intrinsic<Intrinsic::experimental_vp_strided_load>(
171+
m_Value(BasePtr), m_Zero(), m_AllOnes(), m_Value(VL))))
172+
return false;
173+
174+
if (!isKnownNonZero(VL, {*DL, DT, nullptr, &II}))
175+
return false;
176+
177+
auto *VTy = cast<VectorType>(II.getType());
178+
179+
IRBuilder<> Builder(&II);
180+
181+
// Extend VL from i32 to XLen if needed.
182+
if (ST->is64Bit())
183+
VL = Builder.CreateZExt(VL, Builder.getInt64Ty());
184+
185+
Type *STy = VTy->getElementType();
186+
Value *Val = Builder.CreateLoad(STy, BasePtr);
187+
const auto &TLI = *ST->getTargetLowering();
188+
Value *Res;
189+
190+
// TODO: Also support fixed/illegal vector types to splat with evl = vl.
191+
if (isa<ScalableVectorType>(VTy) && TLI.isTypeLegal(EVT::getEVT(VTy))) {
192+
unsigned VMVOp = STy->isFloatingPointTy() ? Intrinsic::riscv_vfmv_v_f
193+
: Intrinsic::riscv_vmv_v_x;
194+
Res = Builder.CreateIntrinsic(VMVOp, {VTy, VL->getType()},
195+
{PoisonValue::get(VTy), Val, VL});
196+
} else {
197+
Res = Builder.CreateVectorSplat(VTy->getElementCount(), Val);
198+
}
199+
200+
II.replaceAllUsesWith(Res);
201+
II.eraseFromParent();
202+
return true;
203+
}
204+
158205
bool RISCVCodeGenPrepare::runOnFunction(Function &F) {
159206
if (skipFunction(F))
160207
return false;
@@ -164,6 +211,7 @@ bool RISCVCodeGenPrepare::runOnFunction(Function &F) {
164211
ST = &TM.getSubtarget<RISCVSubtarget>(F);
165212

166213
DL = &F.getDataLayout();
214+
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
167215

168216
bool MadeChange = false;
169217
for (auto &BB : F)

llvm/test/CodeGen/RISCV/rvv/fixed-vectors-strided-vpload.ll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,29 @@ define <33 x double> @strided_load_v33f64(ptr %ptr, i64 %stride, <33 x i1> %mask
626626
}
627627

628628
declare <33 x double> @llvm.experimental.vp.strided.load.v33f64.p0.i64(ptr, i64, <33 x i1>, i32)
629+
630+
; TODO: Use accurate evl.
631+
; Test unmasked integer zero strided
632+
define <4 x i8> @zero_strided_unmasked_vpload_4i8_i8(ptr %ptr) {
633+
; CHECK-LABEL: zero_strided_unmasked_vpload_4i8_i8:
634+
; CHECK: # %bb.0:
635+
; CHECK-NEXT: lbu a0, 0(a0)
636+
; CHECK-NEXT: vsetivli zero, 4, e8, mf4, ta, ma
637+
; CHECK-NEXT: vmv.v.x v8, a0
638+
; CHECK-NEXT: ret
639+
%load = call <4 x i8> @llvm.experimental.vp.strided.load.4i8.p0.i8(ptr %ptr, i8 0, <4 x i1> splat (i1 true), i32 3)
640+
ret <4 x i8> %load
641+
}
642+
643+
; TODO: Use accurate evl.
644+
; Test unmasked float zero strided
645+
define <4 x half> @zero_strided_unmasked_vpload_4f16(ptr %ptr) {
646+
; CHECK-LABEL: zero_strided_unmasked_vpload_4f16:
647+
; CHECK: # %bb.0:
648+
; CHECK-NEXT: flh fa5, 0(a0)
649+
; CHECK-NEXT: vsetivli zero, 4, e16, mf2, ta, ma
650+
; CHECK-NEXT: vfmv.v.f v8, fa5
651+
; CHECK-NEXT: ret
652+
%load = call <4 x half> @llvm.experimental.vp.strided.load.4f16.p0.i32(ptr %ptr, i32 0, <4 x i1> splat (i1 true), i32 3)
653+
ret <4 x half> %load
654+
}

llvm/test/CodeGen/RISCV/rvv/strided-vpload.ll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,27 @@ define <vscale x 16 x double> @strided_load_nxv17f64(ptr %ptr, i64 %stride, <vsc
780780
declare <vscale x 17 x double> @llvm.experimental.vp.strided.load.nxv17f64.p0.i64(ptr, i64, <vscale x 17 x i1>, i32)
781781
declare <vscale x 1 x double> @llvm.experimental.vector.extract.nxv1f64(<vscale x 17 x double> %vec, i64 %idx)
782782
declare <vscale x 16 x double> @llvm.experimental.vector.extract.nxv16f64(<vscale x 17 x double> %vec, i64 %idx)
783+
784+
; Test unmasked integer zero strided
785+
define <vscale x 1 x i8> @zero_strided_unmasked_vpload_nxv1i8_i8(ptr %ptr) {
786+
; CHECK-LABEL: zero_strided_unmasked_vpload_nxv1i8_i8:
787+
; CHECK: # %bb.0:
788+
; CHECK-NEXT: lbu a0, 0(a0)
789+
; CHECK-NEXT: vsetivli zero, 4, e8, mf8, ta, ma
790+
; CHECK-NEXT: vmv.v.x v8, a0
791+
; CHECK-NEXT: ret
792+
%load = call <vscale x 1 x i8> @llvm.experimental.vp.strided.load.nxv1i8.p0.i8(ptr %ptr, i8 0, <vscale x 1 x i1> splat (i1 true), i32 4)
793+
ret <vscale x 1 x i8> %load
794+
}
795+
796+
; Test unmasked float zero strided
797+
define <vscale x 1 x half> @zero_strided_unmasked_vpload_nxv1f16(ptr %ptr) {
798+
; CHECK-LABEL: zero_strided_unmasked_vpload_nxv1f16:
799+
; CHECK: # %bb.0:
800+
; CHECK-NEXT: flh fa5, 0(a0)
801+
; CHECK-NEXT: vsetivli zero, 4, e16, mf4, ta, ma
802+
; CHECK-NEXT: vfmv.v.f v8, fa5
803+
; CHECK-NEXT: ret
804+
%load = call <vscale x 1 x half> @llvm.experimental.vp.strided.load.nxv1f16.p0.i32(ptr %ptr, i32 0, <vscale x 1 x i1> splat (i1 true), i32 4)
805+
ret <vscale x 1 x half> %load
806+
}

0 commit comments

Comments
 (0)