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
10#include "llvm/ADT/APFloat.h"
11#include "llvm/Support/CheckedArithmetic.h"
12
13using namespace mlir;
14using namespace mlir::detail;
15
16//===----------------------------------------------------------------------===//
17/// Tablegen Interface Definitions
18//===----------------------------------------------------------------------===//
19
20#include "mlir/IR/BuiltinTypeInterfaces.cpp.inc"
21
22//===----------------------------------------------------------------------===//
23// FloatType
24//===----------------------------------------------------------------------===//
25
26unsigned FloatType::getWidth() {
27 return APFloat::semanticsSizeInBits(getFloatSemantics());
28}
29
30unsigned FloatType::getFPMantissaWidth() {
31 return APFloat::semanticsPrecision(getFloatSemantics());
32}
33
34//===----------------------------------------------------------------------===//
35// ShapedType
36//===----------------------------------------------------------------------===//
37
38std::optional<int64_t> ShapedType::tryGetNumElements(ArrayRef<int64_t> shape) {
39 int64_t num = 1;
40 for (int64_t dim : shape) {
41 auto result = llvm::checkedMul(num, dim);
42 if (!result)
43 return std::nullopt;
44 num = *result;
45 }
46 return num;
47}
48
49int64_t ShapedType::getNumElements(ArrayRef<int64_t> shape) {
50#ifndef NDEBUG
51 std::optional<int64_t> num = tryGetNumElements(shape);
52 assert(num.has_value() && "integer overflow in element count computation");
53 return *num;
54#else
55 int64_t num = 1;
56 for (int64_t dim : shape)
57 num *= dim;
58 return num;
59#endif
60}
AttrTypeReplacer.
Include the generated interface declarations.