Skip to content

Commit bed41b1

Browse files
[AIE2] Shuffle pattern for reversing vector order
1 parent aceabde commit bed41b1

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -381,20 +381,21 @@ bool CombinerHelper::tryCombineShuffleVector(MachineInstr &MI) {
381381
const unsigned DstNumElts = DstTy.isVector() ? DstTy.getNumElements() : 1;
382382
const unsigned SrcNumElts = SrcTy.isVector() ? SrcTy.getNumElements() : 1;
383383

384+
// type helper
385+
using generator = std::function<std::optional<int32_t>()>;
386+
384387
// {1, 2, ..., n} -> G_CONCAT_VECTOR
385388
// Turns a shuffle vector that only increments into a concat vector
386389
// instruction
387-
std::function<std::optional<int32_t>()> CountUp =
388-
adderGenerator(0, DstNumElts - 1, 1);
390+
generator CountUp = adderGenerator(0, DstNumElts - 1, 1);
389391
if (matchCombineShuffleVector(MI, Ops, CountUp, 2 * SrcNumElts)) {
390392
applyCombineShuffleVector(MI, Ops);
391393
return true;
392394
}
393395

394396
// {1, 2, ..., |DstVector|} -> G_UNMERGE_VALUES
395397
// Extracts the first chunk of the same size of the destination vector from the source
396-
std::function<std::optional<int32_t>()> FirstQuarter =
397-
adderGenerator(0, DstNumElts - 1, 1);
398+
generator FirstQuarter = adderGenerator(0, DstNumElts - 1, 1);
398399
if (matchCombineShuffleVectorSimple(MI, FirstQuarter, DstNumElts - 1)) {
399400
if (SrcTy == DstTy || ((SrcNumElts / 2) % 2) != 0)
400401
return false;
@@ -405,8 +406,7 @@ bool CombinerHelper::tryCombineShuffleVector(MachineInstr &MI) {
405406

406407
// {|DstVector|, |DstVector|+1, ..., 2 * |DstVector|} -> G_UNMERGE_VALUES
407408
// Extracts the second chunk of the same size of the destination vector from the source
408-
std::function<std::optional<int32_t>()> SecondQuarter =
409-
adderGenerator(DstNumElts, (DstNumElts * 2) - 1, 1);
409+
generator SecondQuarter = adderGenerator(DstNumElts, (DstNumElts * 2) - 1, 1);
410410
if (matchCombineShuffleVectorSimple(MI, SecondQuarter, DstNumElts - 1)) {
411411
if (((SrcNumElts / 2) % 2) != 0)
412412
return false;
@@ -418,14 +418,12 @@ bool CombinerHelper::tryCombineShuffleVector(MachineInstr &MI) {
418418
// {1, 2, ..., n/4, n/2, n/2+1, .... 3n/4} -> G_UNMERGE_VALUES
419419
// Take the first halfs of the two vectors and concatenate them into one
420420
// vector.
421-
std::function<std::optional<int32_t>()> FirstEightA =
422-
adderGenerator(0, (DstNumElts / 2) - 1, 1);
423-
std::function<std::optional<int32_t>()> FirstEightB =
421+
generator FirstEightA = adderGenerator(0, (DstNumElts / 2) - 1, 1);
422+
generator FirstEightB =
424423
adderGenerator(DstNumElts, DstNumElts + (DstNumElts / 2) - 1, 1);
425424

426-
std::function<std::optional<int32_t>()> FirstAndThird =
427-
concatGenerators(SmallVector<std::function<std::optional<int32_t>()>>{
428-
FirstEightA, FirstEightB});
425+
generator FirstAndThird =
426+
concatGenerators(SmallVector<generator>{FirstEightA, FirstEightB});
429427
if (matchCombineShuffleVectorSimple(MI, FirstAndThird,
430428
(DstNumElts / 2) - 1)) {
431429
if (DstNumElts <= 2)
@@ -443,6 +441,18 @@ bool CombinerHelper::tryCombineShuffleVector(MachineInstr &MI) {
443441
MI.eraseFromParent();
444442
return true;
445443
}
444+
445+
// {n/2, n/2+1, ..., n, 0, 1, ..., n/2-1}
446+
generator FirstHalf = adderGenerator(0, SrcNumElts / 2, 1);
447+
generator SecondHalf = adderGenerator(SrcNumElts / 2, SrcNumElts, 1);
448+
generator Reverse =
449+
concatGenerators(SmallVector<generator>{FirstHalf, SecondHalf});
450+
451+
if (matchCombineShuffleVectorSimple(MI, Reverse, SrcNumElts)) {
452+
applyCombineShuffleVector(MI, {Ops[1], Ops[0]});
453+
return true;
454+
}
455+
446456
return false;
447457
}
448458

llvm/test/CodeGen/AIE/aie2/GlobalISel/prelegalizercombiner-shufflevector.mir

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,23 @@ body: |
327327
%2:_(<128 x s8>) = COPY $y3
328328
%3:_(<128 x s8>) = G_SHUFFLE_VECTOR %1:_(<128 x s8>), %2:_(<128 x s8>), shufflemask(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191)
329329
PseudoRET implicit $lr, implicit %3
330+
---
331+
name: concat_vector_reverse_32_512
332+
legalized: false
333+
body: |
334+
bb.1.entry:
335+
liveins: $wl2, $wl4
336+
; CHECK-LABEL: name: concat_vector_reverse_32_512
337+
; CHECK: liveins: $wl2, $wl4
338+
; CHECK-NEXT: {{ $}}
339+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<8 x s32>) = COPY $wl2
340+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:_(<8 x s32>) = COPY $wl4
341+
; CHECK-NEXT: [[CONCAT_VECTORS:%[0-9]+]]:_(<16 x s32>) = G_CONCAT_VECTORS [[COPY1]](<8 x s32>), [[COPY]](<8 x s32>)
342+
; CHECK-NEXT: $x0 = COPY [[CONCAT_VECTORS]](<16 x s32>)
343+
; CHECK-NEXT: PseudoRET implicit $lr, implicit $x0
344+
%1:_(<8 x s32>) = COPY $wl2
345+
%2:_(<8 x s32>) = COPY $wl4
346+
%0:_(<16 x s32>) = G_SHUFFLE_VECTOR %1:_(<8 x s32>), %2:_, shufflemask(8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7)
347+
$x0 = COPY %0:_(<16 x s32>)
348+
PseudoRET implicit $lr, implicit $x0
349+
...

0 commit comments

Comments
 (0)