MLIR-AIE
AIERT.h
Go to the documentation of this file.
1//===- AIERT.h --------------------------------------------------*- C++ -*-===//
2//
3// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7// Copyright (C) 2024, Advanced Micro Devices, Inc. All rights reserved.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef AIE_AIERT_H
12#define AIE_AIERT_H
13
17
18extern "C" {
19#include "xaiengine/xaiegbl_defs.h"
20// above needs to go first for u32, u64 typedefs
21#include "xaiengine/xaie_txn.h"
22#include "xaiengine/xaiegbl.h"
23}
24
25#include "llvm/Support/raw_ostream.h"
26
27#include <map>
28#include <optional>
29#include <string>
30
31#define AIERC_STR(x) x, #x
32static const std::map<AieRC, std::string> AIERCTOSTR = {
33 {AIERC_STR(XAIE_OK)},
34 {AIERC_STR(XAIE_ERR)},
35 {AIERC_STR(XAIE_INVALID_DEVICE)},
36 {AIERC_STR(XAIE_INVALID_RANGE)},
37 {AIERC_STR(XAIE_INVALID_ARGS)},
38 {AIERC_STR(XAIE_INVALID_TILE)},
39 {AIERC_STR(XAIE_ERR_STREAM_PORT)},
40 {AIERC_STR(XAIE_INVALID_DMA_TILE)},
41 {AIERC_STR(XAIE_INVALID_BD_NUM)},
42 {AIERC_STR(XAIE_ERR_OUTOFBOUND)},
43 {AIERC_STR(XAIE_INVALID_DATA_MEM_ADDR)},
44 {AIERC_STR(XAIE_INVALID_ELF)},
45 {AIERC_STR(XAIE_CORE_STATUS_TIMEOUT)},
46 {AIERC_STR(XAIE_INVALID_CHANNEL_NUM)},
47 {AIERC_STR(XAIE_INVALID_LOCK)},
48 {AIERC_STR(XAIE_INVALID_DMA_DIRECTION)},
49 {AIERC_STR(XAIE_INVALID_PLIF_WIDTH)},
50 {AIERC_STR(XAIE_INVALID_LOCK_ID)},
51 {AIERC_STR(XAIE_INVALID_LOCK_VALUE)},
52 {AIERC_STR(XAIE_LOCK_RESULT_FAILED)},
53 {AIERC_STR(XAIE_INVALID_DMA_DESC)},
54 {AIERC_STR(XAIE_INVALID_ADDRESS)},
55 {AIERC_STR(XAIE_FEATURE_NOT_SUPPORTED)},
56 {AIERC_STR(XAIE_INVALID_BURST_LENGTH)},
57 {AIERC_STR(XAIE_INVALID_BACKEND)},
58 {AIERC_STR(XAIE_INSUFFICIENT_BUFFER_SIZE)},
59 {AIERC_STR(XAIE_ERR_MAX)}};
60
61static const std::map<XAie_TxnOpcode, std::string> AIETXNOPCODETOSTR = {
62 {AIERC_STR(XAIE_IO_WRITE)},
63 {AIERC_STR(XAIE_IO_BLOCKWRITE)},
64 {AIERC_STR(XAIE_IO_BLOCKSET)},
65 {AIERC_STR(XAIE_IO_MASKWRITE)},
66 {AIERC_STR(XAIE_IO_MASKPOLL)},
67 {AIERC_STR(XAIE_CONFIG_SHIMDMA_BD)},
68 {AIERC_STR(XAIE_CONFIG_SHIMDMA_DMABUF_BD)},
69 {AIERC_STR(XAIE_IO_CUSTOM_OP_BEGIN)},
70 {AIERC_STR(XAIE_IO_CUSTOM_OP_TCT)},
71 {AIERC_STR(XAIE_IO_CUSTOM_OP_DDR_PATCH)},
72 {AIERC_STR(XAIE_IO_CUSTOM_OP_NEXT)},
73 {AIERC_STR(XAIE_IO_CUSTOM_OP_MAX)}};
74#undef AIERC_STR
75
76static const std::map<xilinx::AIE::WireBundle, StrmSwPortType>
77 WIRE_BUNDLE_TO_STRM_SW_PORT_TYPE = {
78 {xilinx::AIE::WireBundle::Core, StrmSwPortType::CORE},
79 {xilinx::AIE::WireBundle::DMA, StrmSwPortType::DMA},
80 {xilinx::AIE::WireBundle::TileControl, StrmSwPortType::CTRL},
81 {xilinx::AIE::WireBundle::FIFO, StrmSwPortType::FIFO},
82 {xilinx::AIE::WireBundle::South, StrmSwPortType::SOUTH},
83 {xilinx::AIE::WireBundle::West, StrmSwPortType::WEST},
84 {xilinx::AIE::WireBundle::North, StrmSwPortType::NORTH},
85 {xilinx::AIE::WireBundle::East, StrmSwPortType::EAST},
86 // missing PLIO from WireBundle
87 // missing NOC from WireBundle
88 {xilinx::AIE::WireBundle::Trace, StrmSwPortType::TRACE},
89};
90
91#ifndef NDEBUG
92
93// https://stackoverflow.com/a/32230306
94template <typename H1>
95llvm::raw_ostream &showAIEXRTArgs(llvm::raw_ostream &out, const char *label,
96 H1 &&value) {
97 return out << label << "=" << std::forward<H1>(value);
98}
99
100template <typename H1, typename... T>
101llvm::raw_ostream &showAIEXRTArgs(llvm::raw_ostream &out, const char *label,
102 H1 &&value, T &&...rest) {
103 const char *pcomma = strchr(label, ',');
104 return showAIEXRTArgs(out.write(label, pcomma - label)
105 << "=" << std::forward<H1>(value) << ',',
106 pcomma + 1, std::forward<T>(rest)...);
107}
108
109llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const XAie_LocType &loc);
110
111llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const XAie_Lock &lock);
112
113llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const XAie_Packet &packet);
114
115#define SHOW_AIERT_ARGS(os, ...) showAIEXRTArgs(os, #__VA_ARGS__, __VA_ARGS__)
116
117// So that we can use the pattern if(auto r = TRY_XAIE_API...) { // r is nonzero
118// }
119static_assert(XAIE_OK == 0);
120
121#define TRY_XAIE_API_FATAL_ERROR(API, ...) \
122 do { \
123 LLVM_DEBUG(llvm::dbgs() << "trying XAIE API: " << #API << " with args: "); \
124 LLVM_DEBUG(SHOW_AIERT_ARGS(llvm::dbgs(), __VA_ARGS__)); \
125 LLVM_DEBUG(llvm::dbgs() << "\n"); \
126 if (auto r = API(__VA_ARGS__)) \
127 llvm::report_fatal_error(llvm::Twine(#API " failed with ") + \
128 AIERCTOSTR.at(r)); \
129 } while (0)
130
131#define TRY_XAIE_API_EMIT_ERROR(OP, API, ...) \
132 do { \
133 LLVM_DEBUG(llvm::dbgs() << "trying XAIE API: " << #API << " with args: "); \
134 LLVM_DEBUG(SHOW_AIERT_ARGS(llvm::dbgs(), __VA_ARGS__)); \
135 LLVM_DEBUG(llvm::dbgs() << "\n"); \
136 if (auto r = API(__VA_ARGS__)) \
137 return OP.emitOpError() << #API " failed with " << AIERCTOSTR.at(r); \
138 } while (0)
139
140#define TRY_XAIE_API_LOGICAL_RESULT(API, ...) \
141 do { \
142 LLVM_DEBUG(llvm::dbgs() << "trying XAIE API: " << #API << " with args: "); \
143 LLVM_DEBUG(SHOW_AIERT_ARGS(llvm::dbgs(), __VA_ARGS__)); \
144 LLVM_DEBUG(llvm::dbgs() << "\n"); \
145 if (auto r = API(__VA_ARGS__)) { \
146 llvm::errs() << #API " failed with " << AIERCTOSTR.at(r); \
147 return failure(); \
148 } \
149 } while (0)
150
151#else
152
153#define TRY_XAIE_API_FATAL_ERROR(API, ...) \
154 do { \
155 if (auto r = API(__VA_ARGS__)) \
156 llvm::report_fatal_error(llvm::Twine(#API " failed with ") + \
157 AIERCTOSTR.at(r)); \
158 } while (0)
159
160#define TRY_XAIE_API_EMIT_ERROR(OP, API, ...) \
161 do { \
162 if (auto r = API(__VA_ARGS__)) \
163 return OP.emitOpError() << #API " failed with " << AIERCTOSTR.at(r); \
164 } while (0)
165
166#define TRY_XAIE_API_LOGICAL_RESULT(API, ...) \
167 do { \
168 if (auto r = API(__VA_ARGS__)) { \
169 llvm::errs() << #API " failed with " << AIERCTOSTR.at(r); \
170 return failure(); \
171 } \
172 } while (0)
173
174#endif
175
176#define XAIE_BASE_ADDR 0x40000000
177#define XAIE_SHIM_ROW 0
178#define XAIE_MEM_TILE_ROW_START 1
179#define XAIE_PARTITION_BASE_ADDR 0x0
180
181#define NPI_ADDR 0x0
182#define NUM_LOCKS 16
183#define EVEN_BD_NUM_START 0
184#define ODD_BD_NUM_START 24
185
186namespace xilinx::AIE {
188 XAie_Config configPtr;
189 XAie_DevInst devInst;
191
193
194 mlir::LogicalResult setIOBackend(bool aieSim, bool xaieDebug);
195 mlir::LogicalResult configureBdInBlock(XAie_DmaDesc &dmaTileBd,
196 mlir::Block &block,
197 XAie_LocType &tileLoc, int bdId,
198 std::optional<int> nextBdId);
199 mlir::LogicalResult pushToBdQueueAndEnable(mlir::Operation &op,
200 XAie_LocType &tileLoc, int chNum,
201 const DMAChannelDir &channelDir,
202 int bdId, int repeatCount);
203 mlir::LogicalResult configureLocksAndBd(mlir::Block &block,
204 XAie_LocType tileLoc);
205 mlir::LogicalResult initLocks(DeviceOp &targetOp);
206 mlir::LogicalResult initBuffers(DeviceOp &targetOp);
207 mlir::LogicalResult configureSwitches(DeviceOp &targetOp);
208 mlir::LogicalResult addInitConfig(DeviceOp &targetOp);
209 mlir::LogicalResult addCoreEnable(DeviceOp &targetOp);
210 mlir::LogicalResult configureLocksInBdBlock(XAie_DmaDesc &dmaTileBd,
211 mlir::Block &block,
212 XAie_LocType &tileLoc);
213 mlir::LogicalResult addAieElf(uint8_t col, uint8_t row,
214 const mlir::StringRef elfPath, bool aieSim);
215 mlir::LogicalResult addAieElfs(DeviceOp &targetOp,
216 const mlir::StringRef workDirPath,
217 bool aieSim);
218 void startTransaction();
219 void dmaUpdateBdAddr(int col, int row, size_t addr, size_t bdId);
221};
222
223} // namespace xilinx::AIE
224
225#endif // AIE_AIERT_H
llvm::raw_ostream & showAIEXRTArgs(llvm::raw_ostream &out, const char *label, H1 &&value)
Definition AIERT.h:95
llvm::raw_ostream & operator<<(llvm::raw_ostream &os, const XAie_LocType &loc)
Definition AIERT.cpp:36
#define AIERC_STR(x)
Definition AIERT.h:31
Include the generated interface declarations.
mlir::LogicalResult configureLocksAndBd(mlir::Block &block, XAie_LocType tileLoc)
Definition AIERT.cpp:358
void dmaUpdateBdAddr(int col, int row, size_t addr, size_t bdId)
Definition AIERT.cpp:712
mlir::LogicalResult addAieElfs(DeviceOp &targetOp, const mlir::StringRef workDirPath, bool aieSim)
Definition AIERT.cpp:685
mlir::LogicalResult setIOBackend(bool aieSim, bool xaieDebug)
Definition AIERT.cpp:109
mlir::LogicalResult configureBdInBlock(XAie_DmaDesc &dmaTileBd, mlir::Block &block, XAie_LocType &tileLoc, int bdId, std::optional< int > nextBdId)
Definition AIERT.cpp:177
mlir::LogicalResult initLocks(DeviceOp &targetOp)
Definition AIERT.cpp:376
XAie_Config configPtr
Definition AIERT.h:188
mlir::LogicalResult configureSwitches(DeviceOp &targetOp)
Definition AIERT.cpp:457
mlir::LogicalResult initBuffers(DeviceOp &targetOp)
Definition AIERT.cpp:405
mlir::LogicalResult addCoreEnable(DeviceOp &targetOp)
Definition AIERT.cpp:655
mlir::LogicalResult pushToBdQueueAndEnable(mlir::Operation &op, XAie_LocType &tileLoc, int chNum, const DMAChannelDir &channelDir, int bdId, int repeatCount)
Definition AIERT.cpp:342
const BaseNPUTargetModel & targetModel
Definition AIERT.h:190
XAie_DevInst devInst
Definition AIERT.h:189
mlir::LogicalResult addAieElf(uint8_t col, uint8_t row, const mlir::StringRef elfPath, bool aieSim)
Definition AIERT.cpp:665
void exportSerializedTransaction()
Definition AIERT.cpp:722
mlir::LogicalResult addInitConfig(DeviceOp &targetOp)
Definition AIERT.cpp:582
mlir::LogicalResult configureLocksInBdBlock(XAie_DmaDesc &dmaTileBd, mlir::Block &block, XAie_LocType &tileLoc)
Definition AIERT.cpp:124