Skip to content

Commit 36549ac

Browse files
[AIE2P][AIE2] Split symmetric G_BUILD_VECTOR into two G_BUILD_VECTORs
1 parent 17b26e2 commit 36549ac

File tree

5 files changed

+427
-6
lines changed

5 files changed

+427
-6
lines changed

llvm/lib/Target/AIE/AIECombine.td

+38-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@
99
//===----------------------------------------------------------------------===//
1010
include "llvm/Target/GlobalISel/Combine.td"
1111

12+
def aie_all_combines : GICombineGroup<[trivial_combines, vector_ops_combines,
13+
insert_vec_elt_combines, extract_vec_elt_combines, combines_for_extload,
14+
combine_extracted_vector_load,
15+
undef_combines, identity_combines, phi_combines,
16+
simplify_add_to_sub, hoist_logic_op_with_same_opcode_hands, shifts_too_big,
17+
reassocs, ptr_add_immed_chain,
18+
shl_ashr_to_sext_inreg, sext_inreg_of_load,
19+
width_reduction_combines, select_combines,
20+
known_bits_simplifications, ext_ext_fold,
21+
not_cmp_fold, opt_brcond_by_inverting_cond,
22+
unmerge_merge, unmerge_cst, unmerge_dead_to_trunc,
23+
unmerge_zext_to_zext, merge_unmerge, trunc_ext_fold, trunc_shift,
24+
const_combines, xor_of_and_with_same_reg, ptr_add_with_zero,
25+
shift_immed_chain, shift_of_shifted_logic_chain, load_or_combine,
26+
div_rem_to_divrem, funnel_shift_combines, commute_shift,
27+
form_bitfield_extract, constant_fold_binops, constant_fold_fma,
28+
constant_fold_cast_op, fabs_fneg_fold,
29+
intdiv_combines, mulh_combines, redundant_neg_operands,
30+
and_or_disjoint_mask, fma_combines, fold_binop_into_select,
31+
sub_add_reg, select_to_minmax, redundant_binop_in_equality,
32+
fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors,
33+
double_icmp_zero_and_or_combine, match_addos, combine_shuffle_concat]>;
34+
1235
// AIE-specific offset folding for G_GLOBAL_VALUE.
1336
def combine_globalval_offset_matchdata : GIDefMatchData<"uint64_t">;
1437
def combine_globalval_offset : GICombineRule<
@@ -34,6 +57,13 @@ def combine_extract_vector_elt_and_zsa_ext : GICombineRule<
3457
(apply [{ applyExtractVecEltAndExt(*${root}, MRI, B, ${matchinfo}); }])
3558
>;
3659

60+
def combine_symmetric_build_vector : GICombineRule<
61+
(defs root:$root, build_fn_matchinfo:$matchinfo),
62+
(match (wip_match_opcode G_BUILD_VECTOR): $root,
63+
[{ return matchSymmetricBuildVector(*${root}, MRI, Observer, ${matchinfo}); }]),
64+
(apply [{ Helper.applyBuildFnNoErase(*${root}, ${matchinfo}); }])
65+
>;
66+
3767
def combine_splat_vector_matchdata: GIDefMatchData<"std::pair<Register, Register>">;
3868
def combine_splat_vector : GICombineRule<
3969
(defs root:$root, combine_splat_vector_matchdata:$matchinfo),
@@ -122,19 +152,20 @@ def combine_vector_shuffle_bcst_to_copy : GICombineRule<
122152

123153
def AIE2PreLegalizerCombiner
124154
: GICombiner<"AIE2PreLegalizerCombinerImpl", [ combine_unpad_vector, combine_pad_vector,
125-
all_combines, combine_S20NarrowingOpt,
155+
aie_all_combines, combine_S20NarrowingOpt,
126156
combine_globalval_offset,
127157
combine_extract_vector_elt_and_zsa_ext,
128158
combine_splat_vector, combine_concat_to_pad_vector,
129-
combine_single_diff_build_vector]> {
159+
combine_single_diff_build_vector,
160+
combine_symmetric_build_vector]> {
130161
let CombineAllMethodName = "tryCombineAllImpl";
131162
}
132163

133164
def AIE2PPreLegalizerCombiner
134165
: GICombiner<"AIE2PPreLegalizerCombinerImpl", [ combine_unpad_vector, combine_pad_vector,
135166
combine_vector_shuffle_to_copy,
136167
combine_vector_shuffle_extract_subvec,
137-
all_combines, combine_S20NarrowingOpt,
168+
aie_all_combines, combine_S20NarrowingOpt,
138169
combine_globalval_offset,
139170
combine_extract_vector_elt_and_zsa_ext,
140171
combine_splat_vector,
@@ -145,16 +176,17 @@ def AIE2PPreLegalizerCombiner
145176
combine_vector_shuffle_broadcast,
146177
combine_single_diff_build_vector,
147178
combine_vector_shuffle_to_extract_insert_elt,
148-
combine_vector_shuffle_concat_extracted_subvectors]> {
179+
combine_vector_shuffle_concat_extracted_subvectors,
180+
combine_symmetric_build_vector]> {
149181
let CombineAllMethodName = "tryCombineAllImpl";
150182
}
151183

152184
def AIE2PostLegalizerGenericCombiner
153-
: GICombiner<"AIE2PostLegalizerGenericCombinerImpl", [ all_combines ]> {
185+
: GICombiner<"AIE2PostLegalizerGenericCombinerImpl", [ aie_all_combines ]> {
154186
}
155187

156188
def AIE2PPostLegalizerGenericCombiner
157-
: GICombiner<"AIE2PPostLegalizerGenericCombinerImpl", [ all_combines ]> {
189+
: GICombiner<"AIE2PPostLegalizerGenericCombinerImpl", [ aie_all_combines ]> {
158190
}
159191

160192
def combine_extract_concat_matchdata: GIDefMatchData<"Register">;

llvm/lib/Target/AIE/AIECombinerHelper.cpp

+67
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/ADT/SetVector.h"
1717
#include "llvm/Analysis/VectorUtils.h"
1818
#include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h"
19+
#include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
1920
#include "llvm/CodeGen/GlobalISel/Utils.h"
2021
#include "llvm/CodeGen/MachineBasicBlock.h"
2122
#include "llvm/CodeGen/MachineInstr.h"
@@ -1385,6 +1386,72 @@ bool llvm::applySingleDiffLaneBuildVector(
13851386
return true;
13861387
}
13871388

1389+
// Match something like:
1390+
// %0:_(<32 x s16>) = G_BUILD_VECTOR %1:_(s16), ... x16, %2:_(s16), ... x16
1391+
//
1392+
// To turn it into
1393+
// %3:_(<16 x s16>) = G_BUILD_VECTOR %1:_(s16), ... x16
1394+
// %4:_(<16 x s16>) = G_BUILD_VECTOR %2:_(s16), ... x16
1395+
// %0:_(<32 x s16>) = G_CONCAT_VECTORS %3:_(<16 x s16>), %4:_(<16 x s16>)
1396+
// These sub-G_BUILD_VECTOR instructions may later be combined into broadcast
1397+
// instructions by combine_splat_vector.
1398+
bool llvm::matchSymmetricBuildVector(MachineInstr &MI, MachineRegisterInfo &MRI,
1399+
GISelChangeObserver &Observer,
1400+
BuildFnTy &MatchInfo) {
1401+
1402+
assert(MI.getOpcode() == TargetOpcode::G_BUILD_VECTOR &&
1403+
"Expected a G_BUILD_VECTOR");
1404+
const Register DstVecReg = MI.getOperand(0).getReg();
1405+
const LLT DstVecTy = MRI.getType(DstVecReg);
1406+
const unsigned DstVecSize = DstVecTy.getSizeInBits();
1407+
1408+
switch (DstVecSize) {
1409+
case 256:
1410+
case 512:
1411+
case 1024:
1412+
case 2048:
1413+
break;
1414+
default:
1415+
// unimplemented
1416+
return false;
1417+
}
1418+
1419+
auto getSrcOperand = [&](unsigned I) { return MI.getOperand(I + 1); };
1420+
1421+
const unsigned NumElts = MI.getNumOperands() - 1;
1422+
const unsigned HalfNumElts = NumElts / 2;
1423+
const MachineOperand FirstOp = getSrcOperand(0);
1424+
const MachineOperand SecondOp = getSrcOperand(HalfNumElts);
1425+
1426+
// Ensures that each operand in the first half matches FirstOp, and each
1427+
// operand in the second half matches SecondOp.
1428+
for (unsigned i = 0; i < HalfNumElts; i++) {
1429+
if (!getSrcOperand(i).isIdenticalTo(FirstOp)) {
1430+
return false;
1431+
}
1432+
if (!getSrcOperand(HalfNumElts + i).isIdenticalTo(SecondOp)) {
1433+
return false;
1434+
}
1435+
}
1436+
1437+
// If both halves are the same register, it's effectively a splat, and the
1438+
// splat vector combine already handles that case.
1439+
if (FirstOp.isIdenticalTo(SecondOp))
1440+
return false;
1441+
1442+
MatchInfo = [&MI, &Observer, &MRI, DstVecTy](MachineIRBuilder &B) {
1443+
B.setInstrAndDebugLoc(MI);
1444+
LegalizerHelper Helper(B.getMF(), Observer, B);
1445+
// Splits the G_BUILD_VECTOR into two half-sized G_BUILD_VECTOR operations
1446+
// and then emits a G_CONCAT_VECTORS to combine them into final vector.
1447+
Helper.fewerElementsVector(
1448+
MI, 0,
1449+
DstVecTy.changeElementCount(
1450+
DstVecTy.getElementCount().divideCoefficientBy(2)));
1451+
};
1452+
return true;
1453+
}
1454+
13881455
// Match something like:
13891456
// %0(<4 x s32>), dead %1(<4 x s32>), dead %2(<4 x s32>), dead %3(<4 x s32>)
13901457
// = G_UNMERGE_VALUES %10(<16 x s32>)

llvm/lib/Target/AIE/AIECombinerHelper.h

+4
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ bool applySingleDiffLaneBuildVector(
206206
MachineInstr &MI, MachineRegisterInfo &MRI, MachineIRBuilder &B,
207207
AIESingleDiffLaneBuildVectorMatchData &MatchInfo);
208208

209+
bool matchSymmetricBuildVector(MachineInstr &MI, MachineRegisterInfo &MRI,
210+
GISelChangeObserver &Observer,
211+
BuildFnTy &MatchInfo);
212+
209213
bool matchUnpadVector(MachineInstr &MI, MachineRegisterInfo &MRI,
210214
const AIEBaseInstrInfo &TII);
211215
void applyUnpadVector(MachineInstr &MI, MachineRegisterInfo &MRI,

0 commit comments

Comments
 (0)