From: River Riddle Date: Thu, 2 May 2019 21:02:57 +0000 (-0700) Subject: NFC: Move AttributeStorage and AttributeUniquer into a new header AttributeSuppor... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b91aea1516b83e1a6395f492c529c943f3a6e709;p=platform%2Fupstream%2Fllvm.git NFC: Move AttributeStorage and AttributeUniquer into a new header AttributeSupport.h in preparation for them to be used by dialect defined attributes. -- PiperOrigin-RevId: 246385860 --- diff --git a/mlir/include/mlir/IR/AttributeSupport.h b/mlir/include/mlir/IR/AttributeSupport.h new file mode 100644 index 0000000..9bf43b0 --- /dev/null +++ b/mlir/include/mlir/IR/AttributeSupport.h @@ -0,0 +1,121 @@ +//===- AttributeSupport.h ---------------------------------------*- C++ -*-===// +// +// Copyright 2019 The MLIR Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ============================================================================= +// +// This file defines support types for registering dialect extended attributes. +// +//===----------------------------------------------------------------------===// + +#ifndef MLIR_IR_ATTRIBUTESUPPORT_H +#define MLIR_IR_ATTRIBUTESUPPORT_H + +#include "mlir/IR/MLIRContext.h" +#include "mlir/Support/StorageUniquer.h" +#include "llvm/ADT/PointerIntPair.h" +#include "llvm/ADT/StringRef.h" + +namespace mlir { +class MLIRContext; +class Type; + +//===----------------------------------------------------------------------===// +// AttributeStorage +//===----------------------------------------------------------------------===// + +namespace detail { +class AttributeUniquer; +} // end namespace detail + +/// Base storage class appearing in an attribute. Derived storage classes should +/// only be constructed within the context of the AttributeUniquer. +class AttributeStorage : public StorageUniquer::BaseStorage { + friend detail::AttributeUniquer; + friend StorageUniquer; + +public: + /// Returns if the derived attribute is or contains a function pointer. + bool isOrContainsFunctionCache() const { + return typeAndContainsFunctionAttrPair.getInt(); + } + + /// Get the type of this attribute. + Type getType() const; + +protected: + /// Construct a new attribute storage instance with the given type and a + /// boolean that signals if the derived attribute is or contains a function + /// pointer. + /// Note: All attributes require a valid type. If no type is provided here, + /// the type of the attribute will automatically default to NoneType + /// upon initialization in the uniquer. + AttributeStorage(Type type, bool isOrContainsFunctionCache = false); + AttributeStorage(bool isOrContainsFunctionCache); + AttributeStorage(); + + /// Set the type of this attribute. + void setType(Type type); + +private: + /// This field is a pair of: + /// - The type of the attribute value. + /// - A boolean that is true if this is, or contains, a function attribute. + llvm::PointerIntPair typeAndContainsFunctionAttrPair; +}; + +/// Default storage type for attributes that require no additional +/// initialization or storage. +using DefaultAttributeStorage = AttributeStorage; + +//===----------------------------------------------------------------------===// +// AttributeStorageAllocator +//===----------------------------------------------------------------------===// + +// This is a utility allocator used to allocate memory for instances of derived +// Attributes. +using AttributeStorageAllocator = StorageUniquer::StorageAllocator; + +//===----------------------------------------------------------------------===// +// AttributeUniquer +//===----------------------------------------------------------------------===// +namespace detail { +// A utility class to get, or create, unique instances of attributes within an +// MLIRContext. This class manages all creation and uniquing of attributes. +class AttributeUniquer { +public: + /// Get an uniqued instance of attribute T. + template + static T get(MLIRContext *ctx, Kind kind, Args &&... args) { + return ctx->getAttributeUniquer().get( + getInitFn(ctx), static_cast(kind), + std::forward(args)...); + } + + /// Erase a uniqued instance of attribute T. + template + static void erase(MLIRContext *ctx, Kind kind, Args &&... args) { + return ctx->getAttributeUniquer().erase( + static_cast(kind), std::forward(args)...); + } + +private: + /// Returns a functor used to initialize new attribute storage instances. + static std::function getInitFn(MLIRContext *ctx); +}; +} // namespace detail + +} // end namespace mlir + +#endif diff --git a/mlir/include/mlir/IR/Attributes.h b/mlir/include/mlir/IR/Attributes.h index 1819518..2de33d3 100644 --- a/mlir/include/mlir/IR/Attributes.h +++ b/mlir/include/mlir/IR/Attributes.h @@ -18,6 +18,7 @@ #ifndef MLIR_IR_ATTRIBUTES_H #define MLIR_IR_ATTRIBUTES_H +#include "mlir/IR/AttributeSupport.h" #include "mlir/Support/LLVM.h" #include "llvm/ADT/APFloat.h" #include "llvm/ADT/DenseMap.h" @@ -37,7 +38,6 @@ class VectorOrTensorType; namespace detail { -struct AttributeStorage; struct BoolAttributeStorage; struct IntegerAttributeStorage; struct FloatAttributeStorage; @@ -86,11 +86,11 @@ public: FIRST_ELEMENTS_ATTR = SplatElements, LAST_ELEMENTS_ATTR = SparseElements, - FIRST_KIND = Bool, + FIRST_KIND = Unit, LAST_KIND = SparseElements, }; - using ImplType = detail::AttributeStorage; + using ImplType = AttributeStorage; using ValueType = void; Attribute() : attr(nullptr) {} diff --git a/mlir/lib/IR/AttributeDetail.h b/mlir/lib/IR/AttributeDetail.h index bb4d8de..990008b 100644 --- a/mlir/lib/IR/AttributeDetail.h +++ b/mlir/lib/IR/AttributeDetail.h @@ -36,59 +36,6 @@ namespace mlir { namespace detail { -/// Base storage class appearing in an attribute. Derived storage classes should -/// only be constructed within the context of the AttributeUniquer. -struct AttributeStorage : public StorageUniquer::BaseStorage { - /// Construct a new attribute storage instance with the given type and a - /// boolean that signals if the derived attribute is or contains a function - /// pointer. - /// Note: All attributes require a valid type. If a null type is provided - /// here, the type of the attribute will automatically default to - /// NoneType upon initialization in the uniquer. - AttributeStorage(Type type = {}, bool isOrContainsFunctionCache = false) - : typeAndContainsFunctionAttrPair(type, isOrContainsFunctionCache) {} - AttributeStorage(bool isOrContainsFunctionCache) - : AttributeStorage(/*type=*/{}, isOrContainsFunctionCache) {} - - bool isOrContainsFunctionCache() const { - return typeAndContainsFunctionAttrPair.getInt(); - } - - Type getType() const { return typeAndContainsFunctionAttrPair.getPointer(); } - void setType(Type type) { typeAndContainsFunctionAttrPair.setPointer(type); } - - /// This field is a pair of: - /// - The type of the attribute value. - /// - A boolean that is true if this is, or contains, a function attribute. - llvm::PointerIntPair typeAndContainsFunctionAttrPair; -}; - -// A utility class to get, or create, unique instances of attributes within an -// MLIRContext. This class manages all creation and uniquing of attributes. -class AttributeUniquer { -public: - /// Get an uniqued instance of attribute T. - template - static T get(MLIRContext *ctx, Attribute::Kind kind, Args &&... args) { - return ctx->getAttributeUniquer().get( - [ctx](AttributeStorage *storage) { - // If the attribute did not provide a type, then default to NoneType. - if (!storage->getType()) - storage->setType(NoneType::get(ctx)); - }, - static_cast(kind), std::forward(args)...); - } - - /// Erase a uniqued instance of attribute T. - template - static void erase(MLIRContext *ctx, Attribute::Kind kind, Args &&... args) { - return ctx->getAttributeUniquer().erase( - static_cast(kind), std::forward(args)...); - } -}; - -using AttributeStorageAllocator = StorageUniquer::StorageAllocator; - /// An attribute representing a boolean value. struct BoolAttributeStorage : public AttributeStorage { using KeyTy = std::pair; diff --git a/mlir/lib/IR/Attributes.cpp b/mlir/lib/IR/Attributes.cpp index b05dfdb..64f5a7e 100644 --- a/mlir/lib/IR/Attributes.cpp +++ b/mlir/lib/IR/Attributes.cpp @@ -26,6 +26,40 @@ using namespace mlir; using namespace mlir::detail; +//===----------------------------------------------------------------------===// +// AttributeStorage +//===----------------------------------------------------------------------===// + +AttributeStorage::AttributeStorage(Type type, bool isOrContainsFunctionCache) + : typeAndContainsFunctionAttrPair(type.getAsOpaquePointer(), + isOrContainsFunctionCache) {} +AttributeStorage::AttributeStorage(bool isOrContainsFunctionCache) + : AttributeStorage(/*type=*/nullptr, isOrContainsFunctionCache) {} +AttributeStorage::AttributeStorage() + : AttributeStorage(/*type=*/nullptr, /*isOrContainsFunctionCache=*/false) {} + +Type AttributeStorage::getType() const { + return Type::getFromOpaquePointer( + typeAndContainsFunctionAttrPair.getPointer()); +} +void AttributeStorage::setType(Type type) { + typeAndContainsFunctionAttrPair.setPointer(type.getAsOpaquePointer()); +} + +/// Returns a functor used to initialize new attribute storage instances. +std::function +AttributeUniquer::getInitFn(MLIRContext *ctx) { + return [ctx](AttributeStorage *storage) { + // If the attribute did not provide a type, then default to NoneType. + if (!storage->getType()) + storage->setType(NoneType::get(ctx)); + }; +} + +//===----------------------------------------------------------------------===// +// Attribute +//===----------------------------------------------------------------------===// + Attribute::Kind Attribute::getKind() const { return static_cast(attr->getKind()); }