My Project
Identifier.h
Go to the documentation of this file.
1 //===- Identifier.h - MLIR Identifier Class ---------------------*- C++ -*-===//
2 //
3 // Part of the MLIR 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/StringRef.h"
15 
16 namespace mlir {
17 class MLIRContext;
18 
26 class Identifier {
27 public:
29  static Identifier get(StringRef str, MLIRContext *context);
30  Identifier(const Identifier &) = default;
31  Identifier &operator=(const Identifier &other) = default;
32 
34  StringRef strref() const { return StringRef(pointer, size()); }
35 
37  operator StringRef() const { return strref(); }
38 
40  std::string str() const { return strref().str(); }
41 
43  const char *c_str() const { return pointer; }
44 
46  const char *data() const { return pointer; }
47 
49  unsigned size() const { return ::strlen(pointer); }
50 
52  bool is(StringRef string) const { return strref().equals(string); }
53 
54  const char *begin() const { return pointer; }
55  const char *end() const { return pointer + size(); }
56 
57  void print(raw_ostream &os) const;
58  void dump() const;
59 
60  const void *getAsOpaquePointer() const {
61  return static_cast<const void *>(pointer);
62  }
63  static Identifier getFromOpaquePointer(const void *pointer) {
64  return Identifier((const char *)pointer);
65  }
66 
67 private:
69  const char *pointer;
70  explicit Identifier(const char *pointer) : pointer(pointer) {}
71 };
72 
73 inline raw_ostream &operator<<(raw_ostream &os, Identifier identifier) {
74  identifier.print(os);
75  return os;
76 }
77 
78 inline bool operator==(Identifier lhs, Identifier rhs) {
79  return lhs.data() == rhs.data();
80 }
81 
82 inline bool operator!=(Identifier lhs, Identifier rhs) {
83  return lhs.data() != rhs.data();
84 }
85 
86 inline bool operator==(Identifier lhs, StringRef rhs) { return lhs.is(rhs); }
87 inline bool operator!=(Identifier lhs, StringRef rhs) { return !lhs.is(rhs); }
88 inline bool operator==(StringRef lhs, Identifier rhs) { return rhs.is(lhs); }
89 inline bool operator!=(StringRef lhs, Identifier rhs) { return !rhs.is(lhs); }
90 
91 // Make identifiers hashable.
92 inline llvm::hash_code hash_value(Identifier arg) {
93  return llvm::hash_value(arg.strref());
94 }
95 
96 } // end namespace mlir
97 
98 namespace llvm {
99 // Identifiers hash just like pointers, there is no need to hash the bytes.
100 template <>
105  }
109  }
110  static unsigned getHashValue(mlir::Identifier Val) {
112  }
113  static bool isEqual(mlir::Identifier LHS, mlir::Identifier RHS) {
114  return LHS == RHS;
115  }
116 };
117 
121 template <>
122 struct PointerLikeTypeTraits<mlir::Identifier> {
123 public:
124  static inline void *getAsVoidPointer(mlir::Identifier I) {
125  return const_cast<void *>(I.getAsOpaquePointer());
126  }
127  static inline mlir::Identifier getFromVoidPointer(void *P) {
129  }
130  enum { NumLowBitsAvailable = 2 };
131 };
132 
133 } // end namespace llvm
134 #endif
Definition: InferTypeOpInterface.cpp:20
const char * end() const
Definition: Identifier.h:55
Definition: PassRegistry.cpp:413
Definition: LLVM.h:45
bool operator!=(IntInfty lhs, IntInfty rhs)
Definition: SDBM.h:94
Definition: Identifier.h:26
StringRef strref() const
Return a StringRef for the string.
Definition: Identifier.h:34
static mlir::Identifier getFromVoidPointer(void *P)
Definition: Identifier.h:127
std::string str() const
Return an std::string.
Definition: Identifier.h:40
const char * data() const
Return a pointer to the start of the string data.
Definition: Identifier.h:46
static void * getAsVoidPointer(mlir::Identifier I)
Definition: Identifier.h:124
unsigned size() const
Return the number of bytes in this string.
Definition: Identifier.h:49
static mlir::Identifier getTombstoneKey()
Definition: Identifier.h:106
Identifier & operator=(const Identifier &other)=default
bool is(StringRef string) const
Return true if this identifier is the specified string.
Definition: Identifier.h:52
void dump() const
Definition: AsmPrinter.cpp:42
static Identifier getFromOpaquePointer(const void *pointer)
Definition: Identifier.h:63
llvm::hash_code hash_value(Identifier arg)
Definition: Identifier.h:92
bool operator==(IntInfty lhs, IntInfty rhs)
Definition: SDBM.h:90
static unsigned getHashValue(mlir::Identifier Val)
Definition: Identifier.h:110
inline ::llvm::hash_code hash_value(AffineExpr arg)
Make AffineExpr hashable.
Definition: AffineExpr.h:201
static bool isEqual(mlir::Identifier LHS, mlir::Identifier RHS)
Definition: Identifier.h:113
void print(raw_ostream &os) const
Definition: AsmPrinter.cpp:40
Definition: MLIRContext.h:34
Identifier(const Identifier &)=default
raw_ostream & operator<<(raw_ostream &os, SubViewOp::Range &range)
Definition: Ops.cpp:2759
const char * c_str() const
Return a null terminated C string.
Definition: Identifier.h:43
const char * begin() const
Definition: Identifier.h:54
const void * getAsOpaquePointer() const
Definition: Identifier.h:60
static mlir::Identifier getEmptyKey()
Definition: Identifier.h:102