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 .stack_sizes : {
135 *(.stack_sizes)
136 }
137
138)THESCRIPT";
139 auto doBuffer = [&](std::optional<TileID> tile, int offset,
140 std::string dir) {
141 if (tile) {
142 if (tiles.count(*tile))
143 for (auto buf : buffers[tiles[*tile]])
144 writeLDScriptMap(output, buf, offset);
145 } else {
146 output << "/* No tile with memory exists to the " << dir << ". */\n";
147 output << ". = 0x" << llvm::utohexstr(offset) << ";\n";
148 uint32_t localMemSize = targetModel.getLocalMemorySize();
149 output << ". += 0x" << llvm::utohexstr(localMemSize) << ";\n";
150 }
151 };
152
153 // Stack
154 output << ". = 0x"
155 << llvm::utohexstr(targetModel.getMemInternalBaseAddress(srcCoord))
156 << ";\n";
157 output << "_sp_start_value_DM_stack = .;\n";
158
159 if (auto core = tile.getCoreOp())
160 output << ". += 0x" << llvm::utohexstr(core.getStackSize())
161 << "; /* stack */\n";
162 else
163 output << "/* no stack allocated */\n";
164
165 doBuffer(targetModel.getMemSouth(srcCoord),
166 targetModel.getMemSouthBaseAddress(), std::string("south"));
167 doBuffer(targetModel.getMemWest(srcCoord),
168 targetModel.getMemWestBaseAddress(), std::string("west"));
169 doBuffer(targetModel.getMemNorth(srcCoord),
170 targetModel.getMemNorthBaseAddress(), std::string("north"));
171 doBuffer(targetModel.getMemEast(srcCoord),
172 targetModel.getMemEastBaseAddress(), std::string("east"));
173
174 output << " .bss : { *(.bss*) } > data\n";
175 output << "}\n";
176 if (auto coreOp = tile.getCoreOp()) {
177 if (auto fileAttr = coreOp.getLinkWith())
178 output << "INPUT(" << fileAttr.value().str() << ")\n";
179
180 output << "PROVIDE(main = core_" << tile.getCol() << "_"
181 << tile.getRow() << ");\n";
182 }
183 }
184 return success();
185}
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)