MLIR 23.0.0git
BuiltinAttributes.h
Go to the documentation of this file.
1//===- BuiltinAttributes.h - MLIR Builtin Attribute Classes -----*- C++ -*-===//
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
9#ifndef MLIR_IR_BUILTINATTRIBUTES_H
10#define MLIR_IR_BUILTINATTRIBUTES_H
11
13#include "llvm/ADT/APFloat.h"
14#include "llvm/ADT/Sequence.h"
15#include <complex>
16#include <optional>
17
18namespace mlir {
19class AffineMap;
20class AsmResourceBlob;
21class BoolAttr;
22class BuiltinDialect;
24template <typename T>
27class FunctionType;
28class IntegerSet;
29class IntegerType;
30class Location;
31class Operation;
32class RankedTensorType;
33
34namespace detail {
38} // namespace detail
39
40//===----------------------------------------------------------------------===//
41// Elements Attributes
42//===----------------------------------------------------------------------===//
43
44namespace detail {
45/// Pair of raw pointer and a boolean flag of whether the pointer holds a splat,
46using DenseIterPtrAndSplat = std::pair<const char *, bool>;
47
48/// Impl iterator for indexed DenseElementsAttr iterators that records a data
49/// pointer and data index that is adjusted for the case of a splat attribute.
50template <typename ConcreteT, typename T, typename PointerT = T *,
51 typename ReferenceT = T &>
53 : public llvm::indexed_accessor_iterator<ConcreteT, DenseIterPtrAndSplat, T,
54 PointerT, ReferenceT> {
55protected:
56 DenseElementIndexedIteratorImpl(const char *data, bool isSplat,
57 size_t dataIndex)
58 : llvm::indexed_accessor_iterator<ConcreteT, DenseIterPtrAndSplat, T,
59 PointerT, ReferenceT>({data, isSplat},
60 dataIndex) {}
61
62 /// Return the current index for this iterator, adjusted for the case of a
63 /// splat.
64 ptrdiff_t getDataIndex() const {
65 bool isSplat = this->base.second;
66 return isSplat ? 0 : this->index;
67 }
68
69 /// Return the data base pointer.
70 const char *getData() const { return this->base.first; }
71};
72
73/// Type trait detector that checks if a given type T is a complex type.
74template <typename T>
75struct is_complex_t : public std::false_type {};
76template <typename T>
77struct is_complex_t<std::complex<T>> : public std::true_type {};
78} // namespace detail
79
80/// An attribute that represents a reference to a dense vector or tensor
81/// object.
83public:
85
86 /// Allow implicit conversion to ElementsAttr.
87 operator ElementsAttr() const { return cast_if_present<ElementsAttr>(*this); }
88 /// Allow implicit conversion to TypedAttr.
89 operator TypedAttr() const { return ElementsAttr(*this); }
90
91 /// Type trait used to check if the given type T is a potentially valid C++
92 /// floating point type that can be used to access the underlying element
93 /// types of a DenseElementsAttr.
94 template <typename T>
96 /// The type is a valid floating point type if it is a builtin floating
97 /// point type, or is a potentially user defined floating point type. The
98 /// latter allows for supporting users that have custom types defined for
99 /// bfloat16/half/etc.
100 static constexpr bool value = llvm::is_one_of<T, float, double>::value ||
101 (std::numeric_limits<T>::is_specialized &&
102 !std::numeric_limits<T>::is_integer);
103 };
104
105 /// Method for support type inquiry through isa, cast and dyn_cast.
106 static bool classof(Attribute attr);
107
108 /// Constructs a dense elements attribute from an array of element values.
109 /// Each element attribute value is expected to be an element of 'type'.
110 /// 'type' must be a vector or tensor with static shape. If the element of
111 /// `type` is non-integer/index/float it is assumed to be a string type.
112 static DenseElementsAttr get(ShapedType type, ArrayRef<Attribute> values);
113
114 /// Constructs a dense integer elements attribute from an array of integer
115 /// or floating-point values. Each value is expected to be the same bitwidth
116 /// of the element type of 'type'. 'type' must be a vector or tensor with
117 /// static shape.
118 template <typename T,
119 typename = std::enable_if_t<std::numeric_limits<T>::is_integer ||
121 static DenseElementsAttr get(const ShapedType &type, ArrayRef<T> values) {
122 const char *data = reinterpret_cast<const char *>(values.data());
123 return getRawIntOrFloat(
124 type, ArrayRef<char>(data, values.size() * sizeof(T)), sizeof(T),
125 std::numeric_limits<T>::is_integer, std::numeric_limits<T>::is_signed);
126 }
127
128 /// Constructs a dense integer elements attribute from a single element.
129 template <typename T,
130 typename = std::enable_if_t<std::numeric_limits<T>::is_integer ||
133 static DenseElementsAttr get(const ShapedType &type, T value) {
134 return get(type, llvm::ArrayRef(value));
135 }
136
137 /// Constructs a dense complex elements attribute from an array of complex
138 /// values. Each value is expected to be the same bitwidth of the element type
139 /// of 'type'. 'type' must be a vector or tensor with static shape.
140 template <
141 typename T, typename ElementT = typename T::value_type,
142 typename = std::enable_if_t<detail::is_complex_t<T>::value &&
143 (std::numeric_limits<ElementT>::is_integer ||
145 static DenseElementsAttr get(const ShapedType &type, ArrayRef<T> values) {
146 const char *data = reinterpret_cast<const char *>(values.data());
147 return getRawComplex(type, ArrayRef<char>(data, values.size() * sizeof(T)),
148 sizeof(T), std::numeric_limits<ElementT>::is_integer,
149 std::numeric_limits<ElementT>::is_signed);
150 }
151
152 /// Overload of the above 'get' method that is specialized for boolean values.
153 static DenseElementsAttr get(ShapedType type, ArrayRef<bool> values);
154
155 /// Overload of the above 'get' method that is specialized for StringRef
156 /// values.
157 static DenseElementsAttr get(ShapedType type, ArrayRef<StringRef> values);
158
159 /// Constructs a dense integer elements attribute from an array of APInt
160 /// values. Each APInt value is expected to have the same bitwidth as the
161 /// element type of 'type'. 'type' must be a vector or tensor with static
162 /// shape.
163 static DenseElementsAttr get(ShapedType type, ArrayRef<APInt> values);
164
165 /// Constructs a dense complex elements attribute from an array of APInt
166 /// values. Each APInt value is expected to have the same bitwidth as the
167 /// element type of 'type'. 'type' must be a vector or tensor with static
168 /// shape.
169 static DenseElementsAttr get(ShapedType type,
170 ArrayRef<std::complex<APInt>> values);
171
172 /// Constructs a dense float elements attribute from an array of APFloat
173 /// values. Each APFloat value is expected to have the same bitwidth as the
174 /// element type of 'type'. 'type' must be a vector or tensor with static
175 /// shape.
176 static DenseElementsAttr get(ShapedType type, ArrayRef<APFloat> values);
177
178 /// Constructs a dense complex elements attribute from an array of APFloat
179 /// values. Each APFloat value is expected to have the same bitwidth as the
180 /// element type of 'type'. 'type' must be a vector or tensor with static
181 /// shape.
182 static DenseElementsAttr get(ShapedType type,
183 ArrayRef<std::complex<APFloat>> values);
184
185 /// Construct a dense elements attribute for an initializer_list of values.
186 /// Each value is expected to be the same bitwidth of the element type of
187 /// 'type'. 'type' must be a vector or tensor with static shape.
188 template <typename T>
189 static DenseElementsAttr get(const ShapedType &type,
190 const std::initializer_list<T> &list) {
191 return get(type, ArrayRef<T>(list));
192 }
193
194 /// Construct a dense elements attribute from a raw buffer representing the
195 /// data for this attribute. Users are encouraged to use one of the
196 /// constructors above, which provide more safeties. However, this
197 /// constructor is useful for tools which may want to interop and can
198 /// follow the precise definition.
199 ///
200 /// The format of the raw buffer is a densely packed array of values that
201 /// can be bitcast to the storage format of the element type specified.
202 /// Types that are not byte aligned will be rounded up to the next byte.
203 static DenseElementsAttr getFromRawBuffer(ShapedType type,
204 ArrayRef<char> rawBuffer);
205
206 /// Returns true if the given buffer is a valid raw buffer for the given type.
207 static bool isValidRawBuffer(ShapedType type, ArrayRef<char> rawBuffer);
208
209 //===--------------------------------------------------------------------===//
210 // Iterators
211 //===--------------------------------------------------------------------===//
212
213 /// The iterator range over the given iterator type T.
214 template <typename IteratorT>
216
217 /// The iterator for the given element type T.
218 template <typename T, typename AttrT = DenseElementsAttr>
219 using iterator = decltype(std::declval<AttrT>().template value_begin<T>());
220 /// The iterator range over the given element T.
221 template <typename T, typename AttrT = DenseElementsAttr>
223 decltype(std::declval<AttrT>().template getValues<T>());
224
225 /// A utility iterator that allows walking over the internal Attribute values
226 /// of a DenseElementsAttr.
227 class AttributeElementIterator
228 : public llvm::indexed_accessor_iterator<AttributeElementIterator,
229 const void *, Attribute,
231 public:
232 /// Accesses the Attribute value at this iterator position.
233 Attribute operator*() const;
234
235 private:
236 friend DenseElementsAttr;
237
238 /// Constructs a new iterator.
239 AttributeElementIterator(DenseElementsAttr attr, size_t index);
240 };
241
242 /// Iterator for walking raw element values of the specified type 'T', which
243 /// may be any c++ data type matching the stored representation: int32_t,
244 /// float, etc.
245 template <typename T>
246 class ElementIterator
247 : public detail::DenseElementIndexedIteratorImpl<ElementIterator<T>,
248 const T> {
249 public:
250 /// Accesses the raw value at this iterator position.
251 const T &operator*() const {
252 return reinterpret_cast<const T *>(this->getData())[this->getDataIndex()];
253 }
254
255 private:
256 friend DenseElementsAttr;
257
258 /// Constructs a new iterator.
259 ElementIterator(const char *data, bool isSplat, size_t dataIndex)
261 data, isSplat, dataIndex) {}
262 };
263
264 /// A utility iterator that allows walking over the internal bool values.
265 class BoolElementIterator
266 : public detail::DenseElementIndexedIteratorImpl<BoolElementIterator,
267 bool, bool, bool> {
268 public:
269 /// Accesses the bool value at this iterator position.
270 bool operator*() const;
271
272 private:
273 friend DenseElementsAttr;
274
275 /// Constructs a new iterator.
276 BoolElementIterator(DenseElementsAttr attr, size_t dataIndex);
277 };
278
279 /// A utility iterator that allows walking over the internal raw APInt values.
280 class IntElementIterator
281 : public detail::DenseElementIndexedIteratorImpl<IntElementIterator,
282 APInt, APInt, APInt> {
283 public:
284 /// Accesses the raw APInt value at this iterator position.
285 APInt operator*() const;
286
287 private:
288 friend DenseElementsAttr;
289
290 /// Constructs a new iterator.
291 IntElementIterator(DenseElementsAttr attr, size_t dataIndex);
292
293 /// The bitwidth of the element type.
294 size_t bitWidth;
295 };
296
297 /// A utility iterator that allows walking over the internal raw complex APInt
298 /// values.
299 class ComplexIntElementIterator
301 ComplexIntElementIterator, std::complex<APInt>, std::complex<APInt>,
302 std::complex<APInt>> {
303 public:
304 /// Accesses the raw std::complex<APInt> value at this iterator position.
305 std::complex<APInt> operator*() const;
306
307 private:
308 friend DenseElementsAttr;
309
310 /// Constructs a new iterator.
311 ComplexIntElementIterator(DenseElementsAttr attr, size_t dataIndex);
312
313 /// The bitwidth of the element type.
314 size_t bitWidth;
315 };
316
317 /// Iterator for walking over APFloat values.
318 class FloatElementIterator final
319 : public llvm::mapped_iterator_base<FloatElementIterator,
320 IntElementIterator, APFloat> {
321 public:
322 /// Map the element to the iterator result type.
323 APFloat mapElement(const APInt &value) const {
324 return APFloat(*smt, value);
325 }
326
327 private:
328 friend DenseElementsAttr;
329
330 /// Initializes the float element iterator to the specified iterator.
331 FloatElementIterator(const llvm::fltSemantics &smt, IntElementIterator it)
332 : BaseT(it), smt(&smt) {}
333
334 /// The float semantics to use when constructing the APFloat.
335 const llvm::fltSemantics *smt;
336 };
337
338 /// Iterator for walking over complex APFloat values.
339 class ComplexFloatElementIterator final
340 : public llvm::mapped_iterator_base<ComplexFloatElementIterator,
341 ComplexIntElementIterator,
342 std::complex<APFloat>> {
343 public:
344 /// Map the element to the iterator result type.
345 std::complex<APFloat> mapElement(const std::complex<APInt> &value) const {
346 return {APFloat(*smt, value.real()), APFloat(*smt, value.imag())};
347 }
348
349 private:
350 friend DenseElementsAttr;
351
352 /// Initializes the float element iterator to the specified iterator.
353 ComplexFloatElementIterator(const llvm::fltSemantics &smt,
355 : BaseT(it), smt(&smt) {}
356
357 /// The float semantics to use when constructing the APFloat.
358 const llvm::fltSemantics *smt;
359 };
360
361 //===--------------------------------------------------------------------===//
362 // Value Querying
363 //===--------------------------------------------------------------------===//
364
365 /// Returns true if this attribute corresponds to a splat, i.e. if all element
366 /// values are the same.
367 bool isSplat() const;
368
369 /// Return the splat value for this attribute. This asserts that the attribute
370 /// corresponds to a splat.
371 template <typename T>
372 std::enable_if_t<!std::is_base_of<Attribute, T>::value ||
373 std::is_same<Attribute, T>::value,
374 T>
376 assert(isSplat() && "expected the attribute to be a splat");
377 return *value_begin<T>();
378 }
379 /// Return the splat value for derived attribute element types.
380 template <typename T>
381 std::enable_if_t<std::is_base_of<Attribute, T>::value &&
382 !std::is_same<Attribute, T>::value,
383 T>
385 return llvm::cast<T>(getSplatValue<Attribute>());
386 }
387
388 /// Try to get an iterator of the given type to the start of the held element
389 /// values. Return failure if the type cannot be iterated.
390 template <typename T>
391 auto try_value_begin() const {
392 auto range = tryGetValues<T>();
393 using iterator = decltype(range->begin());
394 return failed(range) ? FailureOr<iterator>(failure()) : range->begin();
395 }
396
397 /// Try to get an iterator of the given type to the end of the held element
398 /// values. Return failure if the type cannot be iterated.
399 template <typename T>
400 auto try_value_end() const {
401 auto range = tryGetValues<T>();
402 using iterator = decltype(range->begin());
403 return failed(range) ? FailureOr<iterator>(failure()) : range->end();
404 }
405
406 /// Return the held element values as a range of the given type.
407 template <typename T>
408 auto getValues() const {
409 auto range = tryGetValues<T>();
410 assert(succeeded(range) && "element type cannot be iterated");
411 return std::move(*range);
412 }
413
414 /// Get an iterator of the given type to the start of the held element values.
415 template <typename T>
416 auto value_begin() const {
417 return getValues<T>().begin();
418 }
419
420 /// Get an iterator of the given type to the end of the held element values.
421 template <typename T>
422 auto value_end() const {
423 return getValues<T>().end();
424 }
425
426 /// Try to get the held element values as a range of integer or floating-point
427 /// values.
428 template <typename T>
430 std::enable_if_t<(!std::is_same<T, bool>::value &&
431 std::numeric_limits<T>::is_integer) ||
433 template <typename T, typename = IntFloatValueTemplateCheckT<T>>
434 FailureOr<iterator_range_impl<ElementIterator<T>>> tryGetValues() const {
435 if (!isValidIntOrFloat(sizeof(T), std::numeric_limits<T>::is_integer,
436 std::numeric_limits<T>::is_signed))
437 return failure();
438 const char *rawData = getRawData().data();
439 bool splat = isSplat();
441 getType(), ElementIterator<T>(rawData, splat, 0),
442 ElementIterator<T>(rawData, splat, getNumElements()));
443 }
444
445 /// Try to get the held element values as a range of std::complex.
446 template <typename T, typename ElementT>
448 std::enable_if_t<detail::is_complex_t<T>::value &&
449 (std::numeric_limits<ElementT>::is_integer ||
451 template <typename T, typename ElementT = typename T::value_type,
453 FailureOr<iterator_range_impl<ElementIterator<T>>> tryGetValues() const {
454 if (!isValidComplex(sizeof(T), std::numeric_limits<ElementT>::is_integer,
455 std::numeric_limits<ElementT>::is_signed))
456 return failure();
457 const char *rawData = getRawData().data();
458 bool splat = isSplat();
460 getType(), ElementIterator<T>(rawData, splat, 0),
461 ElementIterator<T>(rawData, splat, getNumElements()));
462 }
463
464 /// Try to get the held element values as a range of StringRef.
465 template <typename T>
467 std::enable_if_t<std::is_same<T, StringRef>::value>;
468 template <typename T, typename = StringRefValueTemplateCheckT<T>>
469 FailureOr<iterator_range_impl<ElementIterator<StringRef>>>
470 tryGetValues() const {
471 auto stringRefs = getRawStringData();
472 const char *ptr = reinterpret_cast<const char *>(stringRefs.data());
473 bool splat = isSplat();
477 }
478
479 /// Try to get the held element values as a range of Attributes.
480 template <typename T>
482 std::enable_if_t<std::is_same<T, Attribute>::value>;
483 template <typename T, typename = AttributeValueTemplateCheckT<T>>
484 FailureOr<iterator_range_impl<AttributeElementIterator>>
490
491 /// Try to get the held element values a range of T, where T is a derived
492 /// attribute type.
493 template <typename T>
495 std::enable_if_t<std::is_base_of<Attribute, T>::value &&
496 !std::is_same<Attribute, T>::value>;
497 template <typename T>
499 : public llvm::mapped_iterator_base<DerivedAttributeElementIterator<T>,
500 AttributeElementIterator, T> {
501 using llvm::mapped_iterator_base<DerivedAttributeElementIterator<T>,
503 T>::mapped_iterator_base;
504
505 /// Map the element to the iterator result type.
506 T mapElement(Attribute attr) const { return llvm::cast<T>(attr); }
507 };
508 template <typename T, typename = DerivedAttrValueTemplateCheckT<T>>
509 FailureOr<iterator_range_impl<DerivedAttributeElementIterator<T>>>
510 tryGetValues() const {
511 using DerivedIterT = DerivedAttributeElementIterator<T>;
513 getType(), DerivedIterT(value_begin<Attribute>()),
514 DerivedIterT(value_end<Attribute>()));
515 }
516
517 /// Try to get the held element values as a range of bool. The element type of
518 /// this attribute must be of integer type of bitwidth 1.
519 template <typename T>
521 std::enable_if_t<std::is_same<T, bool>::value>;
522 template <typename T, typename = BoolValueTemplateCheckT<T>>
523 FailureOr<iterator_range_impl<BoolElementIterator>> tryGetValues() const {
524 if (!isValidBool())
525 return failure();
527 getType(), BoolElementIterator(*this, 0),
529 }
530
531 /// Try to get the held element values as a range of APInts. The element type
532 /// of this attribute must be of integer type.
533 template <typename T>
535 std::enable_if_t<std::is_same<T, APInt>::value>;
536 template <typename T, typename = APIntValueTemplateCheckT<T>>
537 FailureOr<iterator_range_impl<IntElementIterator>> tryGetValues() const {
538 if (!getElementType().isIntOrIndex())
539 return failure();
541 raw_int_end());
542 }
543
544 /// Try to get the held element values as a range of complex APInts. The
545 /// element type of this attribute must be a complex of integer type.
546 template <typename T>
548 std::enable_if_t<std::is_same<T, std::complex<APInt>>::value>;
549 template <typename T, typename = ComplexAPIntValueTemplateCheckT<T>>
550 FailureOr<iterator_range_impl<ComplexIntElementIterator>>
551 tryGetValues() const {
552 return tryGetComplexIntValues();
553 }
554
555 /// Try to get the held element values as a range of APFloat. The element type
556 /// of this attribute must be of float type.
557 template <typename T>
559 std::enable_if_t<std::is_same<T, APFloat>::value>;
560 template <typename T, typename = APFloatValueTemplateCheckT<T>>
561 FailureOr<iterator_range_impl<FloatElementIterator>> tryGetValues() const {
562 return tryGetFloatValues();
563 }
564
565 /// Try to get the held element values as a range of complex APFloat. The
566 /// element type of this attribute must be a complex of float type.
567 template <typename T>
569 std::enable_if_t<std::is_same<T, std::complex<APFloat>>::value>;
570 template <typename T, typename = ComplexAPFloatValueTemplateCheckT<T>>
571 FailureOr<iterator_range_impl<ComplexFloatElementIterator>>
572 tryGetValues() const {
574 }
575
576 /// Return the raw storage data held by this attribute. Users should generally
577 /// not use this directly, as the internal storage format is not always in the
578 /// form the user might expect.
580
581 /// Return the raw StringRef data held by this attribute.
583
584 /// Return the type of this ElementsAttr, guaranteed to be a vector or tensor
585 /// with static shape.
586 ShapedType getType() const;
587
588 /// Return the element type of this DenseElementsAttr.
589 Type getElementType() const;
590
591 /// Returns the number of elements held by this attribute.
592 int64_t getNumElements() const;
593
594 /// Returns the number of elements held by this attribute.
595 int64_t size() const { return getNumElements(); }
596
597 /// Returns if the number of elements held by this attribute is 0.
598 bool empty() const { return size() == 0; }
599
600 //===--------------------------------------------------------------------===//
601 // Mutation Utilities
602 //===--------------------------------------------------------------------===//
603
604 /// Return a new DenseElementsAttr that has the same data as the current
605 /// attribute, but has been reshaped to 'newType'. The new type must have the
606 /// same total number of elements as well as element type.
607 DenseElementsAttr reshape(ShapedType newType);
608
609 /// Return a new DenseElementsAttr that has the same data as the current
610 /// attribute, but with a different shape for a splat type. The new type must
611 /// have the same element type.
612 DenseElementsAttr resizeSplat(ShapedType newType);
613
614 /// Return a new DenseElementsAttr that has the same data as the current
615 /// attribute, but has bitcast elements to 'newElType'. The new type must have
616 /// the same bitwidth as the current element type.
617 DenseElementsAttr bitcast(Type newElType);
618
619 /// Generates a new DenseElementsAttr by mapping each int value to a new
620 /// underlying APInt. The new values can represent either an integer or float.
621 /// This underlying type must be an DenseIntElementsAttr.
622 DenseElementsAttr mapValues(Type newElementType,
623 function_ref<APInt(const APInt &)> mapping) const;
624
625 /// Generates a new DenseElementsAttr by mapping each float value to a new
626 /// underlying APInt. the new values can represent either an integer or float.
627 /// This underlying type must be an DenseFPElementsAttr.
629 mapValues(Type newElementType,
630 function_ref<APInt(const APFloat &)> mapping) const;
631
632protected:
633 /// Iterators to various elements that require out-of-line definition. These
634 /// are hidden from the user to encourage consistent use of the
635 /// getValues/value_begin/value_end API.
637 return IntElementIterator(*this, 0);
638 }
642 FailureOr<iterator_range_impl<ComplexIntElementIterator>>
644 FailureOr<iterator_range_impl<FloatElementIterator>>
645 tryGetFloatValues() const;
646 FailureOr<iterator_range_impl<ComplexFloatElementIterator>>
648
649 /// Overload of the raw 'get' method that asserts that the given type is of
650 /// complex type. This method is used to verify type invariants that the
651 /// templatized 'get' method cannot.
652 static DenseElementsAttr getRawComplex(ShapedType type, ArrayRef<char> data,
653 int64_t dataEltSize, bool isInt,
654 bool isSigned);
655
656 /// Overload of the raw 'get' method that asserts that the given type is of
657 /// integer or floating-point type. This method is used to verify type
658 /// invariants that the templatized 'get' method cannot.
659 static DenseElementsAttr getRawIntOrFloat(ShapedType type,
660 ArrayRef<char> data,
661 int64_t dataEltSize, bool isInt,
662 bool isSigned);
663
664 /// Check the information for a C++ data type, check if this type is valid for
665 /// the current attribute. This method is used to verify specific type
666 /// invariants that the templatized 'getValues' method cannot.
667 bool isValidBool() const { return getElementType().isInteger(1); }
668 bool isValidIntOrFloat(int64_t dataEltSize, bool isInt, bool isSigned) const;
669 bool isValidComplex(int64_t dataEltSize, bool isInt, bool isSigned) const;
670};
671
672/// An attribute that represents a reference to a splat vector or tensor
673/// constant, meaning all of the elements have the same value.
675public:
676 using DenseElementsAttr::DenseElementsAttr;
677
678 /// Method for support type inquiry through isa, cast and dyn_cast.
679 static bool classof(Attribute attr) {
680 auto denseAttr = llvm::dyn_cast<DenseElementsAttr>(attr);
681 return denseAttr && denseAttr.isSplat();
682 }
683};
684
685//===----------------------------------------------------------------------===//
686// DenseResourceElementsAttr
687//===----------------------------------------------------------------------===//
688
690
691} // namespace mlir
692
693//===----------------------------------------------------------------------===//
694// Tablegen Attribute Declarations
695//===----------------------------------------------------------------------===//
696
697#define GET_ATTRDEF_CLASSES
698#include "mlir/IR/BuiltinAttributes.h.inc"
699
700//===----------------------------------------------------------------------===//
701// C++ Attribute Declarations
702//===----------------------------------------------------------------------===//
703
704namespace mlir {
705//===----------------------------------------------------------------------===//
706// DenseArrayAttr
707//===----------------------------------------------------------------------===//
708
709namespace detail {
710/// Base class for DenseArrayAttr that is instantiated and specialized for each
711/// supported element type below.
712template <typename T>
713class DenseArrayAttrImpl : public DenseArrayAttr {
714public:
715 using DenseArrayAttr::DenseArrayAttr;
716
717 /// Implicit conversion to ArrayRef<T>.
718 operator ArrayRef<T>() const;
719 ArrayRef<T> asArrayRef() const { return ArrayRef<T>{*this}; }
720
721 /// Random access to elements.
722 T operator[](std::size_t index) const { return asArrayRef()[index]; }
723
724 /// Builder from ArrayRef<T>.
726
727 /// Print the short form `[42, 100, -1]` without any type prefix.
728 void print(AsmPrinter &printer) const;
729 void print(raw_ostream &os) const;
730 /// Print the short form `42, 100, -1` without any braces or type prefix.
732
733 /// Parse the short form `[42, 100, -1]` without any type prefix.
734 static Attribute parse(AsmParser &parser, Type type);
735
736 /// Parse the short form `42, 100, -1` without any type prefix or braces.
738
739 /// Support for isa<>/cast<>.
740 static bool classof(Attribute attr);
741};
742
743extern template class DenseArrayAttrImpl<bool>;
744extern template class DenseArrayAttrImpl<int8_t>;
745extern template class DenseArrayAttrImpl<int16_t>;
746extern template class DenseArrayAttrImpl<int32_t>;
747extern template class DenseArrayAttrImpl<int64_t>;
748extern template class DenseArrayAttrImpl<float>;
749extern template class DenseArrayAttrImpl<double>;
750} // namespace detail
751
752// Public name for all the supported DenseArrayAttr
760
761//===----------------------------------------------------------------------===//
762// DenseResourceElementsAttr
763//===----------------------------------------------------------------------===//
764
765namespace detail {
766/// Base class for DenseResourceElementsAttr that is instantiated and
767/// specialized for each supported element type below.
768template <typename T>
769class DenseResourceElementsAttrBase : public DenseResourceElementsAttr {
770public:
771 using DenseResourceElementsAttr::DenseResourceElementsAttr;
772
773 /// A builder that inserts a new resource using the provided blob. The handle
774 /// of the inserted blob is used when building the attribute. The provided
775 /// `blobName` is used as a hint for the key of the new handle for the `blob`
776 /// resource, but may be changed if necessary to ensure uniqueness during
777 /// insertion.
779 get(ShapedType type, StringRef blobName, AsmResourceBlob blob);
780
781 /// Return the data of this attribute as an ArrayRef<T> if it is present,
782 /// returns std::nullopt otherwise.
783 std::optional<ArrayRef<T>> tryGetAsArrayRef() const;
784
785 /// Support for isa<>/cast<>.
786 static bool classof(Attribute attr);
787};
788
789extern template class DenseResourceElementsAttrBase<bool>;
790extern template class DenseResourceElementsAttrBase<int8_t>;
791extern template class DenseResourceElementsAttrBase<int16_t>;
792extern template class DenseResourceElementsAttrBase<int32_t>;
793extern template class DenseResourceElementsAttrBase<int64_t>;
794extern template class DenseResourceElementsAttrBase<uint8_t>;
795extern template class DenseResourceElementsAttrBase<uint16_t>;
796extern template class DenseResourceElementsAttrBase<uint32_t>;
797extern template class DenseResourceElementsAttrBase<uint64_t>;
798extern template class DenseResourceElementsAttrBase<float>;
799extern template class DenseResourceElementsAttrBase<double>;
800} // namespace detail
801
802// Public names for all the supported DenseResourceElementsAttr.
803
826
827//===----------------------------------------------------------------------===//
828// BoolAttr
829//===----------------------------------------------------------------------===//
830
831/// Special case of IntegerAttr to represent boolean integers, i.e., signless i1
832/// integers.
833class BoolAttr : public Attribute {
834public:
837
838 static BoolAttr get(MLIRContext *context, bool value);
839
840 /// Enable conversion to IntegerAttr and its interfaces. This uses conversion
841 /// vs. inheritance to avoid bringing in all of IntegerAttrs methods.
842 operator IntegerAttr() const { return IntegerAttr(impl); }
843 operator TypedAttr() const { return IntegerAttr(impl); }
844
845 /// Return the boolean value of this attribute.
846 bool getValue() const;
847
848 /// Methods for support type inquiry through isa, cast, and dyn_cast.
849 static bool classof(Attribute attr);
850};
851
852//===----------------------------------------------------------------------===//
853// FlatSymbolRefAttr
854//===----------------------------------------------------------------------===//
855
856/// A symbol reference with a reference path containing a single element. This
857/// is used to refer to an operation within the current symbol table.
858class FlatSymbolRefAttr : public SymbolRefAttr {
859public:
860 using SymbolRefAttr::SymbolRefAttr;
861 using ValueType = StringRef;
862
863 /// Construct a symbol reference for the given value name.
864 static FlatSymbolRefAttr get(StringAttr value) {
865 return SymbolRefAttr::get(value);
866 }
867 static FlatSymbolRefAttr get(MLIRContext *ctx, StringRef value) {
868 return SymbolRefAttr::get(ctx, value);
869 }
870
871 /// Convenience getter for building a SymbolRefAttr based on an operation
872 /// that implements the SymbolTrait.
874 return SymbolRefAttr::get(symbol);
875 }
876
877 /// Returns the name of the held symbol reference as a StringAttr.
878 StringAttr getAttr() const { return getRootReference(); }
879
880 /// Returns the name of the held symbol reference.
881 StringRef getValue() const { return getAttr().getValue(); }
882
883 /// Methods for support type inquiry through isa, cast, and dyn_cast.
884 static bool classof(Attribute attr) {
885 SymbolRefAttr refAttr = llvm::dyn_cast<SymbolRefAttr>(attr);
886 return refAttr && refAttr.getNestedReferences().empty();
887 }
888
889private:
890 using SymbolRefAttr::get;
891 using SymbolRefAttr::getNestedReferences;
892};
893
894//===----------------------------------------------------------------------===//
895// DenseFPElementsAttr
896//===----------------------------------------------------------------------===//
897
898/// An attribute that represents a reference to a dense float vector or tensor
899/// object. Each element is stored as a double.
901public:
903
904 using DenseIntOrFPElementsAttr::DenseIntOrFPElementsAttr;
905
906 /// Get an instance of a DenseFPElementsAttr with the given arguments. This
907 /// simply wraps the DenseElementsAttr::get calls.
908 template <typename Arg>
909 static DenseFPElementsAttr get(const ShapedType &type, Arg &&arg) {
910 return llvm::cast<DenseFPElementsAttr>(
912 }
913 template <typename T>
914 static DenseFPElementsAttr get(const ShapedType &type,
915 const std::initializer_list<T> &list) {
916 return llvm::cast<DenseFPElementsAttr>(DenseElementsAttr::get(type, list));
917 }
918
919 /// Generates a new DenseElementsAttr by mapping each value attribute, and
920 /// constructing the DenseElementsAttr given the new element type.
922 mapValues(Type newElementType,
923 function_ref<APInt(const APFloat &)> mapping) const;
924
925 /// Iterator access to the float element values.
926 iterator begin() const { return tryGetFloatValues()->begin(); }
927 iterator end() const { return tryGetFloatValues()->end(); }
928
929 /// Method for supporting type inquiry through isa, cast and dyn_cast.
930 static bool classof(Attribute attr);
931};
932
933//===----------------------------------------------------------------------===//
934// DenseIntElementsAttr
935//===----------------------------------------------------------------------===//
936
937/// An attribute that represents a reference to a dense integer vector or tensor
938/// object.
940public:
941 /// DenseIntElementsAttr iterates on APInt, so we can use the raw element
942 /// iterator directly.
944
945 using DenseIntOrFPElementsAttr::DenseIntOrFPElementsAttr;
946
947 /// Get an instance of a DenseIntElementsAttr with the given arguments. This
948 /// simply wraps the DenseElementsAttr::get calls.
949 template <typename Arg>
950 static DenseIntElementsAttr get(const ShapedType &type, Arg &&arg) {
951 return llvm::cast<DenseIntElementsAttr>(
953 }
954 template <typename T>
955 static DenseIntElementsAttr get(const ShapedType &type,
956 const std::initializer_list<T> &list) {
957 return llvm::cast<DenseIntElementsAttr>(DenseElementsAttr::get(type, list));
958 }
959
960 /// Generates a new DenseElementsAttr by mapping each value attribute, and
961 /// constructing the DenseElementsAttr given the new element type.
962 DenseElementsAttr mapValues(Type newElementType,
963 function_ref<APInt(const APInt &)> mapping) const;
964
965 /// Iterator access to the integer element values.
966 iterator begin() const { return raw_int_begin(); }
967 iterator end() const { return raw_int_end(); }
968
969 /// Method for supporting type inquiry through isa, cast and dyn_cast.
970 static bool classof(Attribute attr);
971};
972
973//===----------------------------------------------------------------------===//
974// SparseElementsAttr
975//===----------------------------------------------------------------------===//
976
977template <typename T>
978auto SparseElementsAttr::try_value_begin_impl(OverloadToken<T>) const
979 -> FailureOr<iterator<T>> {
980 auto zeroValue = getZeroValue<T>();
981 auto valueIt = getValues().try_value_begin<T>();
982 if (failed(valueIt))
983 return failure();
984 const SmallVector<ptrdiff_t> flatSparseIndices(getFlattenedSparseIndices());
985 std::function<T(ptrdiff_t)> mapFn =
986 [flatSparseIndices{flatSparseIndices}, valueIt{std::move(*valueIt)},
987 zeroValue{std::move(zeroValue)}](ptrdiff_t index) {
988 // Try to map the current index to one of the sparse indices.
989 for (unsigned i = 0, e = flatSparseIndices.size(); i != e; ++i)
990 if (flatSparseIndices[i] == index)
991 return *std::next(valueIt, i);
992 // Otherwise, return the zero value.
993 return zeroValue;
994 };
995 return iterator<T>(llvm::seq<ptrdiff_t>(0, getNumElements()).begin(), mapFn);
996}
997
998//===----------------------------------------------------------------------===//
999// DistinctAttr
1000//===----------------------------------------------------------------------===//
1001
1002namespace detail {
1003struct DistinctAttrStorage;
1005} // namespace detail
1006
1007/// An attribute that associates a referenced attribute with a unique
1008/// identifier. Every call to the create function allocates a new distinct
1009/// attribute instance. The address of the attribute instance serves as a
1010/// temporary identifier. Similar to the names of SSA values, the final
1011/// identifiers are generated during pretty printing. This delayed numbering
1012/// ensures the printed identifiers are deterministic even if multiple distinct
1013/// attribute instances are created in-parallel.
1014///
1015/// Examples:
1016///
1017/// #distinct = distinct[0]<42.0 : f32>
1018/// #distinct1 = distinct[1]<42.0 : f32>
1019/// #distinct2 = distinct[2]<array<i32: 10, 42>>
1020///
1021/// NOTE: The distinct attribute cannot be defined using ODS since it uses a
1022/// custom distinct attribute uniquer that cannot be set from ODS.
1024 : public detail::StorageUserBase<DistinctAttr, Attribute,
1025 detail::DistinctAttrStorage,
1026 detail::DistinctAttributeUniquer> {
1027public:
1028 using Base::Base;
1029
1030 /// Returns the referenced attribute.
1032
1033 /// Creates a distinct attribute that associates a referenced attribute with a
1034 /// unique identifier.
1035 static DistinctAttr create(Attribute referencedAttr);
1036
1037 static constexpr StringLiteral name = "builtin.distinct";
1038};
1039
1040//===----------------------------------------------------------------------===//
1041// StringAttr
1042//===----------------------------------------------------------------------===//
1043
1044/// Define comparisons for StringAttr against nullptr and itself to avoid the
1045/// StringRef overloads from being chosen when not desirable.
1046inline bool operator==(StringAttr lhs, std::nullptr_t) { return !lhs; }
1047inline bool operator!=(StringAttr lhs, std::nullptr_t) {
1048 return static_cast<bool>(lhs);
1049}
1050inline bool operator==(StringAttr lhs, StringAttr rhs) {
1051 return (Attribute)lhs == (Attribute)rhs;
1052}
1053inline bool operator!=(StringAttr lhs, StringAttr rhs) { return !(lhs == rhs); }
1054
1055/// Allow direct comparison with StringRef.
1056inline bool operator==(StringAttr lhs, StringRef rhs) {
1057 return lhs.getValue() == rhs;
1058}
1059inline bool operator!=(StringAttr lhs, StringRef rhs) { return !(lhs == rhs); }
1060inline bool operator==(StringRef lhs, StringAttr rhs) {
1061 return rhs.getValue() == lhs;
1062}
1063inline bool operator!=(StringRef lhs, StringAttr rhs) { return !(lhs == rhs); }
1064
1065} // namespace mlir
1066
1067//===----------------------------------------------------------------------===//
1068// Attribute Utilities
1069//===----------------------------------------------------------------------===//
1070
1071namespace mlir {
1072
1073/// Given a list of strides (in which ShapedType::kDynamic
1074/// represents a dynamic value), return the single result AffineMap which
1075/// represents the linearized strided layout map. Dimensions correspond to the
1076/// offset followed by the strides in order. Symbols are inserted for each
1077/// dynamic dimension in order. A stride is always positive.
1078///
1079/// Examples:
1080/// =========
1081///
1082/// 1. For offset: 0 strides: ?, ?, 1 return
1083/// (i, j, k)[M, N]->(M * i + N * j + k)
1084///
1085/// 2. For offset: 3 strides: 32, ?, 16 return
1086/// (i, j, k)[M]->(3 + 32 * i + M * j + 16 * k)
1087///
1088/// 3. For offset: ? strides: ?, ?, ? return
1089/// (i, j, k)[off, M, N, P]->(off + M * i + N * j + P * k)
1091 MLIRContext *context);
1092
1093} // namespace mlir
1094
1095namespace llvm {
1096
1097template <>
1098struct DenseMapInfo<mlir::StringAttr> : public DenseMapInfo<mlir::Attribute> {
1099 static mlir::StringAttr getEmptyKey() {
1100 const void *pointer = llvm::DenseMapInfo<const void *>::getEmptyKey();
1101 return mlir::StringAttr::getFromOpaquePointer(pointer);
1102 }
1103 static mlir::StringAttr getTombstoneKey() {
1105 return mlir::StringAttr::getFromOpaquePointer(pointer);
1106 }
1107};
1108template <>
1109struct PointerLikeTypeTraits<mlir::StringAttr>
1110 : public PointerLikeTypeTraits<mlir::Attribute> {
1111 static inline mlir::StringAttr getFromVoidPointer(void *p) {
1112 return mlir::StringAttr::getFromOpaquePointer(p);
1113 }
1114};
1115
1116template <>
1117struct PointerLikeTypeTraits<mlir::IntegerAttr>
1118 : public PointerLikeTypeTraits<mlir::Attribute> {
1119 static inline mlir::IntegerAttr getFromVoidPointer(void *p) {
1120 return mlir::IntegerAttr::getFromOpaquePointer(p);
1121 }
1122};
1123
1124template <>
1125struct PointerLikeTypeTraits<mlir::SymbolRefAttr>
1126 : public PointerLikeTypeTraits<mlir::Attribute> {
1127 static inline mlir::SymbolRefAttr getFromVoidPointer(void *ptr) {
1128 return mlir::SymbolRefAttr::getFromOpaquePointer(ptr);
1129 }
1130};
1131
1132} // namespace llvm
1133
1134#endif // MLIR_IR_BUILTINATTRIBUTES_H
lhs
static int64_t getNumElements(Type t)
Compute the total number of elements in the given type, also taking into account nested types.
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition AffineMap.h:46
This base class exposes generic asm parser hooks, usable across the various derived parsers.
This base class exposes generic asm printer hooks, usable across the various derived printers.
This class represents a processed binary blob of data.
Definition AsmState.h:91
Attributes are known-constant values of operations.
Definition Attributes.h:25
constexpr Attribute()=default
Special case of IntegerAttr to represent boolean integers, i.e., signless i1 integers.
constexpr Attribute()=default
A utility iterator that allows walking over the internal Attribute values of a DenseElementsAttr.
A utility iterator that allows walking over the internal bool values.
bool operator*() const
Accesses the bool value at this iterator position.
Iterator for walking over complex APFloat values.
std::complex< APFloat > mapElement(const std::complex< APInt > &value) const
Map the element to the iterator result type.
A utility iterator that allows walking over the internal raw complex APInt values.
std::complex< APInt > operator*() const
Accesses the raw std::complex<APInt> value at this iterator position.
Iterator for walking raw element values of the specified type 'T', which may be any c++ data type mat...
const T & operator*() const
Accesses the raw value at this iterator position.
Iterator for walking over APFloat values.
APFloat mapElement(const APInt &value) const
Map the element to the iterator result type.
A utility iterator that allows walking over the internal raw APInt values.
APInt operator*() const
Accesses the raw APInt value at this iterator position.
An attribute that represents a reference to a dense vector or tensor object.
ArrayRef< StringRef > getRawStringData() const
Return the raw StringRef data held by this attribute.
IntElementIterator raw_int_begin() const
Iterators to various elements that require out-of-line definition.
static DenseElementsAttr getRawIntOrFloat(ShapedType type, ArrayRef< char > data, int64_t dataEltSize, bool isInt, bool isSigned)
Overload of the raw 'get' method that asserts that the given type is of integer or floating-point typ...
FailureOr< iterator_range_impl< AttributeElementIterator > > tryGetValues() const
auto value_begin() const
Get an iterator of the given type to the start of the held element values.
static DenseElementsAttr getRawComplex(ShapedType type, ArrayRef< char > data, int64_t dataEltSize, bool isInt, bool isSigned)
Overload of the raw 'get' method that asserts that the given type is of complex type.
std::enable_if_t< std::is_same< T, std::complex< APInt > >::value > ComplexAPIntValueTemplateCheckT
Try to get the held element values as a range of complex APInts.
auto try_value_begin() const
Try to get an iterator of the given type to the start of the held element values.
static bool classof(Attribute attr)
Method for support type inquiry through isa, cast and dyn_cast.
bool isValidComplex(int64_t dataEltSize, bool isInt, bool isSigned) const
FailureOr< iterator_range_impl< ElementIterator< T > > > tryGetValues() const
auto getValues() const
Return the held element values as a range of the given type.
DenseElementsAttr resizeSplat(ShapedType newType)
Return a new DenseElementsAttr that has the same data as the current attribute, but with a different ...
decltype(std::declval< AttrT >().template getValues< T >()) iterator_range
The iterator range over the given element T.
detail::ElementsAttrRange< IteratorT > iterator_range_impl
The iterator range over the given iterator type T.
int64_t getNumElements() const
Returns the number of elements held by this attribute.
std::enable_if_t<!std::is_base_of< Attribute, T >::value||std::is_same< Attribute, T >::value, T > getSplatValue() const
Return the splat value for this attribute.
static DenseElementsAttr getFromRawBuffer(ShapedType type, ArrayRef< char > rawBuffer)
Construct a dense elements attribute from a raw buffer representing the data for this attribute.
static bool isValidRawBuffer(ShapedType type, ArrayRef< char > rawBuffer)
Returns true if the given buffer is a valid raw buffer for the given type.
int64_t size() const
Returns the number of elements held by this attribute.
std::enable_if_t< std::is_same< T, Attribute >::value > AttributeValueTemplateCheckT
Try to get the held element values as a range of Attributes.
bool isValidBool() const
Check the information for a C++ data type, check if this type is valid for the current attribute.
bool isSplat() const
Returns true if this attribute corresponds to a splat, i.e.
auto try_value_end() const
Try to get an iterator of the given type to the end of the held element values.
ArrayRef< char > getRawData() const
Return the raw storage data held by this attribute.
constexpr Attribute()=default
FailureOr< iterator_range_impl< ElementIterator< T > > > tryGetValues() const
static DenseElementsAttr get(const ShapedType &type, ArrayRef< T > values)
Constructs a dense integer elements attribute from an array of integer or floating-point values.
DenseElementsAttr mapValues(Type newElementType, function_ref< APInt(const APInt &)> mapping) const
Generates a new DenseElementsAttr by mapping each int value to a new underlying APInt.
std::enable_if_t< std::is_same< T, std::complex< APFloat > >::value > ComplexAPFloatValueTemplateCheckT
Try to get the held element values as a range of complex APFloat.
static DenseElementsAttr get(const ShapedType &type, const std::initializer_list< T > &list)
Construct a dense elements attribute for an initializer_list of values.
decltype(std::declval< AttrT >().template value_begin< T >()) iterator
The iterator for the given element type T.
std::enable_if_t< std::is_base_of< Attribute, T >::value && !std::is_same< Attribute, T >::value > DerivedAttrValueTemplateCheckT
Try to get the held element values a range of T, where T is a derived attribute type.
Type getElementType() const
Return the element type of this DenseElementsAttr.
FailureOr< iterator_range_impl< ComplexFloatElementIterator > > tryGetComplexFloatValues() const
std::enable_if_t< std::is_same< T, StringRef >::value > StringRefValueTemplateCheckT
Try to get the held element values as a range of StringRef.
IntElementIterator raw_int_end() const
std::enable_if_t<(!std::is_same< T, bool >::value && std::numeric_limits< T >::is_integer)|| is_valid_cpp_fp_type< T >::value > IntFloatValueTemplateCheckT
Try to get the held element values as a range of integer or floating-point values.
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
std::enable_if_t< std::is_same< T, APFloat >::value > APFloatValueTemplateCheckT
Try to get the held element values as a range of APFloat.
ShapedType getType() const
Return the type of this ElementsAttr, guaranteed to be a vector or tensor with static shape.
FailureOr< iterator_range_impl< FloatElementIterator > > tryGetFloatValues() const
std::enable_if_t< detail::is_complex_t< T >::value &&(std::numeric_limits< ElementT >::is_integer|| is_valid_cpp_fp_type< ElementT >::value)> ComplexValueTemplateCheckT
Try to get the held element values as a range of std::complex.
std::enable_if_t< std::is_same< T, bool >::value > BoolValueTemplateCheckT
Try to get the held element values as a range of bool.
bool empty() const
Returns if the number of elements held by this attribute is 0.
DenseElementsAttr bitcast(Type newElType)
Return a new DenseElementsAttr that has the same data as the current attribute, but has bitcast eleme...
static DenseElementsAttr get(const ShapedType &type, T value)
Constructs a dense integer elements attribute from a single element.
std::enable_if_t< std::is_base_of< Attribute, T >::value &&!std::is_same< Attribute, T >::value, T > getSplatValue() const
Return the splat value for derived attribute element types.
bool isValidIntOrFloat(int64_t dataEltSize, bool isInt, bool isSigned) const
FailureOr< iterator_range_impl< ElementIterator< StringRef > > > tryGetValues() const
FailureOr< iterator_range_impl< IntElementIterator > > tryGetValues() const
DenseElementsAttr reshape(ShapedType newType)
Return a new DenseElementsAttr that has the same data as the current attribute, but has been reshaped...
FailureOr< iterator_range_impl< ComplexIntElementIterator > > tryGetComplexIntValues() const
auto value_end() const
Get an iterator of the given type to the end of the held element values.
static DenseElementsAttr get(const ShapedType &type, ArrayRef< T > values)
Constructs a dense complex elements attribute from an array of complex values.
std::enable_if_t< std::is_same< T, APInt >::value > APIntValueTemplateCheckT
Try to get the held element values as a range of APInts.
FailureOr< iterator_range_impl< BoolElementIterator > > tryGetValues() const
FailureOr< iterator_range_impl< DerivedAttributeElementIterator< T > > > tryGetValues() const
FailureOr< iterator_range_impl< ComplexFloatElementIterator > > tryGetValues() const
FailureOr< iterator_range_impl< ComplexIntElementIterator > > tryGetValues() const
FailureOr< iterator_range_impl< FloatElementIterator > > tryGetValues() const
An attribute that represents a reference to a dense float vector or tensor object.
static DenseFPElementsAttr get(const ShapedType &type, const std::initializer_list< T > &list)
DenseElementsAttr::FloatElementIterator iterator
static DenseFPElementsAttr get(const ShapedType &type, Arg &&arg)
Get an instance of a DenseFPElementsAttr with the given arguments.
iterator begin() const
Iterator access to the float element values.
An attribute that represents a reference to a dense integer vector or tensor object.
static DenseIntElementsAttr get(const ShapedType &type, const std::initializer_list< T > &list)
DenseElementsAttr::IntElementIterator iterator
DenseIntElementsAttr iterates on APInt, so we can use the raw element iterator directly.
iterator begin() const
Iterator access to the integer element values.
static DenseIntElementsAttr get(const ShapedType &type, Arg &&arg)
Get an instance of a DenseIntElementsAttr with the given arguments.
An attribute that associates a referenced attribute with a unique identifier.
static constexpr StringLiteral name
static DistinctAttr create(Attribute referencedAttr)
Creates a distinct attribute that associates a referenced attribute with a unique identifier.
Attribute getReferencedAttr() const
Returns the referenced attribute.
A symbol reference with a reference path containing a single element.
static FlatSymbolRefAttr get(StringAttr value)
Construct a symbol reference for the given value name.
static FlatSymbolRefAttr get(MLIRContext *ctx, StringRef value)
static bool classof(Attribute attr)
Methods for support type inquiry through isa, cast, and dyn_cast.
static FlatSymbolRefAttr get(Operation *symbol)
Convenience getter for building a SymbolRefAttr based on an operation that implements the SymbolTrait...
StringRef getValue() const
Returns the name of the held symbol reference.
StringAttr getAttr() const
Returns the name of the held symbol reference as a StringAttr.
An integer set representing a conjunction of one or more affine equalities and inequalities.
Definition IntegerSet.h:44
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
An attribute that represents a reference to a splat vector or tensor constant, meaning all of the ele...
static bool classof(Attribute attr)
Method for support type inquiry through isa, cast and dyn_cast.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isInteger() const
Return true if this is an integer type (with the specified width).
Definition Types.cpp:58
Base class for DenseArrayAttr that is instantiated and specialized for each supported element type be...
static DenseArrayAttrImpl get(MLIRContext *context, ArrayRef< T > content)
Builder from ArrayRef<T>.
static bool classof(Attribute attr)
Support for isa<>/cast<>.
void print(raw_ostream &os) const
void print(AsmPrinter &printer) const
Print the short form [42, 100, -1] without any type prefix.
static Attribute parse(AsmParser &parser, Type type)
Parse the short form [42, 100, -1] without any type prefix.
static Attribute parseWithoutBraces(AsmParser &parser, Type type)
Parse the short form 42, 100, -1 without any type prefix or braces.
void printWithoutBraces(raw_ostream &os) const
Print the short form 42, 100, -1 without any braces or type prefix.
T operator[](std::size_t index) const
Random access to elements.
Impl iterator for indexed DenseElementsAttr iterators that records a data pointer and data index that...
ptrdiff_t getDataIndex() const
Return the current index for this iterator, adjusted for the case of a splat.
const char * getData() const
Return the data base pointer.
DenseElementIndexedIteratorImpl(const char *data, bool isSplat, size_t dataIndex)
Base class for DenseResourceElementsAttr that is instantiated and specialized for each supported elem...
static bool classof(Attribute attr)
Support for isa<>/cast<>.
static DenseResourceElementsAttrBase< T > get(ShapedType type, StringRef blobName, AsmResourceBlob blob)
A builder that inserts a new resource using the provided blob.
std::optional< ArrayRef< T > > tryGetAsArrayRef() const
Return the data of this attribute as an ArrayRef<T> if it is present, returns std::nullopt otherwise.
A specialized attribute uniquer for distinct attributes that always allocates since the distinct attr...
This class provides iterator utilities for an ElementsAttr range.
Utility class for implementing users of storage classes uniqued by a StorageUniquer.
StorageUserBase< ConcreteT, BaseT, StorageT, UniquerT, Traits... > Base
Utility declarations for the concrete attribute class.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
AttrTypeReplacer.
std::pair< const char *, bool > DenseIterPtrAndSplat
Pair of raw pointer and a boolean flag of whether the pointer holds a splat,.
Include the generated interface declarations.
detail::DenseArrayAttrImpl< int64_t > DenseI64ArrayAttr
detail::DenseResourceElementsAttrBase< uint64_t > DenseUI64ResourceElementsAttr
detail::DenseResourceElementsAttrBase< int32_t > DenseI32ResourceElementsAttr
detail::DenseResourceElementsAttrBase< uint32_t > DenseUI32ResourceElementsAttr
detail::DenseArrayAttrImpl< int8_t > DenseI8ArrayAttr
DialectResourceBlobHandle< BuiltinDialect > DenseResourceElementsHandle
detail::DenseResourceElementsAttrBase< float > DenseF32ResourceElementsAttr
bool operator==(StringAttr lhs, std::nullptr_t)
Define comparisons for StringAttr against nullptr and itself to avoid the StringRef overloads from be...
detail::DenseResourceElementsAttrBase< bool > DenseBoolResourceElementsAttr
bool operator!=(RegionBranchPoint lhs, RegionBranchPoint rhs)
detail::DenseResourceElementsAttrBase< int16_t > DenseI16ResourceElementsAttr
detail::DenseResourceElementsAttrBase< int64_t > DenseI64ResourceElementsAttr
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr
detail::DenseResourceElementsAttrBase< uint16_t > DenseUI16ResourceElementsAttr
AffineExpr operator*(int64_t val, AffineExpr expr)
Definition AffineExpr.h:252
AffineMap makeStridedLinearLayoutMap(ArrayRef< int64_t > strides, int64_t offset, MLIRContext *context)
Given a list of strides (in which ShapedType::kDynamic represents a dynamic value),...
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
detail::DenseArrayAttrImpl< double > DenseF64ArrayAttr
detail::DenseArrayAttrImpl< bool > DenseBoolArrayAttr
detail::DenseResourceElementsAttrBase< int8_t > DenseI8ResourceElementsAttr
detail::DenseArrayAttrImpl< float > DenseF32ArrayAttr
detail::DenseResourceElementsAttrBase< uint8_t > DenseUI8ResourceElementsAttr
detail::DenseArrayAttrImpl< int16_t > DenseI16ArrayAttr
llvm::function_ref< Fn > function_ref
Definition LLVM.h:144
detail::DenseResourceElementsAttrBase< double > DenseF64ResourceElementsAttr
static mlir::IntegerAttr getFromVoidPointer(void *p)
static mlir::StringAttr getFromVoidPointer(void *p)
static mlir::SymbolRefAttr getFromVoidPointer(void *ptr)
T mapElement(Attribute attr) const
Map the element to the iterator result type.
Type trait used to check if the given type T is a potentially valid C++ floating point type that can ...
static constexpr bool value
The type is a valid floating point type if it is a builtin floating point type, or is a potentially u...
This class defines a dialect specific handle to a resource blob.
An attribute representing a reference to a dense vector or tensor object.
An attribute representing a reference to a dense vector or tensor object containing strings.
An attribute to store a distinct reference to another attribute.
Type trait detector that checks if a given type T is a complex type.