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>
184 std::optional<BlockShapes>
186 bool hasTranspose =
false,
187 bool upConv =
false)
const {
197 virtual std::optional<BlockShapes>
199 bool hasTranspose,
bool upConv)
const = 0;
209template <InstructionKind Kind>
213 "ScatterIO only supports LoadGather / StoreScatter");
218 return B->getInstructionKind() == Kind;
254 std::optional<BlockShapes>
257 bool )
const override {
258 static const int kHeight[] = {1, 2, 4, 8};
259 static const int kWidth16[] = {16};
260 static const int kCount[] = {1};
262 if (elemByteSize == 1 || elemByteSize == 2 || elemByteSize == 4)
282 std::optional<BlockShapes>
284 bool hasTranspose,
bool upConv)
const override {
285 static const int kHeightAtLeast1[] = {1, 2, 4, 8, 16, 32};
286 static const int kHeightAtLeast8[] = {8, 16, 32};
287 static const int kHeightAtLeast16[] = {16, 32};
288 static const int kHeight32[] = {32};
289 static const int kHeight64[] = {64};
291 static const int kWidth64[] = {64};
292 static const int kWidth32[] = {32};
293 static const int kWidth16[] = {16};
294 static const int kWidth8[] = {8};
296 static const int32_t kCount1[] = {1};
297 static const int32_t kCount2[] = {1, 2};
298 static const int32_t kCount4[] = {1, 2, 4};
299 static const int32_t kCount4Only[] = {4};
301 using Key = std::tuple<int, uint8_t, uint8_t, uint8_t>;
309 {{8,
false,
false,
false}, {kWidth32, kHeightAtLeast1, kCount2}},
310 {{8,
false,
false,
true}, {kWidth16, kHeightAtLeast8, kCount4Only}},
311 {{16,
false,
false,
false}, {kWidth16, kHeightAtLeast1, kCount2}},
312 {{32,
false,
false,
false}, {kWidth16, kHeightAtLeast1, kCount1}},
314 {{8,
true,
false,
false}, {kWidth16, kHeight32, kCount4}},
315 {{16,
true,
false,
false}, {kWidth16, kHeightAtLeast16, kCount2}},
317 {{8,
false,
true,
false}, {kWidth32, kHeightAtLeast16, kCount1}},
318 {{16,
false,
true,
false}, {kWidth16, kHeightAtLeast16, kCount1}},
319 {{32,
false,
true,
false}, {kWidth8, kHeightAtLeast16, kCount1}},
321 {{4,
false,
false,
false}, {kWidth64, kHeightAtLeast1, kCount2}},
322 {{4,
false,
false,
true}, {kWidth32, kHeightAtLeast8, kCount4Only}},
323 {{4,
true,
false,
false}, {kWidth16, kHeight64, kCount4}},
324 {{4,
false,
true,
false}, {kWidth64, kHeightAtLeast16, kCount1}}};
326 auto it = kMap.find({elemBitSize, hasTransform, hasTranspose, upConv});
327 if (it != kMap.end())
345 std::optional<BlockShapes>
348 bool )
const override {
349 static const int kHeightAtLeast1[] = {1, 2, 4, 8, 16, 32};
351 static const int kWidth32[] = {32};
352 static const int kWidth16[] = {16};
354 static const int32_t kCount1[] = {1};
355 static const int32_t kCount2[] = {1, 2};
362 {1, {kWidth32, kHeightAtLeast1, kCount2}},
363 {2, {kWidth16, kHeightAtLeast1, kCount2}},
364 {4, {kWidth16, kHeightAtLeast1, kCount1}},
367 auto it = kMap.find(elemByteSize);
368 if (it != kMap.end())
383 return B->getInstructionKind() ==
416 return B->getInstructionKind() ==
451 result.emplace_back(x, y);
462 switch (matrixType) {
477 Type bf16Type = BFloat16Type::get(&context);
478 Type f16Type = Float16Type::get(&context);
479 Type tf32Type = FloatTF32Type::get(&context);
480 Type f32Type = Float32Type::get(&context);
482 switch (matrixType) {
485 return {bf16Type, f16Type, tf32Type};
488 return {bf16Type, f16Type, f32Type};
495 return {1, 2, 3, 4, 5, 6, 7, 8};
500 assert(type.
isIntOrFloat() &&
"Matrix type must be int or float");
517 llvm_unreachable(
"Invalid int or float");
532 switch (matrixType) {
547 Type f8E4M3FNType = Float8E4M3FNType::get(&context);
548 Type f8E5M2Type = Float8E5M2Type::get(&context);
549 Type f4E2M1FNType = Float4E2M1FNType::get(&context);
550 Type bf16Type = BFloat16Type::get(&context);
551 Type f32Type = Float32Type::get(&context);
553 switch (matrixType) {
556 return {f8E4M3FNType, f8E5M2Type, f4E2M1FNType};
559 return {bf16Type, f32Type};
571 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
BlockIOInstructionInterface(InstructionKind kind)
std::optional< BlockShapes > getBlockWidthHeightCount(Type elemTy, bool hasTransform=false, bool hasTranspose=false, bool upConv=false) const
static bool classof(const Instruction *B)
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)