FLANG
TypeConverter.h
1//===-- TypeConverter.h -- type conversion ----------------------*- C++ -*-===//
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// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef FORTRAN_OPTIMIZER_CODEGEN_TYPECONVERTER_H
14#define FORTRAN_OPTIMIZER_CODEGEN_TYPECONVERTER_H
15
16#include "flang/Optimizer/Builder/Todo.h" // remove when TODO's are done
17#include "flang/Optimizer/CodeGen/TBAABuilder.h"
18#include "flang/Optimizer/CodeGen/Target.h"
19#include "flang/Optimizer/Dialect/FIRType.h"
20#include "flang/Optimizer/Dialect/Support/FIRContext.h"
21#include "flang/Optimizer/Dialect/Support/KindMapping.h"
22#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
23#include "llvm/Support/Debug.h"
24
25// Position of the different values in a `fir.box`.
26static constexpr unsigned kAddrPosInBox = 0;
27static constexpr unsigned kElemLenPosInBox = 1;
28static constexpr unsigned kVersionPosInBox = 2;
29static constexpr unsigned kRankPosInBox = 3;
30static constexpr unsigned kTypePosInBox = 4;
31static constexpr unsigned kAttributePosInBox = 5;
32static constexpr unsigned kExtraPosInBox = 6;
33static constexpr unsigned kDimsPosInBox = 7;
34static constexpr unsigned kOptTypePtrPosInBox = 8;
35static constexpr unsigned kOptRowTypePosInBox = 9;
36
37// Position of the different values in [dims]
38static constexpr unsigned kDimLowerBoundPos = 0;
39static constexpr unsigned kDimExtentPos = 1;
40static constexpr unsigned kDimStridePos = 2;
41
42namespace mlir {
43class DataLayout;
44}
45
46namespace fir {
47
50class LLVMTypeConverter : public mlir::LLVMTypeConverter {
51public:
52 LLVMTypeConverter(mlir::ModuleOp module, bool applyTBAA,
53 bool forceUnifiedTBAATree, const mlir::DataLayout &);
54
55 // i32 is used here because LLVM wants i32 constants when indexing into struct
56 // types. Indexing into other aggregate types is more flexible.
57 mlir::Type offsetType() const;
58
59 // i64 can be used to index into aggregates like arrays
60 mlir::Type indexType() const;
61
62 // fir.type<name(p : TY'...){f : TY...}> --> llvm<"%name = { ty... }">
63 std::optional<mlir::Type> convertRecordType(fir::RecordType derived,
64 bool isPacked);
65
66 // Is an extended descriptor needed given the element type of a fir.box type ?
67 // Extended descriptors are required for derived types.
68 bool requiresExtendedDesc(mlir::Type boxElementType) const;
69
70 // Magic value to indicate we do not know the rank of an entity, either
71 // because it is assumed rank or because we have not determined it yet.
72 static constexpr int unknownRank() { return -1; }
73
74 // This corresponds to the descriptor as defined in ISO_Fortran_binding.h and
75 // the addendum defined in descriptor.h.
76 mlir::Type convertBoxType(BaseBoxType box, int rank = unknownRank()) const;
77
80 mlir::Type convertBoxTypeAsStruct(BaseBoxType box, int = unknownRank()) const;
81
82 // fir.boxproc<any> --> llvm<"{ any*, i8* }">
83 mlir::Type convertBoxProcType(BoxProcType boxproc) const;
84
85 unsigned characterBitsize(fir::CharacterType charTy) const;
86
87 // fir.char<k,?> --> llvm<"ix"> where ix is scaled by kind mapping
88 // fir.char<k,n> --> llvm.array<n x "ix">
89 mlir::Type convertCharType(fir::CharacterType charTy) const;
90
91 template <typename A> mlir::Type convertPointerLike(A &ty) const {
92 return mlir::LLVM::LLVMPointerType::get(ty.getContext());
93 }
94
95 // fir.array<c ... :any> --> llvm<"[...[c x any]]">
96 std::optional<mlir::Type> convertSequenceType(SequenceType seq) const;
97
98 // fir.tdesc<any> --> llvm<"i8*">
99 // TODO: For now use a void*, however pointer identity is not sufficient for
100 // the f18 object v. class distinction (F2003).
101 mlir::Type convertTypeDescType(mlir::MLIRContext *ctx) const;
102
103 const KindMapping &getKindMap() const { return kindMapping; }
104
105 // Relay TBAA tag attachment to TBAABuilder.
106 void attachTBAATag(mlir::LLVM::AliasAnalysisOpInterface op,
107 mlir::Type baseFIRType, mlir::Type accessFIRType,
108 mlir::LLVM::GEPOp gep) const;
109
110 const mlir::DataLayout &getDataLayout() const {
111 assert(dataLayout && "must be set in ctor");
112 return *dataLayout;
113 }
114
115private:
116 KindMapping kindMapping;
117 std::unique_ptr<CodeGenSpecifics> specifics;
118 std::unique_ptr<TBAABuilder> tbaaBuilder;
119 const mlir::DataLayout *dataLayout;
120};
121
122} // namespace fir
123
124#endif // FORTRAN_OPTIMIZER_CODEGEN_TYPECONVERTER_H
This class provides a shared interface for box and class types.
Definition FIRType.h:40
Definition KindMapping.h:48
mlir::Type convertBoxType(BaseBoxType box, int rank=unknownRank()) const
Definition TypeConverter.cpp:271
mlir::Type convertBoxTypeAsStruct(BaseBoxType box, int=unknownRank()) const
Definition TypeConverter.cpp:195
Definition AbstractConverter.h:37
Definition AbstractConverter.h:32