Coverage Report

Created: 2020-06-26 05:44

/home/arjun/llvm-project/mlir/include/mlir/IR/Identifier.h
Line
Count
Source (jump to first uncovered line)
1
//===- Identifier.h - MLIR Identifier Class ---------------------*- C++ -*-===//
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
#ifndef MLIR_IR_IDENTIFIER_H
10
#define MLIR_IR_IDENTIFIER_H
11
12
#include "mlir/Support/LLVM.h"
13
#include "llvm/ADT/DenseMapInfo.h"
14
#include "llvm/ADT/StringMapEntry.h"
15
#include "llvm/Support/PointerLikeTypeTraits.h"
16
17
namespace mlir {
18
class MLIRContext;
19
20
/// This class represents a uniqued string owned by an MLIRContext.  Strings
21
/// represented by this type cannot contain nul characters, and may not have a
22
/// zero length.
23
///
24
/// This is a POD type with pointer size, so it should be passed around by
25
/// value.  The underlying data is owned by MLIRContext and is thus immortal for
26
/// almost all clients.
27
class Identifier {
28
  using EntryType = llvm::StringMapEntry<llvm::NoneType>;
29
30
public:
31
  /// Return an identifier for the specified string.
32
  static Identifier get(StringRef str, MLIRContext *context);
33
  Identifier(const Identifier &) = default;
34
  Identifier &operator=(const Identifier &other) = default;
35
36
  /// Return a StringRef for the string.
37
0
  StringRef strref() const { return entry->first(); }
38
39
  /// Identifiers implicitly convert to StringRefs.
40
0
  operator StringRef() const { return strref(); }
41
42
  /// Return an std::string.
43
0
  std::string str() const { return strref().str(); }
44
45
  /// Return a null terminated C string.
46
0
  const char *c_str() const { return entry->getKeyData(); }
47
48
  /// Return a pointer to the start of the string data.
49
0
  const char *data() const { return entry->getKeyData(); }
50
51
  /// Return the number of bytes in this string.
52
0
  unsigned size() const { return entry->getKeyLength(); }
53
54
0
  const char *begin() const { return data(); }
55
0
  const char *end() const { return entry->getKeyData() + size(); }
56
57
0
  bool operator==(Identifier other) const { return entry == other.entry; }
58
0
  bool operator!=(Identifier rhs) const { return !(*this == rhs); }
59
60
  void print(raw_ostream &os) const;
61
  void dump() const;
62
63
0
  const void *getAsOpaquePointer() const {
64
0
    return static_cast<const void *>(entry);
65
0
  }
66
0
  static Identifier getFromOpaquePointer(const void *entry) {
67
0
    return Identifier(static_cast<const EntryType *>(entry));
68
0
  }
69
70
private:
71
  /// This contains the bytes of the string, which is guaranteed to be nul
72
  /// terminated.
73
  const EntryType *entry;
74
0
  explicit Identifier(const EntryType *entry) : entry(entry) {}
75
};
76
77
0
inline raw_ostream &operator<<(raw_ostream &os, Identifier identifier) {
78
0
  identifier.print(os);
79
0
  return os;
80
0
}
81
82
// Identifier/Identifier equality comparisons are defined inline.
83
0
inline bool operator==(Identifier lhs, StringRef rhs) {
84
0
  return lhs.strref() == rhs;
85
0
}
86
0
inline bool operator!=(Identifier lhs, StringRef rhs) { return !(lhs == rhs); }
87
88
0
inline bool operator==(StringRef lhs, Identifier rhs) {
89
0
  return rhs.strref() == lhs;
90
0
}
91
0
inline bool operator!=(StringRef lhs, Identifier rhs) { return !(lhs == rhs); }
92
93
// Make identifiers hashable.
94
0
inline llvm::hash_code hash_value(Identifier arg) {
95
0
  // Identifiers are uniqued, so we can just hash the pointer they contain.
96
0
  return llvm::hash_value(arg.getAsOpaquePointer());
97
0
}
98
} // end namespace mlir
99
100
namespace llvm {
101
// Identifiers hash just like pointers, there is no need to hash the bytes.
102
template <>
103
struct DenseMapInfo<mlir::Identifier> {
104
0
  static mlir::Identifier getEmptyKey() {
105
0
    auto pointer = llvm::DenseMapInfo<const void *>::getEmptyKey();
106
0
    return mlir::Identifier::getFromOpaquePointer(pointer);
107
0
  }
108
0
  static mlir::Identifier getTombstoneKey() {
109
0
    auto pointer = llvm::DenseMapInfo<const void *>::getTombstoneKey();
110
0
    return mlir::Identifier::getFromOpaquePointer(pointer);
111
0
  }
112
0
  static unsigned getHashValue(mlir::Identifier val) {
113
0
    return mlir::hash_value(val);
114
0
  }
115
0
  static bool isEqual(mlir::Identifier lhs, mlir::Identifier rhs) {
116
0
    return lhs == rhs;
117
0
  }
118
};
119
120
/// The pointer inside of an identifier comes from a StringMap, so its alignment
121
/// is always at least 4 and probably 8 (on 64-bit machines).  Allow LLVM to
122
/// steal the low bits.
123
template <>
124
struct PointerLikeTypeTraits<mlir::Identifier> {
125
public:
126
0
  static inline void *getAsVoidPointer(mlir::Identifier i) {
127
0
    return const_cast<void *>(i.getAsOpaquePointer());
128
0
  }
129
0
  static inline mlir::Identifier getFromVoidPointer(void *p) {
130
0
    return mlir::Identifier::getFromOpaquePointer(p);
131
0
  }
132
  static constexpr int NumLowBitsAvailable = 2;
133
};
134
135
} // end namespace llvm
136
#endif