14#ifndef MLIR_DIALECT_XEGPU_UARCH_UARCHBASE_H
15#define MLIR_DIALECT_XEGPU_UARCH_UARCHBASE_H
25#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/STLExtras.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/StringRef.h"
29#include "llvm/Support/Casting.h"
30#include "llvm/Support/DebugLog.h"
31#include "llvm/Support/ErrorHandling.h"
82 llvm_unreachable(
"Unknown InstructionKind");
107 this->instructionRegistry[instr->getInstructionKind()] = instr;
118 "Instruction not found in registry");
128 llvm::SmallDenseMap<InstructionKind, const Instruction *, 32>
174 std::optional<BlockShapes>
176 bool hasTranspose =
false,
177 bool upConv =
false)
const {
187 virtual std::optional<BlockShapes>
189 bool hasTranspose,
bool upConv)
const = 0;
199template <InstructionKind Kind>
203 "ScatterIO only supports LoadGather / StoreScatter");
208 return B->getInstructionKind() == Kind;
246 std::optional<BlockShapes>
249 bool )
const override {
250 static const int kHeight[] = {1, 2, 4, 8};
251 static const int kWidth16[] = {16};
252 static const int kCount[] = {1};
254 if (elemByteSize == 1 || elemByteSize == 2 || elemByteSize == 4)
276 std::optional<BlockShapes>
278 bool hasTranspose,
bool upConv)
const override {
279 static const int kHeightAtLeast1[] = {1, 2, 4, 8, 16, 32};
280 static const int kHeightAtLeast8[] = {8, 16, 32};
281 static const int kHeightAtLeast16[] = {16, 32};
282 static const int kHeight32[] = {32};
283 static const int kHeight64[] = {64};
285 static const int kWidth64[] = {64};
286 static const int kWidth32[] = {32};
287 static const int kWidth16[] = {16};
288 static const int kWidthAtLeast16[] = {16, 32};
289 static const int kWidthAtLeast32[] = {32, 64};
290 static const int kWidth8[] = {8};
292 static const int32_t kCount1[] = {1};
293 static const int32_t kCount2[] = {1, 2};
294 static const int32_t kCount4[] = {1, 2, 4};
295 static const int32_t kCount4Only[] = {4};
297 using Key = std::tuple<int, uint8_t, uint8_t, uint8_t>;
305 {{8,
false,
false,
false}, {kWidthAtLeast16, kHeightAtLeast1, kCount2}},
306 {{8,
false,
false,
true}, {kWidth16, kHeightAtLeast8, kCount4Only}},
307 {{16,
false,
false,
false}, {kWidth16, kHeightAtLeast1, kCount2}},
308 {{32,
false,
false,
false}, {kWidth16, kHeightAtLeast1, kCount1}},
310 {{8,
true,
false,
false}, {kWidth16, kHeight32, kCount4}},
311 {{16,
true,
false,
false}, {kWidth16, kHeightAtLeast16, kCount2}},
313 {{8,
false,
true,
false}, {kWidth32, kHeightAtLeast16, kCount1}},
314 {{16,
false,
true,
false}, {kWidth16, kHeightAtLeast16, kCount1}},
315 {{32,
false,
true,
false}, {kWidth8, kHeightAtLeast16, kCount1}},
317 {{4,
false,
false,
false}, {kWidthAtLeast32, kHeightAtLeast1, kCount2}},
318 {{4,
false,
false,
true}, {kWidth32, kHeightAtLeast8, kCount4Only}},
319 {{4,
true,
false,
false}, {kWidth16, kHeight64, kCount4}},
320 {{4,
false,
true,
false}, {kWidth64, kHeightAtLeast16, kCount1}}};
322 auto it = kMap.find({elemBitSize, hasTransform, hasTranspose, upConv});
323 if (it != kMap.end())
343 std::optional<BlockShapes>
346 bool )
const override {
347 static const int kHeightAtLeast1[] = {1, 2, 4, 8, 16, 32};
349 static const int kWidth32[] = {32};
350 static const int kWidth16[] = {16};
352 static const int32_t kCount1[] = {1};
353 static const int32_t kCount2[] = {1, 2};
360 {1, {kWidth32, kHeightAtLeast1, kCount2}},
361 {2, {kWidth16, kHeightAtLeast1, kCount2}},
362 {4, {kWidth16, kHeightAtLeast1, kCount1}},
365 auto it = kMap.find(elemByteSize);
366 if (it != kMap.end())
381 return B->getInstructionKind() ==
414 return B->getInstructionKind() ==
449 result.emplace_back(x, y);
460 switch (matrixType) {
475 Type bf16Type = BFloat16Type::get(&context);
476 Type f16Type = Float16Type::get(&context);
477 Type tf32Type = FloatTF32Type::get(&context);
478 Type f32Type = Float32Type::get(&context);
480 switch (matrixType) {
483 return {bf16Type, f16Type, tf32Type};
486 return {bf16Type, f16Type, f32Type};
493 return {1, 2, 3, 4, 5, 6, 7, 8};
498 assert(type.
isIntOrFloat() &&
"Matrix type must be int or float");
515 llvm_unreachable(
"Invalid int or float");
530 switch (matrixType) {
545 Type f8E4M3FNType = Float8E4M3FNType::get(&context);
546 Type f8E5M2Type = Float8E5M2Type::get(&context);
547 Type f4E2M1FNType = Float4E2M1FNType::get(&context);
548 Type bf16Type = BFloat16Type::get(&context);
549 Type f32Type = Float32Type::get(&context);
551 switch (matrixType) {
554 return {f8E4M3FNType, f8E5M2Type, f4E2M1FNType};
557 return {bf16Type, f32Type};
569 assert(type.
isIntOrFloat() &&
"Matrix type must be int or float");
MLIRContext is the top-level object for a collection of MLIR operations.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
llvm::SmallVector< std::pair< uint32_t, uint32_t >, 16 > crossProduct(const llvm::SmallVector< uint32_t, 8 > &a, const llvm::SmallVector< uint32_t, 8 > &b)
@ Subgroup2DBlockPrefetch
@ SubgroupMatrixMultiplyAcc
@ SubgroupScaledMatrixMultiplyAcc
Include the generated interface declarations.
std::tuple< llvm::ArrayRef< int >, llvm::ArrayRef< int >, llvm::ArrayRef< int > > BlockShapes
virtual ~BlockIOInstructionInterface()=default
virtual int32_t getPackedFormatBitSize() const =0
virtual std::optional< BlockShapes > computeBlockWidthHeightCount(Type elemTy, bool hasTransform, bool hasTranspose, bool upConv) const =0
std::optional< BlockShapes > getBlockWidthHeightCount(Type elemTy, bool hasTransform=false, bool hasTranspose=false, bool upConv=false) const
Instruction(InstructionKind kind, InstructionScope scope)
const InstructionScope scope
InstructionScope getScope() const
static llvm::StringRef toString(InstructionKind instKind)
InstructionKind getInstructionKind() const
const InstructionKind instKind
int32_t getMaxLaneAccessSizeBytes() const override
virtual llvm::SmallVector< std::pair< uint32_t, uint32_t >, 16 > getSupportedShapes(Type dataType, MMAOpndKind matrixType)=0
virtual llvm::SmallVector< uint32_t, 8 > getSupportedN(Type type) const =0
virtual bool isLaneLayoutRowMajorOrder() const =0
virtual ~MMAInstructionInterface()=default
virtual llvm::SmallVector< uint32_t, 8 > getSupportedK(Type type) const =0
virtual llvm::SmallVector< uint32_t, 8 > getSupportedM(Type type) const =0
virtual llvm::SmallVector< Type, 8 > getSupportedTypes(MLIRContext &context, MMAOpndKind matrixType)=0
ScatterIoInstructionInterface()
virtual ~ScatterIoInstructionInterface()=default
virtual int32_t getMaxLaneAccessSizeBytes() const =0
static bool classof(const Instruction *B)
int32_t getMaxLaneAccessSizeBytes() const override
int32_t getPackedFormatBitSize() const override
Subgroup2DBlockLoadInstruction()
std::optional< BlockShapes > computeBlockWidthHeightCount(Type elemTy, bool hasTransform, bool hasTranspose, bool upConv) const override
static bool classof(const Instruction *B)
static bool classof(const Instruction *B)
std::optional< BlockShapes > computeBlockWidthHeightCount(Type elemTy, bool, bool, bool) const override
int32_t getPackedFormatBitSize() const override
Subgroup2DBlockPrefetchInstruction()
static bool classof(const Instruction *B)
Subgroup2DBlockStoreInstruction()
std::optional< BlockShapes > computeBlockWidthHeightCount(Type elemTy, bool, bool, bool) const override
int32_t getPackedFormatBitSize() const override
llvm::SmallVector< uint32_t, 8 > getSupportedK(Type type) const override
bool isLaneLayoutRowMajorOrder() const override
llvm::SmallVector< std::pair< uint32_t, uint32_t >, 16 > getSupportedShapes(Type dataType, MMAOpndKind matrixType) override
llvm::SmallVector< Type, 8 > getSupportedTypes(MLIRContext &context, MMAOpndKind matrixType) override
llvm::SmallVector< uint32_t, 8 > getSupportedN(Type type) const override
const unsigned packedFormatBitSizeA
SubgroupMatrixMultiplyAcc(unsigned packedFormatBitSizeA, unsigned packedFormatBitSizeB)
static bool classof(const Instruction *B)
unsigned getPackedFormatBitSizeB() const
unsigned getPackedFormatBitSizeA() const
llvm::SmallVector< uint32_t, 8 > getSupportedM(Type type) const override
const unsigned packedFormatBitSizeB
unsigned getPackedFormatBitSizeB() const
unsigned getPackedFormatBitSizeA() const
bool isLaneLayoutRowMajorOrder() const override
llvm::SmallVector< uint32_t, 8 > getSupportedN(Type type) const override
llvm::SmallVector< uint32_t, 8 > getSupportedM(Type type) const override
SubgroupScaledMatrixMultiplyAcc(unsigned packedFormatBitSizeA, unsigned packedFormatBitSizeB)
static bool classof(const Instruction *B)
llvm::SmallVector< std::pair< uint32_t, uint32_t >, 16 > getSupportedShapes(Type dataType, MMAOpndKind matrixType) override
llvm::SmallVector< Type, 8 > getSupportedTypes(MLIRContext &context, MMAOpndKind matrixType) override
llvm::SmallVector< uint32_t, 8 > getSupportedK(Type type) const override
const unsigned packedFormatBitSizeB
const unsigned packedFormatBitSizeA
llvm::SmallDenseMap< InstructionKind, const Instruction *, 32 > instructionRegistry
virtual unsigned getGeneralPackedFormatBitSize() const =0
bool isSupportedInstruction(InstructionKind instr) const
virtual int getSubgroupSize() const =0
const Instruction * getInstruction(InstructionKind instKind) const
uArch(Kind kind, llvm::ArrayRef< const Instruction * > instructionRegistry)