1 //===- Location.cpp - MLIR Location Classes -------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "mlir/IR/Location.h"
10 #include "mlir/IR/BuiltinDialect.h"
11 #include "mlir/IR/Visitors.h"
12 #include "llvm/ADT/SetVector.h"
13 #include "llvm/ADT/TypeSwitch.h"
16 using namespace mlir::detail
;
18 //===----------------------------------------------------------------------===//
19 /// Tablegen Attribute Definitions
20 //===----------------------------------------------------------------------===//
22 #define GET_ATTRDEF_CLASSES
23 #include "mlir/IR/BuiltinLocationAttributes.cpp.inc"
25 //===----------------------------------------------------------------------===//
27 //===----------------------------------------------------------------------===//
29 void BuiltinDialect::registerLocationAttributes() {
31 #define GET_ATTRDEF_LIST
32 #include "mlir/IR/BuiltinLocationAttributes.cpp.inc"
36 //===----------------------------------------------------------------------===//
38 //===----------------------------------------------------------------------===//
40 WalkResult
LocationAttr::walk(function_ref
<WalkResult(Location
)> walkFn
) {
41 AttrTypeWalker walker
;
42 // Walk locations, but skip any other attribute.
43 walker
.addWalk([&](Attribute attr
) {
44 if (auto loc
= llvm::dyn_cast
<LocationAttr
>(attr
))
47 return WalkResult::skip();
49 return walker
.walk
<WalkOrder::PreOrder
>(*this);
52 /// Methods for support type inquiry through isa, cast, and dyn_cast.
53 bool LocationAttr::classof(Attribute attr
) {
54 return attr
.hasTrait
<AttributeTrait::IsLocation
>();
57 //===----------------------------------------------------------------------===//
59 //===----------------------------------------------------------------------===//
61 CallSiteLoc
CallSiteLoc::get(Location name
, ArrayRef
<Location
> frames
) {
62 assert(!frames
.empty() && "required at least 1 call frame");
63 Location caller
= frames
.back();
64 for (auto frame
: llvm::reverse(frames
.drop_back()))
65 caller
= CallSiteLoc::get(frame
, caller
);
66 return CallSiteLoc::get(name
, caller
);
69 //===----------------------------------------------------------------------===//
71 //===----------------------------------------------------------------------===//
73 Location
FusedLoc::get(ArrayRef
<Location
> locs
, Attribute metadata
,
74 MLIRContext
*context
) {
75 // Unique the set of locations to be fused.
76 llvm::SmallSetVector
<Location
, 4> decomposedLocs
;
77 for (auto loc
: locs
) {
78 // If the location is a fused location we decompose it if it has no
79 // metadata or the metadata is the same as the top level metadata.
80 if (auto fusedLoc
= llvm::dyn_cast
<FusedLoc
>(loc
)) {
81 if (fusedLoc
.getMetadata() == metadata
) {
82 // UnknownLoc's have already been removed from FusedLocs so we can
83 // simply add all of the internal locations.
84 decomposedLocs
.insert(fusedLoc
.getLocations().begin(),
85 fusedLoc
.getLocations().end());
89 // Otherwise, only add known locations to the set.
90 if (!llvm::isa
<UnknownLoc
>(loc
))
91 decomposedLocs
.insert(loc
);
93 locs
= decomposedLocs
.getArrayRef();
95 // Handle the simple cases of less than two locations. Ensure the metadata (if
96 // provided) is not dropped.
99 return UnknownLoc::get(context
);
100 // TODO: Investigate ASAN failure when using implicit conversion from
101 // Location to ArrayRef<Location> below.
102 return Base::get(context
, ArrayRef
<Location
>{UnknownLoc::get(context
)},
105 if (locs
.size() == 1 && !metadata
)
108 return Base::get(context
, locs
, metadata
);