xref: /llvm-project/mlir/include/mlir/IR/BuiltinLocationAttributes.td (revision 01e75646a5d4977a9e441e3db1042df0beccc4bb)
1//===- BuiltinLocationAttributes.td - Builtin Locations ----*- tablegen -*-===//
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// Defines the set of builtin MLIR location attributes.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef BUILTIN_LOCATION_ATTRIBUTES_TD
14#define BUILTIN_LOCATION_ATTRIBUTES_TD
15
16include "mlir/IR/AttrTypeBase.td"
17include "mlir/IR/BuiltinDialect.td"
18
19// Base class for Builtin dialect location attributes.
20class Builtin_LocationAttr<string name, list<Trait> traits = []>
21    : LocationAttrDef<Builtin_Dialect, name, traits> {
22  let cppClassName = name;
23  let mnemonic = ?;
24}
25
26//===----------------------------------------------------------------------===//
27// CallSiteLoc
28//===----------------------------------------------------------------------===//
29
30def CallSiteLoc : Builtin_LocationAttr<"CallSiteLoc"> {
31  let summary = "A callsite source location";
32  let description = [{
33    Syntax:
34
35    ```
36    callsite-location ::= `callsite` `(` location `at` location `)`
37    ```
38
39    An instance of this location allows for representing a directed stack of
40    location usages. This connects a location of a `callee` with the location
41    of a `caller`.
42
43    Example:
44
45    ```mlir
46    loc(callsite("foo" at "mysource.cc":10:8))
47    ```
48  }];
49  let parameters = (ins "Location":$callee, "Location":$caller);
50  let builders = [
51    AttrBuilderWithInferredContext<(ins "Location":$callee,
52                                        "Location":$caller), [{
53      return $_get(callee->getContext(), callee, caller);
54    }]>,
55    AttrBuilderWithInferredContext<(ins "Location":$name,
56                                        "ArrayRef<Location>":$frames)>
57  ];
58  let skipDefaultBuilders = 1;
59  let attrName = "builtin.call_site_loc";
60}
61
62//===----------------------------------------------------------------------===//
63// FileLineColRange
64//===----------------------------------------------------------------------===//
65
66def FileLineColRange : Builtin_LocationAttr<"FileLineColRange"> {
67  let summary = "A file:line:column source location range";
68  let description = [{
69    Syntax:
70
71    ```
72    filelinecol-location ::= string-literal `:` integer-literal `:`
73                             integer-literal
74                             (`to` (integer-literal ?) `:` integer-literal ?)
75    ```
76
77    An instance of this location represents a tuple of file, start and end line
78    number, and start and end column number. It allows for the following
79    configurations:
80
81    *   A single file line location: `file:line`;
82    *   A single file line col location: `file:line:column`;
83    *   A single line range: `file:line:column to :column`;
84    *   A single file range: `file:line:column to line:column`;
85
86    Example:
87
88    ```mlir
89    loc("mysource.cc":10:8 to 12:18)
90    ```
91  }];
92
93  let parameters = (ins "StringAttr":$filename,
94    "unsigned":$start_line, "unsigned":$start_column,
95    "unsigned":$end_line, "unsigned":$end_column);
96  let builders = [
97    AttrBuilderWithInferredContext<(ins "StringAttr":$filename), [{
98      return $_get(filename.getContext(), filename, ArrayRef<unsigned>{});
99    }]>,
100    AttrBuilderWithInferredContext<(ins "StringAttr":$filename,
101                                        "unsigned":$line), [{
102      return $_get(filename.getContext(), filename,
103                   ArrayRef<unsigned>{line});
104    }]>,
105    AttrBuilderWithInferredContext<(ins "StringAttr":$filename,
106                                        "unsigned":$line,
107                                        "unsigned":$column), [{
108      return $_get(filename.getContext(), filename,
109                   ArrayRef<unsigned>{line, column});
110    }]>,
111    AttrBuilder<(ins "::llvm::StringRef":$filename,
112                     "unsigned":$start_line,
113                     "unsigned":$start_column), [{
114      return $_get($_ctxt,
115        StringAttr::get($_ctxt, filename.empty() ? "-" : filename),
116        ArrayRef<unsigned>{start_line, start_column});
117    }]>,
118    AttrBuilderWithInferredContext<(ins "::mlir::StringAttr":$filename,
119                                        "unsigned":$line,
120                                        "unsigned":$start_column,
121                                        "unsigned":$end_column), [{
122      return $_get(filename.getContext(), filename,
123                   ArrayRef<unsigned>{line, start_column, end_column});
124    }]>,
125    AttrBuilderWithInferredContext<(ins "::mlir::StringAttr":$filename,
126                                        "unsigned":$start_line,
127                                        "unsigned":$start_column,
128                                        "unsigned":$end_line,
129                                        "unsigned":$end_column), [{
130      return $_get(filename.getContext(), filename,
131        ArrayRef<unsigned>{start_line, start_column, end_column, end_line});
132    }]>,
133    AttrBuilder<(ins "::llvm::StringRef":$filename,
134                     "unsigned":$start_line,
135                     "unsigned":$start_column,
136                     "unsigned":$end_line,
137                     "unsigned":$end_column), [{
138      return $_get($_ctxt,
139        StringAttr::get($_ctxt, filename.empty() ? "-" : filename),
140        ArrayRef<unsigned>{start_line, start_column, end_column, end_line});
141    }]>,
142  ];
143
144  let extraClassDeclaration = [{
145    ::mlir::StringAttr getFilename() const;
146    unsigned getStartLine() const;
147    unsigned getStartColumn() const;
148    unsigned getEndColumn() const;
149    unsigned getEndLine() const;
150  }];
151  let skipDefaultBuilders = 1;
152  let genAccessors = 0;
153  let genStorageClass = 0;
154  let attrName = "builtin.file_line_range";
155}
156
157//===----------------------------------------------------------------------===//
158// FusedLoc
159//===----------------------------------------------------------------------===//
160
161def FusedLoc : Builtin_LocationAttr<"FusedLoc"> {
162  let summary = "A tuple of other source locations";
163  let description = [{
164    Syntax:
165
166    ```
167    fusion-metadata ::= `<` attribute-value `>`
168    fused-location ::= `fused` fusion-metadata? `[` (location (`,` location)* )? `]`
169    ```
170
171    An instance of a `fused` location represents a grouping of several other
172    source locations, with optional metadata that describes the context of the
173    fusion. There are many places within a compiler in which several constructs
174    may be fused together, e.g. pattern rewriting, that normally result partial
175    or even total loss of location information. With `fused` locations, this is
176    a non-issue.
177
178    Example:
179
180    ```mlir
181    loc(fused["mysource.cc":10:8, "mysource.cc":22:8])
182    loc(fused<"CSE">["mysource.cc":10:8, "mysource.cc":22:8])
183    ```
184  }];
185  let parameters = (ins ArrayRefParameter<"Location", "">:$locations,
186                        "Attribute":$metadata);
187  let extraClassDeclaration = [{
188    static Location get(ArrayRef<Location> locs, Attribute metadata,
189                        MLIRContext *context);
190    static Location get(MLIRContext *context, ArrayRef<Location> locs) {
191      return get(locs, Attribute(), context);
192    }
193  }];
194  let attrName = "builtin.fused_loc";
195}
196
197//===----------------------------------------------------------------------===//
198// NameLoc
199//===----------------------------------------------------------------------===//
200
201def NameLoc : Builtin_LocationAttr<"NameLoc"> {
202  let summary = "A named source location";
203  let description = [{
204    Syntax:
205
206    ```
207    name-location ::= string-literal (`(` location `)`)?
208    ```
209
210    An instance of this location allows for attaching a name to a child location.
211    This can be useful for representing the locations of variable, or node,
212    definitions.
213
214    #### Example:
215
216    ```mlir
217    loc("CSE"("mysource.cc":10:8))
218    ```
219  }];
220  let parameters = (ins "StringAttr":$name, "Location":$childLoc);
221  let builders = [
222    AttrBuilderWithInferredContext<(ins "StringAttr":$name,
223                                        "Location":$childLoc), [{
224      return $_get(name.getContext(), name, childLoc);
225    }]>,
226    AttrBuilderWithInferredContext<(ins "StringAttr":$name), [{
227      return $_get(name.getContext(), name,
228                   UnknownLoc::get(name.getContext()));
229    }]>
230  ];
231  let skipDefaultBuilders = 1;
232  let attrName = "builtin.name_loc";
233}
234
235//===----------------------------------------------------------------------===//
236// OpaqueLoc
237//===----------------------------------------------------------------------===//
238
239def OpaqueLoc : Builtin_LocationAttr<"OpaqueLoc"> {
240  let summary = "An opaque source location";
241  let description = [{
242    An instance of this location essentially contains a pointer to some data
243    structure that is external to MLIR and an optional location that can be used
244    if the first one is not suitable. Since it contains an external structure,
245    only the optional location is used during serialization.
246
247    #### Example:
248
249    ```mlir
250    %0 = "example.operation"() : () -> i32 loc("mysource")
251    %1 = arith.constant 4 : index loc(callsite("mysum" at "mysource.cc":10:8))
252    ```
253  }];
254  let parameters = (ins "uintptr_t":$underlyingLocation,
255                        "TypeID":$underlyingTypeID,
256                        "Location":$fallbackLocation);
257  let builders = [
258    AttrBuilderWithInferredContext<(ins "uintptr_t":$underlyingLocation,
259                                        "TypeID":$underlyingTypeID,
260                                        "Location":$fallbackLocation), [{
261      return $_get(fallbackLocation->getContext(), underlyingLocation,
262                   underlyingTypeID, fallbackLocation);
263    }]>
264  ];
265  let extraClassDeclaration = [{
266    /// Returns an instance of opaque location which contains a given pointer to
267    /// an object. The corresponding MLIR location is set to UnknownLoc.
268    template <typename T>
269    static OpaqueLoc get(T underlyingLocation, MLIRContext *context);
270
271    /// Returns an instance of opaque location which contains a given pointer to
272    /// an object and an additional MLIR location.
273    template <typename T>
274    static OpaqueLoc get(T underlyingLocation, Location fallbackLocation) {
275      return get(reinterpret_cast<uintptr_t>(underlyingLocation),
276                 TypeID::get<T>(), fallbackLocation);
277    }
278
279    /// Returns a pointer to some data structure that opaque location stores.
280    template <typename T> static T getUnderlyingLocation(Location location) {
281      assert(isa<T>(location));
282      return reinterpret_cast<T>(
283          mlir::cast<mlir::OpaqueLoc>(static_cast<LocationAttr>(location))
284              .getUnderlyingLocation());
285    }
286
287    /// Returns a pointer to some data structure that opaque location stores.
288    /// Returns nullptr if provided location is not opaque location or if it
289    /// contains a pointer of different type.
290    template <typename T>
291    static T getUnderlyingLocationOrNull(Location location) {
292      return isa<T>(location)
293                ? reinterpret_cast<T>(mlir::cast<mlir::OpaqueLoc>(
294                                          static_cast<LocationAttr>(location))
295                                          .getUnderlyingLocation())
296                : T(nullptr);
297    }
298
299    /// Checks whether provided location is opaque location and contains a
300    /// pointer to an object of particular type.
301    template <typename T> static bool isa(Location location) {
302      auto opaque_loc =
303          mlir::dyn_cast<OpaqueLoc>(static_cast<LocationAttr>(location));
304      return opaque_loc && opaque_loc.getUnderlyingTypeID() == TypeID::get<T>();
305    }
306  }];
307  let skipDefaultBuilders = 1;
308  let attrName = "builtin.opaque_loc";
309}
310
311//===----------------------------------------------------------------------===//
312// UnknownLoc
313//===----------------------------------------------------------------------===//
314
315def UnknownLoc : Builtin_LocationAttr<"UnknownLoc"> {
316  let summary = "An unspecified source location";
317  let description = [{
318    Syntax:
319
320    ```
321    unknown-location ::= `?`
322    ```
323
324    Source location information is an extremely integral part of the MLIR
325    infrastructure. As such, location information is always present in the IR,
326    and must explicitly be set to unknown. Thus, an instance of the `unknown`
327    location represents an unspecified source location.
328
329    Example:
330
331    ```mlir
332    loc(?)
333    ```
334  }];
335  let extraClassDeclaration = [{
336    static UnknownLoc get(MLIRContext *context);
337  }];
338  let attrName = "builtin.unknown_loc";
339}
340
341#endif // BUILTIN_LOCATION_ATTRIBUTES_TD
342