MLIR-AIE 1.4.5.dev
AIEUtils.h
Go to the documentation of this file.
1//===- AIEUtils.h -----------------------------------------------*- C++ -*-===//
2//
3// Copyright (C) 2025 Advanced Micro Devices, Inc.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
8#ifndef AIEX_UTILS_H
9#define AIEX_UTILS_H
10
13#include "mlir/Dialect/MemRef/IR/MemRef.h"
14#include "mlir/IR/BuiltinOps.h"
15#include "mlir/IR/Value.h"
16#include "llvm/ADT/DenseMap.h"
17
18#include "llvm/Support/raw_ostream.h"
19
20using namespace mlir;
21
22namespace xilinx {
23namespace AIEX {
24
25// The private constant `memref.global`s holding a device's blockwrite payloads:
26// one per distinct payload, each new one named `<prefix><n>` past every symbol
27// of that form the device held when the table was made. It holds op handles,
28// so it lives no longer than one rewrite of the device.
30public:
31 BlockwriteData(AIE::DeviceOp dev, llvm::StringRef prefix);
32
33 // The global holding `words`, created at `builder`'s insertion point if the
34 // device has none.
35 memref::GlobalOp getOrCreate(OpBuilder &builder, mlir::Location loc,
36 ArrayRef<uint32_t> words);
37
38 AIE::DeviceOp getDevice() const { return dev; }
39
40private:
41 AIE::DeviceOp dev;
42 std::string prefix;
43 llvm::DenseMap<mlir::Attribute, memref::GlobalOp> byValue;
44 unsigned nextId = 0;
45};
46
47// Result of tracing through supported view/cast operations to a block argument
48// for traceSubviewToBlockArgument function.
50 BlockArgument rootArg;
52};
53
54// Trace through memref.subview, memref.view, memref.cast, and
55// memref.reinterpret_cast operations until the referenced SSA value is a block
56// argument.
57//
58// Returns the root block argument and cumulative byte offset, or std::nullopt
59// if the chain doesn't lead to a block argument or contains unsupported ops.
60//
61// This function checks that all subviews remain static and contiguous. A
62// memref.view must have a constant byte shift and no dynamic result sizes.
63std::optional<SubviewTraceResult> traceSubviewToBlockArgument(Value value);
64
65// Index of `arg` among the host buffers of its runtime sequence.
66//
67// The host passes one buffer per memref argument. Scalar arguments travel in
68// the instruction stream and occupy no buffer slot. The block-argument number
69// therefore over-counts them. Returns nullopt when `arg` is not a memref.
70std::optional<unsigned> getHostBufferArgIndex(BlockArgument arg);
71
72// The number of host buffers `block`'s arguments take: its memref arguments.
73unsigned getHostBufferCount(Block &block);
74
75// Emit an `aiex.npu.update_from_scratchpad` op that adds the runtime offset
76// (held in the scratchpad slot referenced by `bdOp`'s
77// `offset_state_table_idx` attribute, multiplied by the element size of
78// `bufType`) into the BD address register at `registerAddr`.
79LogicalResult emitUpdateBdAddressFromOffsetParameter(OpBuilder &builder,
80 Operation *bdOp,
81 BaseMemRefType bufType,
82 uint64_t registerAddr);
83
84// Emit an `aiex.npu.update_from_scratchpad` op that adds the runtime length
85// (held in the scratchpad slot referenced by `bdOp`'s
86// `length_state_table_idx` attribute, times `lengthUnit` elements of
87// `bufType`) into the buffer length register of BD `bdId` on tile
88// (`col`, `row`), which the firmware counts in 32-bit words. Fails if the tile
89// does not support a runtime length.
90LogicalResult
91emitUpdateBdLengthFromParameter(OpBuilder &builder, Operation *bdOp,
92 BaseMemRefType bufType, int64_t lengthUnit,
93 const AIE::AIETargetModel &targetModel, int col,
94 int row, int bdId);
95
96// The configures a DMA task value can come from. A task carried through
97// runtime control flow is an scf.for iter_arg or an scf.for/scf.if result
98// rather than a configure result; this walks such a value back through every
99// region-branch operand that can feed it (a loop's init and back-edge, each
100// branch's yield), at any nesting depth. Each configure is listed once.
101// Returns false if some path ends at a value that is not a configure result.
102bool getReachableConfigures(Value task,
103 SmallVectorImpl<DMAConfigureTaskOp> &configures);
104
105// The one configure a task value can come from, through runtime control flow
106// as above, or null if there is none or more than one.
107DMAConfigureTaskOp getUniqueReachableConfigure(Value task);
108
109// `device:sequence`, the name a host runs `seq` by.
110std::string scratchpadCommandName(AIE::RuntimeSequenceOp seq);
111
112// Write params.txt for every `aiex.scratchpad_parameter` in `moduleOp`, one
113// entry per line: `<num_parameters> <num_slots>`, then per parameter `<name>
114// <state_table_idx> <type> <kind> <min> <max>`; `<num_joint_bounds>`, then per
115// bound `<length> <offset> <length_step> <max>`; `<num_commands>`, then per
116// command `<device>:<sequence> <num_read> <name>...`. A slot of "-" means no
117// command reads the parameter. kind is core (shift-2 encoded), addr (raw) or
118// repeat; "- -" bounds mean unbounded. A joint bound requires offset +
119// length_step * length <= max. A command's slots are distinct; commands may
120// share slots. See runtime_lib/test_lib/parameter_scratchpad.h for the parser.
121void emitScratchpadParamsFile(mlir::ModuleOp moduleOp, llvm::raw_ostream &os);
122} // namespace AIEX
123} // namespace xilinx
124
125#endif // AIEX_UTILS_H
memref::GlobalOp getOrCreate(OpBuilder &builder, mlir::Location loc, ArrayRef< uint32_t > words)
Definition AIEUtils.cpp:180
BlockwriteData(AIE::DeviceOp dev, llvm::StringRef prefix)
Definition AIEUtils.cpp:161
AIE::DeviceOp getDevice() const
Definition AIEUtils.h:38
unsigned getHostBufferCount(Block &block)
Definition AIEUtils.cpp:155
DMAConfigureTaskOp getUniqueReachableConfigure(Value task)
Definition AIEUtils.cpp:354
std::optional< SubviewTraceResult > traceSubviewToBlockArgument(Value value)
Definition AIEUtils.cpp:18
std::string scratchpadCommandName(AIE::RuntimeSequenceOp seq)
Definition AIEUtils.cpp:254
void emitScratchpadParamsFile(mlir::ModuleOp moduleOp, llvm::raw_ostream &os)
bool getReachableConfigures(Value task, SmallVectorImpl< DMAConfigureTaskOp > &configures)
Definition AIEUtils.cpp:318
LogicalResult emitUpdateBdAddressFromOffsetParameter(OpBuilder &builder, Operation *bdOp, BaseMemRefType bufType, uint64_t registerAddr)
Definition AIEUtils.cpp:200
LogicalResult emitUpdateBdLengthFromParameter(OpBuilder &builder, Operation *bdOp, BaseMemRefType bufType, int64_t lengthUnit, const AIE::AIETargetModel &targetModel, int col, int row, int bdId)
Definition AIEUtils.cpp:221
std::optional< unsigned > getHostBufferArgIndex(BlockArgument arg)
Definition AIEUtils.cpp:142