FLANG
Character.h
1//===-- Character.h -- lowering of characters -------------------*- 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_BUILDER_CHARACTER_H
14#define FORTRAN_OPTIMIZER_BUILDER_CHARACTER_H
15
16#include "flang/Optimizer/Builder/BoxValue.h"
17#include "flang/Optimizer/Builder/LowLevelIntrinsics.h"
18#include "flang/Optimizer/Builder/Runtime/Character.h"
19
20namespace fir {
21class FirOpBuilder;
22}
23
24namespace fir::factory {
25
28public:
30 explicit CharacterExprHelper(FirOpBuilder &builder, mlir::Location loc)
31 : builder{builder}, loc{loc} {}
33
36 void createCopy(const fir::CharBoxValue &dest, const fir::CharBoxValue &src,
37 mlir::Value count);
38
43 void createPadding(const fir::CharBoxValue &str, mlir::Value lower,
44 mlir::Value upper);
45
50
53 mlir::Value genSubstringBase(mlir::Value stringRawAddr,
54 mlir::Value lowerBound,
55 mlir::Type substringAddrType,
56 mlir::Value one = {});
57
59 mlir::Value createBlankConstant(fir::CharacterType type);
60
63 void createAssign(const fir::ExtendedValue &lhs,
64 const fir::ExtendedValue &rhs);
65
69 bool useHeap = false);
70
72 mlir::Value createLenTrim(const fir::CharBoxValue &str);
73
77 mlir::Value createEmboxChar(mlir::Value addr, mlir::Value len);
80 mlir::Value createEmbox(const fir::CharBoxValue &str);
83 mlir::Value createEmbox(const fir::CharArrayBoxValue &str);
84
88
90 std::pair<mlir::Value, mlir::Value> createUnboxChar(mlir::Value boxChar);
91
95 fir::CharBoxValue createCharacterTemp(mlir::Type type, mlir::Value len,
96 bool useHeap = false);
97
100 fir::CharBoxValue createCharacterTemp(mlir::Type type, int len);
101
104
107 static bool isCharacterLiteral(mlir::Type type);
108
113 static bool isCharacterScalar(mlir::Type type);
114
117 static bool hasConstantLengthInType(const fir::ExtendedValue &);
118
120 static fir::KindTy getCharacterKind(mlir::Type type);
121
123 static fir::KindTy getCharacterOrSequenceKind(mlir::Type type);
124
125 // TODO: Do we really need all these flavors of unwrapping to get the fir.char
126 // type? Or can we merge these? It would be better to merge them and eliminate
127 // the confusion.
128
131 static fir::CharacterType getCharType(mlir::Type type);
132
134 static fir::CharacterType getCharacterType(mlir::Type type);
135 static fir::CharacterType getCharacterType(const fir::CharBoxValue &box);
136 static fir::CharacterType getCharacterType(mlir::Value str);
137
151 fir::ExtendedValue toExtendedValue(mlir::Value character,
152 mlir::Value len = {});
153
163 static bool isArray(mlir::Type type);
164
173
175 mlir::Value createSingletonFromCode(mlir::Value code, int kind);
177 mlir::Value extractCodeFromSingleton(mlir::Value singleton);
178
183 mlir::Value getLength(mlir::Value memref);
184
188 mlir::Value readLengthFromBox(mlir::Value box);
189
191 mlir::Value readLengthFromBox(mlir::Value box, fir::CharacterType charTy);
192
193private:
196 fir::CharBoxValue materializeValue(mlir::Value str);
197 mlir::Value getCharBoxBuffer(const fir::CharBoxValue &box);
198 mlir::Value createElementAddr(mlir::Value buffer, mlir::Value index);
199 void createStoreCharAt(mlir::Value str, mlir::Value index, mlir::Value c);
200 void createLengthOneAssign(const fir::CharBoxValue &lhs,
201 const fir::CharBoxValue &rhs);
202 void createAssign(const fir::CharBoxValue &lhs, const fir::CharBoxValue &rhs);
203 mlir::Value createBlankConstantCode(fir::CharacterType type);
204
205private:
206 FirOpBuilder &builder;
207 mlir::Location loc;
208};
209
210//===----------------------------------------------------------------------===//
211// Tools to work with Character dummy procedures
212//===----------------------------------------------------------------------===//
213
217mlir::Type getCharacterProcedureTupleType(mlir::Type funcPointerType);
218
224 mlir::Location loc,
225 mlir::Type tupleType,
226 mlir::Value addr, mlir::Value len);
227
232std::pair<mlir::Value, mlir::Value>
233extractCharacterProcedureTuple(fir::FirOpBuilder &builder, mlir::Location loc,
234 mlir::Value tuple, bool openBoxProc = true);
235
239 mlir::Location loc,
240 fir::CharBoxValue srcBoxChar, int toKind,
241 bool useHeap = false);
242
243} // namespace fir::factory
244
245#endif // FORTRAN_OPTIMIZER_BUILDER_CHARACTER_H
Expressions of type CHARACTER and with rank > 0.
Definition BoxValue.h:169
Definition BoxValue.h:76
Definition BoxValue.h:469
Definition FIRBuilder.h:66
Helper to facilitate lowering of CHARACTER in FIR.
Definition Character.h:27
mlir::Value extractCodeFromSingleton(mlir::Value singleton)
Returns integer value held in a character singleton.
Definition Character.cpp:667
fir::CharBoxValue createCharacterTemp(mlir::Type type, mlir::Value len, bool useHeap=false)
Definition Character.cpp:359
fir::ExtendedValue toExtendedValue(mlir::Value character, mlir::Value len={})
Definition Character.cpp:123
fir::CharBoxValue createTempFrom(const fir::ExtendedValue &source)
Create a temporary with the same kind, length, and value as source.
Definition Character.cpp:379
static fir::CharacterType getCharacterType(mlir::Type type)
Get fir.char<kind> type with the same kind as inside str.
Definition Character.cpp:63
static fir::KindTy getCharacterOrSequenceKind(mlir::Type type)
Extract the kind of a character or array of character type.
Definition Character.cpp:644
static bool isCharacterScalar(mlir::Type type)
Definition Character.cpp:50
mlir::Value getLength(mlir::Value memref)
Definition Character.cpp:697
mlir::Value createEmbox(const fir::CharBoxValue &str)
Definition Character.cpp:199
static bool isArray(mlir::Type type)
Definition Character.cpp:55
fir::CharBoxValue createCharExtremum(bool predIsMin, llvm::ArrayRef< fir::CharBoxValue > opCBVs, bool useHeap=false)
Create {max,min}(lhs,rhs) in a stack or heap temporary.
Definition Character.cpp:764
static bool hasConstantLengthInType(const fir::ExtendedValue &)
Definition Character.cpp:648
void createPadding(const fir::CharBoxValue &str, mlir::Value lower, mlir::Value upper)
Definition Character.cpp:347
fir::ExtendedValue cleanUpCharacterExtendedValue(const fir::ExtendedValue &)
fir::CharBoxValue toScalarCharacter(const fir::CharArrayBoxValue &)
Definition Character.cpp:224
static fir::CharacterType getCharType(mlir::Type type)
Definition Character.cpp:69
mlir::Value createLenTrim(const fir::CharBoxValue &str)
LEN_TRIM intrinsic.
Definition Character.cpp:534
mlir::Value createSingletonFromCode(mlir::Value code, int kind)
Create fir.char<kind> singleton from code integer value.
Definition Character.cpp:655
mlir::Value createBlankConstant(fir::CharacterType type)
Return blank character of given type !fir.char<kind>
Definition Character.cpp:592
mlir::Value createEmboxChar(mlir::Value addr, mlir::Value len)
Definition Character.cpp:612
mlir::Value readLengthFromBox(mlir::Value box)
Definition Character.cpp:679
void createCopy(const fir::CharBoxValue &dest, const fir::CharBoxValue &src, mlir::Value count)
Definition Character.cpp:313
static fir::KindTy getCharacterKind(mlir::Type type)
Extract the kind of a character type.
Definition Character.cpp:638
std::pair< mlir::Value, mlir::Value > createUnboxChar(mlir::Value boxChar)
Unbox boxchar into (fir.ref<fir.char<kind>>, character length type).
Definition Character.cpp:618
void createAssign(const fir::ExtendedValue &lhs, const fir::ExtendedValue &rhs)
Definition Character.cpp:598
static bool isCharacterLiteral(mlir::Type type)
Definition Character.cpp:630
mlir::Value genSubstringBase(mlir::Value stringRawAddr, mlir::Value lowerBound, mlir::Type substringAddrType, mlir::Value one={})
Definition Character.cpp:479
CharacterExprHelper(FirOpBuilder &builder, mlir::Location loc)
Constructor.
Definition Character.h:30
fir::CharBoxValue createSubstring(const fir::CharBoxValue &str, llvm::ArrayRef< mlir::Value > bounds)
Definition Character.cpp:490
Definition FIRType.h:106
Definition BoxValue.h:446
mlir::Type getCharacterProcedureTupleType(mlir::Type funcPointerType)
Definition Character.cpp:758
std::pair< mlir::Value, mlir::Value > extractCharacterProcedureTuple(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value tuple, bool openBoxProc=true)
Definition Character.cpp:714
mlir::Value createCharacterProcedureTuple(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type tupleType, mlir::Value addr, mlir::Value len)
Definition Character.cpp:736
fir::CharBoxValue convertCharacterKind(fir::FirOpBuilder &builder, mlir::Location loc, fir::CharBoxValue srcBoxChar, int toKind, bool useHeap=false)
Definition Character.cpp:830
Definition AbstractConverter.h:37