Skip to content

Commit fc44f43

Browse files
committed
[RISCV] Add TSFlag for reading past VL behaviour. NFCI
Currently we have a switch statement that checks if a vector instruction may read elements past VL. However it currently doesn't account for instructions in vendor extensions. Handling all possible vendor instructions will result in quite a lot of opcodes being added, so I've created a new TSFlag that we can declare in TableGen, and added it to the existing instruction definitions. I've tried to be conservative as possible here: All SiFive vendor vector instructions should be covered by the flag, as well as all of XRivosVizip, and ri.vextract from XRivosVisni. For now this should be NFC because coincidentally, these instructions aren't handled in getOperandInfo, so RISCVVLOptimizer should currently avoid touching them despite them being liberally handled in getMinimumVLForUser. However in an upcoming patch we'll need to also bail in getMinimumVLForUser, so this prepares for it.
1 parent b832c49 commit fc44f43

File tree

7 files changed

+31
-34
lines changed

7 files changed

+31
-34
lines changed

llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ enum {
139139
// 3 -> SEW * 4
140140
DestEEWShift = ElementsDependOnMaskShift + 1,
141141
DestEEWMask = 3ULL << DestEEWShift,
142+
143+
ReadsPastVLShift = DestEEWShift + 2,
144+
ReadsPastVLMask = 1ULL << ReadsPastVLShift,
142145
};
143146

144147
// Helper functions to read TSFlags.
@@ -195,6 +198,12 @@ static inline bool elementsDependOnMask(uint64_t TSFlags) {
195198
return TSFlags & ElementsDependOnMaskMask;
196199
}
197200

201+
/// \returns true if the instruction may read elements past VL, e.g.
202+
/// vslidedown/vrgather
203+
static inline bool readsPastVL(uint64_t TSFlags) {
204+
return TSFlags & ReadsPastVLMask;
205+
}
206+
198207
static inline unsigned getVLOpNum(const MCInstrDesc &Desc) {
199208
const uint64_t TSFlags = Desc.TSFlags;
200209
// This method is only called if we expect to have a VL operand, and all

llvm/lib/Target/RISCV/RISCVInstrFormats.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,12 @@ class RVInstCommon<dag outs, dag ins, string opcodestr, string argstr,
261261
// Indicates the EEW of a vector instruction's destination operand.
262262
EEW DestEEW = EEWSEWx1;
263263
let TSFlags{25-24} = DestEEW.Value;
264+
265+
// Some vector instructions like vslidedown/vrgather will read elements past
266+
// VL, and should be marked to make sure RISCVVLOptimizer doesn't reduce its
267+
// operands' VLs.
268+
bit ReadsPastVL = 0;
269+
let TSFlags{26} = ReadsPastVL;
264270
}
265271

266272
class RVInst<dag outs, dag ins, string opcodestr, string argstr,

llvm/lib/Target/RISCV/RISCVInstrInfoV.td

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,28 +1703,29 @@ let Constraints = "@earlyclobber $vd", RVVConstraint = SlideUp in {
17031703
defm VSLIDEUP_V : VSLD_IV_X_I<"vslideup", 0b001110, /*slidesUp=*/true>;
17041704
defm VSLIDE1UP_V : VSLD1_MV_X<"vslide1up", 0b001110>;
17051705
} // Constraints = "@earlyclobber $vd", RVVConstraint = SlideUp
1706+
let ReadsPastVL = 1 in
17061707
defm VSLIDEDOWN_V : VSLD_IV_X_I<"vslidedown", 0b001111, /*slidesUp=*/false>;
1707-
let ElementsDependOn = EltDepsVL in
1708+
let ElementsDependOn = EltDepsVL, ReadsPastVL = 1 in
17081709
defm VSLIDE1DOWN_V : VSLD1_MV_X<"vslide1down", 0b001111>;
17091710
} // Predicates = [HasVInstructions]
17101711

17111712
let Predicates = [HasVInstructionsAnyF] in {
17121713
let Constraints = "@earlyclobber $vd", RVVConstraint = SlideUp in {
17131714
defm VFSLIDE1UP_V : VSLD1_FV_F<"vfslide1up", 0b001110>;
17141715
} // Constraints = "@earlyclobber $vd", RVVConstraint = SlideUp
1715-
let ElementsDependOn = EltDepsVL in
1716+
let ElementsDependOn = EltDepsVL, ReadsPastVL = 1 in
17161717
defm VFSLIDE1DOWN_V : VSLD1_FV_F<"vfslide1down", 0b001111>;
17171718
} // Predicates = [HasVInstructionsAnyF]
17181719

17191720
let Predicates = [HasVInstructions] in {
17201721
// Vector Register Gather Instruction
1721-
let Constraints = "@earlyclobber $vd", RVVConstraint = Vrgather in {
1722+
let Constraints = "@earlyclobber $vd", RVVConstraint = Vrgather, ReadsPastVL = 1 in {
17221723
defm VRGATHER_V : VGTR_IV_V_X_I<"vrgather", 0b001100>;
17231724
def VRGATHEREI16_VV : VALUVV<0b001110, OPIVV, "vrgatherei16.vv">,
17241725
SchedBinaryMC<"WriteVRGatherEI16VV",
17251726
"ReadVRGatherEI16VV_data",
17261727
"ReadVRGatherEI16VV_index">;
1727-
} // Constraints = "@earlyclobber $vd", RVVConstraint = Vrgather
1728+
} // Constraints = "@earlyclobber $vd", RVVConstraint = Vrgather, ReadsPastVL = 1
17281729

17291730
// Vector Compress Instruction
17301731
let Constraints = "@earlyclobber $vd", RVVConstraint = Vcompress, ElementsDependOn = EltDepsVLMask in {

llvm/lib/Target/RISCV/RISCVInstrInfoXRivos.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class CustomRivosXVI<bits<6> funct6, RISCVVFormat opv, dag outs, dag ins,
5858

5959
let Predicates = [HasVendorXRivosVizip], DecoderNamespace = "XRivos",
6060
Constraints = "@earlyclobber $vd", RVVConstraint = Vrgather,
61-
Inst<6-0> = OPC_CUSTOM_2.Value in {
61+
Inst<6-0> = OPC_CUSTOM_2.Value, ReadsPastVL = 1 in {
6262
defm RI_VZIPEVEN_V : VALU_IV_V<"ri.vzipeven", 0b001100>;
6363
defm RI_VZIPODD_V : VALU_IV_V<"ri.vzipodd", 0b011100>;
6464
defm RI_VZIP2A_V : VALU_IV_V<"ri.vzip2a", 0b000100>;
@@ -126,6 +126,7 @@ def RI_VINSERT : CustomRivosVXI<0b010000, OPMVX, (outs VR:$vd_wb),
126126
(ins VR:$vd, GPR:$rs1, uimm5:$imm),
127127
"ri.vinsert.v.x", "$vd, $rs1, $imm">;
128128

129+
let ReadsPastVL = 1 in
129130
def RI_VEXTRACT : CustomRivosXVI<0b010111, OPMVV, (outs GPR:$rd),
130131
(ins VR:$vs2, uimm5:$imm),
131132
"ri.vextract.x.v", "$rd, $vs2, $imm">;

llvm/lib/Target/RISCV/RISCVInstrInfoXSf.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class RVInstVCCustom2<bits<4> funct6_hi4, bits<3> funct3, dag outs, dag ins,
7474
let Uses = [VL, VTYPE];
7575
let RVVConstraint = NoConstraint;
7676
let ElementsDependOn = EltDepsVLMask;
77+
let ReadsPastVL = 1;
7778
}
7879

7980
class RVInstVCFCustom2<bits<4> funct6_hi4, bits<3> funct3, dag outs, dag ins,
@@ -98,6 +99,7 @@ class RVInstVCFCustom2<bits<4> funct6_hi4, bits<3> funct3, dag outs, dag ins,
9899
let Uses = [VL, VTYPE];
99100
let RVVConstraint = NoConstraint;
100101
let ElementsDependOn = EltDepsVLMask;
102+
let ReadsPastVL = 1;
101103
}
102104

103105
class VCIXInfo<string suffix, VCIXType type, DAGOperand TyRd,

llvm/lib/Target/RISCV/RISCVInstrInfoXSfmm.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class SFInstTileMemOp<dag outs, dag ins, bits<3> nf, RISCVOpcode opcode,
6565
let Inst{6-0} = opcode.Value;
6666

6767
let Uses = [VTYPE, VL];
68+
let ReadsPastVL = 1;
6869
}
6970

7071
let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
@@ -94,6 +95,7 @@ class SFInstTileMoveOp<bits<6> funct6, dag outs, dag ins, string opcodestr,
9495
let Inst{6-0} = OPC_OP_V.Value;
9596

9697
let Uses = [VTYPE, VL];
98+
let ReadsPastVL = 1;
9799
}
98100

99101
let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
@@ -113,6 +115,7 @@ class SFInstMatmulF<dag outs, dag ins, string opcodestr, string argstr>
113115
let Inst{6-0} = OPC_OP_VE.Value;
114116

115117
let Uses = [VTYPE, VL];
118+
let ReadsPastVL = 1;
116119
}
117120

118121
let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
@@ -135,6 +138,7 @@ class SFInstMatmulF8<bit a, bit b, dag outs, dag ins,
135138
let Inst{6-0} = OPC_OP_VE.Value;
136139

137140
let Uses = [VTYPE, VL];
141+
let ReadsPastVL = 1;
138142
}
139143

140144

@@ -167,6 +171,7 @@ class SFInstMatmulI8<bit funct6_1, bit a, bit b, dag outs, dag ins,
167171
let Inst{6-0} = OPC_OP_VE.Value;
168172

169173
let Uses = [VTYPE, VL];
174+
let ReadsPastVL = 1;
170175
}
171176

172177
class I8Encode<bit encoding, string name> {

llvm/lib/Target/RISCV/RISCVVLOptimizer.cpp

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,34 +1227,6 @@ static bool isVectorOpUsedAsScalarOp(const MachineOperand &MO) {
12271227
}
12281228
}
12291229

1230-
/// Return true if MI may read elements past VL.
1231-
static bool mayReadPastVL(const MachineInstr &MI) {
1232-
const RISCVVPseudosTable::PseudoInfo *RVV =
1233-
RISCVVPseudosTable::getPseudoInfo(MI.getOpcode());
1234-
if (!RVV)
1235-
return true;
1236-
1237-
switch (RVV->BaseInstr) {
1238-
// vslidedown instructions may read elements past VL. They are handled
1239-
// according to current tail policy.
1240-
case RISCV::VSLIDEDOWN_VI:
1241-
case RISCV::VSLIDEDOWN_VX:
1242-
case RISCV::VSLIDE1DOWN_VX:
1243-
case RISCV::VFSLIDE1DOWN_VF:
1244-
1245-
// vrgather instructions may read the source vector at any index < VLMAX,
1246-
// regardless of VL.
1247-
case RISCV::VRGATHER_VI:
1248-
case RISCV::VRGATHER_VV:
1249-
case RISCV::VRGATHER_VX:
1250-
case RISCV::VRGATHEREI16_VV:
1251-
return true;
1252-
1253-
default:
1254-
return false;
1255-
}
1256-
}
1257-
12581230
bool RISCVVLOptimizer::isCandidate(const MachineInstr &MI) const {
12591231
const MCInstrDesc &Desc = MI.getDesc();
12601232
if (!RISCVII::hasVLOp(Desc.TSFlags) || !RISCVII::hasSEWOp(Desc.TSFlags))
@@ -1315,7 +1287,8 @@ RISCVVLOptimizer::getMinimumVLForUser(const MachineOperand &UserOp) const {
13151287
return std::nullopt;
13161288
}
13171289

1318-
if (mayReadPastVL(UserMI)) {
1290+
if (RISCVII::readsPastVL(
1291+
TII->get(RISCV::getRVVMCOpcode(UserMI.getOpcode())).TSFlags)) {
13191292
LLVM_DEBUG(dbgs() << " Abort because used by unsafe instruction\n");
13201293
return std::nullopt;
13211294
}

0 commit comments

Comments
 (0)