FLANG
KindMapping.h
1//===-- Optimizer/Support/KindMapping.h -- support kind mapping -*- 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_SUPPORT_KINDMAPPING_H
14#define FORTRAN_OPTIMIZER_SUPPORT_KINDMAPPING_H
15
16#include "mlir/IR/OpDefinition.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/IR/Type.h"
19
20namespace llvm {
21struct fltSemantics;
22} // namespace llvm
23
24namespace fir {
25
49public:
50 using KindTy = unsigned;
51 using Bitsize = unsigned;
52 using LLVMTypeID = llvm::Type::TypeID;
53 using MatchResult = mlir::ParseResult;
54
57 explicit KindMapping(mlir::MLIRContext *context);
63 explicit KindMapping(mlir::MLIRContext *context, llvm::ArrayRef<KindTy> defs);
68 explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map,
69 llvm::ArrayRef<KindTy> defs = std::nullopt);
70 explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map,
71 llvm::StringRef defs)
72 : KindMapping{context, map, toDefaultKinds(defs)} {}
73
75 Bitsize getCharacterBitsize(KindTy kind) const;
76
78 Bitsize getIntegerBitsize(KindTy kind) const;
79
81 Bitsize getLogicalBitsize(KindTy kind) const;
82
84 Bitsize getRealBitsize(KindTy kind) const;
85
87 LLVMTypeID getRealTypeID(KindTy kind) const;
88
90 LLVMTypeID getComplexTypeID(KindTy kind) const;
91
92 mlir::MLIRContext *getContext() const { return context; }
93
95 const llvm::fltSemantics &getFloatSemantics(KindTy kind) const;
96
98 static constexpr const char *getDefaultMap() { return ""; }
99
101 std::string mapToString() const;
102
103 //===--------------------------------------------------------------------===//
104 // Default kinds of intrinsic types
105 //===--------------------------------------------------------------------===//
106
107 KindTy defaultCharacterKind() const;
108 KindTy defaultComplexKind() const;
109 KindTy defaultDoubleKind() const;
110 KindTy defaultIntegerKind() const;
111 KindTy defaultLogicalKind() const;
112 KindTy defaultRealKind() const;
113
115 static constexpr const char *getDefaultKinds() { return "a1c4d8i4l4r4"; }
116
118 std::string defaultsToString() const;
119
122 static std::vector<KindTy> toDefaultKinds(llvm::StringRef defs);
123
124private:
125 MatchResult badMapString(const llvm::Twine &ptr);
126 MatchResult parse(llvm::StringRef kindMap);
127 llvm::LogicalResult setDefaultKinds(llvm::ArrayRef<KindTy> defs);
128
129 mlir::MLIRContext *context;
130 llvm::DenseMap<std::pair<char, KindTy>, Bitsize> intMap;
131 llvm::DenseMap<std::pair<char, KindTy>, LLVMTypeID> floatMap;
132 llvm::DenseMap<char, KindTy> defaultMap;
133};
134
135} // namespace fir
136
137#endif // FORTRAN_OPTIMIZER_SUPPORT_KINDMAPPING_H
Definition: KindMapping.h:48
std::string defaultsToString() const
Convert the current default kinds to a string.
Definition: KindMapping.cpp:380
static constexpr const char * getDefaultMap()
Get the default kind map as a string.
Definition: KindMapping.h:98
Bitsize getIntegerBitsize(KindTy kind) const
Get the size in bits of !fir.int<kind>
Definition: KindMapping.cpp:282
static constexpr const char * getDefaultKinds()
Get the default kinds as a string.
Definition: KindMapping.h:115
const llvm::fltSemantics & getFloatSemantics(KindTy kind) const
Get the float semantics of !fir.real<kind>
Definition: KindMapping.cpp:305
std::string mapToString() const
Convert the current kind map to a string.
Definition: KindMapping.cpp:309
Bitsize getLogicalBitsize(KindTy kind) const
Get the size in bits of !fir.logical<kind>
Definition: KindMapping.cpp:286
static std::vector< KindTy > toDefaultKinds(llvm::StringRef defs)
Definition: KindMapping.cpp:409
Bitsize getCharacterBitsize(KindTy kind) const
Get the size in bits of !fir.char<kind>
Definition: KindMapping.cpp:278
Bitsize getRealBitsize(KindTy kind) const
Get the size in bits of !fir.real<kind>
Definition: KindMapping.cpp:298
LLVMTypeID getRealTypeID(KindTy kind) const
Get the LLVM Type::TypeID of !fir.real<kind>
Definition: KindMapping.cpp:290
LLVMTypeID getComplexTypeID(KindTy kind) const
Get the LLVM Type::TypeID of !fir.complex<kind>
Definition: KindMapping.cpp:294
Definition: FIRType.h:77
Definition: AbstractConverter.h:31