FLANG
Target.h
1//===- Target.h - target specific details -----------------------*- 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_OPTMIZER_CODEGEN_TARGET_H
14#define FORTRAN_OPTMIZER_CODEGEN_TARGET_H
15
16#include "flang/Optimizer/Dialect/FIRType.h"
17#include "flang/Optimizer/Dialect/Support/KindMapping.h"
18#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
19#include "mlir/IR/BuiltinTypes.h"
20#include "llvm/TargetParser/Triple.h"
21#include <memory>
22#include <tuple>
23#include <vector>
24
25namespace mlir {
26class DataLayout;
27}
28
29namespace fir {
30
31namespace details {
36class Attributes {
37public:
38 enum class IntegerExtension { None, Zero, Sign };
39
40 Attributes(unsigned short alignment = 0, bool byval = false,
41 bool sret = false, bool append = false,
42 IntegerExtension intExt = IntegerExtension::None)
43 : alignment{alignment}, byval{byval}, sret{sret}, append{append},
44 intExt{intExt} {}
45
46 unsigned getAlignment() const { return alignment; }
47 bool hasAlignment() const { return alignment != 0; }
48 bool isByVal() const { return byval; }
49 bool isSRet() const { return sret; }
50 bool isAppend() const { return append; }
51 bool isZeroExt() const { return intExt == IntegerExtension::Zero; }
52 bool isSignExt() const { return intExt == IntegerExtension::Sign; }
53 llvm::StringRef getIntExtensionAttrName() const;
54
55private:
56 unsigned short alignment{};
57 bool byval : 1;
58 bool sret : 1;
59 bool append : 1;
60 IntegerExtension intExt;
61};
62
63} // namespace details
64
68class CodeGenSpecifics {
69public:
70 using Attributes = details::Attributes;
71 using TypeAndAttr = std::tuple<mlir::Type, Attributes>;
72 using Marshalling = std::vector<TypeAndAttr>;
73
74 static std::unique_ptr<CodeGenSpecifics>
75 get(mlir::MLIRContext *ctx, llvm::Triple &&trp, KindMapping &&kindMap,
76 llvm::StringRef targetCPU, mlir::LLVM::TargetFeaturesAttr targetFeatures,
77 llvm::StringRef targetABI, const mlir::DataLayout &dl);
78
79 static std::unique_ptr<CodeGenSpecifics>
80 get(mlir::MLIRContext *ctx, llvm::Triple &&trp, KindMapping &&kindMap,
81 llvm::StringRef targetCPU, mlir::LLVM::TargetFeaturesAttr targetFeatures,
82 llvm::StringRef targetABI, const mlir::DataLayout &dl,
83 llvm::StringRef tuneCPU);
84
85 static TypeAndAttr getTypeAndAttr(mlir::Type t) { return TypeAndAttr{t, {}}; }
86
87 CodeGenSpecifics(mlir::MLIRContext *ctx, llvm::Triple &&trp,
88 KindMapping &&kindMap, llvm::StringRef targetCPU,
89 mlir::LLVM::TargetFeaturesAttr targetFeatures,
90 llvm::StringRef targetABI, const mlir::DataLayout &dl)
91 : context{*ctx}, triple{std::move(trp)}, kindMap{std::move(kindMap)},
92 targetCPU{targetCPU}, targetFeatures{targetFeatures},
93 targetABI{targetABI}, dataLayout{&dl}, tuneCPU{""} {}
94
95 CodeGenSpecifics(mlir::MLIRContext *ctx, llvm::Triple &&trp,
96 KindMapping &&kindMap, llvm::StringRef targetCPU,
97 mlir::LLVM::TargetFeaturesAttr targetFeatures,
98 llvm::StringRef targetABI, const mlir::DataLayout &dl,
99 llvm::StringRef tuneCPU)
100 : context{*ctx}, triple{std::move(trp)}, kindMap{std::move(kindMap)},
101 targetCPU{targetCPU}, targetFeatures{targetFeatures},
102 targetABI{targetABI}, dataLayout{&dl}, tuneCPU{tuneCPU} {}
103
104 CodeGenSpecifics() = delete;
105 virtual ~CodeGenSpecifics() {}
106
108 virtual mlir::Type complexMemoryType(mlir::Type eleTy) const = 0;
109
113 virtual Marshalling complexArgumentType(mlir::Location loc,
114 mlir::Type eleTy) const = 0;
115
118 virtual Marshalling complexReturnType(mlir::Location loc,
119 mlir::Type eleTy) const = 0;
120
122 virtual mlir::Type boxcharMemoryType(mlir::Type eleTy) const = 0;
123
127 virtual Marshalling
128 structArgumentType(mlir::Location loc, fir::RecordType recTy,
129 const Marshalling &previousArguments) const = 0;
130
133 virtual Marshalling structReturnType(mlir::Location loc,
134 fir::RecordType eleTy) const = 0;
135
138 virtual Marshalling boxcharArgumentType(mlir::Type eleTy) const = 0;
139
140 // Compute ABI rules for an integer argument of the given mlir::IntegerType
141 // \p argTy. Note that this methods is supposed to be called for
142 // arguments passed by value not via reference, e.g. the 'i1' argument here:
143 // declare i1 @_FortranAioOutputLogical(ptr, i1)
144 //
145 // \p loc is the location of the operation using/specifying the argument.
146 //
147 // Currently, the only supported marshalling is whether the argument
148 // should be zero or sign extended.
149 //
150 // The zero/sign extension is especially important to comply with the ABI
151 // used by C/C++ compiler that builds Fortran runtime. As in the above
152 // example the callee will expect the caller to zero extend the second
153 // argument up to the size of the C/C++'s 'int' type.
154 // The corresponding handling in clang is done in
155 // DefaultABIInfo::classifyArgumentType(), and the logic may brielfy
156 // be explained as some sort of extension is required if the integer
157 // type is shorter than the size of 'int' for the target.
158 // The related code is located in ASTContext::isPromotableIntegerType()
159 // and ABIInfo::isPromotableIntegerTypeForABI().
160 // In particular, the latter returns 'true' for 'bool', several kinds
161 // of 'char', 'short', 'wchar' and enumerated types.
162 // The type of the extensions (zero or sign) depends on the signedness
163 // of the original language type.
164 //
165 // It is not clear how to handle signless integer types.
166 // From the point of Fortran-C interface all supported integer types
167 // seem to be signed except for CFI_type_Bool/bool that is supported
168 // via signless 'i1', but that is treated as unsigned type by clang
169 // (e.g. 'bool' arguments are using 'zeroext' ABI).
170 virtual Marshalling integerArgumentType(mlir::Location loc,
171 mlir::IntegerType argTy) const = 0;
172
173 // By default, integer argument and return values use the same
174 // zero/sign extension rules.
175 virtual Marshalling integerReturnType(mlir::Location loc,
176 mlir::IntegerType argTy) const = 0;
177
178 // Returns width in bits of C/C++ 'int' type size.
179 virtual unsigned char getCIntTypeWidth() const = 0;
180
181 llvm::StringRef getTargetCPU() const { return targetCPU; }
182 llvm::StringRef getTuneCPU() const { return tuneCPU; }
183
184 mlir::LLVM::TargetFeaturesAttr getTargetFeatures() const {
185 return targetFeatures;
186 }
187
188 llvm::StringRef getTargetABI() const { return targetABI; }
189
190 const mlir::DataLayout &getDataLayout() const {
191 assert(dataLayout && "dataLayout must be set");
192 return *dataLayout;
193 }
194
195protected:
196 mlir::MLIRContext &context;
197 llvm::Triple triple;
198 KindMapping kindMap;
199 llvm::StringRef targetCPU;
200 mlir::LLVM::TargetFeaturesAttr targetFeatures;
201 llvm::StringRef targetABI;
202 const mlir::DataLayout *dataLayout = nullptr;
203 llvm::StringRef tuneCPU;
204};
205
206} // namespace fir
207
208#endif // FORTRAN_OPTMIZER_CODEGEN_TARGET_H
virtual Marshalling complexArgumentType(mlir::Location loc, mlir::Type eleTy) const =0
virtual mlir::Type complexMemoryType(mlir::Type eleTy) const =0
Type presentation of a complex<ele> type value in memory.
virtual mlir::Type boxcharMemoryType(mlir::Type eleTy) const =0
Type presentation of a boxchar<n> type value in memory.
virtual Marshalling structArgumentType(mlir::Location loc, fir::RecordType recTy, const Marshalling &previousArguments) const =0
virtual Marshalling boxcharArgumentType(mlir::Type eleTy) const =0
virtual Marshalling structReturnType(mlir::Location loc, fir::RecordType eleTy) const =0
virtual Marshalling complexReturnType(mlir::Location loc, mlir::Type eleTy) const =0
Definition KindMapping.h:48
Definition Target.h:36
Definition AbstractConverter.h:37
llvm::StringRef getTuneCPU(mlir::ModuleOp mod)
Get the tune CPU string from the Module or return a null reference.
Definition FIRContext.cpp:142
Definition AbstractConverter.h:32