FLANG
CallInterface.h
1//===-- Lower/CallInterface.h -- Procedure call interface ------*- 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// Utility that defines fir call interface for procedure both on caller and
14// and callee side and get the related FuncOp.
15// It does not emit any FIR code but for the created mlir::func::FuncOp, instead
16// it provides back a container of Symbol (callee side)/ActualArgument (caller
17// side) with additional information for each element describing how it must be
18// plugged with the mlir::func::FuncOp.
19// It handles the fact that hidden arguments may be inserted for the result.
20// while lowering.
21//
22// This utility uses the characteristic of Fortran procedures to operate, which
23// is a term and concept used in Fortran to refer to the signature of a function
24// or subroutine.
25//===----------------------------------------------------------------------===//
26
27#ifndef FORTRAN_LOWER_CALLINTERFACE_H
28#define FORTRAN_LOWER_CALLINTERFACE_H
29
30#include "flang/Common/reference.h"
31#include "flang/Evaluate/characteristics.h"
32#include "mlir/Dialect/Func/IR/FuncOps.h"
33#include <optional>
34
35namespace Fortran::semantics {
36class Symbol;
37}
38
39namespace mlir {
40class Location;
41}
42
43namespace fir {
44class FortranProcedureFlagsEnumAttr;
45}
46
47namespace Fortran::lower {
49class SymMap;
51namespace pft {
52struct FunctionLikeUnit;
53}
54
60class CallerInterface;
61class CalleeInterface;
62template <typename T>
64template <>
66 using FortranEntity = const Fortran::evaluate::ActualArgument *;
67 using FirValue = int;
68};
69template <>
71 using FortranEntity =
72 std::optional<common::Reference<const semantics::Symbol>>;
73 using FirValue = mlir::Value;
74};
75
77template <typename T>
79
94template <typename T>
95class CallInterface {
97
98public:
100 enum class PassEntityBy {
101 BaseAddress,
102 BoxChar,
103 // passing a read-only descriptor
104 Box,
105 // passing a writable descriptor
106 MutableBox,
107 AddressAndLength,
113 CharBoxValueAttribute, // BoxChar with VALUE
114 // Passing a character procedure as a <procedure address, result length>
115 // tuple.
116 CharProcTuple,
117 BoxProcRef
118 };
119
122 enum class Property {
123 BaseAddress,
124 BoxChar,
125 CharAddress,
126 CharLength,
127 CharProcTuple,
128 Box,
129 MutableBox,
130 Value,
131 BoxProcRef
132 };
133
134 using FortranEntity = typename PassedEntityTypes<T>::FortranEntity;
135 using FirValue = typename PassedEntityTypes<T>::FirValue;
136
139 struct FirPlaceHolder {
140 FirPlaceHolder(mlir::Type t, int passedPosition, Property p,
142 : type{t}, passedEntityPosition{passedPosition}, property{p},
143 attributes{attrs} {}
145 mlir::Type type;
149 static constexpr int resultEntityPosition = -1;
155 };
156
161 bool isOptional() const;
163 bool mayBeModifiedByCall() const;
165 bool mayBeReadByCall() const;
167 bool testTKR(Fortran::common::IgnoreTKR flag) const;
169 bool isIntentOut() const;
171 bool mustBeMadeContiguous() const;
173 bool hasValueAttribute() const;
175 bool hasAllocatableAttribute() const;
190 FortranEntity entity;
191 FirValue firArgument;
192 FirValue firLength; /* only for AddressAndLength */
193
196 nullptr;
197 };
198
201 mlir::func::FuncOp getFuncOp() const { return func; }
203 std::size_t getNumFIRArguments() const { return inputs.size(); }
204 std::size_t getNumFIRResults() const { return outputs.size(); }
207
211 return passedArguments;
212 }
213
215 std::optional<PassedEntity> getPassedResult() const { return passedResult; }
217 mlir::FunctionType genFunctionType();
218
221 void
222 determineInterface(bool isImplicit,
224
226 bool callerAllocateResult() const {
227 return mustPassResult() || mustSaveResult();
228 }
229
231 bool mustPassResult() const { return passedResult.has_value(); }
233 bool mustSaveResult() const { return saveResult; }
234
237 return characteristic && characteristic->CanBeCalledViaImplicitInterface();
238 }
239
242 fir::FortranProcedureFlagsEnumAttr
243 getProcedureAttrs(mlir::MLIRContext *) const;
244
245protected:
248 T &side() { return *static_cast<T *>(this); }
249 const T &side() const { return *static_cast<const T *>(this); }
252 void declare();
255 void mapPassedEntities();
256 void mapBackInputToPassedEntity(const FirPlaceHolder &, FirValue);
257
260 mlir::func::FuncOp func;
261 llvm::SmallVector<PassedEntity> passedArguments;
262 std::optional<PassedEntity> passedResult;
263 bool saveResult = false;
264
268 std::optional<Fortran::evaluate::characteristics::Procedure> characteristic =
269 std::nullopt;
270};
271
272//===----------------------------------------------------------------------===//
273// Caller side interface
274//===----------------------------------------------------------------------===//
275
280class CallerInterface : public CallInterface<CallerInterface> {
281public:
282 CallerInterface(const Fortran::evaluate::ProcedureRef &p,
284 : CallInterface{c}, procRef{p} {
285 declare();
287 actualInputs.resize(getNumFIRArguments());
288 }
289
291 bool hasAlternateReturns() const;
292 std::string getMangledName() const;
293 mlir::Location getCalleeLocation() const;
295
296 const Fortran::evaluate::ProcedureRef &getCallDescription() const {
297 return procRef;
298 }
299
303
304 bool isMainProgram() const { return false; }
305
308 bool isIndirectCall() const;
309
312 bool requireDispatchCall() const;
313
316 std::optional<unsigned> getPassArgIndex() const;
317
321 mlir::Value getIfPassedArg() const;
322
326
331 getDummySymbol(const PassedEntity &entity) const;
332
335 void placeInput(const PassedEntity &passedEntity, mlir::Value arg);
336 void placeAddressAndLengthInput(const PassedEntity &passedEntity,
337 mlir::Value addr, mlir::Value len);
338
340 mlir::Value getInput(const PassedEntity &passedEntity);
341
345
348 if (!verifyActualInputs())
349 llvm::report_fatal_error("lowered arguments are incomplete");
350 return actualInputs;
351 }
352
360 bool mustMapInterfaceSymbolsForDummyArgument(const PassedEntity &) const;
361
365 std::function<void(evaluate::Expr<evaluate::SomeType>, bool)>;
366
368 void walkResultExtents(const ExprVisitor &) const;
369
371 void walkResultLengths(const ExprVisitor &) const;
373 void walkDummyArgumentExtents(const PassedEntity &,
374 const ExprVisitor &) const;
376 void walkDummyArgumentLengths(const PassedEntity &,
377 const ExprVisitor &) const;
378
382 mlir::Value getArgumentValue(const semantics::Symbol &sym) const;
383
388
391 mlir::Type getResultStorageType() const;
392
394 mlir::Type getDummyArgumentType(const PassedEntity &) const;
395
396 // Copy of base implementation.
397 static constexpr bool hasHostAssociated() { return false; }
398 mlir::Type getHostAssociatedTy() const {
399 llvm_unreachable("getting host associated type in CallerInterface");
400 }
401
402 std::optional<mlir::Value> getOriginalPassArg() const {
403 return originalPassArg;
404 }
405 void setOriginalPassArg(mlir::Value x) { originalPassArg = x; }
406
407private:
409 bool verifyActualInputs() const;
410 const Fortran::evaluate::ProcedureRef &procRef;
411 llvm::SmallVector<mlir::Value> actualInputs;
412 std::optional<mlir::Value> originalPassArg;
413};
414
415//===----------------------------------------------------------------------===//
416// Callee side interface
417//===----------------------------------------------------------------------===//
418
421class CalleeInterface : public CallInterface<CalleeInterface> {
422public:
423 CalleeInterface(Fortran::lower::pft::FunctionLikeUnit &f,
425 : CallInterface{c}, funit{f} {
426 declare();
427 }
428
429 bool hasAlternateReturns() const;
430 std::string getMangledName() const;
431 mlir::Location getCalleeLocation() const;
433 bool isMainProgram() const;
434
435 Fortran::lower::pft::FunctionLikeUnit &getCallDescription() const {
436 return funit;
437 }
438
441 bool isIndirectCall() const { return false; }
442
445 bool requireDispatchCall() const { return false; };
446
450
453 mlir::func::FuncOp addEntryBlockAndMapArguments();
454
455 bool hasHostAssociated() const;
456 mlir::Type getHostAssociatedTy() const;
457 mlir::Value getHostAssociatedTuple() const;
458
459private:
461};
462
464mlir::FunctionType
467
474mlir::func::FuncOp
477
482mlir::Type getDummyProcedureType(const Fortran::semantics::Symbol &dummyProc,
484
487mlir::Type
490
492mlir::Type getUntypedBoxProcType(mlir::MLIRContext *context);
493
496bool isCPtrArgByValueType(mlir::Type ty);
497
499// This is required to convey the length of character functions passed as dummy
500// procedures.
504
505} // namespace Fortran::lower
506
507#endif // FORTRAN_LOWER_FIRBUILDER_H
Definition common.h:215
Definition call.h:334
Definition AbstractConverter.h:87
Implementation helper.
Definition CallInterface.cpp:896
Definition CallInterface.h:95
void declare()
Definition CallInterface.cpp:749
bool mustPassResult() const
Is the Fortran result passed as an extra MLIR argument ?
Definition CallInterface.h:231
llvm::SmallVector< mlir::Type > getResultType() const
Return the MLIR output types.
Definition CallInterface.cpp:1627
llvm::ArrayRef< PassedEntity > getPassedArguments() const
Definition CallInterface.h:210
bool callerAllocateResult() const
Does the caller need to allocate storage for the result ?
Definition CallInterface.h:226
std::size_t getNumFIRArguments() const
Number of MLIR inputs/outputs of the created FuncOp.
Definition CallInterface.h:203
std::optional< Fortran::evaluate::characteristics::Procedure > characteristic
Definition CallInterface.h:268
void mapPassedEntities()
Definition CallInterface.cpp:811
mlir::func::FuncOp getFuncOp() const
Definition CallInterface.h:201
T & side()
CRTP handle.
Definition CallInterface.h:248
bool canBeCalledViaImplicitInterface() const
Can the associated procedure be called via an implicit interface?
Definition CallInterface.h:236
void determineInterface(bool isImplicit, const Fortran::evaluate::characteristics::Procedure &)
Definition CallInterface.cpp:1596
PassEntityBy
Enum the different ways an entity can be passed-by.
Definition CallInterface.h:100
@ BaseAddressValueAttribute
ValueAttribute means dummy has the Fortran VALUE attribute.
Definition CallInterface.h:112
@ Value
Definition CallInterface.h:110
mlir::FunctionType genFunctionType()
Returns the mlir function type.
Definition CallInterface.cpp:1614
bool mustSaveResult() const
Must the MLIR result be saved with a fir.save_result ?
Definition CallInterface.h:233
Property
Definition CallInterface.h:122
std::optional< PassedEntity > getPassedResult() const
Definition CallInterface.h:215
fir::FortranProcedureFlagsEnumAttr getProcedureAttrs(mlir::MLIRContext *) const
Definition CallInterface.cpp:1636
Definition CallInterface.h:421
mlir::func::FuncOp addEntryBlockAndMapArguments()
Definition CallInterface.cpp:615
const Fortran::semantics::Symbol * getProcedureSymbol() const
Definition CallInterface.cpp:587
bool isIndirectCall() const
Definition CallInterface.h:441
bool requireDispatchCall() const
Definition CallInterface.h:445
Definition CallInterface.h:280
std::function< void(evaluate::Expr< evaluate::SomeType >, bool)> ExprVisitor
Definition CallInterface.h:364
const Fortran::semantics::SubprogramDetails * getInterfaceDetails() const
Definition CallInterface.cpp:563
mlir::Value getIfPassedArg() const
Definition CallInterface.cpp:172
bool mustMapInterfaceSymbolsForResult() const
Definition CallInterface.cpp:477
mlir::Type getDummyArgumentType(const PassedEntity &) const
Return FIR type of argument.
Definition CallInterface.cpp:546
mlir::Value getInput(const PassedEntity &passedEntity)
Get lowered FIR argument given the Fortran argument.
Definition CallInterface.cpp:379
void walkDummyArgumentExtents(const PassedEntity &, const ExprVisitor &) const
Walk non-deferred extent specification expressions of a dummy argument.
Definition CallInterface.cpp:466
void walkDummyArgumentLengths(const PassedEntity &, const ExprVisitor &) const
Walk non-deferred length specification expressions of a dummy argument.
Definition CallInterface.cpp:414
bool requireDispatchCall() const
Definition CallInterface.cpp:126
const Fortran::evaluate::ProcedureDesignator * getIfIndirectCall() const
Definition CallInterface.cpp:182
bool mustMapInterfaceSymbolsForDummyArgument(const PassedEntity &) const
Definition CallInterface.cpp:493
const Fortran::semantics::Symbol * getProcedureSymbol() const
Definition CallInterface.cpp:111
bool isIndirectCall() const
Definition CallInterface.cpp:119
void placeInput(const PassedEntity &passedEntity, mlir::Value arg)
Definition CallInterface.cpp:348
const Fortran::semantics::Symbol * getDummySymbol(const PassedEntity &entity) const
Definition CallInterface.cpp:522
void walkResultExtents(const ExprVisitor &) const
Walk the result non-deferred extent specification expressions.
Definition CallInterface.cpp:450
std::optional< unsigned > getPassArgIndex() const
Definition CallInterface.cpp:148
mlir::Type getResultStorageType() const
Definition CallInterface.cpp:539
const Fortran::semantics::Symbol & getResultSymbol() const
Definition CallInterface.cpp:552
mlir::Value getArgumentValue(const semantics::Symbol &sym) const
Definition CallInterface.cpp:504
void walkResultLengths(const ExprVisitor &) const
Walk the result non-deferred length specification expressions.
Definition CallInterface.cpp:403
bool hasAlternateReturns() const
CRTP callbacks.
Definition CallInterface.cpp:61
llvm::ArrayRef< mlir::Value > getInputs() const
Get the input vector once it is complete.
Definition CallInterface.h:347
Definition HostAssociations.h:28
Definition SymbolMap.h:181
Definition symbol.h:896
Definition FIRType.h:106
Definition OpenACC.h:20
Definition ParserActions.h:24
bool mustPassLengthWithDummyProcedure(const Fortran::evaluate::ProcedureDesignator &proc, Fortran::lower::AbstractConverter &)
Is it required to pass proc as a tuple<function address, result length> ?
Definition CallInterface.cpp:1835
mlir::FunctionType translateSignature(const Fortran::evaluate::ProcedureDesignator &, Fortran::lower::AbstractConverter &)
Translate a procedure characteristics to an mlir::FunctionType signature.
Definition CallInterface.cpp:1803
bool isCPtrArgByValueType(mlir::Type ty)
Definition CallInterface.cpp:1868
mlir::func::FuncOp getOrDeclareFunction(const Fortran::evaluate::ProcedureDesignator &, Fortran::lower::AbstractConverter &)
Definition CallInterface.cpp:1809
mlir::Type getDummyProcedurePointerType(const Fortran::semantics::Symbol &dummyProcPtr, Fortran::lower::AbstractConverter &)
Definition CallInterface.cpp:1857
mlir::Type getUntypedBoxProcType(mlir::MLIRContext *context)
Return !fir.boxproc<() -> ()> type.
Definition CallInterface.cpp:34
mlir::Type getDummyProcedureType(const Fortran::semantics::Symbol &dummyProc, Fortran::lower::AbstractConverter &)
Definition CallInterface.cpp:1844
Definition AbstractConverter.h:37
Definition AbstractConverter.h:32
Definition characteristics.h:367
int passedEntityPosition
Definition CallInterface.h:148
Property property
Definition CallInterface.h:152
llvm::SmallVector< mlir::NamedAttribute > attributes
MLIR attributes for this argument.
Definition CallInterface.h:154
mlir::Type type
Type for this input/output.
Definition CallInterface.h:145
Definition CallInterface.h:159
FortranEntity entity
Definition CallInterface.h:190
bool testTKR(Fortran::common::IgnoreTKR flag) const
Does the argument have the specified IgnoreTKR flag?
Definition CallInterface.cpp:1481
bool mayRequireIntentoutFinalization() const
Definition CallInterface.cpp:1547
bool mayBeModifiedByCall() const
Can the argument be modified by the callee?
Definition CallInterface.cpp:1465
bool mayBeReadByCall() const
Can the argument be read by the callee?
Definition CallInterface.cpp:1474
bool hasValueAttribute() const
Does the dummy argument have the VALUE attribute?
Definition CallInterface.cpp:1522
bool isIntentOut() const
Is the argument INTENT(OUT)
Definition CallInterface.cpp:1494
bool hasAllocatableAttribute() const
Does the dummy argument have the ALLOCATABLE attribute?
Definition CallInterface.cpp:1534
const Fortran::evaluate::characteristics::DummyArgument * characteristics
Pointer to the argument characteristics. Nullptr for results.
Definition CallInterface.h:195
bool isSequenceAssociatedDescriptor() const
Definition CallInterface.cpp:1586
PassEntityBy passBy
How entity is passed by.
Definition CallInterface.h:186
bool mustBeMadeContiguous() const
Does the argument have the CONTIGUOUS attribute or have explicit shape?
Definition CallInterface.cpp:1500
bool isOptional() const
Is the dummy argument optional?
Definition CallInterface.cpp:1459
Definition CallInterface.h:63
Definition PFTBuilder.h:629