Skip to content

Commit e96a3ab

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 6c1a607 commit e96a3ab

File tree

7 files changed

+1733
-1
lines changed

7 files changed

+1733
-1
lines changed

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3006,6 +3006,15 @@ LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) {
30063006
Observer.changedInstr(MI);
30073007
return Legalized;
30083008
}
3009+
case TargetOpcode::G_SPLAT_VECTOR: {
3010+
if (TypeIdx != 1)
3011+
return UnableToLegalize;
3012+
3013+
Observer.changingInstr(MI);
3014+
widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
3015+
Observer.changedInstr(MI);
3016+
return Legalized;
3017+
}
30093018
}
30103019
}
30113020

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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,19 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST)
418418
.clampScalar(0, sXLen, sXLen)
419419
.customFor({sXLen});
420420

421+
auto &SplatActions =
422+
getActionDefinitionsBuilder(G_SPLAT_VECTOR)
423+
.legalIf(all(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST),
424+
typeIs(1, sXLen)))
425+
.customIf(all(typeIsLegalBoolVec(0, BoolVecTys, ST), typeIs(1, s1)));
426+
// Handle case of s64 element vectors on RV32. We don't know whether the type
427+
// is an f64 or an i64. As a result mark it as legal here and lower to
428+
// G_SPLAT_VECTOR_SPLIT_64_VL or G_VFMV_VL later.
429+
if (XLen == 32)
430+
SplatActions.legalIf(
431+
all(typeIsLegalIntOrFPVec(0, IntOrFPVecTys, ST), typeIs(1, s64)));
432+
SplatActions.clampScalar(1, sXLen, sXLen);
433+
421434
getLegacyLegalizerInfo().computeTables();
422435
}
423436

@@ -608,6 +621,82 @@ bool RISCVLegalizerInfo::legalizeExt(MachineInstr &MI,
608621
return true;
609622
}
610623

624+
/// Return the type of the mask type suitable for masking the provided
625+
/// vector type. This is simply an i1 element type vector of the same
626+
/// (possibly scalable) length.
627+
static LLT getMaskTypeFor(LLT VecTy) {
628+
assert(VecTy.isVector());
629+
ElementCount EC = VecTy.getElementCount();
630+
return LLT::vector(EC, LLT::scalar(1));
631+
}
632+
633+
/// Creates an all ones mask suitable for masking a vector of type VecTy with
634+
/// vector length VL.
635+
static MachineInstrBuilder buildAllOnesMask(LLT VecTy, const SrcOp &VL,
636+
MachineIRBuilder &MIB,
637+
MachineRegisterInfo &MRI) {
638+
LLT MaskTy = getMaskTypeFor(VecTy);
639+
return MIB.buildInstr(RISCV::G_VMSET_VL, {MaskTy}, {VL});
640+
}
641+
642+
/// Gets the two common "VL" operands: an all-ones mask and the vector length.
643+
/// VecTy is a scalable vector type.
644+
static std::pair<MachineInstrBuilder, Register>
645+
buildDefaultVLOps(const DstOp &Dst, MachineIRBuilder &MIB,
646+
MachineRegisterInfo &MRI) {
647+
LLT VecTy = Dst.getLLTTy(MRI);
648+
assert(VecTy.isScalableVector() && "Expecting scalable container type");
649+
Register VL(RISCV::X0);
650+
MachineInstrBuilder Mask = buildAllOnesMask(VecTy, VL, MIB, MRI);
651+
return {Mask, VL};
652+
}
653+
654+
// Lower splats of s1 types to G_ICMP. For each mask vector type, we have a
655+
// legal equivalently-sized i8 type, so we can use that as a go-between.
656+
// Splats of s1 types that have constant value can be legalized as VMSET_VL or
657+
// VMCLR_VL.
658+
bool RISCVLegalizerInfo::legalizeSplatVector(MachineInstr &MI,
659+
MachineIRBuilder &MIB) const {
660+
assert(MI.getOpcode() == TargetOpcode::G_SPLAT_VECTOR);
661+
662+
MachineRegisterInfo &MRI = *MIB.getMRI();
663+
664+
Register Dst = MI.getOperand(0).getReg();
665+
Register SplatVal = MI.getOperand(1).getReg();
666+
667+
LLT VecTy = MRI.getType(Dst);
668+
LLT XLenTy(STI.getXLenVT());
669+
670+
// All-zeros or all-ones splats are handled specially.
671+
MachineInstr &SplatValMI = *MRI.getVRegDef(SplatVal);
672+
if (isAllOnesOrAllOnesSplat(SplatValMI, MRI)) {
673+
auto VL = buildDefaultVLOps(VecTy, MIB, MRI).second;
674+
MIB.buildInstr(RISCV::G_VMSET_VL, {Dst}, {VL});
675+
MI.eraseFromParent();
676+
return true;
677+
}
678+
if (isNullOrNullSplat(SplatValMI, MRI)) {
679+
auto VL = buildDefaultVLOps(VecTy, MIB, MRI).second;
680+
MIB.buildInstr(RISCV::G_VMCLR_VL, {Dst}, {VL});
681+
MI.eraseFromParent();
682+
return true;
683+
}
684+
685+
// Handle non-constant mask splat (i.e. not sure if it's all zeros or all
686+
// ones) by promoting it to an s8 splat.
687+
LLT InterEltTy = LLT::scalar(8);
688+
LLT InterTy = VecTy.changeElementType(InterEltTy);
689+
auto ZExtSplatVal = MIB.buildZExt(InterEltTy, SplatVal);
690+
auto And =
691+
MIB.buildAnd(InterEltTy, ZExtSplatVal, MIB.buildConstant(InterEltTy, 1));
692+
auto LHS = MIB.buildSplatVector(InterTy, And);
693+
auto ZeroSplat =
694+
MIB.buildSplatVector(InterTy, MIB.buildConstant(InterEltTy, 0));
695+
MIB.buildICmp(CmpInst::Predicate::ICMP_NE, Dst, LHS, ZeroSplat);
696+
MI.eraseFromParent();
697+
return true;
698+
}
699+
611700
bool RISCVLegalizerInfo::legalizeCustom(
612701
LegalizerHelper &Helper, MachineInstr &MI,
613702
LostDebugLocObserver &LocObserver) const {
@@ -671,6 +760,8 @@ bool RISCVLegalizerInfo::legalizeCustom(
671760
case TargetOpcode::G_SEXT:
672761
case TargetOpcode::G_ANYEXT:
673762
return legalizeExt(MI, MIRBuilder);
763+
case TargetOpcode::G_SPLAT_VECTOR:
764+
return legalizeSplatVector(MI, MIRBuilder);
674765
}
675766

676767
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)