-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
DataLayoutImporter.cpp
330 lines (290 loc) · 10.7 KB
/
DataLayoutImporter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//===- DataLayoutImporter.cpp - LLVM to MLIR data layout conversion -------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "DataLayoutImporter.h"
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/Interfaces/DataLayoutInterfaces.h"
#include "mlir/Target/LLVMIR/Import.h"
#include "llvm/IR/DataLayout.h"
using namespace mlir;
using namespace mlir::LLVM;
using namespace mlir::LLVM::detail;
/// The default data layout used during the translation.
static constexpr StringRef kDefaultDataLayout =
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-"
"f16:16:16-f64:64:64-f128:128:128";
FloatType mlir::LLVM::detail::getFloatType(MLIRContext *context,
unsigned width) {
switch (width) {
case 16:
return FloatType::getF16(context);
case 32:
return FloatType::getF32(context);
case 64:
return FloatType::getF64(context);
case 80:
return FloatType::getF80(context);
case 128:
return FloatType::getF128(context);
default:
return {};
}
}
FailureOr<StringRef>
DataLayoutImporter::tryToParseAlphaPrefix(StringRef &token) const {
if (token.empty())
return failure();
StringRef prefix = token.take_while(isalpha);
if (prefix.empty())
return failure();
token.consume_front(prefix);
return prefix;
}
FailureOr<uint64_t> DataLayoutImporter::tryToParseInt(StringRef &token) const {
uint64_t parameter;
if (token.consumeInteger(/*Radix=*/10, parameter))
return failure();
return parameter;
}
FailureOr<SmallVector<uint64_t>>
DataLayoutImporter::tryToParseIntList(StringRef token) const {
SmallVector<StringRef> tokens;
token.consume_front(":");
token.split(tokens, ':');
// Parse an integer list.
SmallVector<uint64_t> results(tokens.size());
for (auto [result, token] : llvm::zip(results, tokens))
if (token.getAsInteger(/*Radix=*/10, result))
return failure();
return results;
}
FailureOr<DenseIntElementsAttr>
DataLayoutImporter::tryToParseAlignment(StringRef token) const {
FailureOr<SmallVector<uint64_t>> alignment = tryToParseIntList(token);
if (failed(alignment))
return failure();
if (alignment->empty() || alignment->size() > 2)
return failure();
// Alignment specifications (such as 32 or 32:64) are of the
// form <abi>[:<pref>], where abi specifies the minimal alignment and pref the
// optional preferred alignment. The preferred alignment is set to the minimal
// alignment if not available.
uint64_t minimal = (*alignment)[0];
uint64_t preferred = alignment->size() == 1 ? minimal : (*alignment)[1];
return DenseIntElementsAttr::get(
VectorType::get({2}, IntegerType::get(context, 64)),
{minimal, preferred});
}
FailureOr<DenseIntElementsAttr>
DataLayoutImporter::tryToParsePointerAlignment(StringRef token) const {
FailureOr<SmallVector<uint64_t>> alignment = tryToParseIntList(token);
if (failed(alignment))
return failure();
if (alignment->size() < 2 || alignment->size() > 4)
return failure();
// Pointer alignment specifications (such as 64:32:64:32 or 32:32) are of
// the form <size>:<abi>[:<pref>][:<idx>], where size is the pointer size, abi
// specifies the minimal alignment, pref the optional preferred alignment, and
// idx the optional index computation bit width. The preferred alignment is
// set to the minimal alignment if not available and the index computation
// width is set to the pointer size if not available.
uint64_t size = (*alignment)[0];
uint64_t minimal = (*alignment)[1];
uint64_t preferred = alignment->size() < 3 ? minimal : (*alignment)[2];
uint64_t idx = alignment->size() < 4 ? size : (*alignment)[3];
return DenseIntElementsAttr::get<uint64_t>(
VectorType::get({4}, IntegerType::get(context, 64)),
{size, minimal, preferred, idx});
}
LogicalResult DataLayoutImporter::tryToEmplaceAlignmentEntry(Type type,
StringRef token) {
auto key = TypeAttr::get(type);
if (typeEntries.count(key))
return success();
FailureOr<DenseIntElementsAttr> params = tryToParseAlignment(token);
if (failed(params))
return failure();
typeEntries.try_emplace(key, DataLayoutEntryAttr::get(type, *params));
return success();
}
LogicalResult
DataLayoutImporter::tryToEmplacePointerAlignmentEntry(LLVMPointerType type,
StringRef token) {
auto key = TypeAttr::get(type);
if (typeEntries.count(key))
return success();
FailureOr<DenseIntElementsAttr> params = tryToParsePointerAlignment(token);
if (failed(params))
return failure();
typeEntries.try_emplace(key, DataLayoutEntryAttr::get(type, *params));
return success();
}
LogicalResult
DataLayoutImporter::tryToEmplaceEndiannessEntry(StringRef endianness,
StringRef token) {
auto key = StringAttr::get(context, DLTIDialect::kDataLayoutEndiannessKey);
if (keyEntries.count(key))
return success();
if (!token.empty())
return failure();
keyEntries.try_emplace(
key, DataLayoutEntryAttr::get(key, StringAttr::get(context, endianness)));
return success();
}
LogicalResult
DataLayoutImporter::tryToEmplaceAddrSpaceEntry(StringRef token,
llvm::StringLiteral spaceKey) {
auto key = StringAttr::get(context, spaceKey);
if (keyEntries.count(key))
return success();
FailureOr<uint64_t> space = tryToParseInt(token);
if (failed(space))
return failure();
// Only store the address space if it has a non-default value.
if (*space == 0)
return success();
OpBuilder builder(context);
keyEntries.try_emplace(
key,
DataLayoutEntryAttr::get(
key, builder.getIntegerAttr(
builder.getIntegerType(64, /*isSigned=*/false), *space)));
return success();
}
LogicalResult
DataLayoutImporter::tryToEmplaceStackAlignmentEntry(StringRef token) {
auto key =
StringAttr::get(context, DLTIDialect::kDataLayoutStackAlignmentKey);
if (keyEntries.count(key))
return success();
FailureOr<uint64_t> alignment = tryToParseInt(token);
if (failed(alignment))
return failure();
// Only store the stack alignment if it has a non-default value.
if (*alignment == 0)
return success();
OpBuilder builder(context);
keyEntries.try_emplace(key, DataLayoutEntryAttr::get(
key, builder.getI64IntegerAttr(*alignment)));
return success();
}
void DataLayoutImporter::translateDataLayout(
const llvm::DataLayout &llvmDataLayout) {
dataLayout = {};
// Transform the data layout to its string representation and append the
// default data layout string specified in the language reference
// (https://llvm.org/docs/LangRef.html#data-layout). The translation then
// parses the string and ignores the default value if a specific kind occurs
// in both strings. Additionally, the following default values exist:
// - non-default address space pointer specifications default to the default
// address space pointer specification
// - the alloca address space defaults to the default address space.
layoutStr = llvmDataLayout.getStringRepresentation();
if (!layoutStr.empty())
layoutStr += "-";
layoutStr += kDefaultDataLayout;
StringRef layout(layoutStr);
// Split the data layout string into tokens separated by a dash.
SmallVector<StringRef> tokens;
layout.split(tokens, '-');
for (StringRef token : tokens) {
lastToken = token;
FailureOr<StringRef> prefix = tryToParseAlphaPrefix(token);
if (failed(prefix))
return;
// Parse the endianness.
if (*prefix == "e") {
if (failed(tryToEmplaceEndiannessEntry(
DLTIDialect::kDataLayoutEndiannessLittle, token)))
return;
continue;
}
if (*prefix == "E") {
if (failed(tryToEmplaceEndiannessEntry(
DLTIDialect::kDataLayoutEndiannessBig, token)))
return;
continue;
}
// Parse the program address space.
if (*prefix == "P") {
if (failed(tryToEmplaceAddrSpaceEntry(
token, DLTIDialect::kDataLayoutProgramMemorySpaceKey)))
return;
continue;
}
// Parse the global address space.
if (*prefix == "G") {
if (failed(tryToEmplaceAddrSpaceEntry(
token, DLTIDialect::kDataLayoutGlobalMemorySpaceKey)))
return;
continue;
}
// Parse the alloca address space.
if (*prefix == "A") {
if (failed(tryToEmplaceAddrSpaceEntry(
token, DLTIDialect::kDataLayoutAllocaMemorySpaceKey)))
return;
continue;
}
// Parse the stack alignment.
if (*prefix == "S") {
if (failed(tryToEmplaceStackAlignmentEntry(token)))
return;
continue;
}
// Parse integer alignment specifications.
if (*prefix == "i") {
FailureOr<uint64_t> width = tryToParseInt(token);
if (failed(width))
return;
Type type = IntegerType::get(context, *width);
if (failed(tryToEmplaceAlignmentEntry(type, token)))
return;
continue;
}
// Parse float alignment specifications.
if (*prefix == "f") {
FailureOr<uint64_t> width = tryToParseInt(token);
if (failed(width))
return;
Type type = getFloatType(context, *width);
if (failed(tryToEmplaceAlignmentEntry(type, token)))
return;
continue;
}
// Parse pointer alignment specifications.
if (*prefix == "p") {
FailureOr<uint64_t> space =
token.starts_with(":") ? 0 : tryToParseInt(token);
if (failed(space))
return;
auto type = LLVMPointerType::get(context, *space);
if (failed(tryToEmplacePointerAlignmentEntry(type, token)))
return;
continue;
}
// Store all tokens that have not been handled.
unhandledTokens.push_back(lastToken);
}
// Assemble all entries to a data layout specification.
SmallVector<DataLayoutEntryInterface> entries;
entries.reserve(typeEntries.size() + keyEntries.size());
for (const auto &it : typeEntries)
entries.push_back(it.second);
for (const auto &it : keyEntries)
entries.push_back(it.second);
dataLayout = DataLayoutSpecAttr::get(context, entries);
}
DataLayoutSpecInterface
mlir::translateDataLayout(const llvm::DataLayout &dataLayout,
MLIRContext *context) {
return DataLayoutImporter(context, dataLayout).getDataLayout();
}