MLIR 23.0.0git
ABITypeMapper.cpp
Go to the documentation of this file.
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
10#include "llvm/ADT/APFloat.h"
11#include "llvm/Support/Alignment.h"
12
13using namespace mlir;
14using namespace mlir::abi;
15
17 : dl(dataLayout), builder(allocator) {}
18
19const llvm::abi::Type *ABITypeMapper::map(mlir::Type type) {
20 if (auto intTy = dyn_cast<mlir::IntegerType>(type))
21 return mapIntegerType(intTy);
22
23 if (auto floatTy = dyn_cast<mlir::FloatType>(type))
24 return mapFloatType(floatTy);
25
26 if (auto indexTy = dyn_cast<mlir::IndexType>(type))
27 return mapIndexType(indexTy);
28
29 if (auto vecTy = dyn_cast<mlir::VectorType>(type))
30 return mapVectorType(vecTy);
31
32 if (auto memRefTy = dyn_cast<mlir::MemRefType>(type))
33 return mapMemRefType(memRefTy);
34
35 if (auto noneTy = dyn_cast<mlir::NoneType>(type))
36 return mapNoneType(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(type);
41 uint64_t abiAlign = dl.getTypeABIAlignment(type);
42 return builder.getIntegerType(sizeInBits.getFixedValue(),
43 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(type);
50 bool isSigned = type.isSigned() || type.isSignless();
51 return builder.getIntegerType(width, llvm::Align(abiAlign), isSigned);
52}
53
54const llvm::abi::Type *ABITypeMapper::mapFloatType(mlir::FloatType type) {
55 uint64_t abiAlign = dl.getTypeABIAlignment(type);
56 const llvm::fltSemantics &semantics = type.getFloatSemantics();
57 return builder.getFloatType(semantics, llvm::Align(abiAlign));
58}
59
60const llvm::abi::Type *ABITypeMapper::mapIndexType(mlir::IndexType type) {
61 llvm::TypeSize sizeInBits = dl.getTypeSizeInBits(type);
62 uint64_t abiAlign = dl.getTypeABIAlignment(type);
63 return builder.getIntegerType(sizeInBits.getFixedValue(),
64 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.getElementType());
70 if (!elementTy)
71 return nullptr;
72
73 auto shape = type.getShape();
74 uint64_t totalElements = 1;
75 for (int64_t dim : shape)
76 totalElements *= dim;
77
78 llvm::ElementCount ec = llvm::ElementCount::getFixed(totalElements);
79 uint64_t abiAlign = dl.getTypeABIAlignment(type);
80 return builder.getVectorType(elementTy, ec, llvm::Align(abiAlign));
81}
82
83const llvm::abi::Type *ABITypeMapper::mapMemRefType(mlir::MemRefType type) {
84 llvm::TypeSize sizeInBits = dl.getTypeSizeInBits(type);
85 uint64_t abiAlign = dl.getTypeABIAlignment(type);
86 unsigned addrSpace = 0;
87 if (auto as = type.getMemorySpace())
88 if (auto intAttr = dyn_cast<IntegerAttr>(as))
89 addrSpace = intAttr.getInt();
90 return builder.getPointerType(sizeInBits.getFixedValue(),
91 llvm::Align(abiAlign), addrSpace);
92}
93
94const llvm::abi::Type *ABITypeMapper::mapNoneType(mlir::NoneType type) {
95 return builder.getVoidType();
96}
The main mechanism for performing data layout queries.
uint64_t getTypeABIAlignment(Type t) const
Returns the required alignment of the given type in the current scope.
llvm::TypeSize getTypeSizeInBits(Type t) const
Returns the size in bits of the given type in the current scope.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
const llvm::abi::Type * map(mlir::Type type)
Map an MLIR type to its ABI type representation.
ABITypeMapper(const DataLayout &dl)
Include the generated interface declarations.