Skip to content

Commit 3a19c3b

Browse files
AinsleySnowimkiva
authored andcommitted
[LLVM][XTHeadVector] Implement intrinsics for vnsrl/vnsra. (llvm#57)
* [LLVM][XTHeadVector] Define intrinsics for vnsra/vnsrl. * [LLVM][XTHeadVector] Define pseudos and pats for vnsrl/vnsra. * [LLVM][XTHeadVector] Add test cases. * [NFC][XTHeadVector] Update Readme.
1 parent b2f9c97 commit 3a19c3b

File tree

5 files changed

+3670
-0
lines changed

5 files changed

+3670
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Any feature not listed below but present in the specification should be consider
4343
- (Done) `vsll.{vv,vx,vi}`
4444
- (Done) `vsrl.{vv,vx,vi}`
4545
- (Done) `vsra.{vv,vx,vi}`
46+
- (Done) `12.6. Vector Narrowing Integer Right Shift Instructions`
4647
- (WIP) Clang intrinsics related to the `XTHeadVector` extension:
4748
- (WIP) `6. Configuration-Setting and Utility`
4849
- (Done) `6.1. Set vl and vtype`

llvm/include/llvm/IR/IntrinsicsRISCVXTHeadV.td

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,18 @@ let TargetPrefix = "riscv" in {
600600
let VLOperand = 4;
601601
}
602602

603+
// For destination vector type is the same as
604+
// first source vector (with mask but no policy).
605+
// The second source operand must match the destination type or be an XLen scalar.
606+
// Input: (maskedoff, vector_in, vector_in/scalar_in, mask, vl)
607+
class XVBinaryABShiftMasked
608+
: DefaultAttrsIntrinsic<[llvm_anyvector_ty],
609+
[LLVMMatchType<0>, llvm_anyvector_ty, llvm_any_ty,
610+
LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>, llvm_anyint_ty],
611+
[IntrNoMem]>, RISCVVIntrinsic {
612+
let VLOperand = 4;
613+
}
614+
603615
multiclass XVBinaryAAX {
604616
def "int_riscv_" # NAME : RISCVBinaryAAXUnMasked;
605617
def "int_riscv_" # NAME # "_mask" : XVBinaryAAXMasked;
@@ -614,6 +626,11 @@ let TargetPrefix = "riscv" in {
614626
def "int_riscv_" # NAME : RISCVBinaryAAShiftUnMasked;
615627
def "int_riscv_" # NAME # "_mask" : XVBinaryAAShiftMasked;
616628
}
629+
630+
multiclass XVBinaryABShift {
631+
def "int_riscv_" # NAME : RISCVBinaryABShiftUnMasked;
632+
def "int_riscv_" # NAME # "_mask" : XVBinaryABShiftMasked;
633+
}
617634
}
618635

619636
let TargetPrefix = "riscv" in {
@@ -648,6 +665,10 @@ let TargetPrefix = "riscv" in {
648665
defm th_vsll : XVBinaryAAShift;
649666
defm th_vsrl : XVBinaryAAShift;
650667
defm th_vsra : XVBinaryAAShift;
668+
669+
// 12.6. Vector Narrowing Integer Right Shift Instructions
670+
defm th_vnsrl : XVBinaryABShift;
671+
defm th_vnsra : XVBinaryABShift;
651672
} // TargetPrefix = "riscv"
652673

653674
let TargetPrefix = "riscv" in {

llvm/lib/Target/RISCV/RISCVInstrInfoXTHeadVPseudos.td

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,28 @@ multiclass XVPseudoBinaryW_WX<LMULInfo m> {
16451645
defm _WX : XVPseudoBinary<m.wvrclass, m.wvrclass, GPR, m>;
16461646
}
16471647

1648+
// In RVV 1.0 `@earlyclobber` is used here to prevent the source and destination
1649+
// from overlapping. The case where LMUL <= 1 is excluded because of the
1650+
// exception from the 1.0 spec:
1651+
// "The destination EEW is smaller than the source EEW and the overlap is in the
1652+
// lowest-numbered part of the source register group."
1653+
// However, the 0.7 spec is unclear about the source-destination overlapping of
1654+
// narrowing instructions like vnsrl/vnsra. Here we simply follow the 1.0 spec.
1655+
multiclass XVPseudoBinaryVNSHT_VV<LMULInfo m> {
1656+
defm _VV : XVPseudoBinary<m.vrclass, m.wvrclass, m.vrclass, m,
1657+
!if(!ge(m.octuple, 8), "@earlyclobber $rd", "")>;
1658+
}
1659+
1660+
multiclass XVPseudoBinaryVNSHT_VX<LMULInfo m> {
1661+
defm _VX : XVPseudoBinary<m.vrclass, m.wvrclass, GPR, m,
1662+
!if(!ge(m.octuple, 8), "@earlyclobber $rd", "")>;
1663+
}
1664+
1665+
multiclass XVPseudoBinaryVNSHT_VI<LMULInfo m> {
1666+
defm _VI : XVPseudoBinary<m.vrclass, m.wvrclass, uimm5, m,
1667+
!if(!ge(m.octuple, 8), "@earlyclobber $rd", "")>;
1668+
}
1669+
16481670
multiclass XVPseudoVALU_VV_VX_VI<Operand ImmType = simm5, string Constraint = ""> {
16491671
foreach m = MxListXTHeadV in {
16501672
defvar mx = m.MX;
@@ -1807,6 +1829,24 @@ multiclass XVPseudoVSHT_VV_VX_VI<Operand ImmType = simm5, string Constraint = ""
18071829
}
18081830
}
18091831

1832+
multiclass XVPseudoVNSHT_VV_VX_VI {
1833+
foreach m = MxListWXTHeadV in {
1834+
defvar mx = m.MX;
1835+
defvar WriteVNShiftV_MX = !cast<SchedWrite>("WriteVNShiftV_" # mx);
1836+
defvar WriteVNShiftX_MX = !cast<SchedWrite>("WriteVNShiftX_" # mx);
1837+
defvar WriteVNShiftI_MX = !cast<SchedWrite>("WriteVNShiftI_" # mx);
1838+
defvar ReadVNShiftV_MX = !cast<SchedRead>("ReadVNShiftV_" # mx);
1839+
defvar ReadVNShiftX_MX = !cast<SchedRead>("ReadVNShiftX_" # mx);
1840+
1841+
defm "" : XVPseudoBinaryVNSHT_VV<m>,
1842+
Sched<[WriteVNShiftV_MX, ReadVNShiftV_MX, ReadVNShiftV_MX, ReadVMask]>;
1843+
defm "" : XVPseudoBinaryVNSHT_VX<m>,
1844+
Sched<[WriteVNShiftX_MX, ReadVNShiftV_MX, ReadVNShiftX_MX, ReadVMask]>;
1845+
defm "" : XVPseudoBinaryVNSHT_VI<m>,
1846+
Sched<[WriteVNShiftI_MX, ReadVNShiftV_MX, ReadVMask]>;
1847+
}
1848+
}
1849+
18101850
//===----------------------------------------------------------------------===//
18111851
// Helpers to define the intrinsic patterns for the XTHeadVector extension.
18121852
//===----------------------------------------------------------------------===//
@@ -2053,6 +2093,49 @@ multiclass XVPatBinaryV_IM<string intrinsic, string instruction,
20532093
vti.RegClass, simm5>;
20542094
}
20552095

2096+
multiclass XVPatBinaryVNSHT_VV<string intrinsic, string instruction,
2097+
list<VTypeInfoToWide> vtilist> {
2098+
foreach VtiToWti = vtilist in {
2099+
defvar Vti = VtiToWti.Vti;
2100+
defvar Wti = VtiToWti.Wti;
2101+
let Predicates = !listconcat(GetXVTypePredicates<Vti>.Predicates,
2102+
GetXVTypePredicates<Wti>.Predicates) in
2103+
defm : XVPatBinary<intrinsic, instruction # "_VV_" # Vti.LMul.MX,
2104+
Vti.Vector, Wti.Vector, Vti.Vector, Vti.Mask,
2105+
Vti.Log2SEW, Vti.RegClass,
2106+
Wti.RegClass, Vti.RegClass>;
2107+
}
2108+
}
2109+
2110+
multiclass XVPatBinaryVNSHT_VX<string intrinsic, string instruction,
2111+
list<VTypeInfoToWide> vtilist> {
2112+
foreach VtiToWti = vtilist in {
2113+
defvar Vti = VtiToWti.Vti;
2114+
defvar Wti = VtiToWti.Wti;
2115+
defvar kind = "V"#Vti.ScalarSuffix;
2116+
let Predicates = !listconcat(GetXVTypePredicates<Vti>.Predicates,
2117+
GetXVTypePredicates<Wti>.Predicates) in
2118+
defm : XVPatBinary<intrinsic, instruction#"_"#kind#"_"#Vti.LMul.MX,
2119+
Vti.Vector, Wti.Vector, Vti.Scalar, Vti.Mask,
2120+
Vti.Log2SEW, Vti.RegClass,
2121+
Wti.RegClass, Vti.ScalarRegClass>;
2122+
}
2123+
}
2124+
2125+
multiclass XVPatBinaryVNSHT_VI<string intrinsic, string instruction,
2126+
list<VTypeInfoToWide> vtilist> {
2127+
foreach VtiToWti = vtilist in {
2128+
defvar Vti = VtiToWti.Vti;
2129+
defvar Wti = VtiToWti.Wti;
2130+
let Predicates = !listconcat(GetXVTypePredicates<Vti>.Predicates,
2131+
GetXVTypePredicates<Wti>.Predicates) in
2132+
defm : XVPatBinary<intrinsic, instruction # "_VI_" # Vti.LMul.MX,
2133+
Vti.Vector, Wti.Vector, XLenVT, Vti.Mask,
2134+
Vti.Log2SEW, Vti.RegClass,
2135+
Wti.RegClass, uimm5>;
2136+
}
2137+
}
2138+
20562139
multiclass XVPatBinaryV_VV_VX_VI<string intrinsic, string instruction,
20572140
list<VTypeInfo> vtilist, Operand ImmType = simm5>
20582141
: XVPatBinaryV_VV<intrinsic, instruction, vtilist>,
@@ -2097,6 +2180,12 @@ multiclass XVPatBinaryM_VM_XM<string intrinsic, string instruction>
20972180
: XVPatBinaryV_VM<intrinsic, instruction, CarryOut=1>,
20982181
XVPatBinaryV_XM<intrinsic, instruction, CarryOut=1>;
20992182

2183+
multiclass XVPatBinaryVNSHT_VV_VX_VI<string intrinsic, string instruction,
2184+
list<VTypeInfoToWide> vtilist>
2185+
: XVPatBinaryVNSHT_VV<intrinsic, instruction, vtilist>,
2186+
XVPatBinaryVNSHT_VX<intrinsic, instruction, vtilist>,
2187+
XVPatBinaryVNSHT_VI<intrinsic, instruction, vtilist>;
2188+
21002189
//===----------------------------------------------------------------------===//
21012190
// 12.1. Vector Single-Width Saturating Add and Subtract
21022191
//===----------------------------------------------------------------------===//
@@ -2267,6 +2356,19 @@ let Predicates = [HasVendorXTHeadV] in {
22672356
}
22682357
} // Predicates = [HasVendorXTHeadV]
22692358

2359+
//===----------------------------------------------------------------------===//
2360+
// 12.6. Vector Narrowing Integer Right Shift Instructions
2361+
//===----------------------------------------------------------------------===//
2362+
let Predicates = [HasVendorXTHeadV] in {
2363+
defm PseudoTH_VNSRL : XVPseudoVNSHT_VV_VX_VI;
2364+
defm PseudoTH_VNSRA : XVPseudoVNSHT_VV_VX_VI;
2365+
} // Predicates = [HasVendorXTHeadV]
2366+
2367+
let Predicates = [HasVendorXTHeadV] in {
2368+
defm : XVPatBinaryVNSHT_VV_VX_VI<"int_riscv_th_vnsrl", "PseudoTH_VNSRL", AllWidenableIntXVectors>;
2369+
defm : XVPatBinaryVNSHT_VV_VX_VI<"int_riscv_th_vnsra", "PseudoTH_VNSRA", AllWidenableIntXVectors>;
2370+
}
2371+
22702372
//===----------------------------------------------------------------------===//
22712373
// 12.14. Vector Integer Merge and Move Instructions
22722374
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)