2018-10-26 06:46:10 +08:00
|
|
|
//===- AttributeDetail.h - MLIR Affine Map details Class --------*- 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 holds implementation details of Attribute.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef ATTRIBUTEDETAIL_H_
|
|
|
|
#define ATTRIBUTEDETAIL_H_
|
|
|
|
|
2018-10-26 13:39:14 +08:00
|
|
|
#include "mlir/IR/AffineMap.h"
|
2018-10-26 06:46:10 +08:00
|
|
|
#include "mlir/IR/Attributes.h"
|
2019-02-27 10:01:46 +08:00
|
|
|
#include "mlir/IR/Identifier.h"
|
2018-10-26 13:39:14 +08:00
|
|
|
#include "mlir/IR/IntegerSet.h"
|
2018-10-26 06:46:10 +08:00
|
|
|
#include "mlir/IR/MLIRContext.h"
|
2019-01-04 06:29:52 +08:00
|
|
|
#include "mlir/IR/StandardTypes.h"
|
2018-11-16 09:53:51 +08:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
2018-10-26 13:39:14 +08:00
|
|
|
#include "llvm/Support/TrailingObjects.h"
|
2018-10-26 06:46:10 +08:00
|
|
|
|
|
|
|
namespace mlir {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/// Base storage class appearing in an attribute.
|
|
|
|
struct AttributeStorage {
|
2019-03-23 15:40:25 +08:00
|
|
|
AttributeStorage(Attribute::Kind kind, bool isOrContainsFunctionCache = false)
|
|
|
|
: kind(kind), isOrContainsFunctionCache(isOrContainsFunctionCache) {}
|
|
|
|
|
2018-10-26 06:46:10 +08:00
|
|
|
Attribute::Kind kind : 8;
|
|
|
|
|
|
|
|
/// This field is true if this is, or contains, a function attribute.
|
|
|
|
bool isOrContainsFunctionCache : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An attribute representing a boolean value.
|
|
|
|
struct BoolAttributeStorage : public AttributeStorage {
|
2019-03-23 15:40:25 +08:00
|
|
|
BoolAttributeStorage(Type type, bool value)
|
|
|
|
: AttributeStorage(Attribute::Kind::Bool), type(type), value(value) {}
|
2019-02-03 22:15:43 +08:00
|
|
|
const Type type;
|
2018-10-26 06:46:10 +08:00
|
|
|
bool value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An attribute representing a integral value.
|
2018-11-12 22:33:22 +08:00
|
|
|
struct IntegerAttributeStorage final
|
|
|
|
: public AttributeStorage,
|
|
|
|
public llvm::TrailingObjects<IntegerAttributeStorage, uint64_t> {
|
2019-03-23 15:40:25 +08:00
|
|
|
IntegerAttributeStorage(Type type, size_t numObjects)
|
|
|
|
: AttributeStorage(Attribute::Kind::Integer), type(type),
|
|
|
|
numObjects(numObjects) {
|
2018-11-16 09:53:51 +08:00
|
|
|
assert((type.isIndex() || type.isa<IntegerType>()) && "invalid type");
|
|
|
|
}
|
|
|
|
|
|
|
|
const Type type;
|
2018-11-12 22:33:22 +08:00
|
|
|
size_t numObjects;
|
|
|
|
|
|
|
|
/// Returns an APInt representing the stored value.
|
|
|
|
APInt getValue() const {
|
2018-11-16 09:53:51 +08:00
|
|
|
if (type.isIndex())
|
|
|
|
return APInt(64, {getTrailingObjects<uint64_t>(), numObjects});
|
2018-12-18 02:05:56 +08:00
|
|
|
return APInt(type.getIntOrFloatBitWidth(),
|
2018-11-16 09:53:51 +08:00
|
|
|
{getTrailingObjects<uint64_t>(), numObjects});
|
2018-11-12 22:33:22 +08:00
|
|
|
}
|
2018-10-26 06:46:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// An attribute representing a floating point value.
|
|
|
|
struct FloatAttributeStorage final
|
|
|
|
: public AttributeStorage,
|
|
|
|
public llvm::TrailingObjects<FloatAttributeStorage, uint64_t> {
|
2019-03-23 15:40:25 +08:00
|
|
|
FloatAttributeStorage(const llvm::fltSemantics &semantics, Type type,
|
2018-11-16 09:53:51 +08:00
|
|
|
size_t numObjects)
|
2019-03-23 15:40:25 +08:00
|
|
|
: AttributeStorage(Attribute::Kind::Float), semantics(semantics),
|
2018-11-16 09:53:51 +08:00
|
|
|
type(type.cast<FloatType>()), numObjects(numObjects) {}
|
2018-10-26 06:46:10 +08:00
|
|
|
const llvm::fltSemantics &semantics;
|
2018-11-16 09:53:51 +08:00
|
|
|
const FloatType type;
|
2018-10-26 06:46:10 +08:00
|
|
|
size_t numObjects;
|
|
|
|
|
|
|
|
/// Returns an APFloat representing the stored value.
|
|
|
|
APFloat getValue() const {
|
|
|
|
auto val = APInt(APFloat::getSizeInBits(semantics),
|
|
|
|
{getTrailingObjects<uint64_t>(), numObjects});
|
|
|
|
return APFloat(semantics, val);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An attribute representing a string value.
|
|
|
|
struct StringAttributeStorage : public AttributeStorage {
|
2019-03-23 15:40:25 +08:00
|
|
|
StringAttributeStorage(StringRef value)
|
|
|
|
: AttributeStorage(Attribute::Kind::String), value(value) {}
|
2018-10-26 06:46:10 +08:00
|
|
|
StringRef value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An attribute representing an array of other attributes.
|
|
|
|
struct ArrayAttributeStorage : public AttributeStorage {
|
2019-03-23 15:40:25 +08:00
|
|
|
ArrayAttributeStorage(bool hasFunctionAttr, ArrayRef<Attribute> value)
|
|
|
|
: AttributeStorage(Attribute::Kind::Array, hasFunctionAttr),
|
|
|
|
value(value) {}
|
2018-10-26 06:46:10 +08:00
|
|
|
ArrayRef<Attribute> value;
|
|
|
|
};
|
|
|
|
|
|
|
|
// An attribute representing a reference to an affine map.
|
|
|
|
struct AffineMapAttributeStorage : public AttributeStorage {
|
2019-03-23 15:40:25 +08:00
|
|
|
AffineMapAttributeStorage(AffineMap value)
|
|
|
|
: AttributeStorage(Attribute::Kind::AffineMap), value(value) {}
|
2018-10-26 06:46:10 +08:00
|
|
|
AffineMap value;
|
|
|
|
};
|
|
|
|
|
2018-10-26 13:13:03 +08:00
|
|
|
// An attribute representing a reference to an integer set.
|
|
|
|
struct IntegerSetAttributeStorage : public AttributeStorage {
|
2019-03-23 15:40:25 +08:00
|
|
|
IntegerSetAttributeStorage(IntegerSet value)
|
|
|
|
: AttributeStorage(Attribute::Kind::IntegerSet), value(value) {}
|
2018-10-26 13:13:03 +08:00
|
|
|
IntegerSet value;
|
|
|
|
};
|
|
|
|
|
2018-10-26 06:46:10 +08:00
|
|
|
/// An attribute representing a reference to a type.
|
|
|
|
struct TypeAttributeStorage : public AttributeStorage {
|
2019-03-23 15:40:25 +08:00
|
|
|
TypeAttributeStorage(Type value)
|
|
|
|
: AttributeStorage(Attribute::Kind::Type), value(value) {}
|
2018-10-31 05:59:22 +08:00
|
|
|
Type value;
|
2018-10-26 06:46:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// An attribute representing a reference to a function.
|
|
|
|
struct FunctionAttributeStorage : public AttributeStorage {
|
2019-03-23 15:40:25 +08:00
|
|
|
FunctionAttributeStorage(Function *value)
|
|
|
|
: AttributeStorage(Attribute::Kind::Function,
|
|
|
|
/*isOrContainsFunctionCache=*/true),
|
|
|
|
value(value) {}
|
2018-10-26 06:46:10 +08:00
|
|
|
Function *value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A base attribute representing a reference to a vector or tensor constant.
|
|
|
|
struct ElementsAttributeStorage : public AttributeStorage {
|
2019-03-23 15:40:25 +08:00
|
|
|
ElementsAttributeStorage(Attribute::Kind kind, VectorOrTensorType type)
|
|
|
|
: AttributeStorage(kind), type(type) {}
|
2018-10-31 05:59:22 +08:00
|
|
|
VectorOrTensorType type;
|
2018-10-26 06:46:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// An attribute representing a reference to a vector or tensor constant,
|
|
|
|
/// inwhich all elements have the same value.
|
|
|
|
struct SplatElementsAttributeStorage : public ElementsAttributeStorage {
|
2019-03-23 15:40:25 +08:00
|
|
|
SplatElementsAttributeStorage(VectorOrTensorType type, Attribute elt)
|
|
|
|
: ElementsAttributeStorage(Attribute::Kind::SplatElements, type),
|
|
|
|
elt(elt) {}
|
2018-10-26 06:46:10 +08:00
|
|
|
Attribute elt;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An attribute representing a reference to a dense vector or tensor object.
|
|
|
|
struct DenseElementsAttributeStorage : public ElementsAttributeStorage {
|
2019-03-23 15:40:25 +08:00
|
|
|
DenseElementsAttributeStorage(Attribute::Kind kind, VectorOrTensorType type,
|
2019-03-19 23:45:06 +08:00
|
|
|
ArrayRef<char> data)
|
2019-03-23 15:40:25 +08:00
|
|
|
: ElementsAttributeStorage(kind, type), data(data) {}
|
2018-10-26 06:46:10 +08:00
|
|
|
ArrayRef<char> data;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An attribute representing a reference to a tensor constant with opaque
|
|
|
|
/// content.
|
|
|
|
struct OpaqueElementsAttributeStorage : public ElementsAttributeStorage {
|
2019-03-23 15:40:25 +08:00
|
|
|
OpaqueElementsAttributeStorage(VectorOrTensorType type, Dialect *dialect,
|
|
|
|
StringRef bytes)
|
|
|
|
: ElementsAttributeStorage(Attribute::Kind::OpaqueElements, type),
|
|
|
|
dialect(dialect), bytes(bytes) {}
|
2019-02-12 14:51:34 +08:00
|
|
|
Dialect *dialect;
|
2018-10-26 06:46:10 +08:00
|
|
|
StringRef bytes;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An attribute representing a reference to a sparse vector or tensor object.
|
|
|
|
struct SparseElementsAttributeStorage : public ElementsAttributeStorage {
|
2019-03-23 15:40:25 +08:00
|
|
|
SparseElementsAttributeStorage(VectorOrTensorType type,
|
2019-03-19 23:45:06 +08:00
|
|
|
DenseIntElementsAttr indices,
|
|
|
|
DenseElementsAttr values)
|
2019-03-23 15:40:25 +08:00
|
|
|
: ElementsAttributeStorage(Attribute::Kind::SparseElements, type),
|
|
|
|
indices(indices), values(values) {}
|
2018-10-26 06:46:10 +08:00
|
|
|
DenseIntElementsAttr indices;
|
|
|
|
DenseElementsAttr values;
|
|
|
|
};
|
|
|
|
|
2019-02-27 10:01:46 +08:00
|
|
|
/// A raw list of named attributes stored as a trailing array.
|
|
|
|
class AttributeListStorage final
|
|
|
|
: private llvm::TrailingObjects<AttributeListStorage, NamedAttribute> {
|
|
|
|
friend class llvm::TrailingObjects<AttributeListStorage, NamedAttribute>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Given a list of NamedAttribute's, canonicalize the list (sorting
|
|
|
|
/// by name) and return the unique'd result. Note that the empty list is
|
|
|
|
/// represented with a null pointer.
|
|
|
|
static AttributeListStorage *get(ArrayRef<NamedAttribute> attrs,
|
|
|
|
MLIRContext *context);
|
|
|
|
|
|
|
|
/// Return the element constants for this aggregate constant. These are
|
|
|
|
/// known to all be constants.
|
|
|
|
ArrayRef<NamedAttribute> getElements() const {
|
|
|
|
return {getTrailingObjects<NamedAttribute>(), numElements};
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// This is used by the llvm::TrailingObjects base class.
|
|
|
|
size_t numTrailingObjects(OverloadToken<NamedAttribute>) const {
|
|
|
|
return numElements;
|
|
|
|
}
|
|
|
|
AttributeListStorage() = delete;
|
|
|
|
AttributeListStorage(const AttributeListStorage &) = delete;
|
|
|
|
AttributeListStorage(unsigned numElements) : numElements(numElements) {}
|
|
|
|
|
|
|
|
/// This is the number of attributes.
|
|
|
|
const unsigned numElements;
|
|
|
|
};
|
2018-10-26 06:46:10 +08:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace mlir
|
|
|
|
|
|
|
|
#endif // ATTRIBUTEDETAIL_H_
|