Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions lib/PTO/IR/PTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3621,7 +3621,8 @@ static LogicalResult verifyTColArgReductionOpCommon(Operation *op, Type srcTy,
/*requireNonZeroSrc=*/true)))
return failure();
Type srcElemTy = getElemTy(srcTy);
unsigned srcElemBits = srcElemTy ? srcElemTy.getIntOrFloatBitWidth() : 0;
unsigned srcElemBytes = srcElemTy ? getPTOStorageElemByteSize(srcElemTy) : 0;
unsigned srcElemBits = srcElemBytes * 8;
if (!(mlir::isa<IntegerType, FloatType>(srcElemTy) &&
(srcElemBits == 8 || srcElemBits == 16 || srcElemBits == 32)))
return op->emitOpError(
Expand Down Expand Up @@ -5324,9 +5325,29 @@ mlir::LogicalResult mlir::pto::TInsertOp::verify() {
return success();
}

// A5 acc->vec path (L0C->UB, ND/NZ modes in pto-isa).
if (*srcSpace == pto::AddressSpace::ACC && *dstSpace == pto::AddressSpace::VEC) {
if (!isColMajorRowMajorNZ(srcTb))
return emitOpError(
"expects A5 acc->vec tinsert src to use blayout=col_major and slayout=row_major");
bool dstIsND = isRowMajorNoneBoxND(dstTb);
bool dstIsNZ = isColMajorRowMajorNZ(dstTb);
if (!dstIsND && !dstIsNZ)
return emitOpError(
"expects A5 acc->vec tinsert dst to use ND (row_major/none_box) or NZ (col_major/row_major) layout");
bool okTypes = (srcElem.isF32() &&
(dstElem.isF16() || dstElem.isBF16() || dstElem.isF32())) ||
(srcElem.isInteger(32) && dstElem.isInteger(32));
if (!okTypes)
return emitOpError(
"expects A5 acc->vec tinsert element types to be "
"(src=f32,dst=f16/bf16/f32) or (src=i32,dst=i32)");
return success();
}

return emitOpError(
"expects A5 tinsert to use a supported src/dst loc pair: "
"acc->mat, vec->mat, or vec->vec");
"acc->mat, acc->vec, vec->mat, or vec->vec");
};
return dispatchVerifierByArch(getOperation(), verifyA2A3, verifyA5);
}
Expand Down Expand Up @@ -5696,8 +5717,8 @@ llvm::LogicalResult mlir::pto::TGatherOp::verify() {
if (!srcSpace || !dstSpace || *srcSpace != pto::AddressSpace::VEC ||
*dstSpace != pto::AddressSpace::VEC)
return emitOpError("expects src and dst to be in the vec address space");
unsigned srcElemBytes = srcElem.getIntOrFloatBitWidth() / 8;
unsigned dstElemBytes = dstElem.getIntOrFloatBitWidth() / 8;
unsigned srcElemBytes = getPTOStorageElemByteSize(srcElem);
unsigned dstElemBytes = getPTOStorageElemByteSize(dstElem);
if (srcElemBytes != dstElemBytes)
return emitOpError("expects src and dst element sizes to match");

Expand Down Expand Up @@ -9457,7 +9478,7 @@ mlir::LogicalResult mlir::pto::TTransOp::verify() {
if (srcTb.getBLayoutValueI32() != static_cast<int32_t>(pto::BLayout::RowMajor))
return emitOpError() << "expects A2/A3 transpose src to use the row_major blayout";
}
unsigned elemBytes = srcElem.getIntOrFloatBitWidth() / 8;
unsigned elemBytes = getPTOStorageElemByteSize(srcElem);
if (elemBytes != 1 && elemBytes != 2 && elemBytes != 4)
return emitOpError() << "expects transpose element size to be 1, 2, or 4 bytes";
auto isAllowedWidthType = [&](Type ty) {
Expand All @@ -9484,7 +9505,7 @@ mlir::LogicalResult mlir::pto::TTransOp::verify() {
Type dstElem = getElemTy(dstTy);
if (!srcElem || !tmpElem || !dstElem || srcElem != dstElem || srcElem != tmpElem)
return emitOpError() << "expects src, tmp, and dst to have the same element type";
unsigned elemBytes = srcElem.getIntOrFloatBitWidth() / 8;
unsigned elemBytes = getPTOStorageElemByteSize(srcElem);
if (elemBytes != 1 && elemBytes != 2 && elemBytes != 4)
return emitOpError() << "expects transpose element size to be 1, 2, or 4 bytes";
auto isAllowedWidthType = [&](Type ty) {
Expand Down
17 changes: 13 additions & 4 deletions lib/PTO/IR/VPTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2625,12 +2625,21 @@ static ParseResult parseStructuredAccStoreClauses(
if (seenClause) {
if (failed(parser.parseOptionalComma()))
return success();
} else {
// First iteration: optionally consume the comma between operands and
// clauses. For some callers (MteL0cUbOp after dst_mode parsing with
// parseOptionalComma already consumed), the comma may already be gone.
(void)parser.parseOptionalComma();
}
StringRef keyword;
if (parser.parseKeyword(&keyword)) {
if (failed(parser.parseOptionalKeyword(&keyword))) {
// No keyword found. If this is the first iteration (no clauses yet),
// that means there are zero clauses — return success without error.
// On subsequent iterations, a keyword is expected after the comma.
if (!seenClause)
return success();
return failure();
return parser.emitError(parser.getCurrentLocation(),
"expected mte_l0c clause keyword");
}
seenClause = true;

Expand Down Expand Up @@ -6996,7 +7005,7 @@ ParseResult MteL0cL1Op::parse(OpAsmParser &parser, OperationState &result) {
parseRequiredOperandWithComma(parser, m) ||
parseRequiredOperandWithComma(parser, n) ||
parseRequiredOperandWithComma(parser, srcStride) ||
parseRequiredOperandWithComma(parser, dstStride) ||
parser.parseOperand(dstStride) ||
parseStructuredAccStoreClauses(parser, state) ||
parser.parseOptionalAttrDict(result.attributes) || parser.parseColon())
return failure();
Expand Down Expand Up @@ -7164,7 +7173,7 @@ ParseResult MteL0cGmOp::parse(OpAsmParser &parser, OperationState &result) {
parseRequiredOperandWithComma(parser, srcStride) ||
parseRequiredOperandWithComma(parser, dstStride) ||
parseRequiredOperandWithComma(parser, sid) ||
parseRequiredOperandWithComma(parser, l2CacheCtrl) ||
parser.parseOperand(l2CacheCtrl) ||
parseStructuredAccStoreClauses(parser, state) ||
parser.parseOptionalAttrDict(result.attributes) || parser.parseColon())
return failure();
Expand Down
7 changes: 6 additions & 1 deletion lib/PTO/Transforms/FoldTileBufIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ static std::optional<TileHandleInfo> resolveTileHandle(Value tileBuf,

user->emitError("FoldTileBufIntrinsics: expected tile_buf to be defined by "
"the active materialized tile-handle bridge "
"(pto.alloc_tile or pto.materialize_tile)");
"(pto.alloc_tile or pto.materialize_tile)")
<< "; got value of type: " << tileBuf.getType()
<< "; defined by: "
<< (tileBuf.getDefiningOp()
? tileBuf.getDefiningOp()->getName().getStringRef()
: StringRef("block-argument"));
return std::nullopt;
}

Expand Down
7 changes: 4 additions & 3 deletions lib/PTO/Transforms/GraphSyncSolver/MemInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "PTO/Transforms/GraphSyncSolver/MemInfo.h"
#include "PTO/IR/PTO.h"
#include "PTO/IR/PTOTypeUtils.h"
#include "../Utils.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/IR/BuiltinTypeInterfaces.h"
Expand All @@ -29,11 +30,11 @@ static std::optional<int64_t> getBufferBitSize(Value value) {
return ShapedType::kDynamic;
}
Type elementType = shaped.getElementType();
auto bitWidth = elementType.getIntOrFloatBitWidth();
if (bitWidth == 0) {
unsigned byteSize = getPTOStorageElemByteSize(elementType);
if (byteSize == 0) {
return ShapedType::kDynamic;
}
return shaped.getNumElements() * bitWidth;
return shaped.getNumElements() * byteSize * 8;
}

llvm::SmallVector<int64_t> getAddresses(const llvm::SmallVector<Value> &addrs) {
Expand Down
14 changes: 7 additions & 7 deletions lib/PTO/Transforms/InsertSync/PTOIRTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See LICENSE in the root of the software repository for the full text of the License.

#include "PTO/Transforms/InsertSync/PTOIRTranslator.h"
#include "PTO/IR/PTOTypeUtils.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
Expand All @@ -20,7 +21,6 @@
#include "llvm/Support/FormatVariadic.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/IR/Matchers.h"
// [P0 新增] 引入副作用接口和 PTO 接口
#include "mlir/Interfaces/SideEffectInterfaces.h"

#define DEBUG_TYPE "pto-ir-translator"
Expand All @@ -35,7 +35,7 @@ static std::pair<int64_t, int64_t> getStaticOffsetAndSize(Operation *op, Value s
auto srcType = dyn_cast<MemRefType>(src.getType());
if (!srcType) return {0, 0};

int64_t elemSize = srcType.getElementType().getIntOrFloatBitWidth() / 8;
int64_t elemSize = getPTOStorageElemByteSize(srcType.getElementType());
if (elemSize == 0) elemSize = 1;

// === Case 1: memref.subview ===
Expand Down Expand Up @@ -235,7 +235,7 @@ LogicalResult PTOIRTranslator::UpdateAllocTileOpMemInfo(pto::AllocTileOp op) {
}

if (isStatic) {
int64_t elemSize = tileType.getElementType().getIntOrFloatBitWidth() / 8;
int64_t elemSize = getPTOStorageElemByteSize(tileType.getElementType());
int64_t numElements = 1;
for (auto dim : shape) numElements *= dim;
sizeInBytes = numElements * elemSize;
Expand Down Expand Up @@ -280,7 +280,7 @@ LogicalResult PTOIRTranslator::UpdatePointerCastOpMemInfo(pto::PointerCastOp op)

uint64_t sizeInBytes = 0;
if (memRefType.hasStaticShape()) {
int64_t elemSize = memRefType.getElementType().getIntOrFloatBitWidth() / 8;
int64_t elemSize = getPTOStorageElemByteSize(memRefType.getElementType());
int64_t numElements = 1;
for (auto dim : memRefType.getShape()) numElements *= dim;
sizeInBytes = numElements * elemSize;
Expand Down Expand Up @@ -314,7 +314,7 @@ PTOIRTranslator::UpdateDeclareTileMemRefOpMemInfo(pto::DeclareTileMemRefOp op) {

uint64_t sizeInBytes = 0;
if (memRefType.hasStaticShape()) {
int64_t elemSize = memRefType.getElementType().getIntOrFloatBitWidth() / 8;
int64_t elemSize = getPTOStorageElemByteSize(memRefType.getElementType());
if (elemSize == 0)
elemSize = 1;

Expand Down Expand Up @@ -593,8 +593,8 @@ LogicalResult PTOIRTranslator::UpdateMemrefAllocOpMemInfo(memref::AllocOp op) {
// 1. 计算大小 (Bytes)
uint64_t sizeInBytes = 0;
if (memRefType.hasStaticShape()) {
int64_t elemSize = memRefType.getElementType().getIntOrFloatBitWidth() / 8;
if (elemSize == 0) elemSize = 1; // bool case
int64_t elemSize = getPTOStorageElemByteSize(memRefType.getElementType());
if (elemSize == 0) elemSize = 1;

int64_t numElements = 1;
for (auto dim : memRefType.getShape()) numElements *= dim;
Expand Down
15 changes: 6 additions & 9 deletions lib/PTO/Transforms/VPTOExpandWrapperOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,9 @@ struct LoadCbufToCbControl {
static FailureOr<LoadCbufToCbControl>
deriveLoadCbufToCbControl(Location loc, Value k, Value n, Type elementType,
bool transpose, PatternRewriter &rewriter) {
unsigned elemBitWidth = elementType.getIntOrFloatBitWidth();
if (elemBitWidth == 0 || (elemBitWidth % 8) != 0)
uint64_t elemBytes = pto::getPTOStorageElemByteSize(elementType);
if (elemBytes == 0)
return failure();
uint64_t elemBytes = elemBitWidth / 8;

auto constant = [&](uint64_t value) -> Value {
return rewriter.create<arith::ConstantIntOp>(loc, value, 64);
Expand Down Expand Up @@ -749,10 +748,9 @@ deriveLoadCbufToCbControl(Location loc, Value k, Value n, Type elementType,
static FailureOr<LoadCbufToCbControl>
deriveLoadCbufToCaControl(Location loc, Value m, Value k, Type elementType,
bool transpose, PatternRewriter &rewriter) {
unsigned elemBitWidth = elementType.getIntOrFloatBitWidth();
if (elemBitWidth == 0 || (elemBitWidth % 8) != 0)
uint64_t elemBytes = pto::getPTOStorageElemByteSize(elementType);
if (elemBytes == 0)
return failure();
uint64_t elemBytes = elemBitWidth / 8;

auto constant = [&](uint64_t value) -> Value {
return rewriter.create<arith::ConstantIntOp>(loc, value, 64);
Expand Down Expand Up @@ -1324,10 +1322,9 @@ struct ExpandRightLoadMxPattern : public OpRewritePattern<pto::MteL1L0bMxOp> {
if (!sourceType)
return rewriter.notifyMatchFailure(op, "expected typed L1 source");

unsigned elemBitWidth = sourceType.getElementType().getIntOrFloatBitWidth();
if (elemBitWidth == 0 || (elemBitWidth % 8) != 0)
uint64_t elemBytes = pto::getPTOStorageElemByteSize(sourceType.getElementType());
if (elemBytes == 0)
return rewriter.notifyMatchFailure(op, "unsupported element type");
uint64_t elemBytes = elemBitWidth / 8;

auto constant = [&](uint64_t value) -> Value {
return rewriter.create<arith::ConstantIntOp>(loc, value, 64);
Expand Down
9 changes: 4 additions & 5 deletions lib/PTO/Transforms/VPTOLLVMEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4833,11 +4833,10 @@ class LowerLoadCbufToCaMxOpPattern final
return rewriter.notifyMatchFailure(op, "failed to map cbuf/ca pointer spaces");

Type sourceElemType = cast<pto::PtrType>(op.getSource().getType()).getElementType();
unsigned elemBitWidth = sourceElemType.getIntOrFloatBitWidth();
if (elemBitWidth == 0 || (elemBitWidth % 8) != 0)
unsigned elemBytes = getPTOStorageElemByteSize(sourceElemType);
if (elemBytes == 0)
return rewriter.notifyMatchFailure(op,
"unsupported load_cbuf_to_ca_mx element type");
uint64_t elemBytes = elemBitWidth / 8;
Location loc = op.getLoc();
auto constant = [&](uint64_t value) -> Value {
return rewriter.create<arith::ConstantIntOp>(loc, value, 64);
Expand Down Expand Up @@ -4911,8 +4910,8 @@ class LowerLoadCbufToCbMxOpPattern final
return rewriter.notifyMatchFailure(op, "failed to map cbuf/cb pointer spaces");

Type sourceElemType = cast<pto::PtrType>(op.getSource().getType()).getElementType();
unsigned elemBitWidth = sourceElemType.getIntOrFloatBitWidth();
if (elemBitWidth == 0 || (elemBitWidth % 8) != 0)
unsigned elemBytes = getPTOStorageElemByteSize(sourceElemType);
if (elemBytes == 0)
return rewriter.notifyMatchFailure(op,
"unsupported load_cbuf_to_cb_mx element type");
FailureOr<Value> config0 =
Expand Down
Loading
Loading