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 #ifndef OPTIMIZER_SUPPORT_KINDMAPPING_H 10 #define OPTIMIZER_SUPPORT_KINDMAPPING_H 11 12 #include "mlir/IR/OpDefinition.h" 13 #include "llvm/ADT/DenseMap.h" 14 #include "llvm/IR/Type.h" 15 16 namespace llvm { 17 struct fltSemantics; 18 } // namespace llvm 19 20 namespace fir { 21 22 /// The kind mapping is an encoded string that informs FIR how the Fortran KIND 23 /// values from the front-end should be converted to LLVM IR types. This 24 /// encoding allows the mapping from front-end KIND values to backend LLVM IR 25 /// types to be customized by the front-end. 26 /// 27 /// The provided string uses the following syntax. 28 /// 29 /// intrinsic-key `:` kind-value (`,` intrinsic-key `:` kind-value)* 30 /// 31 /// intrinsic-key is a single character for the intrinsic type. 32 /// 'i' : INTEGER (size in bits) 33 /// 'l' : LOGICAL (size in bits) 34 /// 'a' : CHARACTER (size in bits) 35 /// 'r' : REAL (encoding value) 36 /// 'c' : COMPLEX (encoding value) 37 /// 38 /// kind-value is either an unsigned integer (for 'i', 'l', and 'a') or one of 39 /// 'Half', 'Float', 'Double', 'X86_FP80', or 'FP128' (for 'r' and 'c'). 40 /// 41 /// If LLVM adds support for new floating-point types, the final list should be 42 /// extended. 43 class KindMapping { 44 public: 45 using KindTy = unsigned; 46 using Bitsize = unsigned; 47 using LLVMTypeID = llvm::Type::TypeID; 48 using MatchResult = mlir::ParseResult; 49 50 explicit KindMapping(mlir::MLIRContext *context); 51 explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map); 52 53 /// Get the size in bits of !fir.char<kind> 54 Bitsize getCharacterBitsize(KindTy kind) const; 55 56 /// Get the size in bits of !fir.int<kind> 57 Bitsize getIntegerBitsize(KindTy kind) const; 58 59 /// Get the size in bits of !fir.logical<kind> 60 Bitsize getLogicalBitsize(KindTy kind) const; 61 62 /// Get the size in bits of !fir.real<kind> 63 Bitsize getRealBitsize(KindTy kind) const; 64 65 /// Get the LLVM Type::TypeID of !fir.real<kind> 66 LLVMTypeID getRealTypeID(KindTy kind) const; 67 68 /// Get the LLVM Type::TypeID of !fir.complex<kind> 69 LLVMTypeID getComplexTypeID(KindTy kind) const; 70 getContext()71 mlir::MLIRContext *getContext() const { return context; } 72 73 /// Get the float semantics of !fir.real<kind> 74 const llvm::fltSemantics &getFloatSemantics(KindTy kind) const; 75 76 private: 77 MatchResult badMapString(const llvm::Twine &ptr); 78 MatchResult parse(llvm::StringRef kindMap); 79 80 mlir::MLIRContext *context; 81 llvm::DenseMap<std::pair<char, KindTy>, Bitsize> intMap; 82 llvm::DenseMap<std::pair<char, KindTy>, LLVMTypeID> floatMap; 83 }; 84 85 } // namespace fir 86 87 #endif // OPTIMIZER_SUPPORT_KINDMAPPING_H 88