@@ -229,19 +229,16 @@ LLVMPointerType::verify(function_ref<InFlightDiagnostic()> emitError,
229
229
return success ();
230
230
}
231
231
232
- namespace {
233
- // / The positions of different values in the data layout entry.
234
- enum class DLEntryPos { Size = 0 , Abi = 1 , Preferred = 2 , Address = 3 };
235
- } // namespace
236
-
237
232
constexpr const static unsigned kDefaultPointerSizeBits = 64 ;
238
233
constexpr const static unsigned kDefaultPointerAlignment = 8 ;
239
234
240
- // / Returns the value that corresponds to named position `pos` from the
241
- // / attribute `attr` assuming it's a dense integer elements attribute.
242
- static unsigned extractPointerSpecValue (Attribute attr, DLEntryPos pos) {
243
- return attr.cast <DenseIntElementsAttr>()
244
- .getValues <unsigned >()[static_cast <unsigned >(pos)];
235
+ Optional<unsigned > mlir::LLVM::extractPointerSpecValue (Attribute attr,
236
+ PtrDLEntryPos pos) {
237
+ auto spec = attr.cast <DenseIntElementsAttr>();
238
+ auto idx = static_cast <unsigned >(pos);
239
+ if (idx >= spec.size ())
240
+ return None;
241
+ return spec.getValues <unsigned >()[idx];
245
242
}
246
243
247
244
// / Returns the part of the data layout entry that corresponds to `pos` for the
@@ -250,7 +247,7 @@ static unsigned extractPointerSpecValue(Attribute attr, DLEntryPos pos) {
250
247
// / do not provide a custom one, for other address spaces returns None.
251
248
static Optional<unsigned >
252
249
getPointerDataLayoutEntry (DataLayoutEntryListRef params, LLVMPointerType type,
253
- DLEntryPos pos) {
250
+ PtrDLEntryPos pos) {
254
251
// First, look for the entry for the pointer in the current address space.
255
252
Attribute currentEntry;
256
253
for (DataLayoutEntryInterface entry : params) {
@@ -263,15 +260,15 @@ getPointerDataLayoutEntry(DataLayoutEntryListRef params, LLVMPointerType type,
263
260
}
264
261
}
265
262
if (currentEntry) {
266
- return extractPointerSpecValue (currentEntry, pos) /
267
- (pos == DLEntryPos ::Size ? 1 : kBitsInByte );
263
+ return * extractPointerSpecValue (currentEntry, pos) /
264
+ (pos == PtrDLEntryPos ::Size ? 1 : kBitsInByte );
268
265
}
269
266
270
267
// If not found, and this is the pointer to the default memory space, assume
271
268
// 64-bit pointers.
272
269
if (type.getAddressSpace () == 0 ) {
273
- return pos == DLEntryPos ::Size ? kDefaultPointerSizeBits
274
- : kDefaultPointerAlignment ;
270
+ return pos == PtrDLEntryPos ::Size ? kDefaultPointerSizeBits
271
+ : kDefaultPointerAlignment ;
275
272
}
276
273
277
274
return llvm::None;
@@ -281,7 +278,7 @@ unsigned
281
278
LLVMPointerType::getTypeSizeInBits (const DataLayout &dataLayout,
282
279
DataLayoutEntryListRef params) const {
283
280
if (Optional<unsigned > size =
284
- getPointerDataLayoutEntry (params, *this , DLEntryPos ::Size))
281
+ getPointerDataLayoutEntry (params, *this , PtrDLEntryPos ::Size))
285
282
return *size;
286
283
287
284
// For other memory spaces, use the size of the pointer to the default memory
@@ -294,7 +291,7 @@ LLVMPointerType::getTypeSizeInBits(const DataLayout &dataLayout,
294
291
unsigned LLVMPointerType::getABIAlignment (const DataLayout &dataLayout,
295
292
DataLayoutEntryListRef params) const {
296
293
if (Optional<unsigned > alignment =
297
- getPointerDataLayoutEntry (params, *this , DLEntryPos ::Abi))
294
+ getPointerDataLayoutEntry (params, *this , PtrDLEntryPos ::Abi))
298
295
return *alignment;
299
296
300
297
if (isOpaque ())
@@ -306,7 +303,7 @@ unsigned
306
303
LLVMPointerType::getPreferredAlignment (const DataLayout &dataLayout,
307
304
DataLayoutEntryListRef params) const {
308
305
if (Optional<unsigned > alignment =
309
- getPointerDataLayoutEntry (params, *this , DLEntryPos ::Preferred))
306
+ getPointerDataLayoutEntry (params, *this , PtrDLEntryPos ::Preferred))
310
307
return *alignment;
311
308
312
309
if (isOpaque ())
@@ -339,13 +336,13 @@ bool LLVMPointerType::areCompatible(DataLayoutEntryListRef oldLayout,
339
336
});
340
337
}
341
338
if (it != oldLayout.end ()) {
342
- size = extractPointerSpecValue (*it, DLEntryPos ::Size);
343
- abi = extractPointerSpecValue (*it, DLEntryPos ::Abi);
339
+ size = * extractPointerSpecValue (*it, PtrDLEntryPos ::Size);
340
+ abi = * extractPointerSpecValue (*it, PtrDLEntryPos ::Abi);
344
341
}
345
342
346
343
Attribute newSpec = newEntry.getValue ().cast <DenseIntElementsAttr>();
347
- unsigned newSize = extractPointerSpecValue (newSpec, DLEntryPos ::Size);
348
- unsigned newAbi = extractPointerSpecValue (newSpec, DLEntryPos ::Abi);
344
+ unsigned newSize = * extractPointerSpecValue (newSpec, PtrDLEntryPos ::Size);
345
+ unsigned newAbi = * extractPointerSpecValue (newSpec, PtrDLEntryPos ::Abi);
349
346
if (size != newSize || abi < newAbi || abi % newAbi != 0 )
350
347
return false ;
351
348
}
@@ -369,8 +366,8 @@ LogicalResult LLVMPointerType::verifyEntries(DataLayoutEntryListRef entries,
369
366
return emitError (loc) << " unexpected layout attribute for pointer to "
370
367
<< key.getElementType ();
371
368
}
372
- if (extractPointerSpecValue (values, DLEntryPos ::Abi) >
373
- extractPointerSpecValue (values, DLEntryPos ::Preferred)) {
369
+ if (extractPointerSpecValue (values, PtrDLEntryPos ::Abi) >
370
+ extractPointerSpecValue (values, PtrDLEntryPos ::Preferred)) {
374
371
return emitError (loc) << " preferred alignment is expected to be at least "
375
372
" as large as ABI alignment" ;
376
373
}
0 commit comments