Changeset View
Changeset View
Standalone View
Standalone View
mlir/include/mlir/IR/AttributeSupport.h
Show All 9 Lines | |||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
#ifndef MLIR_IR_ATTRIBUTESUPPORT_H | #ifndef MLIR_IR_ATTRIBUTESUPPORT_H | ||||
#define MLIR_IR_ATTRIBUTESUPPORT_H | #define MLIR_IR_ATTRIBUTESUPPORT_H | ||||
#include "mlir/IR/MLIRContext.h" | #include "mlir/IR/MLIRContext.h" | ||||
#include "mlir/IR/StorageUniquerSupport.h" | #include "mlir/IR/StorageUniquerSupport.h" | ||||
#include "mlir/IR/Types.h" | |||||
#include "llvm/ADT/PointerIntPair.h" | #include "llvm/ADT/PointerIntPair.h" | ||||
#include "llvm/ADT/Twine.h" | #include "llvm/ADT/Twine.h" | ||||
namespace mlir { | namespace mlir { | ||||
class MLIRContext; | class MLIRContext; | ||||
class Type; | class Type; | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
▲ Show 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | |||||
/// Base storage class appearing in an attribute. Derived storage classes should | /// Base storage class appearing in an attribute. Derived storage classes should | ||||
/// only be constructed within the context of the AttributeUniquer. | /// only be constructed within the context of the AttributeUniquer. | ||||
class alignas(8) AttributeStorage : public StorageUniquer::BaseStorage { | class alignas(8) AttributeStorage : public StorageUniquer::BaseStorage { | ||||
friend detail::AttributeUniquer; | friend detail::AttributeUniquer; | ||||
friend StorageUniquer; | friend StorageUniquer; | ||||
public: | public: | ||||
/// Get the type of this attribute. | /// Get the type of this attribute. | ||||
Type getType() const; | Type getType() const { return type; } | ||||
/// Return the abstract descriptor for this attribute. | /// Return the abstract descriptor for this attribute. | ||||
const AbstractAttribute &getAbstractAttribute() const { | const AbstractAttribute &getAbstractAttribute() const { | ||||
assert(abstractAttribute && "Malformed attribute storage object."); | assert(abstractAttribute && "Malformed attribute storage object."); | ||||
return *abstractAttribute; | return *abstractAttribute; | ||||
} | } | ||||
protected: | protected: | ||||
/// Construct a new attribute storage instance with the given type. | /// Construct a new attribute storage instance with the given type. | ||||
/// Note: All attributes require a valid type. If no type is provided here, | /// Note: All attributes require a valid type. If no type is provided here, | ||||
/// the type of the attribute will automatically default to NoneType | /// the type of the attribute will automatically default to NoneType | ||||
/// upon initialization in the uniquer. | /// upon initialization in the uniquer. | ||||
AttributeStorage(Type type); | AttributeStorage(Type type = nullptr) : type(type) {} | ||||
AttributeStorage(); | |||||
/// Set the type of this attribute. | /// Set the type of this attribute. | ||||
void setType(Type type); | void setType(Type newType) { type = newType; } | ||||
// Set the abstract attribute for this storage instance. This is used by the | /// Set the abstract attribute for this storage instance. This is used by the | ||||
// AttributeUniquer when initializing a newly constructed storage object. | /// AttributeUniquer when initializing a newly constructed storage object. | ||||
void initialize(const AbstractAttribute &abstractAttr) { | void initializeAbstractAttribute(const AbstractAttribute &abstractAttr) { | ||||
abstractAttribute = &abstractAttr; | abstractAttribute = &abstractAttr; | ||||
} | } | ||||
/// Default initialization for attribute storage classes that require no | |||||
/// additional initialization. | |||||
void initialize(MLIRContext *context) {} | |||||
private: | private: | ||||
/// The type of the attribute value. | |||||
Type type; | |||||
/// The abstract descriptor for this attribute. | /// The abstract descriptor for this attribute. | ||||
const AbstractAttribute *abstractAttribute; | const AbstractAttribute *abstractAttribute; | ||||
/// The opaque type of the attribute value. | |||||
const void *type; | |||||
}; | }; | ||||
/// Default storage type for attributes that require no additional | /// Default storage type for attributes that require no additional | ||||
/// initialization or storage. | /// initialization or storage. | ||||
using DefaultAttributeStorage = AttributeStorage; | using DefaultAttributeStorage = AttributeStorage; | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
// AttributeStorageAllocator | // AttributeStorageAllocator | ||||
Show All 23 Lines | if (!ctx->getAttributeUniquer().isParametricStorageInitialized( | ||||
llvm::Twine("can't create Attribute '") + llvm::getTypeName<T>() + | llvm::Twine("can't create Attribute '") + llvm::getTypeName<T>() + | ||||
"' because storage uniquer isn't initialized: the dialect was likely " | "' because storage uniquer isn't initialized: the dialect was likely " | ||||
"not loaded, or the attribute wasn't added with addAttributes<...>() " | "not loaded, or the attribute wasn't added with addAttributes<...>() " | ||||
"in the Dialect::initialize() method."); | "in the Dialect::initialize() method."); | ||||
#endif | #endif | ||||
return ctx->getAttributeUniquer().get<typename T::ImplType>( | return ctx->getAttributeUniquer().get<typename T::ImplType>( | ||||
[ctx](AttributeStorage *storage) { | [ctx](AttributeStorage *storage) { | ||||
initializeAttributeStorage(storage, ctx, T::getTypeID()); | initializeAttributeStorage(storage, ctx, T::getTypeID()); | ||||
// Execute any additional attribute storage initialization with the | |||||
// context. | |||||
static_cast<typename T::ImplType *>(storage)->initialize(ctx); | |||||
}, | }, | ||||
T::getTypeID(), std::forward<Args>(args)...); | T::getTypeID(), std::forward<Args>(args)...); | ||||
} | } | ||||
/// Get an uniqued instance of a singleton attribute T. | /// Get an uniqued instance of a singleton attribute T. | ||||
template <typename T> | template <typename T> | ||||
static typename std::enable_if_t< | static typename std::enable_if_t< | ||||
std::is_same<typename T::ImplType, AttributeStorage>::value, T> | std::is_same<typename T::ImplType, AttributeStorage>::value, T> | ||||
get(MLIRContext *ctx) { | get(MLIRContext *ctx) { | ||||
▲ Show 20 Lines • Show All 50 Lines • Show Last 20 Lines |