MLIR-AIE
AIETargetLdScript.cpp
Go to the documentation of this file.
1//===- AIETargetLdScript.cpp -----------------------------------*- 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// (c) Copyright 2023 Advanced Micro Devices, Inc.
8//
9//===----------------------------------------------------------------------===//
10
13
14using namespace mlir;
15using namespace xilinx;
16using namespace xilinx::AIE;
17
18// Output the memorymap in gnu linker format for the given buffer operations,
19// with the given offset. The offset is different depending on where the buffers
20// are accessed from.
21static void writeLDScriptMap(raw_ostream &output, BufferOp buf, int offset) {
22 std::string bufName(buf.name().getValue());
23 int bufferBaseAddr = getBufferBaseAddress(buf);
24 int numBytes = buf.getAllocationSize();
25 output << ". = 0x" << llvm::utohexstr(offset + bufferBaseAddr) << ";\n";
26 output << bufName << " = .;\n";
27 output << ". += 0x" << llvm::utohexstr(numBytes) << ";\n";
28}
29
30///// ld.script format:
31//
32// MEMORY
33// {
34// program (RX) : ORIGIN = 0, LENGTH = 0x0020000
35// data (!RX) : ORIGIN = 0x20000, LENGTH = 0x0020000
36// }
37// ENTRY(__start)
38// INPUT(something.o)
39// SECTIONS
40// {
41// . = 0x0;
42// .text : {
43// // the __start symbol has to come at address zero.
44// *crt0.o(.text*)
45// __ctors_start__ = .;
46// __init_array_start = .;
47// KEEP(SORT(*)(.init_array))
48// __ctors_end__ = .;
49// __init_array_end = .;
50// __dtors_start__ = .;
51// __dtors_end__ = .;
52// *(.text*)
53// } > program
54// .data : { *(.data*) } > data
55// . = 0x20000;
56// _sp_start_value_DM_stack = .;
57// . = 0x24000;
58// a = .;
59// . += 1024;
60// .bss : { *(.bss*) } > data
61// }
62// PROVIDE(main = core_3_3);
63
64LogicalResult xilinx::AIE::AIETranslateToLdScript(ModuleOp module,
65 raw_ostream &output,
66 int tileCol, int tileRow) {
67 DenseMap<TileID, Operation *> tiles;
68 DenseMap<Operation *, SmallVector<BufferOp, 4>> buffers;
69
70 if (module.getOps<DeviceOp>().empty()) {
71 module.emitOpError("expected AIE.device operation at toplevel");
72 }
73 DeviceOp targetOp = *(module.getOps<DeviceOp>().begin());
74
75 collectTiles(targetOp, tiles);
76 collectBuffers(targetOp, buffers);
77
78 for (auto tile : targetOp.getOps<TileOp>())
79 if (tile.colIndex() == tileCol && tile.rowIndex() == tileRow) {
80 TileID srcCoord = {tile.colIndex(), tile.rowIndex()};
81 const auto &targetModel = getTargetModel(tile);
82
83 // Figure out how much memory we have left for random allocations
84 auto core = tile.getCoreOp();
85 int max = core.getStackSize();
86 for (auto buf : buffers[tiles[srcCoord]]) {
87 int bufferBaseAddr = getBufferBaseAddress(buf);
88 int numBytes = buf.getAllocationSize();
89 max = std::max(max, bufferBaseAddr + numBytes);
90 }
91 int origin = targetModel.getMemInternalBaseAddress(srcCoord) + max;
92 int length = targetModel.getLocalMemorySize() - max;
93 output << R"THESCRIPT(
94MEMORY
95{
96 program (RX) : ORIGIN = 0, LENGTH = 0x0020000
97)THESCRIPT";
98 output << " data (!RX) : ORIGIN = 0x" << llvm::utohexstr(origin)
99 << ", LENGTH = 0x" << llvm::utohexstr(length);
100 output << R"THESCRIPT(
101}
102ENTRY(__start)
103SECTIONS
104{
105 . = 0x0;
106 .text : {
107 /* the __start symbol has to come at address zero. */
108 *crt0.o(.text*)
109 _ctors_start = .;
110 _init_array_start = .;
111 KEEP(SORT(*.init_array))
112 _ctors_end = .;
113 _init_array_end = .;
114 _dtors_start = .;
115 _dtors_end = .;
116 *(.text*)
117 } > program
118 .data : {
119 *(.data*)
120 *(.rodata*)
121 } > data
122 .comment : {
123 *(.comment*)
124 }
125 .symtab : {
126 *(.symtab)
127 }
128 .shstrtab : {
129 *(.shstrtab)
130 }
131 .strtab : {
132 *(.strtab)
133 }
134
135)THESCRIPT";
136 auto doBuffer = [&](std::optional<TileID> tile, int offset,
137 std::string dir) {
138 if (tile) {
139 if (tiles.count(*tile))
140 for (auto buf : buffers[tiles[*tile]])
141 writeLDScriptMap(output, buf, offset);
142 } else {
143 output << "/* No tile with memory exists to the " << dir << ". */\n";
144 output << ". = 0x" << llvm::utohexstr(offset) << ";\n";
145 uint32_t localMemSize = targetModel.getLocalMemorySize();
146 output << ". += 0x" << llvm::utohexstr(localMemSize) << ";\n";
147 }
148 };
149
150 // Stack
151 output << ". = 0x"
152 << llvm::utohexstr(targetModel.getMemInternalBaseAddress(srcCoord))
153 << ";\n";
154 output << "_sp_start_value_DM_stack = .;\n";
155
156 if (auto core = tile.getCoreOp())
157 output << ". += 0x" << llvm::utohexstr(core.getStackSize())
158 << "; /* stack */\n";
159 else
160 output << "/* no stack allocated */\n";
161
162 doBuffer(targetModel.getMemSouth(srcCoord),
163 targetModel.getMemSouthBaseAddress(), std::string("south"));
164 doBuffer(targetModel.getMemWest(srcCoord),
165 targetModel.getMemWestBaseAddress(), std::string("west"));
166 doBuffer(targetModel.getMemNorth(srcCoord),
167 targetModel.getMemNorthBaseAddress(), std::string("north"));
168 doBuffer(targetModel.getMemEast(srcCoord),
169 targetModel.getMemEastBaseAddress(), std::string("east"));
170
171 output << " .bss : { *(.bss*) } > data\n";
172 output << "}\n";
173 if (auto coreOp = tile.getCoreOp()) {
174 if (auto fileAttr = coreOp.getLinkWith())
175 output << "INPUT(" << fileAttr.value().str() << ")\n";
176
177 output << "PROVIDE(main = core_" << tile.getCol() << "_"
178 << tile.getRow() << ");\n";
179 }
180 }
181 return success();
182}
Include the generated interface declarations.
TileID { friend std::ostream &operator<<(std::ostream &os, const TileID &s) { os<< "TileID("<< s.col<< ", "<< s.row<< ")" TileID
int32_t getBufferBaseAddress(mlir::Operation *bufOp)
mlir::LogicalResult AIETranslateToLdScript(mlir::ModuleOp module, llvm::raw_ostream &output, int tileCol, int tileRow)
const AIETargetModel & getTargetModel(mlir::Operation *op)