1//===- ABITypeMapper.cpp - Map MLIR types to ABI types --------------------===//
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/ABI/ABITypeMapper.h"
10#include "llvm/ADT/APFloat.h"
11#include "llvm/Support/Alignment.h"
12
13using namespace mlir;
14using namespace mlir::abi;
15
16ABITypeMapper::ABITypeMapper(const DataLayout &dataLayout)
17 : dl(dataLayout), builder(allocator) {}
18
19const llvm::abi::Type *ABITypeMapper::map(mlir::Type type) {
20 if (auto intTy = dyn_cast<mlir::IntegerType>(Val&: type))
21 return mapIntegerType(type: intTy);
22
23 if (auto floatTy = dyn_cast<mlir::FloatType>(Val&: type))
24 return mapFloatType(type: floatTy);
25
26 if (auto indexTy = dyn_cast<mlir::IndexType>(Val&: type))
27 return mapIndexType(type: indexTy);
28
29 if (auto vecTy = dyn_cast<mlir::VectorType>(Val&: type))
30 return mapVectorType(type: vecTy);
31
32 if (auto memRefTy = dyn_cast<mlir::MemRefType>(Val&: type))
33 return mapMemRefType(type: memRefTy);
34
35 if (auto noneTy = dyn_cast<mlir::NoneType>(Val&: type))
36 return mapNoneType(type: noneTy);
37
38 // For dialect-specific types, fall back to DataLayout queries.
39 // The type must implement DataLayoutTypeInterface for this to work.
40 llvm::TypeSize sizeInBits = dl.getTypeSizeInBits(t: type);
41 uint64_t abiAlign = dl.getTypeABIAlignment(t: type);
42 return builder.getIntegerType(BitWidth: sizeInBits.getFixedValue(),
43 Align: llvm::Align(abiAlign),
44 /*Signed=*/false);
45}
46
47const llvm::abi::Type *ABITypeMapper::mapIntegerType(mlir::IntegerType type) {
48 uint64_t width = type.getWidth();
49 uint64_t abiAlign = dl.getTypeABIAlignment(t: type);
50 bool isSigned = type.isSigned() || type.isSignless();
51 return builder.getIntegerType(BitWidth: width, Align: llvm::Align(abiAlign), Signed: isSigned);
52}
53
54const llvm::abi::Type *ABITypeMapper::mapFloatType(mlir::FloatType type) {
55 uint64_t abiAlign = dl.getTypeABIAlignment(t: type);
56 const llvm::fltSemantics &semantics = type.getFloatSemantics();
57 return builder.getFloatType(Semantics: semantics, Align: llvm::Align(abiAlign));
58}
59
60const llvm::abi::Type *ABITypeMapper::mapIndexType(mlir::IndexType type) {
61 llvm::TypeSize sizeInBits = dl.getTypeSizeInBits(t: type);
62 uint64_t abiAlign = dl.getTypeABIAlignment(t: type);
63 return builder.getIntegerType(BitWidth: sizeInBits.getFixedValue(),
64 Align: llvm::Align(abiAlign),
65 /*Signed=*/false);
66}
67
68const llvm::abi::Type *ABITypeMapper::mapVectorType(mlir::VectorType type) {
69 const llvm::abi::Type *elementTy = map(type: type.getElementType());
70 if (!elementTy)
71 return nullptr;
72
73 // ElementCount supports at most one scalable dimension, hence
74 // vectors with more than one scalable dimension (e.g. vector<[2]x[4]xf32>)
75 // cannot be represented and mapVectorType must return nullptr in this case.
76 if (llvm::count(Range: type.getScalableDims(), Element: true) > 1)
77 return nullptr;
78
79 auto shape = type.getShape();
80 uint64_t totalElements = 1;
81 for (int64_t dim : shape)
82 totalElements *= dim;
83
84 llvm::ElementCount ec =
85 llvm::ElementCount::get(MinVal: totalElements, Scalable: type.isScalable());
86 uint64_t abiAlign = dl.getTypeABIAlignment(t: type);
87 return builder.getVectorType(ElementType: elementTy, NumElements: ec, Align: llvm::Align(abiAlign));
88}
89
90const llvm::abi::Type *ABITypeMapper::mapMemRefType(mlir::MemRefType type) {
91 llvm::TypeSize sizeInBits = dl.getTypeSizeInBits(t: type);
92 uint64_t abiAlign = dl.getTypeABIAlignment(t: type);
93 unsigned addrSpace = 0;
94 bool hasAddressSpaceQualifier = false;
95 if (auto as = type.getMemorySpace()) {
96 if (auto intAttr = dyn_cast<IntegerAttr>(Val&: as)) {
97 addrSpace = intAttr.getInt();
98 hasAddressSpaceQualifier = true;
99 }
100 }
101 return builder.getPointerType(Size: sizeInBits.getFixedValue(),
102 Align: llvm::Align(abiAlign), Addrspace: addrSpace,
103 HasAddressSpaceQualifier: hasAddressSpaceQualifier);
104}
105
106const llvm::abi::Type *ABITypeMapper::mapNoneType(mlir::NoneType type) {
107 return builder.getVoidType();
108}
109