1 //===-- Lower/HostAssociations.h --------------------------------*- 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 FORTRAN_LOWER_HOSTASSOCIATIONS_H 10 #define FORTRAN_LOWER_HOSTASSOCIATIONS_H 11 12 #include "mlir/IR/Location.h" 13 #include "mlir/IR/Types.h" 14 #include "mlir/IR/Value.h" 15 #include "llvm/ADT/SetVector.h" 16 17 namespace Fortran { 18 namespace semantics { 19 class Symbol; 20 class Scope; 21 } // namespace semantics 22 23 namespace lower { 24 class AbstractConverter; 25 class SymMap; 26 27 /// Internal procedures in Fortran may access variables declared in the host 28 /// procedure directly. We bundle these variables together in a tuple and pass 29 /// them as an extra argument. 30 class HostAssociations { 31 public: 32 /// Returns true iff there are no host associations. empty()33 bool empty() const { return tupleSymbols.empty() && globalSymbols.empty(); } 34 35 /// Returns true iff there are host associations that are conveyed through 36 /// an extra tuple argument. hasTupleAssociations()37 bool hasTupleAssociations() const { return !tupleSymbols.empty(); } 38 39 /// Adds a set of Symbols that will be the host associated bindings for this 40 /// host procedure. 41 void addSymbolsToBind( 42 const llvm::SetVector<const Fortran::semantics::Symbol *> &symbols, 43 const Fortran::semantics::Scope &hostScope); 44 45 /// Code gen the FIR for the local bindings for the host associated symbols 46 /// for the host (parent) procedure using `builder`. 47 void hostProcedureBindings(AbstractConverter &converter, SymMap &symMap); 48 49 /// Code gen the FIR for the local bindings for the host associated symbols 50 /// for an internal (child) procedure using `builder`. 51 void internalProcedureBindings(AbstractConverter &converter, SymMap &symMap); 52 53 /// Return the type of the extra argument to add to each internal procedure. 54 mlir::Type getArgumentType(AbstractConverter &convert); 55 56 /// Is \p symbol host associated ? isAssociated(const Fortran::semantics::Symbol & symbol)57 bool isAssociated(const Fortran::semantics::Symbol &symbol) const { 58 return tupleSymbols.contains(&symbol) || globalSymbols.contains(&symbol); 59 } 60 61 private: 62 /// Canonical vector of host associated local symbols. 63 llvm::SetVector<const Fortran::semantics::Symbol *> tupleSymbols; 64 65 /// Canonical vector of host associated global symbols. 66 llvm::SetVector<const Fortran::semantics::Symbol *> globalSymbols; 67 68 /// The type of the extra argument to be added to each internal procedure. 69 mlir::Type argType; 70 71 /// Scope of the parent procedure if addSymbolsToBind was called. 72 const Fortran::semantics::Scope *hostScope; 73 }; 74 } // namespace lower 75 } // namespace Fortran 76 77 #endif // FORTRAN_LOWER_HOSTASSOCIATIONS_H 78