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