My Project
TypeSupport.h
Go to the documentation of this file.
1 //===- TypeSupport.h --------------------------------------------*- 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 // This file defines support types for registering dialect extended types.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_IR_TYPE_SUPPORT_H
14 #define MLIR_IR_TYPE_SUPPORT_H
15 
16 #include "mlir/IR/MLIRContext.h"
18 
19 namespace mlir {
20 struct ClassID;
21 class Dialect;
22 class MLIRContext;
23 
24 //===----------------------------------------------------------------------===//
25 // TypeStorage
26 //===----------------------------------------------------------------------===//
27 
28 namespace detail {
29 class TypeUniquer;
30 } // end namespace detail
31 
34  friend detail::TypeUniquer;
35  friend StorageUniquer;
36 
37 protected:
41  TypeStorage(unsigned subclassData = 0)
42  : dialect(nullptr), subclassData(subclassData) {}
43 
44 public:
47  assert(dialect && "Malformed type storage object.");
48  return *dialect;
49  }
51  unsigned getSubclassData() const { return subclassData; }
52 
54  void setSubclassData(unsigned val) { subclassData = val; }
55 
56 private:
57  // Set the dialect for this storage instance. This is used by the TypeUniquer
58  // when initializing a newly constructed type storage object.
59  void initializeDialect(Dialect &newDialect) { dialect = &newDialect; }
60 
62  Dialect *dialect;
63 
65  unsigned subclassData;
66 };
67 
71 
72 //===----------------------------------------------------------------------===//
73 // TypeStorageAllocator
74 //===----------------------------------------------------------------------===//
75 
76 // This is a utility allocator used to allocate memory for instances of derived
77 // Types.
79 
80 //===----------------------------------------------------------------------===//
81 // TypeUniquer
82 //===----------------------------------------------------------------------===//
83 namespace detail {
84 // A utility class to get, or create, unique instances of types within an
85 // MLIRContext. This class manages all creation and uniquing of types.
86 class TypeUniquer {
87 public:
89  template <typename T, typename... Args>
90  static T get(MLIRContext *ctx, unsigned kind, Args &&... args) {
91  return ctx->getTypeUniquer().get<typename T::ImplType>(
92  [&](TypeStorage *storage) {
93  storage->initializeDialect(lookupDialectForType<T>(ctx));
94  },
95  kind, std::forward<Args>(args)...);
96  }
97 
98 private:
100  template <typename T> static Dialect &lookupDialectForType(MLIRContext *ctx) {
101  return lookupDialectForType(ctx, T::getClassID());
102  }
103 
105  static Dialect &lookupDialectForType(MLIRContext *ctx,
106  const ClassID *const typeID);
107 };
108 } // namespace detail
109 
110 } // end namespace mlir
111 
112 #endif
Definition: InferTypeOpInterface.cpp:20
Definition: STLExtras.h:95
Base storage class appearing in a Type.
Definition: TypeSupport.h:33
Definition: StorageUniquer.h:89
unsigned getSubclassData() const
Get the subclass data.
Definition: TypeSupport.h:51
static T get(MLIRContext *ctx, unsigned kind, Args &&... args)
Get an uniqued instance of a type T.
Definition: TypeSupport.h:90
Definition: Dialect.h:39
Definition: TypeSupport.h:86
Dialect & getDialect()
Get the dialect that this type is registered to.
Definition: TypeSupport.h:46
Definition: StorageUniquer.h:64
Definition: StorageUniquer.h:71
Definition: MLIRContext.h:34
void setSubclassData(unsigned val)
Set the subclass data.
Definition: TypeSupport.h:54
TypeStorage(unsigned subclassData=0)
Definition: TypeSupport.h:41