1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
//===- Location.cpp - MLIR Location Classes -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/IR/Location.h"
#include "mlir/IR/AttributeSupport.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinDialect.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Visitors.h"
#include "mlir/Support/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/TrailingObjects.h"
#include <cassert>
#include <tuple>
#include <utility>
using namespace mlir;
using namespace mlir::detail;
namespace mlir::detail {
struct FileLineColRangeAttrStorage final
: public ::mlir::AttributeStorage,
private llvm::TrailingObjects<FileLineColRangeAttrStorage, unsigned> {
friend llvm::TrailingObjects<FileLineColRangeAttrStorage, unsigned>;
using PointerPair = llvm::PointerIntPair<StringAttr, 2>;
using KeyTy = std::tuple<StringAttr, ::llvm::ArrayRef<unsigned>>;
FileLineColRangeAttrStorage(StringAttr filename, int numLocs)
: filenameAndTrailing(filename, numLocs) {}
static FileLineColRangeAttrStorage *
construct(::mlir::AttributeStorageAllocator &allocator, KeyTy &&tblgenKey) {
auto numInArray = std::get<1>(tblgenKey).size();
// Note: Considered asserting that numInArray is at least 1, but this
// is not needed in memory or in printed form. This should very rarely be
// 0 here as that means a NamedLoc would have been more efficient. But this
// does allow for location with just a file, and also having the interface
// be more uniform.
auto locEnc = numInArray == 0 ? 1 : numInArray;
// Allocate a new storage instance.
auto byteSize =
FileLineColRangeAttrStorage::totalSizeToAlloc<unsigned>(locEnc - 1);
auto *rawMem =
allocator.allocate(byteSize, alignof(FileLineColRangeAttrStorage));
auto *result = ::new (rawMem) FileLineColRangeAttrStorage(
std::move(std::get<0>(tblgenKey)), locEnc - 1);
if (numInArray > 0) {
ArrayRef<unsigned> elements = std::get<1>(tblgenKey);
result->startLine = elements[0];
// Copy in the element types into the trailing storage.
llvm::uninitialized_copy(elements.drop_front(),
result->getTrailingObjects());
}
return result;
}
// Return the number of held types.
unsigned size() const { return filenameAndTrailing.getInt() + 1; }
bool operator==(const KeyTy &tblgenKey) const {
return (filenameAndTrailing.getPointer() == std::get<0>(tblgenKey)) &&
(size() == std::get<1>(tblgenKey).size()) &&
(startLine == std::get<1>(tblgenKey)[0]) &&
(getTrailingObjects(size() - 1) ==
std::get<1>(tblgenKey).drop_front());
}
unsigned getLineCols(unsigned index) const {
return getTrailingObjects()[index - 1];
}
unsigned getStartLine() const { return startLine; }
unsigned getStartColumn() const {
if (size() <= 1)
return 0;
return getLineCols(1);
}
unsigned getEndColumn() const {
if (size() <= 2)
return getStartColumn();
return getLineCols(2);
}
unsigned getEndLine() const {
if (size() <= 3)
return getStartLine();
return getLineCols(3);
}
static ::llvm::hash_code hashKey(const KeyTy &tblgenKey) {
return ::llvm::hash_combine(std::get<0>(tblgenKey), std::get<1>(tblgenKey));
}
// Supports
// - 0 (file:line)
// - 1 (file:line:col)
// - 2 (file:line:start_col to file:line:end_col) and
// - 3 (file:start_line:start_col to file:end_line:end_col)
llvm::PointerIntPair<StringAttr, 2> filenameAndTrailing;
unsigned startLine = 0;
};
} // namespace mlir::detail
//===----------------------------------------------------------------------===//
/// Tablegen Attribute Definitions
//===----------------------------------------------------------------------===//
#define GET_ATTRDEF_CLASSES
#include "mlir/IR/BuiltinLocationAttributes.cpp.inc"
//===----------------------------------------------------------------------===//
// LocationAttr
//===----------------------------------------------------------------------===//
WalkResult LocationAttr::walk(function_ref<WalkResult(Location)> walkFn) {
AttrTypeWalker walker;
// Walk locations, but skip any other attribute.
walker.addWalk([&](Attribute attr) {
if (auto loc = llvm::dyn_cast<LocationAttr>(attr))
return walkFn(loc);
return WalkResult::skip();
});
return walker.walk<WalkOrder::PreOrder>(*this);
}
/// Methods for support type inquiry through isa, cast, and dyn_cast.
bool LocationAttr::classof(Attribute attr) {
return attr.hasTrait<AttributeTrait::IsLocation>();
}
//===----------------------------------------------------------------------===//
// CallSiteLoc
//===----------------------------------------------------------------------===//
CallSiteLoc CallSiteLoc::get(Location name, ArrayRef<Location> frames) {
assert(!frames.empty() && "required at least 1 call frame");
Location caller = frames.back();
for (auto frame : llvm::reverse(frames.drop_back()))
caller = CallSiteLoc::get(frame, caller);
return CallSiteLoc::get(name, caller);
}
//===----------------------------------------------------------------------===//
// FileLineColLoc
//===----------------------------------------------------------------------===//
FileLineColLoc FileLineColLoc::get(StringAttr filename, unsigned line,
unsigned column) {
return llvm::cast<FileLineColLoc>(
FileLineColRange::get(filename, line, column));
}
FileLineColLoc FileLineColLoc::get(MLIRContext *context, StringRef fileName,
unsigned line, unsigned column) {
return llvm::cast<FileLineColLoc>(
FileLineColRange::get(context, fileName, line, column));
}
StringAttr FileLineColLoc::getFilename() const {
return FileLineColRange::getFilename();
}
unsigned FileLineColLoc::getLine() const { return getStartLine(); }
unsigned FileLineColLoc::getColumn() const { return getStartColumn(); }
bool mlir::isStrictFileLineColLoc(Location loc) {
if (auto range = mlir::dyn_cast<FileLineColRange>(loc))
return range.getImpl()->size() == 2;
return false;
}
//===----------------------------------------------------------------------===//
// FileLineColRange
//===----------------------------------------------------------------------===//
StringAttr FileLineColRange::getFilename() const {
return getImpl()->filenameAndTrailing.getPointer();
}
unsigned FileLineColRange::getStartLine() const {
return getImpl()->getStartLine();
}
unsigned FileLineColRange::getStartColumn() const {
return getImpl()->getStartColumn();
}
unsigned FileLineColRange::getEndColumn() const {
return getImpl()->getEndColumn();
}
unsigned FileLineColRange::getEndLine() const {
return getImpl()->getEndLine();
}
//===----------------------------------------------------------------------===//
// FusedLoc
//===----------------------------------------------------------------------===//
Location FusedLoc::get(ArrayRef<Location> locs, Attribute metadata,
MLIRContext *context) {
// Unique the set of locations to be fused.
llvm::SmallSetVector<Location, 4> decomposedLocs;
for (auto loc : locs) {
// If the location is a fused location we decompose it if it has no
// metadata or the metadata is the same as the top level metadata.
if (auto fusedLoc = llvm::dyn_cast<FusedLoc>(loc)) {
if (fusedLoc.getMetadata() == metadata) {
// UnknownLoc's have already been removed from FusedLocs so we can
// simply add all of the internal locations.
decomposedLocs.insert_range(fusedLoc.getLocations());
continue;
}
}
// Otherwise, only add known locations to the set.
if (!llvm::isa<UnknownLoc>(loc))
decomposedLocs.insert(loc);
}
locs = decomposedLocs.getArrayRef();
// Handle the simple cases of less than two locations. Ensure the metadata (if
// provided) is not dropped.
if (locs.empty()) {
if (!metadata)
return UnknownLoc::get(context);
// TODO: Investigate ASAN failure when using implicit conversion from
// Location to ArrayRef<Location> below.
return Base::get(context, ArrayRef<Location>{UnknownLoc::get(context)},
metadata);
}
if (locs.size() == 1 && !metadata)
return locs.front();
return Base::get(context, locs, metadata);
}
//===----------------------------------------------------------------------===//
// BuiltinDialect
//===----------------------------------------------------------------------===//
void BuiltinDialect::registerLocationAttributes() {
addAttributes<
#define GET_ATTRDEF_LIST
#include "mlir/IR/BuiltinLocationAttributes.cpp.inc"
>();
}
|