MLIR 24.0.0git
DataLayoutInterfaces.cpp
Go to the documentation of this file.
1//===- DataLayoutInterfaces.cpp - Data Layout Interface Implementation ----===//
2//
3// Part of the LLVM Project, 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//===----------------------------------------------------------------------===//
8
11#include "mlir/IR/BuiltinOps.h"
13#include "mlir/IR/Operation.h"
14
15#include "llvm/ADT/TypeSwitch.h"
16#include "llvm/Support/MathExtras.h"
17
18using namespace mlir;
19
20//===----------------------------------------------------------------------===//
21// Default implementations
22//===----------------------------------------------------------------------===//
23
24/// Reports that the given type is missing the data layout information and
25/// exits.
26[[noreturn]] static void reportMissingDataLayout(Type type) {
27 std::string message;
28 llvm::raw_string_ostream os(message);
29 os << "neither the scoping op nor the type class provide data layout "
30 "information for "
31 << type;
32 llvm::report_fatal_error(Twine(message));
33}
34
35/// Returns the bitwidth of the index type if specified in the param list.
36/// Assumes 64-bit index otherwise.
38 if (params.empty())
39 return 64;
40 auto attr = cast<IntegerAttr>(params.front().getValue());
41 return attr.getValue().getZExtValue();
42}
43
44llvm::TypeSize
47 llvm::TypeSize bits = getDefaultTypeSizeInBits(type, dataLayout, params);
48 return divideCeil(bits, 8);
49}
50
51llvm::TypeSize
54 if (type.isIntOrFloat())
55 return llvm::TypeSize::getFixed(type.getIntOrFloatBitWidth());
56
57 if (auto ctype = dyn_cast<ComplexType>(type)) {
58 Type et = ctype.getElementType();
59 uint64_t innerAlignment =
60 getDefaultPreferredAlignment(et, dataLayout, params) * 8;
61 llvm::TypeSize innerSize = getDefaultTypeSizeInBits(et, dataLayout, params);
62
63 // Include padding required to align the imaginary value in the complex
64 // type.
65 return llvm::alignTo(innerSize, innerAlignment) + innerSize;
66 }
67
68 // Index is an integer of some bitwidth.
69 if (isa<IndexType>(type))
70 return dataLayout.getTypeSizeInBits(
71 IntegerType::get(type.getContext(), getIndexBitwidth(params)));
72
73 // Sizes of vector types are rounded up to those of types with closest
74 // power-of-two number of elements in the innermost dimension.
75 // TODO: make this extensible.
76 if (auto vecType = dyn_cast<VectorType>(type)) {
77 uint64_t baseSize = vecType.getNumElements() / vecType.getShape().back() *
78 llvm::PowerOf2Ceil(vecType.getShape().back()) *
79 dataLayout.getTypeSizeInBits(vecType.getElementType());
80 return llvm::TypeSize::get(baseSize, vecType.isScalable());
81 }
82
83 if (auto typeInterface = dyn_cast<DataLayoutTypeInterface>(type))
84 return typeInterface.getTypeSizeInBits(dataLayout, params);
85
87}
88
89static DataLayoutEntryInterface
90findEntryForIntegerType(IntegerType intType,
92 assert(!params.empty() && "expected non-empty parameter list");
93 std::map<unsigned, DataLayoutEntryInterface> sortedParams;
94 for (DataLayoutEntryInterface entry : params) {
95 sortedParams.insert(std::make_pair(
96 cast<Type>(entry.getKey()).getIntOrFloatBitWidth(), entry));
97 }
98 auto iter = sortedParams.lower_bound(intType.getWidth());
99 if (iter == sortedParams.end())
100 iter = std::prev(iter);
101
102 return iter->second;
103}
104
105constexpr const static uint64_t kDefaultBitsInByte = 8u;
106
107static uint64_t extractABIAlignment(DataLayoutEntryInterface entry) {
108 auto values =
109 cast<DenseIntElementsAttr>(entry.getValue()).getValues<uint64_t>();
110 return static_cast<uint64_t>(*values.begin()) / kDefaultBitsInByte;
111}
112
113static uint64_t
114getIntegerTypeABIAlignment(IntegerType intType,
116 constexpr uint64_t kDefaultSmallIntAlignment = 4u;
117 constexpr unsigned kSmallIntSize = 64;
118 if (params.empty()) {
119 return intType.getWidth() < kSmallIntSize
120 ? llvm::PowerOf2Ceil(
121 llvm::divideCeil(intType.getWidth(), kDefaultBitsInByte))
122 : kDefaultSmallIntAlignment;
123 }
124
125 return extractABIAlignment(findEntryForIntegerType(intType, params));
126}
127
128static uint64_t
129getFloatTypeABIAlignment(FloatType fltType, const DataLayout &dataLayout,
131 assert(params.size() <= 1 && "at most one data layout entry is expected for "
132 "the singleton floating-point type");
133 if (params.empty())
134 return llvm::PowerOf2Ceil(dataLayout.getTypeSize(fltType).getFixedValue());
135 return extractABIAlignment(params[0]);
136}
137
139 Type type, const DataLayout &dataLayout,
141 // Natural alignment is the closest power-of-two number above. For scalable
142 // vectors, aligning them to the same as the base vector is sufficient.
143 if (isa<VectorType>(type))
144 return llvm::PowerOf2Ceil(dataLayout.getTypeSize(type).getKnownMinValue());
145
146 if (auto fltType = dyn_cast<FloatType>(type))
147 return getFloatTypeABIAlignment(fltType, dataLayout, params);
148
149 // Index is an integer of some bitwidth.
150 if (isa<IndexType>(type))
151 return dataLayout.getTypeABIAlignment(
152 IntegerType::get(type.getContext(), getIndexBitwidth(params)));
153
154 if (auto intType = dyn_cast<IntegerType>(type))
155 return getIntegerTypeABIAlignment(intType, params);
156
157 if (auto ctype = dyn_cast<ComplexType>(type))
158 return getDefaultABIAlignment(ctype.getElementType(), dataLayout, params);
159
160 if (auto typeInterface = dyn_cast<DataLayoutTypeInterface>(type))
161 return typeInterface.getABIAlignment(dataLayout, params);
162
164}
165
166static uint64_t extractPreferredAlignment(DataLayoutEntryInterface entry) {
167 auto values =
168 cast<DenseIntElementsAttr>(entry.getValue()).getValues<uint64_t>();
169 return *std::next(values.begin(), values.size() - 1) / kDefaultBitsInByte;
170}
171
172static uint64_t
174 const DataLayout &dataLayout,
176 if (params.empty())
177 return llvm::PowerOf2Ceil(dataLayout.getTypeSize(intType).getFixedValue());
178
179 return extractPreferredAlignment(findEntryForIntegerType(intType, params));
180}
181
182static uint64_t
183getFloatTypePreferredAlignment(FloatType fltType, const DataLayout &dataLayout,
185 assert(params.size() <= 1 && "at most one data layout entry is expected for "
186 "the singleton floating-point type");
187 if (params.empty())
188 return dataLayout.getTypeABIAlignment(fltType);
189 return extractPreferredAlignment(params[0]);
190}
191
193 Type type, const DataLayout &dataLayout,
195 // Preferred alignment is same as natural for floats and vectors.
196 if (isa<VectorType>(type))
197 return dataLayout.getTypeABIAlignment(type);
198
199 if (auto fltType = dyn_cast<FloatType>(type))
200 return getFloatTypePreferredAlignment(fltType, dataLayout, params);
201
202 // Preferred alignment is the closest power-of-two number above for integers
203 // (ABI alignment may be smaller).
204 if (auto intType = dyn_cast<IntegerType>(type))
205 return getIntegerTypePreferredAlignment(intType, dataLayout, params);
206
207 if (isa<IndexType>(type)) {
208 return dataLayout.getTypePreferredAlignment(
209 IntegerType::get(type.getContext(), getIndexBitwidth(params)));
210 }
211
212 if (auto ctype = dyn_cast<ComplexType>(type))
213 return getDefaultPreferredAlignment(ctype.getElementType(), dataLayout,
214 params);
215
216 if (auto typeInterface = dyn_cast<DataLayoutTypeInterface>(type))
217 return typeInterface.getPreferredAlignment(dataLayout, params);
218
220}
221
223 Type type, const DataLayout &dataLayout,
225 if (isa<IndexType>(type))
226 return getIndexBitwidth(params);
227
228 if (auto typeInterface = dyn_cast<DataLayoutTypeInterface>(type))
229 if (std::optional<uint64_t> indexBitwidth =
230 typeInterface.getIndexBitwidth(dataLayout, params))
231 return *indexBitwidth;
232
233 // Return std::nullopt for all other types, which are assumed to be non
234 // pointer-like types.
235 return std::nullopt;
236}
237
238// Returns the endianness if specified in the given entry. If the entry is empty
239// the default endianness represented by an empty attribute is returned.
240Attribute mlir::detail::getDefaultEndianness(DataLayoutEntryInterface entry) {
241 if (entry == DataLayoutEntryInterface())
242 return Attribute();
243
244 return entry.getValue();
245}
246
247// Returns the default memory space if specified in the given entry. If the
248// entry is empty the default memory space represented by an empty attribute is
249// returned.
250Attribute mlir::detail::getDefaultMemorySpace(DataLayoutEntryInterface entry) {
251 if (!entry)
252 return Attribute();
253
254 return entry.getValue();
255}
256
257// Returns the memory space used for alloca operations if specified in the
258// given entry. If the entry is empty the default memory space represented by
259// an empty attribute is returned.
261mlir::detail::getDefaultAllocaMemorySpace(DataLayoutEntryInterface entry) {
262 if (entry == DataLayoutEntryInterface()) {
263 return Attribute();
264 }
265
266 return entry.getValue();
267}
268
269// Returns the mangling mode if specified in the given entry.
270// If the entry is empty, an empty attribute is returned.
271Attribute mlir::detail::getDefaultManglingMode(DataLayoutEntryInterface entry) {
272 if (entry == DataLayoutEntryInterface())
273 return Attribute();
274
275 return entry.getValue();
276}
277
278// Returns the memory space used for the program memory space. if
279// specified in the given entry. If the entry is empty the default
280// memory space represented by an empty attribute is returned.
282mlir::detail::getDefaultProgramMemorySpace(DataLayoutEntryInterface entry) {
283 if (entry == DataLayoutEntryInterface()) {
284 return Attribute();
285 }
286
287 return entry.getValue();
288}
289
290// Returns the memory space used for global the global memory space. if
291// specified in the given entry. If the entry is empty the default memory
292// space represented by an empty attribute is returned.
294mlir::detail::getDefaultGlobalMemorySpace(DataLayoutEntryInterface entry) {
295 if (entry == DataLayoutEntryInterface()) {
296 return Attribute();
297 }
298
299 return entry.getValue();
300}
301
302// Returns the stack alignment if specified in the given entry. If the entry is
303// empty the default alignment zero is returned.
304uint64_t
305mlir::detail::getDefaultStackAlignment(DataLayoutEntryInterface entry) {
306 if (entry == DataLayoutEntryInterface())
307 return 0;
308
309 auto value = cast<IntegerAttr>(entry.getValue());
310 return value.getValue().getZExtValue();
311}
312
313// Returns the function pointer alignment if specified in the given entry. If
314// the entry is empty the default alignment zero is returned.
316 DataLayoutEntryInterface entry) {
317 if (entry == DataLayoutEntryInterface())
318 return Attribute();
319 return entry.getValue();
320}
321
322// Returns the legal int widths if specified in the given entry. If the entry is
323// empty the default legal int widths represented by an empty attribute is
324// returned.
326mlir::detail::getDefaultLegalIntWidths(DataLayoutEntryInterface entry) {
327 if (entry == DataLayoutEntryInterface())
328 return Attribute();
329 return entry.getValue();
330}
331
332std::optional<Attribute>
333mlir::detail::getDevicePropertyValue(DataLayoutEntryInterface entry) {
334 if (entry == DataLayoutEntryInterface())
335 return std::nullopt;
336
337 return entry.getValue();
338}
339
342 TypeID typeID) {
343 return llvm::filter_to_vector<4>(
344 entries, [typeID](DataLayoutEntryInterface entry) {
345 auto type = llvm::dyn_cast_if_present<Type>(entry.getKey());
346 return type && type.getTypeID() == typeID;
347 });
348}
349
350DataLayoutEntryInterface
352 StringAttr id) {
353 const auto *it = llvm::find_if(entries, [id](DataLayoutEntryInterface entry) {
354 if (auto attr = dyn_cast<StringAttr>(entry.getKey()))
355 return attr == id;
356 return false;
357 });
358 return it == entries.end() ? DataLayoutEntryInterface() : *it;
359}
360
361static DataLayoutSpecInterface getSpec(Operation *operation) {
363 .Case<ModuleOp, DataLayoutOpInterface>(
364 [&](auto op) { return op.getDataLayoutSpec(); })
365 .DefaultUnreachable("expected an op with data layout spec");
366}
367
368static TargetSystemSpecInterface getTargetSystemSpec(Operation *operation) {
369 if (operation) {
370 ModuleOp moduleOp = dyn_cast<ModuleOp>(operation);
371 if (!moduleOp)
372 moduleOp = operation->getParentOfType<ModuleOp>();
373 return moduleOp.getTargetSystemSpec();
374 }
375 return TargetSystemSpecInterface();
376}
377
378/// Populates `opsWithLayout` with the list of proper ancestors of `leaf` that
379/// are either modules or implement the `DataLayoutOpInterface`.
380static void
383 SmallVectorImpl<Location> *opLocations = nullptr) {
384 if (!leaf)
385 return;
386
387 for (Operation *parent = leaf->getParentOp(); parent != nullptr;
388 parent = parent->getParentOp()) {
390 .Case([&](ModuleOp op) {
391 // Skip top-level module op unless it has a layout. Top-level module
392 // without layout is most likely the one implicitly added by the
393 // parser and it doesn't have location. Top-level null specification
394 // would have had the same effect as not having a specification at all
395 // (using type defaults).
396 if (!op->getParentOp() && !op.getDataLayoutSpec())
397 return;
398 specs.push_back(op.getDataLayoutSpec());
399 if (opLocations)
400 opLocations->push_back(op.getLoc());
401 })
402 .Case([&](DataLayoutOpInterface op) {
403 specs.push_back(op.getDataLayoutSpec());
404 if (opLocations)
405 opLocations->push_back(op.getLoc());
406 });
407 }
408}
409
410/// Returns a layout spec that is a combination of the layout specs attached
411/// to the given operation and all its ancestors.
412static DataLayoutSpecInterface getCombinedDataLayout(Operation *leaf) {
413 if (!leaf)
414 return {};
415
416 assert((isa<ModuleOp, DataLayoutOpInterface>(leaf)) &&
417 "expected an op with data layout spec");
418
420 collectParentLayouts(leaf, specs);
421
422 // Fast track if there are no ancestors.
423 if (specs.empty())
424 return getSpec(leaf);
425
426 // Create the list of non-null specs (null/missing specs can be safely
427 // ignored) from the outermost to the innermost.
428 auto nonNullSpecs = llvm::filter_to_vector<2>(
429 llvm::reverse(specs),
430 [](DataLayoutSpecInterface iface) { return iface != nullptr; });
431
432 // Combine the specs using the innermost as anchor.
433 if (DataLayoutSpecInterface current = getSpec(leaf))
434 return current.combineWith(nonNullSpecs);
435 if (nonNullSpecs.empty())
436 return {};
437 return nonNullSpecs.back().combineWith(
438 llvm::ArrayRef(nonNullSpecs).drop_back());
439}
440
442 DataLayoutSpecInterface spec = getSpec(op);
443 // The layout specification may be missing and it's fine.
444 if (!spec)
445 return success();
446
447 if (failed(spec.verifySpec(op->getLoc())))
448 return failure();
449 if (!getCombinedDataLayout(op)) {
451 op->emitError()
452 << "data layout does not combine with layouts of enclosing ops";
454 SmallVector<Location> opLocations;
455 collectParentLayouts(op, specs, &opLocations);
456 for (Location loc : opLocations)
457 diag.attachNote(loc) << "enclosing op with data layout";
458 return diag;
459 }
460 return success();
461}
462
463llvm::TypeSize mlir::detail::divideCeil(llvm::TypeSize numerator,
464 uint64_t denominator) {
465 uint64_t divided =
466 llvm::divideCeil(numerator.getKnownMinValue(), denominator);
467 return llvm::TypeSize::get(divided, numerator.isScalable());
468}
469
470//===----------------------------------------------------------------------===//
471// DataLayout
472//===----------------------------------------------------------------------===//
473
474template <typename OpTy>
475void checkMissingLayout(DataLayoutSpecInterface originalLayout, OpTy op) {
476 if (!originalLayout) {
477 assert((!op || !op.getDataLayoutSpec()) &&
478 "could not compute layout information for an op (failed to "
479 "combine attributes?)");
480 }
481}
482
484
485mlir::DataLayout::DataLayout(DataLayoutOpInterface op)
486 : originalLayout(getCombinedDataLayout(op)),
487 originalTargetSystemDesc(getTargetSystemSpec(op)), scope(op),
488 allocaMemorySpace(std::nullopt), programMemorySpace(std::nullopt),
489 globalMemorySpace(std::nullopt), stackAlignment(std::nullopt) {
490#if LLVM_ENABLE_ABI_BREAKING_CHECKS
491 checkMissingLayout(originalLayout, op);
492 collectParentLayouts(op, layoutStack);
493#endif
494}
495
497 : originalLayout(getCombinedDataLayout(op)),
498 originalTargetSystemDesc(getTargetSystemSpec(op)), scope(op),
499 allocaMemorySpace(std::nullopt), programMemorySpace(std::nullopt),
500 globalMemorySpace(std::nullopt), stackAlignment(std::nullopt) {
501#if LLVM_ENABLE_ABI_BREAKING_CHECKS
502 checkMissingLayout(originalLayout, op);
503 collectParentLayouts(op, layoutStack);
504#endif
505}
506
508 // Search the closest parent either being a module operation or implementing
509 // the data layout interface.
510 while (op) {
511 if (auto module = dyn_cast<ModuleOp>(op))
512 return DataLayout(module);
513 if (auto iface = dyn_cast<DataLayoutOpInterface>(op))
514 return DataLayout(iface);
515 op = op->getParentOp();
516 }
517 return DataLayout();
518}
519
520void mlir::DataLayout::checkValid() const {
521#if LLVM_ENABLE_ABI_BREAKING_CHECKS
523 collectParentLayouts(scope, specs);
524 assert(specs.size() == layoutStack.size() &&
525 "data layout object used, but no longer valid due to the change in "
526 "number of nested layouts");
527 for (auto pair : llvm::zip(specs, layoutStack)) {
528 Attribute newLayout = std::get<0>(pair);
529 Attribute origLayout = std::get<1>(pair);
530 assert(newLayout == origLayout &&
531 "data layout object used, but no longer valid "
532 "due to the change in layout attributes");
533 }
534#endif
535 assert(((!scope && !this->originalLayout) ||
536 (scope && this->originalLayout == getCombinedDataLayout(scope))) &&
537 "data layout object used, but no longer valid due to the change in "
538 "layout spec");
539}
540
541/// Looks up the value for the given type key in the given cache. If there is no
542/// such value in the cache, compute it using the given callback and put it in
543/// the cache before returning.
544template <typename T>
546 function_ref<T(Type)> compute) {
547 auto it = cache.find(t);
548 if (it != cache.end())
549 return it->second;
550
551 auto result = cache.try_emplace(t, compute(t));
552 return result.first->second;
553}
554
556 checkValid();
557 if (!originalLayout)
558 return false;
559 return cachedLookup<bool>(type, typeEntries, [&](Type ty) {
560 return llvm::any_of(
561 originalLayout.getEntries(), [ty](DataLayoutEntryInterface entry) {
562 return llvm::dyn_cast_if_present<Type>(entry.getKey()) == ty;
563 });
564 });
565}
566
567llvm::TypeSize mlir::DataLayout::getTypeSize(Type t) const {
568 checkValid();
569 return cachedLookup<llvm::TypeSize>(t, sizes, [&](Type ty) {
571 if (originalLayout)
572 list = originalLayout.getSpecForType(ty.getTypeID());
573 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
574 return iface.getTypeSize(ty, *this, list);
575 return detail::getDefaultTypeSize(ty, *this, list);
576 });
577}
578
580 checkValid();
581 return cachedLookup<llvm::TypeSize>(t, bitsizes, [&](Type ty) {
583 if (originalLayout)
584 list = originalLayout.getSpecForType(ty.getTypeID());
585 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
586 return iface.getTypeSizeInBits(ty, *this, list);
587 return detail::getDefaultTypeSizeInBits(ty, *this, list);
588 });
589}
590
592 return llvm::alignTo(getTypeSize(t), getTypeABIAlignment(t));
593}
594
596 checkValid();
597 return cachedLookup<uint64_t>(t, abiAlignments, [&](Type ty) {
599 if (originalLayout)
600 list = originalLayout.getSpecForType(ty.getTypeID());
601 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
602 return iface.getTypeABIAlignment(ty, *this, list);
603 return detail::getDefaultABIAlignment(ty, *this, list);
604 });
605}
606
608 checkValid();
609 return cachedLookup<uint64_t>(t, preferredAlignments, [&](Type ty) {
611 if (originalLayout)
612 list = originalLayout.getSpecForType(ty.getTypeID());
613 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
614 return iface.getTypePreferredAlignment(ty, *this, list);
615 return detail::getDefaultPreferredAlignment(ty, *this, list);
616 });
617}
618
619std::optional<uint64_t> mlir::DataLayout::getTypeIndexBitwidth(Type t) const {
620 checkValid();
621 return cachedLookup<std::optional<uint64_t>>(t, indexBitwidths, [&](Type ty) {
623 if (originalLayout)
624 list = originalLayout.getSpecForType(ty.getTypeID());
625 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
626 return iface.getIndexBitwidth(ty, *this, list);
627 return detail::getDefaultIndexBitwidth(ty, *this, list);
628 });
629}
630
632 checkValid();
633 if (endianness)
634 return *endianness;
635 DataLayoutEntryInterface entry;
636 if (originalLayout)
637 entry = originalLayout.getSpecForIdentifier(
638 originalLayout.getEndiannessIdentifier(originalLayout.getContext()));
639
640 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
641 endianness = iface.getEndianness(entry);
642 else
643 endianness = detail::getDefaultEndianness(entry);
644 return *endianness;
645}
646
648 checkValid();
649 if (defaultMemorySpace)
650 return *defaultMemorySpace;
651 DataLayoutEntryInterface entry;
652 if (originalLayout)
653 entry = originalLayout.getSpecForIdentifier(
654 originalLayout.getDefaultMemorySpaceIdentifier(
655 originalLayout.getContext()));
656 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
657 defaultMemorySpace = iface.getDefaultMemorySpace(entry);
658 else
659 defaultMemorySpace = detail::getDefaultMemorySpace(entry);
660 return *defaultMemorySpace;
661}
662
664 checkValid();
665 if (allocaMemorySpace)
666 return *allocaMemorySpace;
667 DataLayoutEntryInterface entry;
668 if (originalLayout)
669 entry = originalLayout.getSpecForIdentifier(
670 originalLayout.getAllocaMemorySpaceIdentifier(
671 originalLayout.getContext()));
672 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
673 allocaMemorySpace = iface.getAllocaMemorySpace(entry);
674 else
675 allocaMemorySpace = detail::getDefaultAllocaMemorySpace(entry);
676 return *allocaMemorySpace;
677}
678
680 checkValid();
681 if (manglingMode)
682 return *manglingMode;
683 DataLayoutEntryInterface entry;
684 if (originalLayout)
685 entry = originalLayout.getSpecForIdentifier(
686 originalLayout.getManglingModeIdentifier(originalLayout.getContext()));
687
688 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
689 manglingMode = iface.getManglingMode(entry);
690 else
691 manglingMode = detail::getDefaultManglingMode(entry);
692 return *manglingMode;
693}
694
696 checkValid();
697 if (programMemorySpace)
698 return *programMemorySpace;
699 DataLayoutEntryInterface entry;
700 if (originalLayout)
701 entry = originalLayout.getSpecForIdentifier(
702 originalLayout.getProgramMemorySpaceIdentifier(
703 originalLayout.getContext()));
704 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
705 programMemorySpace = iface.getProgramMemorySpace(entry);
706 else
707 programMemorySpace = detail::getDefaultProgramMemorySpace(entry);
708 return *programMemorySpace;
709}
710
712 checkValid();
713 if (globalMemorySpace)
714 return *globalMemorySpace;
715 DataLayoutEntryInterface entry;
716 if (originalLayout)
717 entry = originalLayout.getSpecForIdentifier(
718 originalLayout.getGlobalMemorySpaceIdentifier(
719 originalLayout.getContext()));
720 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
721 globalMemorySpace = iface.getGlobalMemorySpace(entry);
722 else
723 globalMemorySpace = detail::getDefaultGlobalMemorySpace(entry);
724 return *globalMemorySpace;
725}
726
728 checkValid();
729 if (stackAlignment)
730 return *stackAlignment;
731 DataLayoutEntryInterface entry;
732 if (originalLayout)
733 entry = originalLayout.getSpecForIdentifier(
734 originalLayout.getStackAlignmentIdentifier(
735 originalLayout.getContext()));
736 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
737 stackAlignment = iface.getStackAlignment(entry);
738 else
739 stackAlignment = detail::getDefaultStackAlignment(entry);
740 return *stackAlignment;
741}
742
744 checkValid();
745 if (functionPointerAlignment)
746 return *functionPointerAlignment;
747 DataLayoutEntryInterface entry;
748 if (originalLayout)
749 entry = originalLayout.getSpecForIdentifier(
750 originalLayout.getFunctionPointerAlignmentIdentifier(
751 originalLayout.getContext()));
752 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
753 functionPointerAlignment = iface.getFunctionPointerAlignment(entry);
754 else
755 functionPointerAlignment =
757 return *functionPointerAlignment;
758}
759
761 checkValid();
762 if (legalIntWidths)
763 return *legalIntWidths;
764 DataLayoutEntryInterface entry;
765 if (originalLayout)
766 entry = originalLayout.getSpecForIdentifier(
767 originalLayout.getLegalIntWidthsIdentifier(
768 originalLayout.getContext()));
769 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
770 legalIntWidths = iface.getLegalIntWidths(entry);
771 else
772 legalIntWidths = detail::getDefaultLegalIntWidths(entry);
773 return *legalIntWidths;
774}
775
777 TargetSystemSpecInterface::DeviceID deviceID,
778 StringAttr propertyName) const {
779 checkValid();
780 DataLayoutEntryInterface entry;
781 if (originalTargetSystemDesc) {
782 if (std::optional<TargetDeviceSpecInterface> device =
783 originalTargetSystemDesc.getDeviceSpecForDeviceID(deviceID))
784 entry = device->getSpecForIdentifier(propertyName);
785 }
786 // Currently I am not caching the results because we do not return
787 // default values of these properties. Instead if the property is
788 // missing, we return std::nullopt so that the users can resort to
789 // the default value however they want.
790 if (auto iface = dyn_cast_or_null<DataLayoutOpInterface>(scope))
791 return iface.getDevicePropertyValue(entry);
792 else
793 return detail::getDevicePropertyValue(entry);
794}
795
796//===----------------------------------------------------------------------===//
797// DataLayoutSpecInterface
798//===----------------------------------------------------------------------===//
799
800void DataLayoutSpecInterface::bucketEntriesByType(
803 for (DataLayoutEntryInterface entry : getEntries()) {
804 if (auto type = llvm::dyn_cast_if_present<Type>(entry.getKey()))
805 types[type.getTypeID()].push_back(entry);
806 else
807 ids[llvm::cast<StringAttr>(entry.getKey())] = entry;
808 }
809}
810
811LogicalResult mlir::detail::verifyDataLayoutSpec(DataLayoutSpecInterface spec,
812 Location loc) {
813 // First, verify individual entries.
814 for (DataLayoutEntryInterface entry : spec.getEntries())
815 if (failed(entry.verifyEntry(loc)))
816 return failure();
817
818 // Second, dispatch verifications of entry groups to types or dialects they
819 // are associated with.
822 spec.bucketEntriesByType(types, ids);
823
824 for (const auto &kvp : types) {
825 auto sampleType = cast<Type>(kvp.second.front().getKey());
826 if (isa<IndexType>(sampleType)) {
827 assert(kvp.second.size() == 1 &&
828 "expected one data layout entry for non-parametric 'index' type");
829 auto width = dyn_cast<IntegerAttr>(kvp.second.front().getValue());
830 if (!width)
831 return emitError(loc)
832 << "expected integer attribute in the data layout entry for "
833 << sampleType;
834 if (width.getValue().isZero() ||
835 width.getValue().ugt(IntegerType::kMaxWidth))
836 return emitError(loc) << "index bit width must be between 1 and "
837 << IntegerType::kMaxWidth;
838 continue;
839 }
840
841 if (sampleType.isIntOrFloat()) {
842 for (DataLayoutEntryInterface entry : kvp.second) {
843 auto value = dyn_cast<DenseIntElementsAttr>(entry.getValue());
844 if (!value || !value.getElementType().isSignlessInteger(64)) {
845 emitError(loc) << "expected a dense i64 elements attribute in the "
846 "data layout entry "
847 << entry;
848 return failure();
849 }
850
851 auto elements = llvm::to_vector<2>(value.getValues<uint64_t>());
852 unsigned numElements = elements.size();
853 if (numElements < 1 || numElements > 2) {
854 emitError(loc) << "expected 1 or 2 elements in the data layout entry "
855 << entry;
856 return failure();
857 }
858
859 uint64_t abi = elements[0];
860 uint64_t preferred = numElements == 2 ? elements[1] : abi;
861 if (preferred < abi) {
862 emitError(loc)
863 << "preferred alignment is expected to be greater than or equal "
864 "to the abi alignment in data layout entry "
865 << entry;
866 return failure();
867 }
868 }
869 continue;
870 }
871
872 if (isa<BuiltinDialect>(&sampleType.getDialect()))
873 return emitError(loc) << "unexpected data layout for a built-in type";
874
875 auto dlType = dyn_cast<DataLayoutTypeInterface>(sampleType);
876 if (!dlType)
877 return emitError(loc)
878 << "data layout specified for a type that does not support it";
879 if (failed(dlType.verifyEntries(kvp.second, loc)))
880 return failure();
881 }
882
883 for (const auto &kvp : ids) {
884 StringAttr identifier = cast<StringAttr>(kvp.second.getKey());
885 Dialect *dialect = identifier.getReferencedDialect();
886
887 // Ignore attributes that belong to an unknown dialect, the dialect may
888 // actually implement the relevant interface but we don't know about that.
889 if (!dialect)
890 continue;
891
892 const auto *iface = dyn_cast<DataLayoutDialectInterface>(dialect);
893 if (!iface) {
894 return emitError(loc)
895 << "the '" << dialect->getNamespace()
896 << "' dialect does not support identifier data layout entries";
897 }
898 if (failed(iface->verifyEntry(kvp.second, loc)))
899 return failure();
900 }
901
902 return success();
903}
904
905LogicalResult
906mlir::detail::verifyTargetSystemSpec(TargetSystemSpecInterface spec,
907 Location loc) {
910 for (const auto &entry : spec.getEntries()) {
911 auto targetDeviceSpec =
912 dyn_cast<TargetDeviceSpecInterface>(entry.getValue());
913
914 if (!targetDeviceSpec)
915 return failure();
916
917 // First, verify individual target device desc specs.
918 if (failed(targetDeviceSpec.verifyEntry(loc)))
919 return failure();
920
921 // Check that device IDs are unique across all entries.
922 auto deviceID =
923 llvm::dyn_cast<TargetSystemSpecInterface::DeviceID>(entry.getKey());
924 if (!deviceID)
925 return failure();
926
927 if (!deviceIDs.insert(deviceID).second) {
928 return failure();
929 }
930
931 // collect all the keys used by all the target device specs.
932 for (DataLayoutEntryInterface entry : targetDeviceSpec.getEntries()) {
933 if (auto type = llvm::dyn_cast_if_present<Type>(entry.getKey())) {
934 // targetDeviceSpec does not support Type as a key.
935 return failure();
936 } else {
937 deviceDescKeys[cast<StringAttr>(entry.getKey())] = entry;
938 }
939 }
940 }
941
942 for (const auto &[keyName, keyVal] : deviceDescKeys) {
943 Dialect *dialect = keyName.getReferencedDialect();
944
945 // Ignore attributes that belong to an unknown dialect, the dialect may
946 // actually implement the relevant interface but we don't know about that.
947 if (!dialect)
948 return failure();
949
950 const auto *iface = dyn_cast<DataLayoutDialectInterface>(dialect);
951 if (!iface) {
952 return emitError(loc)
953 << "the '" << dialect->getNamespace()
954 << "' dialect does not support identifier data layout entries";
955 }
956 if (failed(iface->verifyEntry(keyVal, loc)))
957 return failure();
958 }
959
960 return success();
961}
962
963#include "mlir/Interfaces/DataLayoutAttrInterface.cpp.inc"
964#include "mlir/Interfaces/DataLayoutOpInterface.cpp.inc"
965#include "mlir/Interfaces/DataLayoutTypeInterface.cpp.inc"
return success()
static uint64_t getIntegerTypePreferredAlignment(IntegerType intType, const DataLayout &dataLayout, ArrayRef< DataLayoutEntryInterface > params)
static DataLayoutSpecInterface getCombinedDataLayout(Operation *leaf)
Returns a layout spec that is a combination of the layout specs attached to the given operation and a...
static TargetSystemSpecInterface getTargetSystemSpec(Operation *operation)
constexpr static const uint64_t kDefaultBitsInByte
static uint64_t getIntegerTypeABIAlignment(IntegerType intType, ArrayRef< DataLayoutEntryInterface > params)
static T cachedLookup(Type t, DenseMap< Type, T > &cache, function_ref< T(Type)> compute)
Looks up the value for the given type key in the given cache.
static uint64_t extractPreferredAlignment(DataLayoutEntryInterface entry)
static void collectParentLayouts(Operation *leaf, SmallVectorImpl< DataLayoutSpecInterface > &specs, SmallVectorImpl< Location > *opLocations=nullptr)
Populates opsWithLayout with the list of proper ancestors of leaf that are either modules or implemen...
static uint64_t getFloatTypePreferredAlignment(FloatType fltType, const DataLayout &dataLayout, ArrayRef< DataLayoutEntryInterface > params)
static uint64_t getFloatTypeABIAlignment(FloatType fltType, const DataLayout &dataLayout, ArrayRef< DataLayoutEntryInterface > params)
static DataLayoutSpecInterface getSpec(Operation *operation)
static uint64_t getIndexBitwidth(DataLayoutEntryListRef params)
Returns the bitwidth of the index type if specified in the param list.
static uint64_t extractABIAlignment(DataLayoutEntryInterface entry)
static DataLayoutEntryInterface findEntryForIntegerType(IntegerType intType, ArrayRef< DataLayoutEntryInterface > params)
void checkMissingLayout(DataLayoutSpecInterface originalLayout, OpTy op)
static void reportMissingDataLayout(Type type)
Reports that the given type is missing the data layout information and exits.
Attributes are known-constant values of operations.
Definition Attributes.h:25
The main mechanism for performing data layout queries.
Attribute getAllocaMemorySpace() const
Returns the memory space used for AllocaOps.
static DataLayout closest(Operation *op)
Returns the layout of the closest parent operation carrying layout info.
std::optional< uint64_t > getTypeIndexBitwidth(Type t) const
Returns the bitwidth that should be used when performing index computations for the given pointer-lik...
llvm::TypeSize getTypeSize(Type t) const
Returns the size of the given type in the current scope.
Attribute getManglingMode() const
Returns the mangling mode.
llvm::TypeSize getTypeAllocSize(Type t) const
Returns the offset in bytes between successive objects of the given type in the current scope,...
uint64_t getStackAlignment() const
Returns the natural alignment of the stack in bits.
bool hasExactTypeEntry(Type type) const
Returns true if the layout in the current scope has an entry whose key is exactly the given type.
Attribute getProgramMemorySpace() const
Returns the memory space used for program memory operations.
uint64_t getTypePreferredAlignment(Type t) const
Returns the preferred of the given type in the current scope.
Attribute getFunctionPointerAlignment() const
Returns function pointer alignment.
Attribute getGlobalMemorySpace() const
Returns the memory space used for global operations.
uint64_t getTypeABIAlignment(Type t) const
Returns the required alignment of the given type in the current scope.
llvm::TypeSize getTypeSizeInBits(Type t) const
Returns the size in bits of the given type in the current scope.
Attribute getDefaultMemorySpace() const
Returns the default memory space used for memory operations.
Attribute getEndianness() const
Returns the specified endianness.
Attribute getLegalIntWidths() const
Returns the legal int widths.
std::optional< Attribute > getDevicePropertyValue(TargetSystemSpecInterface::DeviceID, StringAttr propertyName) const
Returns the value of the specified property if the property is defined for the given device ID,...
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition Dialect.h:38
StringRef getNamespace() const
Definition Dialect.h:54
This class represents a diagnostic that is inflight and set to be reported.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:240
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Definition Operation.h:251
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition Operation.h:255
This class provides an efficient unique identifier for a specific C++ type.
Definition TypeID.h:107
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
Definition Types.cpp:35
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
Definition Types.cpp:118
TypeID getTypeID()
Return a unique identifier for the concrete type.
Definition Types.h:101
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:124
Attribute getDefaultAllocaMemorySpace(DataLayoutEntryInterface entry)
Default handler for alloca memory space request.
Attribute getDefaultProgramMemorySpace(DataLayoutEntryInterface entry)
Default handler for program memory space request.
Attribute getDefaultEndianness(DataLayoutEntryInterface entry)
Default handler for endianness request.
std::optional< uint64_t > getDefaultIndexBitwidth(Type type, const DataLayout &dataLayout, ArrayRef< DataLayoutEntryInterface > params)
Default handler for the index bitwidth request.
DataLayoutEntryList filterEntriesForType(DataLayoutEntryListRef entries, TypeID typeID)
Given a list of data layout entries, returns a new list containing the entries with keys having the g...
LogicalResult verifyTargetSystemSpec(TargetSystemSpecInterface spec, Location loc)
Verifies that a target system desc spec is valid.
std::optional< Attribute > getDevicePropertyValue(DataLayoutEntryInterface entry)
Returns the value of the property from the specified DataLayoutEntry.
Attribute getDefaultManglingMode(DataLayoutEntryInterface entry)
Default handler for mangling mode request.
uint64_t getDefaultABIAlignment(Type type, const DataLayout &dataLayout, ArrayRef< DataLayoutEntryInterface > params)
Default handler for the required alignment request.
Attribute getDefaultLegalIntWidths(DataLayoutEntryInterface entry)
Default handler for the legal int widths request.
llvm::TypeSize getDefaultTypeSize(Type type, const DataLayout &dataLayout, DataLayoutEntryListRef params)
Default handler for the type size request.
llvm::TypeSize getDefaultTypeSizeInBits(Type type, const DataLayout &dataLayout, DataLayoutEntryListRef params)
Default handler for the type size in bits request.
uint64_t getDefaultPreferredAlignment(Type type, const DataLayout &dataLayout, ArrayRef< DataLayoutEntryInterface > params)
Default handler for the preferred alignment request.
llvm::TypeSize divideCeil(llvm::TypeSize numerator, uint64_t denominator)
Divides the known min value of the numerator by the denominator and rounds the result up to the next ...
uint64_t getDefaultStackAlignment(DataLayoutEntryInterface entry)
Default handler for the stack alignment request.
Attribute getDefaultGlobalMemorySpace(DataLayoutEntryInterface entry)
Default handler for global memory space request.
Attribute getDefaultMemorySpace(DataLayoutEntryInterface entry)
Default handler for the default memory space request.
LogicalResult verifyDataLayoutOp(Operation *op)
Verifies that the operation implementing the data layout interface, or a module operation,...
LogicalResult verifyDataLayoutSpec(DataLayoutSpecInterface spec, Location loc)
Verifies that a data layout spec is valid.
Attribute getDefaultFunctionPointerAlignment(DataLayoutEntryInterface entry)
Default handler for the function pointer alignment request.
DataLayoutEntryInterface filterEntryForIdentifier(DataLayoutEntryListRef entries, StringAttr id)
Given a list of data layout entries, returns the entry that has the given identifier as key,...
Include the generated interface declarations.
llvm::SmallVector< DataLayoutEntryInterface, 4 > DataLayoutEntryList
llvm::DenseSet< ValueT, ValueInfoT > DenseSet
Definition LLVM.h:125
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:123
llvm::ArrayRef< DataLayoutEntryInterface > DataLayoutEntryListRef
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155