Elffuss BraidHub

kikocis/ncnn public

master · 4.1 KiBRawPermalink
1/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tf_attributes.h"
17
18namespace mlir {
19namespace TF {
20
21namespace detail {
22
23// The storage class for ShapeAttr.
24struct ShapeAttrStorage : public AttributeStorage
25{
26 using KeyTy = std::pair<ArrayRef<int64_t>, bool>;
27
28 explicit ShapeAttrStorage(ArrayRef<int64_t> shape, bool unranked = false)
29 : shape(shape), unranked(unranked)
30 {
31 }
32
33 bool operator==(const KeyTy& key) const
34 {
35 return key == KeyTy(shape, unranked);
36 }
37 static unsigned hashKey(const KeyTy& key)
38 {
39 return llvm::hash_combine(key.first, static_cast<char>(key.second));
40 }
41
42 // NOLINTNEXTLINE
43 static ShapeAttrStorage* construct(mlir::AttributeStorageAllocator& allocator,
44 const KeyTy& key)
45 {
46 return new (allocator.allocate<ShapeAttrStorage>())
47 ShapeAttrStorage(allocator.copyInto(key.first), key.second);
48 }
49
50 ArrayRef<int64_t> shape;
51 bool unranked = false;
52};
53
54// The storage class for FuncAttr.
55struct FuncAttrStorage : public AttributeStorage
56{
57 using KeyTy = std::pair<Attribute, Attribute>;
58
59 explicit FuncAttrStorage(Attribute name, Attribute attrs)
60 : name(name), attrs(attrs)
61 {
62 }
63
64 bool operator==(const KeyTy& key) const
65 {
66 return key == KeyTy(name, attrs);
67 }
68 static unsigned hashKey(const KeyTy& key)
69 {
70 return llvm::hash_combine(key.first, key.second);
71 }
72
73 static FuncAttrStorage* construct(mlir::AttributeStorageAllocator& allocator,
74 const KeyTy& key)
75 {
76 return new (allocator.allocate<FuncAttrStorage>())
77 FuncAttrStorage(key.first, key.second);
78 }
79
80 Attribute name;
81 Attribute attrs;
82};
83
84} // namespace detail
85
86// Get or create a shape attribute.
87ShapeAttr ShapeAttr::get(mlir::MLIRContext* context,
88 llvm::Optional<ArrayRef<int64_t> > shape)
89{
90 if (shape) return Base::get(context, *shape, /*unranked=*/false);
91
92 return Base::get(context, ArrayRef<int64_t>(), /*unranked=*/true);
93}
94
95// Get or create a shape attribute.
96ShapeAttr ShapeAttr::get(mlir::MLIRContext* context, ShapedType shaped_type)
97{
98 if (shaped_type.hasRank())
99 return Base::get(context, shaped_type.getShape(), /*unranked=*/false);
100
101 return Base::get(context, ArrayRef<int64_t>(), /*unranked=*/true);
102}
103
104llvm::Optional<ArrayRef<int64_t> > ShapeAttr::getValue() const
105{
106 if (hasRank()) return getShape();
107 return llvm::None;
108}
109
110bool ShapeAttr::hasRank() const
111{
112 return !getImpl()->unranked;
113}
114
115int64_t ShapeAttr::getRank() const
116{
117 assert(hasRank());
118 return getImpl()->shape.size();
119}
120
121ArrayRef<int64_t> ShapeAttr::getShape() const
122{
123 assert(hasRank());
124 return getImpl()->shape;
125}
126
127bool ShapeAttr::hasStaticShape() const
128{
129 if (!hasRank()) return false;
130
131 for (auto dim : getShape())
132 {
133 if (dim < 0) return false;
134 }
135
136 return true;
137}
138
139FuncAttr FuncAttr::get(mlir::MLIRContext* context, llvm::StringRef name,
140 DictionaryAttr attr)
141{
142 auto symbol = SymbolRefAttr::get(context, name);
143 return Base::get(context, symbol, attr);
144}
145
146FuncAttr FuncAttr::get(mlir::MLIRContext* context, SymbolRefAttr symbol,
147 DictionaryAttr attr)
148{
149 return Base::get(context, symbol, attr);
150}
151
152SymbolRefAttr FuncAttr::GetName() const
153{
154 return getImpl()->name.cast<SymbolRefAttr>();
155}
156
157DictionaryAttr FuncAttr::GetAttrs() const
158{
159 return getImpl()->attrs.cast<DictionaryAttr>();
160}
161
162} // namespace TF
163} // namespace mlir