Skip to content

Commit 763a4f4

Browse files
[RISCV][GISEL] Legalize G_SPLAT_VECTOR
On RV64, the splat operand should be sXLen and the vector type should be a legal integer or fp vector type. If the vector type is a mask type it should be sign extended to a legal integer or fp vector type unless it is all ones or zeros. On RV32, the splat operand should be sXLen and the vector type should be a legal integer or fp vector type. If the vector type is a mask type it should be sign extended to a legal integer or fp vector type unless it is all ones or zeros. If the vector element type is s64 and that is a legal vector type, then we lower to SPLIT_SPLAT_VECTOR_I64_VL.
1 parent a2a4716 commit 763a4f4

File tree

7 files changed

+1697
-1
lines changed

7 files changed

+1697
-1
lines changed

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,15 @@ LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI,
17131713
MI.eraseFromParent();
17141714
return Legalized;
17151715
}
1716+
case TargetOpcode::G_SPLAT_VECTOR: {
1717+
if (TypeIdx != 1)
1718+
return UnableToLegalize;
1719+
1720+
Observer.changingInstr(MI);
1721+
narrowScalarSrc(MI, NarrowTy, 1);
1722+
Observer.changedInstr(MI);
1723+
return Legalized;
1724+
}
17161725
}
17171726
}
17181727

@@ -3006,6 +3015,15 @@ LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) {
30063015
Observer.changedInstr(MI);
30073016
return Legalized;
30083017
}
3018+
case TargetOpcode::G_SPLAT_VECTOR: {
3019+
if (TypeIdx != 1)
3020+
return UnableToLegalize;
3021+
3022+
Observer.changingInstr(MI);
3023+
widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
3024+
Observer.changedInstr(MI);
3025+
return Legalized;
3026+
}
30093027
}
30103028
}
30113029

llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ MachineIRBuilder::buildInstr(unsigned Opc, ArrayRef<DstOp> DstOps,
12781278
return DstTy.isScalar();
12791279
else
12801280
return DstTy.isVector() &&
1281-
DstTy.getNumElements() == Op0Ty.getNumElements();
1281+
DstTy.getElementCount() == Op0Ty.getElementCount();
12821282
}() && "Type Mismatch");
12831283
break;
12841284
}

llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,17 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST)
419419
.clampScalar(0, sXLen, sXLen)
420420
.customFor({sXLen});
421421

422+
auto &SplatActions =
423+
getActionDefinitionsBuilder(G_SPLAT_VECTOR)
424+
.legalIf(all(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST),
425+
typeIs(1, sXLen)))
426+
.customIf(all(typeIsLegalBoolVec(0, BoolVecTys, ST), typeIs(1, s1)));
427+
// s64 splat on RV32 should be lowered to RISCV::G_SPLAT_VECTOR_PARTS_I64
428+
if (XLen == 32)
429+
SplatActions.customIf(
430+
all(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST), typeIs(1, s64)));
431+
SplatActions.clampScalar(1, sXLen, sXLen);
432+
422433
getLegacyLegalizerInfo().computeTables();
423434
}
424435

@@ -609,6 +620,118 @@ bool RISCVLegalizerInfo::legalizeExt(MachineInstr &MI,
609620
return true;
610621
}
611622

623+
/// Return the type of the mask type suitable for masking the provided
624+
/// vector type. This is simply an i1 element type vector of the same
625+
/// (possibly scalable) length.
626+
static LLT getMaskTypeFor(LLT VecTy) {
627+
assert(VecTy.isVector());
628+
ElementCount EC = VecTy.getElementCount();
629+
return LLT::vector(EC, LLT::scalar(1));
630+
}
631+
632+
/// Creates an all ones mask suitable for masking a vector of type VecTy with
633+
/// vector length VL.
634+
static MachineInstrBuilder buildAllOnesMask(LLT VecTy, const SrcOp &VL,
635+
MachineIRBuilder &MIB,
636+
MachineRegisterInfo &MRI) {
637+
LLT MaskTy = getMaskTypeFor(VecTy);
638+
return MIB.buildInstr(RISCV::G_VMSET_VL, {MaskTy}, {VL});
639+
}
640+
641+
/// Gets the two common "VL" operands: an all-ones mask and the vector length.
642+
/// VecTy is a scalable vector type.
643+
static std::pair<MachineInstrBuilder, Register>
644+
buildDefaultVLOps(const DstOp &Dst, MachineIRBuilder &MIB,
645+
MachineRegisterInfo &MRI) {
646+
LLT VecTy = Dst.getLLTTy(MRI);
647+
assert(VecTy.isScalableVector() && "Expecting scalable container type");
648+
Register VL(RISCV::X0);
649+
MachineInstrBuilder Mask = buildAllOnesMask(VecTy, VL, MIB, MRI);
650+
return {Mask, VL};
651+
}
652+
653+
static MachineInstrBuilder
654+
buildSplatPartsS64WithVL(const DstOp &Dst, const SrcOp &Passthru, Register Lo,
655+
Register Hi, Register VL, MachineIRBuilder &MIB,
656+
MachineRegisterInfo &MRI) {
657+
// TODO: If the Hi bits of the splat are undefined, then it's fine to just
658+
// splat Lo even if it might be sign extended. I don't think we have
659+
// introduced a case where we're build a s64 where the upper bits are undef
660+
// yet.
661+
662+
// Fall back to a stack store and stride x0 vector load.
663+
// TODO: need to lower G_SPLAT_VECTOR_SPLIT_I64. This is done in
664+
// preprocessDAG in SDAG.
665+
return MIB.buildInstr(RISCV::G_SPLAT_VECTOR_SPLIT_I64_VL, {Dst},
666+
{Passthru, Lo, Hi, VL});
667+
}
668+
669+
static MachineInstrBuilder
670+
buildSplatSplitS64WithVL(const DstOp &Dst, const SrcOp &Passthru,
671+
const SrcOp &Scalar, Register VL,
672+
MachineIRBuilder &MIB, MachineRegisterInfo &MRI) {
673+
assert(Scalar.getLLTTy(MRI) == LLT::scalar(64) && "Unexpected VecTy!");
674+
auto Unmerge = MIB.buildUnmerge(LLT::scalar(32), Scalar);
675+
return buildSplatPartsS64WithVL(Dst, Passthru, Unmerge.getReg(0),
676+
Unmerge.getReg(1), VL, MIB, MRI);
677+
}
678+
679+
// Lower splats of s1 types to G_ICMP. For each mask vector type, we have a
680+
// legal equivalently-sized i8 type, so we can use that as a go-between.
681+
// Splats of s1 types that have constant value can be legalized as VMSET_VL or
682+
// VMCLR_VL.
683+
bool RISCVLegalizerInfo::legalizeSplatVector(MachineInstr &MI,
684+
MachineIRBuilder &MIB) const {
685+
assert(MI.getOpcode() == TargetOpcode::G_SPLAT_VECTOR);
686+
687+
MachineRegisterInfo &MRI = *MIB.getMRI();
688+
689+
Register Dst = MI.getOperand(0).getReg();
690+
Register SplatVal = MI.getOperand(1).getReg();
691+
692+
LLT VecTy = MRI.getType(Dst);
693+
LLT XLenTy(STI.getXLenVT());
694+
695+
// Handle case of s64 element vectors on rv32
696+
if (XLenTy.getSizeInBits() == 32 &&
697+
VecTy.getElementType().getSizeInBits() == 64) {
698+
auto [_, VL] = buildDefaultVLOps(Dst, MIB, MRI);
699+
buildSplatSplitS64WithVL(Dst, MIB.buildUndef(VecTy), SplatVal, VL, MIB,
700+
MRI);
701+
MI.eraseFromParent();
702+
return true;
703+
}
704+
705+
// All-zeros or all-ones splats are handled specially.
706+
MachineInstr &SplatValMI = *MRI.getVRegDef(SplatVal);
707+
if (isAllOnesOrAllOnesSplat(SplatValMI, MRI)) {
708+
auto VL = buildDefaultVLOps(VecTy, MIB, MRI).second;
709+
MIB.buildInstr(RISCV::G_VMSET_VL, {Dst}, {VL});
710+
MI.eraseFromParent();
711+
return true;
712+
}
713+
if (isNullOrNullSplat(SplatValMI, MRI)) {
714+
auto VL = buildDefaultVLOps(VecTy, MIB, MRI).second;
715+
MIB.buildInstr(RISCV::G_VMCLR_VL, {Dst}, {VL});
716+
MI.eraseFromParent();
717+
return true;
718+
}
719+
720+
// Handle non-constant mask splat (i.e. not sure if it's all zeros or all
721+
// ones) by promoting it to an s8 splat.
722+
LLT InterEltTy = LLT::scalar(8);
723+
LLT InterTy = VecTy.changeElementType(InterEltTy);
724+
auto ZExtSplatVal = MIB.buildZExt(InterEltTy, SplatVal);
725+
auto And =
726+
MIB.buildAnd(InterEltTy, ZExtSplatVal, MIB.buildConstant(InterEltTy, 1));
727+
auto LHS = MIB.buildSplatVector(InterTy, And);
728+
auto ZeroSplat =
729+
MIB.buildSplatVector(InterTy, MIB.buildConstant(InterEltTy, 0));
730+
MIB.buildICmp(CmpInst::Predicate::ICMP_NE, Dst, LHS, ZeroSplat);
731+
MI.eraseFromParent();
732+
return true;
733+
}
734+
612735
bool RISCVLegalizerInfo::legalizeCustom(
613736
LegalizerHelper &Helper, MachineInstr &MI,
614737
LostDebugLocObserver &LocObserver) const {
@@ -672,6 +795,8 @@ bool RISCVLegalizerInfo::legalizeCustom(
672795
case TargetOpcode::G_SEXT:
673796
case TargetOpcode::G_ANYEXT:
674797
return legalizeExt(MI, MIRBuilder);
798+
case TargetOpcode::G_SPLAT_VECTOR:
799+
return legalizeSplatVector(MI, MIRBuilder);
675800
}
676801

677802
llvm_unreachable("expected switch to return");

llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class RISCVLegalizerInfo : public LegalizerInfo {
4444
bool legalizeVAStart(MachineInstr &MI, MachineIRBuilder &MIRBuilder) const;
4545
bool legalizeVScale(MachineInstr &MI, MachineIRBuilder &MIB) const;
4646
bool legalizeExt(MachineInstr &MI, MachineIRBuilder &MIRBuilder) const;
47+
bool legalizeSplatVector(MachineInstr &MI, MachineIRBuilder &MIB) const;
4748
};
4849
} // end namespace llvm
4950
#endif

llvm/lib/Target/RISCV/RISCVInstrGISel.td

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,28 @@ def G_READ_VLENB : RISCVGenericInstruction {
3232
let hasSideEffects = false;
3333
}
3434
def : GINodeEquiv<G_READ_VLENB, riscv_read_vlenb>;
35+
36+
// Pseudo equivalent to a RISCVISD::VMCLR_VL
37+
def G_VMCLR_VL : RISCVGenericInstruction {
38+
let OutOperandList = (outs type0:$dst);
39+
let InOperandList = (ins type1:$vl);
40+
let hasSideEffects = false;
41+
}
42+
def : GINodeEquiv<G_VMCLR_VL, riscv_vmclr_vl>;
43+
44+
// Pseudo equivalent to a RISCVISD::VMSET_VL
45+
def G_VMSET_VL : RISCVGenericInstruction {
46+
let OutOperandList = (outs type0:$dst);
47+
let InOperandList = (ins type1:$vl);
48+
let hasSideEffects = false;
49+
}
50+
def : GINodeEquiv<G_VMSET_VL, riscv_vmset_vl>;
51+
52+
// Pseudo equivalent to a RISCVISD::SPLAT_VECTOR_SPLIT_I64_VL. There is no
53+
// record to mark is equivalent to using GINodeEquiv because it gets lowered
54+
// before instruction selection.
55+
def G_SPLAT_VECTOR_SPLIT_I64_VL : RISCVGenericInstruction {
56+
let OutOperandList = (outs type0:$dst);
57+
let InOperandList = (ins type0:$passthru, type1:$hi, type1:$lo, type2:$vl);
58+
let hasSideEffects = false;
59+
}

0 commit comments

Comments
 (0)