1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #include "mlir/IR/BuiltinAttributes.h" // from @llvm-project 17 #include "mlir/IR/BuiltinTypes.h" // from @llvm-project 18 19 namespace mlir { 20 namespace TFL { 21 ExtractSingleElementAsFloat(ElementsAttr attr)22FloatAttr ExtractSingleElementAsFloat(ElementsAttr attr) { 23 if (attr.getType().getNumElements() != 1 || 24 !attr.getType().getElementType().isa<FloatType>()) { 25 return {}; 26 } 27 SmallVector<uint64_t, 8> index(attr.getType().getRank(), 0); 28 return attr.getValue<FloatAttr>(index); 29 } 30 GetSingleElementAsFloatOrSelf(Attribute attr)31FloatAttr GetSingleElementAsFloatOrSelf(Attribute attr) { 32 if (auto m = attr.dyn_cast_or_null<ElementsAttr>()) { 33 return ExtractSingleElementAsFloat(m); 34 } else { 35 return attr.dyn_cast_or_null<FloatAttr>(); 36 } 37 } 38 ExtractSingleElementAsInteger(ElementsAttr attr)39IntegerAttr ExtractSingleElementAsInteger(ElementsAttr attr) { 40 if (attr.getType().getNumElements() != 1 || 41 !attr.getType().getElementType().isSignlessInteger()) { 42 return {}; 43 } 44 SmallVector<uint64_t, 8> index(attr.getType().getRank(), 0); 45 return attr.getValue<IntegerAttr>(index); 46 } 47 48 } // namespace TFL 49 } // namespace mlir 50