Skip to content

Commit a3ae452

Browse files
[GISel][CombinerHelper] Add a generator that counts from 0 to n
1 parent 2ae63a8 commit a3ae452

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/CodeGenTypes/LowLevelType.h"
2828
#include "llvm/IR/InstrTypes.h"
2929
#include <functional>
30+
#include <optional>
3031

3132
namespace llvm {
3233

@@ -246,6 +247,12 @@ class CombinerHelper {
246247
void applyCombineShuffleConcat(MachineInstr &MI, SmallVector<Register> &Ops);
247248

248249
/// Try to combine G_SHUFFLE_VECTOR into G_CONCAT_VECTORS.
250+
/// A function type that returns either the next value in a
251+
/// shufflemask or an empty value. Each iteration should return
252+
/// one value, like a Python iterator or a Lisp stream.
253+
using GeneratorType = std::function<std::optional<int32_t>()>;
254+
255+
/// Try to combine G_SHUFFLE_VECTOR into more efficient opcodes.
249256
/// Returns true if MI changed.
250257
///
251258
/// \pre MI.getOpcode() == G_SHUFFLE_VECTOR.

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#include "llvm/Support/MathExtras.h"
4343
#include "llvm/Target/TargetMachine.h"
4444
#include <cmath>
45+
#include <cstdint>
46+
#include <functional>
4547
#include <optional>
4648
#include <tuple>
4749

@@ -384,6 +386,19 @@ void CombinerHelper::applyCombineShuffleConcat(MachineInstr &MI,
384386
MI.eraseFromParent();
385387
}
386388

389+
// Create a stream from 0 to n with a specified number of steps
390+
CombinerHelper::GeneratorType
391+
adderGenerator(const int32_t From, const int32_t To, const int32_t StepSize) {
392+
int32_t Counter = From;
393+
return [Counter, To, StepSize]() mutable {
394+
std::optional<int32_t> OldCount = std::optional<int32_t>(Counter);
395+
Counter += StepSize;
396+
if (OldCount == (To + StepSize))
397+
OldCount = {};
398+
return OldCount;
399+
};
400+
}
401+
387402
bool CombinerHelper::tryCombineShuffleVector(MachineInstr &MI) {
388403
SmallVector<Register, 4> Ops;
389404
if (matchCombineShuffleVector(MI, Ops)) {

0 commit comments

Comments
 (0)