1//===- BuiltinTypeInterfaces.cpp ------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "mlir/IR/BuiltinAttributes.h"
10#include "mlir/IR/BuiltinTypes.h"
11#include "llvm/ADT/APFloat.h"
12#include "llvm/Support/CheckedArithmetic.h"
13#include "llvm/Support/MathExtras.h"
14#include <climits>
15
16using namespace mlir;
17using namespace mlir::detail;
18
19//===----------------------------------------------------------------------===//
20/// Tablegen Interface Definitions
21//===----------------------------------------------------------------------===//
22
23#include "mlir/IR/BuiltinTypeInterfaces.cpp.inc"
24
25//===----------------------------------------------------------------------===//
26// DenseElementTypeInterface implementations for float types
27//===----------------------------------------------------------------------===//
28
29size_t mlir::detail::getFloatTypeDenseElementBitSize(Type type) {
30 return cast<FloatType>(Val&: type).getWidth();
31}
32
33Attribute mlir::detail::convertFloatTypeToAttribute(Type type,
34 ArrayRef<char> rawData) {
35 auto floatType = cast<FloatType>(Val&: type);
36 APInt intVal = readBits(rawData: rawData.data(), /*bitPos=*/0, bitWidth: floatType.getWidth());
37 APFloat floatVal(floatType.getFloatSemantics(), intVal);
38 return FloatAttr::get(type, value: floatVal);
39}
40
41LogicalResult
42mlir::detail::convertFloatTypeFromAttribute(Type type, Attribute attr,
43 SmallVectorImpl<char> &result) {
44 auto floatType = cast<FloatType>(Val&: type);
45 auto floatAttr = dyn_cast<FloatAttr>(Val&: attr);
46 if (!floatAttr || floatAttr.getType() != type)
47 return failure();
48 size_t byteSize =
49 llvm::divideCeil(Numerator: floatType.getWidth(), Denominator: static_cast<unsigned>(CHAR_BIT));
50 size_t bitPos = result.size() * CHAR_BIT;
51 result.resize(N: result.size() + byteSize);
52 writeBits(rawData: result.data(), bitPos, value: floatAttr.getValue().bitcastToAPInt());
53 return success();
54}
55
56//===----------------------------------------------------------------------===//
57// FloatType
58//===----------------------------------------------------------------------===//
59
60unsigned FloatType::getWidth() {
61 return APFloat::semanticsSizeInBits(getFloatSemantics());
62}
63
64unsigned FloatType::getFPMantissaWidth() {
65 return APFloat::semanticsPrecision(getFloatSemantics());
66}
67
68//===----------------------------------------------------------------------===//
69// ShapedType
70//===----------------------------------------------------------------------===//
71
72std::optional<int64_t> ShapedType::tryGetNumElements(ArrayRef<int64_t> shape) {
73 int64_t num = 1;
74 for (int64_t dim : shape) {
75 auto result = llvm::checkedMul(LHS: num, RHS: dim);
76 if (!result)
77 return std::nullopt;
78 num = *result;
79 }
80 return num;
81}
82
83int64_t ShapedType::getNumElements(ArrayRef<int64_t> shape) {
84#ifndef NDEBUG
85 std::optional<int64_t> num = tryGetNumElements(shape);
86 assert(num.has_value() && "integer overflow in element count computation");
87 return *num;
88#else
89 int64_t num = 1;
90 for (int64_t dim : shape)
91 num *= dim;
92 return num;
93#endif
94}
95