Skip to content

Commit

Permalink
[flang] Non-type-bound defined IO lowering
Browse files Browse the repository at this point in the history
Generate supporting data structures and calls to new runtime IO functions
for defined IO that accesses non-type-bound procedures, such as `wft` in:

module m1
  type t
    integer n
  end type
  interface write(formatted)
    module procedure wft
  end interface
 contains
  subroutine wft(dtv, unit, iotype, v_list, iostat, iomsg)
    class(t), intent(in) :: dtv
    integer, intent(in) :: unit
    character(*), intent(in) :: iotype
    integer, intent(in) :: v_list(:)
    integer, intent(out) :: iostat
    character(*), intent(inout) :: iomsg
    iostat = 0
    write(unit,*,iostat=iostat,iomsg=iomsg) 'wft was called: ', dtv%n
  end subroutine
end module

module m2
 contains
  subroutine test1
    use m1
    print *, 'test1, should call wft: ', t(1)
  end subroutine
  subroutine test2
    use m1, only: t
    print *, 'test2, should not call wft: ', t(2)
  end subroutine
end module

use m1
use m2
call test1
call test2
print *, 'main, should call wft: ', t(3)
end
  • Loading branch information
vdonaldson committed May 17, 2023
1 parent 53aed47 commit 6f7a3b0
Show file tree
Hide file tree
Showing 16 changed files with 741 additions and 203 deletions.
9 changes: 7 additions & 2 deletions flang/include/flang/Lower/AbstractConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ class AbstractConverter {
/// Generate the location as converted from a CharBlock
virtual mlir::Location genLocation(const Fortran::parser::CharBlock &) = 0;

/// Get the converter's current scope
virtual const Fortran::semantics::Scope &getCurrentScope() = 0;

//===--------------------------------------------------------------------===//
// FIR/MLIR
//===--------------------------------------------------------------------===//
Expand All @@ -237,11 +240,13 @@ class AbstractConverter {
virtual mlir::ModuleOp &getModuleOp() = 0;
/// Get the MLIRContext
virtual mlir::MLIRContext &getMLIRContext() = 0;
/// Unique a symbol
/// Unique a symbol (add a containing scope specific prefix)
virtual std::string mangleName(const Fortran::semantics::Symbol &) = 0;
/// Unique a derived type
/// Unique a derived type (add a containing scope specific prefix)
virtual std::string
mangleName(const Fortran::semantics::DerivedTypeSpec &) = 0;
/// Unique a compiler generated name (add a containing scope specific prefix)
virtual std::string mangleName(std::string &) = 0;
/// Get the KindMap.
virtual const fir::KindMapping &getKindMap() = 0;

Expand Down
4 changes: 4 additions & 0 deletions flang/include/flang/Lower/Mangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ std::string mangleName(const semantics::Symbol &,
/// Convert a derived type instance to an internal name.
std::string mangleName(const semantics::DerivedTypeSpec &, ScopeBlockIdMap &);

/// Add a scope specific mangling prefix to a compiler generated name.
std::string mangleName(std::string &, const Fortran::semantics::Scope &,
ScopeBlockIdMap &);

/// Recover the bare name of the original symbol from an internal name.
std::string demangleName(llvm::StringRef name);

Expand Down
6 changes: 5 additions & 1 deletion flang/include/flang/Optimizer/Support/InternalNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ struct NameUniquer {
std::int64_t block, llvm::StringRef name,
llvm::ArrayRef<std::int64_t> kinds);

/// Unique a compiler generated name
/// Unique a compiler generated name without scope context.
static std::string doGenerated(llvm::StringRef name);
/// Unique a compiler generated name with scope context.
static std::string doGenerated(llvm::ArrayRef<llvm::StringRef> modules,
llvm::ArrayRef<llvm::StringRef> procs,
std::int64_t blockId, llvm::StringRef name);

/// Unique an intrinsic type descriptor
static std::string
Expand Down
10 changes: 9 additions & 1 deletion flang/include/flang/Semantics/runtime-type-info.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ struct NonTbpDefinedIo {
};

std::multimap<const Symbol *, NonTbpDefinedIo>
CollectNonTbpDefinedIoGenericInterfaces(const Scope &scope);
CollectNonTbpDefinedIoGenericInterfaces(
const Scope &, bool useRuntimeTypeInfoEntries);

bool ShouldIgnoreRuntimeTypeInfoNonTbpGenericInterfaces(
const Scope &, const DerivedTypeSpec *);
bool ShouldIgnoreRuntimeTypeInfoNonTbpGenericInterfaces(
const Scope &, const DeclTypeSpec *);
bool ShouldIgnoreRuntimeTypeInfoNonTbpGenericInterfaces(
const Scope &, const Symbol *);

} // namespace Fortran::semantics
#endif // FORTRAN_SEMANTICS_RUNTIME_TYPE_INFO_H_
9 changes: 8 additions & 1 deletion flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,10 @@ class FirConverter : public Fortran::lower::AbstractConverter {
return genUnknownLocation();
}

const Fortran::semantics::Scope &getCurrentScope() override final {
return bridge.getSemanticsContext().FindScope(currentPosition);
}

fir::FirOpBuilder &getFirOpBuilder() override final { return *builder; }

mlir::ModuleOp &getModuleOp() override final { return bridge.getModule(); }
Expand All @@ -758,7 +762,10 @@ class FirConverter : public Fortran::lower::AbstractConverter {
const Fortran::semantics::DerivedTypeSpec &derivedType) override final {
return Fortran::lower::mangle::mangleName(derivedType, scopeBlockIdMap);
}

std::string mangleName(std::string &name) override final {
return Fortran::lower::mangle::mangleName(name, getCurrentScope(),
scopeBlockIdMap);
}
const fir::KindMapping &getKindMap() override final {
return bridge.getKindMap();
}
Expand Down

0 comments on commit 6f7a3b0

Please sign in to comment.