MLIR-AIE 1.4.6.dev
AIEXDialect.h
Go to the documentation of this file.
1//===- AIEDialect.h ---------------------------------------------*- C++ -*-===//
2//
3// Copyright (C) 2019-2022 Xilinx, Inc.
4// Copyright (C) 2022-2026 Advanced Micro Devices, Inc.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef MLIR_AIEX_DIALECT_H
10#define MLIR_AIEX_DIALECT_H
11
13
14#include "mlir/IR/BuiltinAttributes.h"
15
16// Include dialect declarations such as parseAttributes, parseType
17#include "aie/Dialect/AIEX/IR/AIEXDialect.h.inc"
19#include "mlir/IR/Operation.h"
20
21#include "aie/Dialect/AIEX/IR/AIEXInterfaces.h.inc"
22
23#define GET_ATTRDEF_CLASSES
24#include "aie/Dialect/AIEX/IR/AIEXAttrDefs.h.inc"
25
26// include TableGen generated Op definitions
27#define GET_OP_CLASSES
28#include "aie/Dialect/AIEX/IR/AIEX.h.inc"
29
30#define GET_TYPEDEF_CLASSES
31#include "aie/Dialect/AIEX/IR/AIEXTypes.h.inc"
32
33#include "llvm/ADT/StringRef.h"
34
35#include <optional>
36#include <set>
37
38namespace xilinx {
39namespace AIEX {
40
41void getHardwareStridesWraps(const AIE::AIETargetModel &targetModel,
42 mlir::Operation *op, int tileCol, int tileRow,
43 mlir::BaseMemRefType referencedBufType,
44 llvm::SmallVector<int64_t, 4> inputSizes,
45 llvm::SmallVector<int64_t, 4> inputStrides,
46 llvm::SmallVector<int64_t, 4> &sizes,
47 llvm::SmallVector<int64_t, 4> &strides);
48mlir::LogicalResult
49verifyStridesWraps(mlir::Operation *forOp,
50 llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
51 mlir::BaseMemRefType referencedBufType, int tileCol,
52 int tileRow, llvm::SmallVector<int64_t, 4> inputSizes,
53 llvm::SmallVector<int64_t, 4> inputStrides,
54 llvm::SmallVector<int64_t, 4> hardwareSizes,
55 llvm::SmallVector<int64_t, 4> hardwareStrides,
56 bool skipTransformationChecks = false);
57bool isLinearTransfer(llvm::ArrayRef<int64_t> sizes,
58 llvm::ArrayRef<int64_t> strides);
59
60// Returns true when sizes/strides (innermost-first, same convention as
61// NpuDmaMemcpyNdOp) describe a contiguous row-major scan:
62// strides[0] == 1
63// for i in 1..2: if sizes[i] > 1 then strides[i] == product of sizes[0..i-1]
64// The repeat dimension (index 3) is excluded.
65// Size-1 dimensions are allowed to carry any stride value because that stride
66// is never applied during the transfer (the loop runs only once).
67// This is the vector-form counterpart of AIE::isContiguousBDTransfer.
68bool isContiguousTransfer(llvm::ArrayRef<int64_t> sizes,
69 llvm::ArrayRef<int64_t> strides);
70
71// Whether the DMA lowerings encode the BD in linear mode (no ND wrap limits):
72// a linear transfer, or a contiguous one on a shim NOC tile. Innermost first.
73bool isLinearModeTransfer(const AIE::AIETargetModel &targetModel, int col,
74 int row, llvm::ArrayRef<int64_t> sizes,
75 llvm::ArrayRef<int64_t> strides);
76
77// Folds an SSA value to a compile-time uint32_t if it is a constant integer,
78// otherwise returns nullopt. Used by ops whose operands carry
79// compile-time-known values in the static lowering path.
80std::optional<uint32_t> getConstantIntOperand(mlir::Value v);
81std::optional<uint64_t> getConstantInt64Operand(mlir::Value v);
82
83// Materializes a 32-bit integer constant. The inverse of getConstantIntOperand:
84// used when lowering passes feed a compile-time-known value into an npu op
85// whose operand is an SSA i32. The bit pattern (including high-bit values such
86// as 0x80000000) is preserved.
87mlir::Value createConstantI32(mlir::OpBuilder &builder, mlir::Location loc,
88 uint32_t value);
89
90// Materializes an address_patch `arg_plus` offset as the narrowest constant
91// that holds it: i32 while the value fits, i64 beyond that. aie-codegen carries
92// the field as u64, so truncating a wider offset mis-addresses the buffer.
93mlir::Value createConstantArgPlus(mlir::OpBuilder &builder, mlir::Location loc,
94 uint64_t value);
95
96// The first `aie.dma_start` or `aie.dma` in one of `device`'s shim DMAs, which
97// starts a channel teardown cannot stop, or null.
98mlir::Operation *findShimDmaStart(AIE::DeviceOp device);
99
100// The assigned controller_id of each tile of `device` that has one, which the
101// control packets to that tile route by.
102llvm::DenseMap<AIE::TileID, AIE::PacketInfoAttr>
103controllerIdsOf(AIE::DeviceOp device);
104
106 AIE::ShimDMAAllocationOp allocation;
107 uint32_t packetId;
108 mlir::ArrayAttr tiles;
109};
110
111mlir::FailureOr<llvm::SmallVector<ControlPacketMulticast>>
112controlPacketMulticasts(mlir::ModuleOp module,
113 mlir::FlatSymbolRefAttr reference);
114
115// A BD id a static DMA takes, with its channel if already known.
116struct StaticBd {
117 std::optional<std::pair<int, AIE::DMAChannelDir>> channel;
118 uint32_t id;
119};
120
121// The buffer descriptor ids the static DMAs of `devices` take on `tile`.
122llvm::SmallVector<StaticBd> staticBdIds(llvm::ArrayRef<AIE::DeviceOp> devices,
123 AIE::TileID tile);
124
125} // namespace AIEX
126} // namespace xilinx
127
128#endif
mlir::Operation * findShimDmaStart(AIE::DeviceOp device)
mlir::FailureOr< llvm::SmallVector< ControlPacketMulticast > > controlPacketMulticasts(mlir::ModuleOp module, mlir::FlatSymbolRefAttr reference)
bool isContiguousTransfer(llvm::ArrayRef< int64_t > sizes, llvm::ArrayRef< int64_t > strides)
llvm::DenseMap< AIE::TileID, AIE::PacketInfoAttr > controllerIdsOf(AIE::DeviceOp device)
mlir::LogicalResult verifyStridesWraps(mlir::Operation *forOp, llvm::function_ref< mlir::InFlightDiagnostic()> emitError, mlir::BaseMemRefType referencedBufType, int tileCol, int tileRow, llvm::SmallVector< int64_t, 4 > inputSizes, llvm::SmallVector< int64_t, 4 > inputStrides, llvm::SmallVector< int64_t, 4 > hardwareSizes, llvm::SmallVector< int64_t, 4 > hardwareStrides, bool skipTransformationChecks=false)
std::optional< uint64_t > getConstantInt64Operand(mlir::Value v)
llvm::SmallVector< StaticBd > staticBdIds(llvm::ArrayRef< AIE::DeviceOp > devices, AIE::TileID tile)
std::optional< uint32_t > getConstantIntOperand(mlir::Value v)
mlir::Value createConstantArgPlus(mlir::OpBuilder &builder, mlir::Location loc, uint64_t value)
bool isLinearModeTransfer(const AIE::AIETargetModel &targetModel, int col, int row, llvm::ArrayRef< int64_t > sizes, llvm::ArrayRef< int64_t > strides)
bool isLinearTransfer(llvm::ArrayRef< int64_t > sizes, llvm::ArrayRef< int64_t > strides)
void getHardwareStridesWraps(const AIE::AIETargetModel &targetModel, mlir::Operation *op, int tileCol, int tileRow, mlir::BaseMemRefType referencedBufType, llvm::SmallVector< int64_t, 4 > inputSizes, llvm::SmallVector< int64_t, 4 > inputStrides, llvm::SmallVector< int64_t, 4 > &sizes, llvm::SmallVector< int64_t, 4 > &strides)
mlir::Value createConstantI32(mlir::OpBuilder &builder, mlir::Location loc, uint32_t value)
struct TileID { friend std::ostream &operator<<(std::ostream &os, const TileID &s) { os<< "TileID("<< s.col<< ", "<< s.row<< ")"; return os; } friend std::string to_string(const TileID &s) { std::ostringstream ss; ss<< s; return ss.str(); } friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const TileID &s) { os<< to_string(s); return os; } inline bool operator<(const TileID &rhs) const { return std::tie(col, row)< std::tie(rhs.col, rhs.row); } bool operator==(const TileID &rhs) const { return std::tie(col, row)==std::tie(rhs.col, rhs.row); } bool operator!=(const TileID &rhs) const { return !(*this==rhs);} int col, row;} TileID
AIE::ShimDMAAllocationOp allocation
std::optional< std::pair< int, AIE::DMAChannelDir > > channel