MLIR 23.0.0git
BuiltinTypeInterfaces.cpp
Go to the documentation of this file.
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
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
30 return cast<FloatType>(type).getWidth();
31}
32
34 ArrayRef<char> rawData) {
35 auto floatType = cast<FloatType>(type);
36 APInt intVal = readBits(rawData.data(), /*bitPos=*/0, floatType.getWidth());
37 APFloat floatVal(floatType.getFloatSemantics(), intVal);
38 return FloatAttr::get(type, floatVal);
39}
40
41LogicalResult
44 auto floatType = cast<FloatType>(type);
45 auto floatAttr = dyn_cast<FloatAttr>(attr);
46 if (!floatAttr || floatAttr.getType() != type)
47 return failure();
48 size_t byteSize =
49 llvm::divideCeil(floatType.getWidth(), static_cast<unsigned>(CHAR_BIT));
50 size_t bitPos = result.size() * CHAR_BIT;
51 result.resize(result.size() + byteSize);
52 writeBits(result.data(), bitPos, 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(num, 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}
return success()
Attributes are known-constant values of operations.
Definition Attributes.h:25
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
AttrTypeReplacer.
llvm::APInt readBits(const char *rawData, size_t bitPos, size_t bitWidth)
Read bitWidth bits from byte-aligned position in rawData and return as an APInt.
LogicalResult convertFloatTypeFromAttribute(Type type, Attribute attr, llvm::SmallVectorImpl< char > &result)
Float type implementation of DenseElementTypeInterface::convertFromAttribute.
size_t getFloatTypeDenseElementBitSize(Type type)
Float type implementation of DenseElementTypeInterface::getDenseElementBitSize.
Attribute convertFloatTypeToAttribute(Type type, llvm::ArrayRef< char > rawData)
Float type implementation of DenseElementTypeInterface::convertToAttribute.
void writeBits(char *rawData, size_t bitPos, llvm::APInt value)
Write value to byte-aligned position bitPos in rawData.
Include the generated interface declarations.