MLIR 23.0.0git
OpenACCUtilsType.cpp
Go to the documentation of this file.
1//===- OpenACCUtilsType.cpp - OpenACC Type Utilities ----------------------===//
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
14#include "mlir/IR/BuiltinOps.h"
17
18namespace mlir {
19namespace acc {
20
21static std::optional<TypeSizeAndAlignment>
22getTypeSizeAndAlignmentHelper(Type ty, ModuleOp module, const DataLayout &dl,
23 OpenACCSupport *support) {
24 if (support)
25 return support->getTypeSizeAndAlignment(ty, module);
26 return getTypeSizeAndAlignment(ty, module, dl);
27}
28
29std::optional<TypeSizeAndAlignment>
30getTypeSizeAndAlignment(Type ty, ModuleOp module, const DataLayout &dl,
31 OpenACCSupport *support) {
32 if (ty.isIntOrIndexOrFloat() ||
33 isa<ComplexType, VectorType, DataLayoutTypeInterface>(ty))
35 dl.getTypeSize(ty),
36 llvm::TypeSize::getFixed(dl.getTypeABIAlignment(ty))};
37
38 // Product of element size and static dimensions; no inter-element padding or
39 // array alignment rules are applied. This is acceptable as per API
40 // documentation.
41 if (auto memrefTy = dyn_cast<MemRefType>(ty)) {
42 if (!memrefTy.hasStaticShape())
43 return std::nullopt;
44 auto elemSizeAndAlignment = getTypeSizeAndAlignmentHelper(
45 memrefTy.getElementType(), module, dl, support);
46 if (!elemSizeAndAlignment)
47 return std::nullopt;
48 int64_t totalSize = elemSizeAndAlignment->first.getFixedValue();
49 int64_t alignment = elemSizeAndAlignment->second.getFixedValue();
50 for (int64_t dim : memrefTy.getShape())
51 totalSize *= dim;
52 return TypeSizeAndAlignment{llvm::TypeSize::getFixed(totalSize),
53 llvm::TypeSize::getFixed(alignment)};
54 }
55
56 // Sum of member sizes with no padding between members or tuple alignment
57 // rules applied. This is acceptable as per API documentation.
58 if (auto tupleTy = dyn_cast<TupleType>(ty)) {
59 if (tupleTy.size() == 0)
60 return std::nullopt;
61 auto sizeAndAlignment =
62 getTypeSizeAndAlignmentHelper(tupleTy.getType(0), module, dl, support);
63 if (!sizeAndAlignment)
64 return std::nullopt;
65 llvm::TypeSize size = sizeAndAlignment->first;
66 for (unsigned i = 1, e = tupleTy.size(); i < e; ++i) {
67 auto next = getTypeSizeAndAlignmentHelper(tupleTy.getType(i), module, dl,
68 support);
69 if (!next)
70 return std::nullopt;
71 size += next->first;
72 }
73 return TypeSizeAndAlignment{size, sizeAndAlignment->second};
74 }
75
76 if (isa<FunctionType>(ty))
78 LLVM::LLVMPointerType::get(ty.getContext()), module, dl, support);
79
80 return std::nullopt;
81}
82
83std::optional<TypeSizeAndAlignment>
84getTypeSizeAndAlignment(Type ty, ModuleOp module, OpenACCSupport *support) {
85 std::optional<DataLayout> dl = getDataLayout(module);
86 if (!dl)
87 return std::nullopt;
88 return getTypeSizeAndAlignment(ty, module, *dl, support);
89}
90
91} // namespace acc
92} // namespace mlir
The main mechanism for performing data layout queries.
llvm::TypeSize getTypeSize(Type t) const
Returns the size of the given type in the current scope.
uint64_t getTypeABIAlignment(Type t) const
Returns the required alignment 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
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
Definition Types.cpp:35
bool isIntOrIndexOrFloat() const
Return true if this is an integer (of any signedness), index, or float type.
Definition Types.cpp:122
std::optional< TypeSizeAndAlignment > getTypeSizeAndAlignment(Type ty, ModuleOp module)
Returns the size and ABI alignment in bytes for ty.
std::optional< DataLayout > getDataLayout(Operation *op, bool allowDefault=true)
Get the data layout for an operation.
static std::optional< TypeSizeAndAlignment > getTypeSizeAndAlignmentHelper(Type ty, ModuleOp module, const DataLayout &dl, OpenACCSupport *support)
std::optional< TypeSizeAndAlignment > getTypeSizeAndAlignment(Type ty, ModuleOp module, const DataLayout &dl, OpenACCSupport *support=nullptr)
Returns the size and ABI alignment in bytes.
std::pair< llvm::TypeSize, llvm::TypeSize > TypeSizeAndAlignment
Include the generated interface declarations.