github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/lib/IR/LocationDetail.h (about)

     1  //===- LocationDetail.h - MLIR Location storage details ---------*- C++ -*-===//
     2  //
     3  // Copyright 2019 The MLIR Authors.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //   http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  // =============================================================================
    17  //
    18  // This holds implementation details of the location attributes.
    19  //
    20  //===----------------------------------------------------------------------===//
    21  #ifndef MLIR_IR_LOCATIONDETAIL_H_
    22  #define MLIR_IR_LOCATIONDETAIL_H_
    23  
    24  #include "mlir/IR/Attributes.h"
    25  #include "mlir/IR/Identifier.h"
    26  #include "mlir/IR/Location.h"
    27  #include "llvm/ADT/StringRef.h"
    28  #include "llvm/Support/TrailingObjects.h"
    29  
    30  namespace mlir {
    31  
    32  namespace detail {
    33  
    34  struct CallSiteLocationStorage : public AttributeStorage {
    35    CallSiteLocationStorage(Location callee, Location caller)
    36        : callee(callee), caller(caller) {}
    37  
    38    /// The hash key used for uniquing.
    39    using KeyTy = std::pair<Location, Location>;
    40    bool operator==(const KeyTy &key) const {
    41      return key == KeyTy(callee, caller);
    42    }
    43  
    44    /// Construct a new storage instance.
    45    static CallSiteLocationStorage *
    46    construct(AttributeStorageAllocator &allocator, const KeyTy &key) {
    47      return new (allocator.allocate<CallSiteLocationStorage>())
    48          CallSiteLocationStorage(key.first, key.second);
    49    }
    50  
    51    Location callee, caller;
    52  };
    53  
    54  struct FileLineColLocationStorage : public AttributeStorage {
    55    FileLineColLocationStorage(Identifier filename, unsigned line,
    56                               unsigned column)
    57        : filename(filename), line(line), column(column) {}
    58  
    59    /// The hash key used for uniquing.
    60    using KeyTy = std::tuple<Identifier, unsigned, unsigned>;
    61    bool operator==(const KeyTy &key) const {
    62      return key == KeyTy(filename, line, column);
    63    }
    64  
    65    /// Construct a new storage instance.
    66    static FileLineColLocationStorage *
    67    construct(AttributeStorageAllocator &allocator, const KeyTy &key) {
    68      return new (allocator.allocate<FileLineColLocationStorage>())
    69          FileLineColLocationStorage(std::get<0>(key), std::get<1>(key),
    70                                     std::get<2>(key));
    71    }
    72  
    73    Identifier filename;
    74    unsigned line, column;
    75  };
    76  
    77  struct FusedLocationStorage final
    78      : public AttributeStorage,
    79        public llvm::TrailingObjects<FusedLocationStorage, Location> {
    80    FusedLocationStorage(unsigned numLocs, Attribute metadata)
    81        : numLocs(numLocs), metadata(metadata) {}
    82  
    83    ArrayRef<Location> getLocations() const {
    84      return ArrayRef<Location>(getTrailingObjects<Location>(), numLocs);
    85    }
    86  
    87    /// The hash key used for uniquing.
    88    using KeyTy = std::pair<ArrayRef<Location>, Attribute>;
    89    bool operator==(const KeyTy &key) const {
    90      return key == KeyTy(getLocations(), metadata);
    91    }
    92  
    93    /// Construct a new storage instance.
    94    static FusedLocationStorage *construct(AttributeStorageAllocator &allocator,
    95                                           const KeyTy &key) {
    96      ArrayRef<Location> locs = key.first;
    97  
    98      auto byteSize = totalSizeToAlloc<Location>(locs.size());
    99      auto rawMem = allocator.allocate(byteSize, alignof(FusedLocationStorage));
   100      auto result = new (rawMem) FusedLocationStorage(locs.size(), key.second);
   101  
   102      std::uninitialized_copy(locs.begin(), locs.end(),
   103                              result->getTrailingObjects<Location>());
   104      return result;
   105    }
   106  
   107    // This stuff is used by the TrailingObjects template.
   108    friend llvm::TrailingObjects<FusedLocationStorage, Location>;
   109    size_t numTrailingObjects(OverloadToken<Location>) const { return numLocs; }
   110  
   111    /// Number of trailing location objects.
   112    unsigned numLocs;
   113  
   114    /// Metadata used to reason about the generation of this fused location.
   115    Attribute metadata;
   116  };
   117  
   118  struct NameLocationStorage : public AttributeStorage {
   119    NameLocationStorage(Identifier name, Location child)
   120        : name(name), child(child) {}
   121  
   122    /// The hash key used for uniquing.
   123    using KeyTy = std::pair<Identifier, Location>;
   124    bool operator==(const KeyTy &key) const { return key == KeyTy(name, child); }
   125  
   126    /// Construct a new storage instance.
   127    static NameLocationStorage *construct(AttributeStorageAllocator &allocator,
   128                                          const KeyTy &key) {
   129      return new (allocator.allocate<NameLocationStorage>())
   130          NameLocationStorage(key.first, key.second);
   131    }
   132  
   133    Identifier name;
   134    Location child;
   135  };
   136  
   137  } // end namespace detail
   138  } // end namespace mlir
   139  
   140  #endif // MLIR_IR_LOCATIONDETAIL_H_