MLIR 24.0.0git
LLVMMemoryOps.h
Go to the documentation of this file.
1//===- LLVMMemoryOps.h - LLVM dialect memory ops ----------------*- 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// This file declares the ops of op partition `Memory` of the LLVM dialect: the
10// memory access and addressing ops.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_DIALECT_LLVMIR_LLVMMEMORYOPS_H_
15#define MLIR_DIALECT_LLVMIR_LLVMMEMORYOPS_H_
16
19#include "llvm/ADT/PointerEmbeddedInt.h"
20
21namespace mlir {
22namespace LLVM {
23template <typename Values>
25
26/// Bit-width of a 'GEPConstantIndex' within GEPArg.
27constexpr int kGEPConstantBitWidth = 29;
28/// Wrapper around a int32_t for use in a PointerUnion.
30 llvm::PointerEmbeddedInt<int32_t, kGEPConstantBitWidth>;
31
32/// Class used for building a 'llvm.getelementptr'. A single instance represents
33/// a sum type that is either a 'Value' or a constant 'GEPConstantIndex' index.
34/// The former represents a dynamic index in a GEP operation, while the later is
35/// a constant index as is required for indices into struct types.
36class GEPArg : public PointerUnion<Value, GEPConstantIndex> {
38
39public:
40 /// Constructs a GEPArg with a constant index.
41 /*implicit*/ GEPArg(int32_t integer) : BaseT(integer) {}
42
43 /// Constructs a GEPArg with a dynamic index.
44 /*implicit*/ GEPArg(Value value) : BaseT(value) {}
45
46 using BaseT::operator=;
47};
48} // namespace LLVM
49} // namespace mlir
50
51#define GET_OP_CLASSES
52#include "mlir/Dialect/LLVMIR/LLVMMemoryOps.h.inc"
53
54namespace mlir {
55namespace LLVM {
56/// Class used for convenient access and iteration over GEP indices.
57/// This class is templated to support not only retrieving the dynamic operands
58/// of a GEP operation, but also as an adaptor during folding or conversion to
59/// LLVM IR.
60///
61/// GEP indices may either be constant indices or dynamic indices. The
62/// 'rawConstantIndices' is specially encoded by GEPOp and contains either the
63/// constant index or the information that an index is a dynamic index.
64///
65/// When an access to such an index is made it is done through the
66/// 'DynamicRange' of this class. This way it can be used as getter in GEPOp via
67/// 'GEPIndicesAdaptor<ValueRange>' or during folding via
68/// 'GEPIndicesAdaptor<ArrayRef<Attribute>>'.
69template <typename DynamicRange>
71public:
72 /// Return type of 'operator[]' and the iterators 'operator*'. It is depended
73 /// upon the value type of 'DynamicRange'. If 'DynamicRange' contains
74 /// Attributes or subclasses thereof, then value_type is 'Attribute'. In
75 /// all other cases it is a pointer union between the value type of
76 /// 'DynamicRange' and IntegerAttr.
77 using value_type = std::conditional_t<
78 std::is_base_of<Attribute,
79 llvm::detail::ValueOfRange<DynamicRange>>::value,
82
83 /// Constructs a GEPIndicesAdaptor with the raw constant indices of a GEPOp
84 /// and the range that is indexed into for retrieving dynamic indices.
85 GEPIndicesAdaptor(DenseI32ArrayAttr rawConstantIndices, DynamicRange values)
86 : rawConstantIndices(rawConstantIndices), values(std::move(values)) {}
87
88 /// Returns the GEP index at the given position. Note that this operation has
89 /// a linear complexity in regards to the accessed position. To iterate over
90 /// all indices, use the iterators.
91 ///
92 /// This operation is invalid if the index is out of bounds.
93 value_type operator[](size_t index) const {
94 assert(index < size() && "index out of bounds");
95 return *std::next(begin(), index);
96 }
97
98 /// Returns whether the GEP index at the given position is a dynamic index.
99 bool isDynamicIndex(size_t index) const {
100 return rawConstantIndices[index] == GEPOp::kDynamicIndex;
101 }
102
103 /// Returns the amount of indices of the GEPOp.
104 size_t size() const { return rawConstantIndices.size(); }
105
106 /// Returns true if this GEPOp does not have any indices.
107 bool empty() const { return rawConstantIndices.empty(); }
108
110 : public llvm::iterator_facade_base<iterator, std::forward_iterator_tag,
111 value_type, std::ptrdiff_t,
112 value_type *, value_type> {
113 public:
115 ArrayRef<int32_t>::iterator rawConstantIter,
116 llvm::detail::IterOfRange<const DynamicRange> valuesIter)
117 : base(base), rawConstantIter(rawConstantIter), valuesIter(valuesIter) {
118 }
119
121 if (*rawConstantIter == GEPOp::kDynamicIndex)
122 return *valuesIter;
123
124 return IntegerAttr::get(base->rawConstantIndices.getElementType(),
125 *rawConstantIter);
126 }
127
129 if (*rawConstantIter == GEPOp::kDynamicIndex)
130 valuesIter++;
131 rawConstantIter++;
132 return *this;
133 }
134
135 bool operator==(const iterator &rhs) const {
136 return base == rhs.base && rawConstantIter == rhs.rawConstantIter &&
137 valuesIter == rhs.valuesIter;
138 }
139
140 private:
141 const GEPIndicesAdaptor *base;
142 ArrayRef<int32_t>::const_iterator rawConstantIter;
143 llvm::detail::IterOfRange<const DynamicRange> valuesIter;
144 };
145
146 /// Returns the begin iterator, iterating over all GEP indices.
147 iterator begin() const {
148 return iterator(this, rawConstantIndices.asArrayRef().begin(),
149 values.begin());
150 }
151
152 /// Returns the end iterator, iterating over all GEP indices.
153 iterator end() const {
154 return iterator(this, rawConstantIndices.asArrayRef().end(), values.end());
155 }
156
157private:
158 DenseI32ArrayAttr rawConstantIndices;
159 DynamicRange values;
160};
161} // namespace LLVM
162} // namespace mlir
163
164namespace llvm {
165// Allow llvm::cast style functions.
166template <typename To>
167struct CastInfo<To, mlir::LLVM::GEPArg>
168 : public CastInfo<To, mlir::LLVM::GEPArg::PointerUnion> {};
169
170template <typename To>
171struct CastInfo<To, const mlir::LLVM::GEPArg>
172 : public CastInfo<To, const mlir::LLVM::GEPArg::PointerUnion> {};
173} // namespace llvm
174
175#endif // MLIR_DIALECT_LLVMIR_LLVMMEMORYOPS_H_
Attributes are known-constant values of operations.
Definition Attributes.h:25
GEPArg(int32_t integer)
Constructs a GEPArg with a constant index.
GEPArg(Value value)
Constructs a GEPArg with a dynamic index.
iterator(const GEPIndicesAdaptor *base, ArrayRef< int32_t >::iterator rawConstantIter, llvm::detail::IterOfRange< const DynamicRange > valuesIter)
bool operator==(const iterator &rhs) const
Class used for convenient access and iteration over GEP indices.
iterator begin() const
Returns the begin iterator, iterating over all GEP indices.
std::conditional_t< std::is_base_of< Attribute, llvm::detail::ValueOfRange< DynamicRange > >::value, Attribute, PointerUnion< IntegerAttr, llvm::detail::ValueOfRange< DynamicRange > > > value_type
Return type of 'operator[]' and the iterators 'operator*'.
iterator end() const
Returns the end iterator, iterating over all GEP indices.
bool isDynamicIndex(size_t index) const
Returns whether the GEP index at the given position is a dynamic index.
size_t size() const
Returns the amount of indices of the GEPOp.
bool empty() const
Returns true if this GEPOp does not have any indices.
value_type operator[](size_t index) const
Returns the GEP index at the given position.
GEPIndicesAdaptor(DenseI32ArrayAttr rawConstantIndices, DynamicRange values)
Constructs a GEPIndicesAdaptor with the raw constant indices of a GEPOp and the range that is indexed...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
llvm::PointerEmbeddedInt< int32_t, kGEPConstantBitWidth > GEPConstantIndex
Wrapper around a int32_t for use in a PointerUnion.
constexpr int kGEPConstantBitWidth
Bit-width of a 'GEPConstantIndex' within GEPArg.
Include the generated interface declarations.
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr