summaryrefslogtreecommitdiff
path: root/mlir/lib/IR/Location.cpp
blob: 1ea75d5e30e6d51653f2a243dbfea6038ccc6205 (plain)
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
//===- Location.cpp - MLIR Location Classes -------------------------------===//
//
// Copyright 2019 The MLIR Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================

#include "mlir/IR/Location.h"
#include "LocationDetail.h"
#include "llvm/ADT/SetVector.h"

using namespace mlir;
using namespace mlir::detail;

//===----------------------------------------------------------------------===//
// CallSiteLoc
//===----------------------------------------------------------------------===//

Location CallSiteLoc::get(Location callee, Location caller) {
  return Base::get(callee->getContext(), StandardAttributes::CallSiteLocation,
                   callee, caller);
}

Location 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);
}

Location CallSiteLoc::getCallee() const { return getImpl()->callee; }

Location CallSiteLoc::getCaller() const { return getImpl()->caller; }

//===----------------------------------------------------------------------===//
// FileLineColLoc
//===----------------------------------------------------------------------===//

Location FileLineColLoc::get(Identifier filename, unsigned line,
                             unsigned column, MLIRContext *context) {
  return Base::get(context, StandardAttributes::FileLineColLocation, filename,
                   line, column);
}

Location FileLineColLoc::get(StringRef filename, unsigned line, unsigned column,
                             MLIRContext *context) {
  return get(Identifier::get(filename.empty() ? "-" : filename, context), line,
             column, context);
}

StringRef FileLineColLoc::getFilename() const { return getImpl()->filename; }
unsigned FileLineColLoc::getLine() const { return getImpl()->line; }
unsigned FileLineColLoc::getColumn() const { return getImpl()->column; }

//===----------------------------------------------------------------------===//
// 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 = loc.dyn_cast<FusedLoc>()) {
      if (fusedLoc.getMetadata() == metadata) {
        // UnknownLoc's have already been removed from FusedLocs so we can
        // simply add all of the internal locations.
        decomposedLocs.insert(fusedLoc.getLocations().begin(),
                              fusedLoc.getLocations().end());
        continue;
      }
    }
    // Otherwise, only add known locations to the set.
    if (!loc.isa<UnknownLoc>())
      decomposedLocs.insert(loc);
  }
  locs = decomposedLocs.getArrayRef();

  // Handle the simple cases of less than two locations.
  if (locs.empty())
    return UnknownLoc::get(context);
  if (locs.size() == 1)
    return locs.front();
  return Base::get(context, StandardAttributes::FusedLocation, locs, metadata);
}

ArrayRef<Location> FusedLoc::getLocations() const {
  return getImpl()->getLocations();
}

Attribute FusedLoc::getMetadata() const { return getImpl()->metadata; }

//===----------------------------------------------------------------------===//
// NameLoc
//===----------------------------------------------------------------------===//

Location NameLoc::get(Identifier name, Location child) {
  assert(!child.isa<NameLoc>() &&
         "a NameLoc cannot be used as a child of another NameLoc");
  return Base::get(child->getContext(), StandardAttributes::NameLocation, name,
                   child);
}

Location NameLoc::get(Identifier name, MLIRContext *context) {
  return get(name, UnknownLoc::get(context));
}

/// Return the name identifier.
Identifier NameLoc::getName() const { return getImpl()->name; }

/// Return the child location.
Location NameLoc::getChildLoc() const { return getImpl()->child; }

//===----------------------------------------------------------------------===//
// OpaqueLoc
//===----------------------------------------------------------------------===//

Location OpaqueLoc::get(uintptr_t underlyingLocation, ClassID *classID,
                        Location fallbackLocation) {
  return Base::get(fallbackLocation->getContext(),
                   StandardAttributes::OpaqueLocation, underlyingLocation,
                   classID, fallbackLocation);
}

uintptr_t OpaqueLoc::getUnderlyingLocation() const {
  return Base::getImpl()->underlyingLocation;
}

ClassID *OpaqueLoc::getClassId() const { return getImpl()->classId; }

Location OpaqueLoc::getFallbackLocation() const {
  return Base::getImpl()->fallbackLocation;
}