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
9#include "mlir/Dialect/OpenACC/OpenACCUtilsType.h"
10#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
11#include "mlir/Dialect/MemRef/IR/MemRef.h"
12#include "mlir/Dialect/OpenACC/Analysis/OpenACCSupport.h"
13#include "mlir/Dialect/OpenACC/OpenACC.h"
14#include "mlir/Dialect/OpenACC/OpenACCUtilsCG.h"
15#include "mlir/IR/Builders.h"
16#include "mlir/IR/BuiltinOps.h"
17#include "mlir/IR/BuiltinTypes.h"
18#include "mlir/Interfaces/DataLayoutInterfaces.h"
19
20namespace mlir {
21namespace acc {
22
23static std::optional<TypeSizeAndAlignment>
24getTypeSizeAndAlignmentHelper(Type ty, ModuleOp module, const DataLayout &dl,
25 OpenACCSupport *support) {
26 if (support)
27 return support->getTypeSizeAndAlignment(ty, module);
28 return getTypeSizeAndAlignment(ty, module, dl);
29}
30
31std::optional<TypeSizeAndAlignment>
32getTypeSizeAndAlignment(Type ty, ModuleOp module, const DataLayout &dl,
33 OpenACCSupport *support, Value var) {
34 if (ty.isIntOrIndexOrFloat() ||
35 isa<ComplexType, VectorType, DataLayoutTypeInterface>(Val: ty))
36 return TypeSizeAndAlignment{
37 dl.getTypeSize(t: ty),
38 llvm::TypeSize::getFixed(ExactSize: dl.getTypeABIAlignment(t: ty))};
39
40 // Product of element size and static dimensions; no inter-element padding or
41 // array alignment rules are applied. This is acceptable as per API
42 // documentation.
43 if (auto memrefTy = dyn_cast<MemRefType>(Val&: ty)) {
44 if (!memrefTy.hasStaticShape())
45 return std::nullopt;
46 auto elemSizeAndAlignment = getTypeSizeAndAlignmentHelper(
47 ty: memrefTy.getElementType(), module, dl, support);
48 if (!elemSizeAndAlignment)
49 return std::nullopt;
50 int64_t totalSize = elemSizeAndAlignment->first.getFixedValue();
51 int64_t alignment = elemSizeAndAlignment->second.getFixedValue();
52 for (int64_t dim : memrefTy.getShape())
53 totalSize *= dim;
54 return TypeSizeAndAlignment{llvm::TypeSize::getFixed(ExactSize: totalSize),
55 llvm::TypeSize::getFixed(ExactSize: alignment)};
56 }
57
58 // Sum of member sizes with no padding between members or tuple alignment
59 // rules applied. This is acceptable as per API documentation.
60 if (auto tupleTy = dyn_cast<TupleType>(Val&: ty)) {
61 if (tupleTy.size() == 0)
62 return std::nullopt;
63 auto sizeAndAlignment =
64 getTypeSizeAndAlignmentHelper(ty: tupleTy.getType(index: 0), module, dl, support);
65 if (!sizeAndAlignment)
66 return std::nullopt;
67 llvm::TypeSize size = sizeAndAlignment->first;
68 for (unsigned i = 1, e = tupleTy.size(); i < e; ++i) {
69 auto next = getTypeSizeAndAlignmentHelper(ty: tupleTy.getType(index: i), module, dl,
70 support);
71 if (!next)
72 return std::nullopt;
73 size += next->first;
74 }
75 return TypeSizeAndAlignment{size, sizeAndAlignment->second};
76 }
77
78 if (isa<FunctionType>(Val: ty))
79 return getTypeSizeAndAlignmentHelper(
80 ty: LLVM::LLVMPointerType::get(context: ty.getContext()), module, dl, support);
81
82 // Mapped-object size for MappableType when a value is available.
83 if (var) {
84 if (auto mappableTy = dyn_cast<MappableType>(Val&: ty)) {
85 std::optional<llvm::TypeSize> size =
86 mappableTy.getSizeInBytes(var, /*accBounds=*/{}, dataLayout: dl);
87 if (!size || size->isScalable())
88 return std::nullopt;
89 llvm::TypeSize alignment = llvm::TypeSize::getFixed(ExactSize: 1);
90 if (ty.isIntOrIndexOrFloat() || isa<DataLayoutTypeInterface>(Val: ty))
91 alignment = llvm::TypeSize::getFixed(ExactSize: dl.getTypeABIAlignment(t: ty));
92 return TypeSizeAndAlignment{*size, alignment};
93 }
94 }
95
96 return std::nullopt;
97}
98
99std::optional<TypeSizeAndAlignment>
100getTypeSizeAndAlignment(Type ty, ModuleOp module, OpenACCSupport *support,
101 Value var) {
102 std::optional<DataLayout> dl = getDataLayout(op: module);
103 if (!dl)
104 return std::nullopt;
105 return getTypeSizeAndAlignment(ty, module, dl: *dl, support, var);
106}
107
108Value castPointerLikeTypeIfNeeded(OpBuilder &builder, Location loc, Value value,
109 Type resultType) {
110 if (value.getType() == resultType)
111 return value;
112 if (PointerLikeType ptrLike = dyn_cast<PointerLikeType>(Val: value.getType())) {
113 if (Value casted = ptrLike.genCast(builder, loc, value, resultType))
114 return casted;
115 }
116 if (PointerLikeType ptrLike = dyn_cast<PointerLikeType>(Val&: resultType)) {
117 if (Value casted = ptrLike.genCast(builder, loc, value, resultType))
118 return casted;
119 }
120 emitError(loc) << "unsupported pointer-like type cast from "
121 << value.getType() << " to " << resultType;
122 return value;
123}
124
125} // namespace acc
126} // namespace mlir
127