My Project
StorageUniquerSupport.h
Go to the documentation of this file.
1 //===- StorageUniquerSupport.h - MLIR Storage Uniquer Utilities -*- 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 utility classes for interfacing with StorageUniquer.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_IR_STORAGEUNIQUERSUPPORT_H
14 #define MLIR_IR_STORAGEUNIQUERSUPPORT_H
15 
17 #include "mlir/Support/STLExtras.h"
19 
20 namespace mlir {
21 class Location;
22 class MLIRContext;
23 
24 namespace detail {
28 template <typename ConcreteT, typename BaseT, typename StorageT,
29  typename UniquerT>
30 class StorageUserBase : public BaseT {
31 public:
32  using BaseT::BaseT;
33 
36  using ImplType = StorageT;
37 
39  static ClassID *getClassID() { return ClassID::getID<ConcreteT>(); }
40 
43  template <typename T> static bool classof(T val) {
44  static_assert(std::is_convertible<ConcreteT, T>::value,
45  "casting from a non-convertible type");
46  return ConcreteT::kindof(val.getKind());
47  }
48 
49 protected:
53  template <typename... Args>
54  static ConcreteT get(MLIRContext *ctx, unsigned kind, Args... args) {
55  // Ensure that the invariants are correct for construction.
56  assert(succeeded(
57  ConcreteT::verifyConstructionInvariants(llvm::None, ctx, args...)));
58  return UniquerT::template get<ConcreteT>(ctx, kind, args...);
59  }
60 
64  template <typename... Args>
65  static ConcreteT getChecked(const Location &loc, MLIRContext *ctx,
66  unsigned kind, Args... args) {
67  // If the construction invariants fail then we return a null attribute.
68  if (failed(ConcreteT::verifyConstructionInvariants(loc, ctx, args...)))
69  return ConcreteT();
70  return UniquerT::template get<ConcreteT>(ctx, kind, args...);
71  }
72 
74  template <typename... Args>
76  return success();
77  }
78 
80  ImplType *getImpl() const { return static_cast<ImplType *>(this->impl); }
81 };
82 } // namespace detail
83 } // namespace mlir
84 
85 #endif
Definition: InferTypeOpInterface.cpp:20
Definition: STLExtras.h:95
bool failed(LogicalResult result)
Definition: LogicalResult.h:45
bool succeeded(LogicalResult result)
Definition: LogicalResult.h:39
static ConcreteT getChecked(const Location &loc, MLIRContext *ctx, unsigned kind, Args... args)
Definition: StorageUniquerSupport.h:65
Definition: Location.h:52
static ClassID * getClassID()
Return a unique identifier for the concrete type.
Definition: StorageUniquerSupport.h:39
LogicalResult success(bool isSuccess=true)
Definition: LogicalResult.h:25
Definition: LogicalResult.h:18
ImplType * getImpl() const
Utility for easy access to the storage instance.
Definition: StorageUniquerSupport.h:80
static bool classof(T val)
Definition: StorageUniquerSupport.h:43
Definition: StorageUniquerSupport.h:30
Definition: MLIRContext.h:34
Definition: StandardTypes.h:63
static LogicalResult verifyConstructionInvariants(Args... args)
Default implementation that just returns success.
Definition: StorageUniquerSupport.h:75
StorageT ImplType
Definition: StorageUniquerSupport.h:36