My Project
AttributeSupport.h
Go to the documentation of this file.
1 //===- AttributeSupport.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 attributes.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_IR_ATTRIBUTESUPPORT_H
14 #define MLIR_IR_ATTRIBUTESUPPORT_H
15 
16 #include "mlir/IR/MLIRContext.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 
20 namespace mlir {
21 class MLIRContext;
22 class Type;
23 
24 //===----------------------------------------------------------------------===//
25 // AttributeStorage
26 //===----------------------------------------------------------------------===//
27 
28 namespace detail {
29 class AttributeUniquer;
30 } // end namespace detail
31 
36  friend StorageUniquer;
37 
38 public:
40  Type getType() const;
41 
43  Dialect &getDialect() const {
44  assert(dialect && "Malformed attribute storage object.");
45  return const_cast<Dialect &>(*dialect);
46  }
47 
48 protected:
53  AttributeStorage(Type type);
55 
57  void setType(Type type);
58 
59  // Set the dialect for this storage instance. This is used by the
60  // AttributeUniquer when initializing a newly constructed storage object.
61  void initializeDialect(Dialect &newDialect) { dialect = &newDialect; }
62 
63 private:
65  Dialect *dialect;
66 
68  const void *type;
69 };
70 
74 
75 //===----------------------------------------------------------------------===//
76 // AttributeStorageAllocator
77 //===----------------------------------------------------------------------===//
78 
79 // This is a utility allocator used to allocate memory for instances of derived
80 // Attributes.
82 
83 //===----------------------------------------------------------------------===//
84 // AttributeUniquer
85 //===----------------------------------------------------------------------===//
86 namespace detail {
87 // A utility class to get, or create, unique instances of attributes within an
88 // MLIRContext. This class manages all creation and uniquing of attributes.
90 public:
92  template <typename T, typename... Args>
93  static T get(MLIRContext *ctx, unsigned kind, Args &&... args) {
94  return ctx->getAttributeUniquer().get<typename T::ImplType>(
95  getInitFn(ctx, T::getClassID()), kind, std::forward<Args>(args)...);
96  }
97 
98 private:
100  static std::function<void(AttributeStorage *)>
101  getInitFn(MLIRContext *ctx, const ClassID *const attrID);
102 };
103 } // namespace detail
104 
105 } // end namespace mlir
106 
107 #endif
Definition: InferTypeOpInterface.cpp:20
Definition: STLExtras.h:95
Definition: Attributes.h:139
Definition: StorageUniquer.h:89
static T get(MLIRContext *ctx, unsigned kind, Args &&... args)
Get an uniqued instance of attribute T.
Definition: AttributeSupport.h:93
Definition: Dialect.h:39
Definition: StorageUniquer.h:64
Definition: Types.h:84
Definition: StorageUniquer.h:71
void initializeDialect(Dialect &newDialect)
Definition: AttributeSupport.h:61
Definition: MLIRContext.h:34
Definition: AttributeSupport.h:34
Dialect & getDialect() const
Get the dialect of this attribute.
Definition: AttributeSupport.h:43
Definition: AttributeSupport.h:89