21#include "mlir/Dialect/Arith/IR/Arith.h"
22#include "mlir/Dialect/Func/IR/FuncOps.h"
23#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
24#include "mlir/Dialect/SCF/IR/SCF.h"
25#include "mlir/IR/Builders.h"
26#include "mlir/Pass/Pass.h"
29#define GEN_PASS_DEF_AIELOWERCOREISSUEDCONFIGURATION
30#include "aie/Dialect/AIE/Transforms/AIEPasses.h.inc"
41constexpr uint64_t kTileMemoryMappedBase = 0x80000;
44struct TileAccessIntrinsics {
45 std::string write, read;
48static std::optional<TileAccessIntrinsics> tileAccessIntrinsics(AIEArch arch) {
51 return TileAccessIntrinsics{
"llvm.aie2.write.tm",
"llvm.aie2.read.tm"};
53 return TileAccessIntrinsics{
"llvm.aie2p.write.tm",
"llvm.aie2p.read.tm"};
61pushWrites(DmaPushQueueOp op,
int col,
int row,
63 SmallVectorImpl<std::pair<uint64_t, uint32_t>> &writes) {
65 return op.emitOpError(
"names its descriptor by tag; run "
66 "--aie-assign-bd-ids first");
68 if (failed(ctl.setIOBackend(
false,
false)))
69 return op.emitOpError(
"could not start aie-codegen to collect its writes");
70 ctl.startTransaction();
74 if (failed(ctl.pushToBdQueueAndEnable(
75 *op.getOperation(), col, row, op.getChannel(), op.getDirection(),
76 op.getBdId().value_or(0), op.getRepeatCount(), 0,
77 op.getOutOfOrder(),
false)))
80 std::vector<uint8_t> txn = ctl.exportSerializedTransaction();
81 std::vector<TransactionBinaryOperation> ops;
83 return op.emitOpError(
"could not read back what aie-codegen recorded");
90 uint64_t tileSpan = 1ull << targetModel.
getRowShift();
92 if (txnOp.cmd.Opcode != XAie_TxnOpcode::XAIE_IO_WRITE)
93 return op.emitOpError(
"pushing this task takes a ")
94 <<
static_cast<int>(txnOp.cmd.Opcode)
95 <<
" transaction command, which a core cannot make; only plain "
96 "register writes lower here";
97 uint64_t addr = txnOp.cmd.RegOff;
98 if (addr < tileBase || addr >= tileBase + tileSpan)
99 return op.emitOpError(
"pushing this task writes ")
100 << addr <<
", outside tile (" << col <<
", " << row
101 <<
"); a core reaches only its own tile's registers";
102 writes.emplace_back(addr - tileBase, txnOp.cmd.Value);
105 return op.emitOpError(
"aie-codegen recorded no writes for this push");
109struct AIELowerCoreIssuedConfigurationPass
110 :
public xilinx::AIE::impl::AIELowerCoreIssuedConfigurationBase<
111 AIELowerCoreIssuedConfigurationPass> {
116 func::FuncOp declare(DeviceOp device, StringRef name, TypeRange argTypes,
117 TypeRange resultTypes) {
118 if (
auto decl = device.lookupSymbol<func::FuncOp>(name))
120 OpBuilder builder(device.getContext());
121 builder.setInsertionPointToStart(device.getBody());
122 auto decl = func::FuncOp::create(
123 builder, device.getLoc(), name,
124 FunctionType::get(device.getContext(), argTypes, resultTypes));
130 Value tilePointer(OpBuilder &builder, Location loc, uint64_t offset) {
131 Type i32 = builder.getI32Type();
132 Type ptr = LLVM::LLVMPointerType::get(builder.getContext());
133 Value addr = arith::ConstantOp::create(
135 builder.getI32IntegerAttr(
136 static_cast<int32_t
>(kTileMemoryMappedBase + offset)));
137 return LLVM::IntToPtrOp::create(builder, loc, ptr, addr);
140 LogicalResult lowerPush(DmaPushQueueOp op,
const AIETargetModel &targetModel,
141 func::FuncOp write) {
142 CoreOp core = op->getParentOfType<CoreOp>();
143 TileOp tile = core.getTileOp();
144 SmallVector<std::pair<uint64_t, uint32_t>> writes;
146 pushWrites(op, tile.getCol(), tile.getRow(), targetModel, writes)))
149 core.setProcessorBus(
true);
151 OpBuilder builder(op);
152 Type i32 = builder.getI32Type();
153 for (
auto [offset, value] : writes) {
154 Value valueOp = arith::ConstantOp::create(
155 builder, op.getLoc(), i32,
156 builder.getI32IntegerAttr(
static_cast<int32_t
>(value)));
157 Value addrPtr = tilePointer(builder, op.getLoc(), offset);
162 func::CallOp::create(builder, op.getLoc(), write,
163 ValueRange{valueOp, addrPtr});
169 LogicalResult lowerPoll(DmaPollForQueueSpaceOp op,
170 const AIETargetModel &targetModel,
172 CoreOp core = op->getParentOfType<CoreOp>();
173 TileOp tile = core.getTileOp();
175 tile.getCol(), tile.getRow(), op.getChannel(), op.getDirection());
177 return op.emitOpError(
"this tile reports no DMA status register to read");
180 return op.emitOpError(
181 "this tile reports no task-queue field in its DMA status");
182 core.setProcessorBus(
true);
184 OpBuilder builder(op);
185 Location loc = op.getLoc();
186 Type i32 = builder.getI32Type();
188 *statusAddr - targetModel.
getTileAddress(tile.getCol(), tile.getRow());
189 Value addrPtr = tilePointer(builder, loc, localAddr);
190 Value maskOp = arith::ConstantOp::create(
192 builder.getI32IntegerAttr(
static_cast<int32_t
>(mask)));
196 << llvm::countr_zero(mask);
197 Value fullOp = arith::ConstantOp::create(
199 builder.getI32IntegerAttr(
static_cast<int32_t
>(full)));
203 auto loop = scf::WhileOp::create(builder, loc, TypeRange{}, ValueRange{});
204 OpBuilder::InsertionGuard guard(builder);
205 Block *before = builder.createBlock(&loop.getBefore());
206 builder.setInsertionPointToStart(before);
207 Value status = func::CallOp::create(builder, loc, read, ValueRange{addrPtr})
209 Value size = arith::AndIOp::create(builder, loc, status, maskOp);
210 Value isFull = arith::CmpIOp::create(builder, loc, arith::CmpIPredicate::eq,
212 scf::ConditionOp::create(builder, loc, isFull, ValueRange{});
214 Block *after = builder.createBlock(&loop.getAfter());
215 builder.setInsertionPointToStart(after);
216 scf::YieldOp::create(builder, loc, ValueRange{});
222 void runOnOperation()
override {
223 DeviceOp device = getOperation();
224 const AIETargetModel &targetModel = device.getTargetModel();
226 SmallVector<DmaPushQueueOp> pushes;
227 SmallVector<DmaPollForQueueSpaceOp> polls;
228 device.walk([&](Operation *op) {
229 if (
auto push = dyn_cast<DmaPushQueueOp>(op))
230 pushes.push_back(push);
231 else if (
auto poll = dyn_cast<DmaPollForQueueSpaceOp>(op))
232 polls.push_back(poll);
234 if (pushes.empty() && polls.empty())
237 std::optional<TileAccessIntrinsics> intrinsics =
240 Operation *first = pushes.empty() ? polls.front().getOperation()
241 : pushes.front().getOperation();
242 first->emitOpError(
"this device has no access a core can make to its "
243 "own tile's registers");
244 return signalPassFailure();
247 OpBuilder builder(device.getContext());
248 Type i32 = builder.getI32Type();
249 Type ptr = LLVM::LLVMPointerType::get(device.getContext());
251 if (!polls.empty()) {
252 func::FuncOp read = declare(device, intrinsics->read, {ptr}, {i32});
253 for (DmaPollForQueueSpaceOp op : polls)
254 if (failed(lowerPoll(op, targetModel, read)))
255 return signalPassFailure();
257 if (!pushes.empty()) {
259 declare(device, intrinsics->write, {i32, ptr}, TypeRange{});
260 for (DmaPushQueueOp op : pushes)
261 if (failed(lowerPush(op, targetModel, write)))
262 return signalPassFailure();
269std::unique_ptr<OperationPass<DeviceOp>>
271 return std::make_unique<AIELowerCoreIssuedConfigurationPass>();
virtual uint32_t getTileAddress(int col, int row) const
Return the base address of the tile's window.
virtual AIEArch getTargetArch() const =0
Return the target architecture.
virtual uint32_t getDmaTaskQueueDepth() const =0
Return how many DMA tasks a single channel's task queue holds (unsupported = 0).
virtual uint32_t getDmaTaskQueueSizeMask() const
Return the mask selecting the live task-queue occupancy field within the getDmaStatusAddress() regist...
virtual std::optional< uint32_t > getDmaStatusAddress(int, int, int, AIE::DMAChannelDir) const
Return the address of the DMA status register for a channel, or nullopt when this target's layout has...
virtual uint32_t getRowShift() const =0
std::unique_ptr< mlir::OperationPass< DeviceOp > > createAIELowerCoreIssuedConfigurationPass()
std::optional< int > parseTransactionBinary(const std::vector< uint8_t > &data, std::vector< TransactionBinaryOperation > &ops)
Parse an exported transaction.