Skip to content

Commit 2d0edd5

Browse files
committed
Fix visibility issue with interdependent module variables
The function instantiateGlobal was wrongfully assuming it would never be called on module variables being defined (as opposed to used) in the current scope. This is wrong since module variable may take each other addresses in their initializer, causing instantiateGlobal to be called in order to instantiate the module variable (create the global if needed + fir.address_of) in the initializer region of the other module variable. This caused some module symbols to be given local visibility, which was an issue when they are used in other compilation units. Remove this assumption and test if the variable requires external visibility in instantiateGlobal.
1 parent 38926f5 commit 2d0edd5

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

flang/lib/Lower/ConvertVariable.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,11 @@ static void instantiateGlobal(Fortran::lower::AbstractConverter &converter,
468468
mlir::StringAttr externalLinkage;
469469
global = declareGlobal(converter, var, globalName, externalLinkage);
470470
} else {
471-
// Module and common globals are defined elsewhere.
472-
// The only globals defined here are the globals owned by procedures
473-
// and they do not need to be visible in other compilation unit.
474-
auto internalLinkage = builder.createInternalLinkage();
475-
global = defineGlobal(converter, var, globalName, internalLinkage);
471+
mlir::StringAttr linkage; // external if remains empty.
472+
if (!sym.owner().IsModule() &&
473+
!Fortran::semantics::FindCommonBlockContaining(sym))
474+
linkage = builder.createInternalLinkage();
475+
global = defineGlobal(converter, var, globalName, linkage);
476476
}
477477
}
478478
auto addrOf = builder.create<fir::AddrOfOp>(loc, global.resultType(),

flang/test/Lower/module_definition.f90

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,21 @@ module modCommonInit1
4949
! CHECK-LABEL: fir.global @_QBnamed2 : tuple<i32> {
5050
! CHECK: %[[init:.*]] = fir.insert_value %{{.*}}, %c42{{.*}}, [0 : index] : (tuple<i32>, i32) -> tuple<i32>
5151
! CHECK: fir.has_value %[[init]] : tuple<i32>
52+
53+
! Test defining two module variables whose initializers depend on each others
54+
! addresses.
55+
module global_init_depending_on_each_other_address
56+
type a
57+
type(b), pointer :: pb
58+
end type
59+
type b
60+
type(a), pointer :: pa
61+
end type
62+
type(a), target :: xa
63+
type(b), target :: xb
64+
data xa, xb/a(xb), b(xa)/
65+
end module
66+
! CHECK-LABEL: fir.global @_QMglobal_init_depending_on_each_other_addressExb
67+
! CHECK: fir.address_of(@_QMglobal_init_depending_on_each_other_addressExa)
68+
! CHECK-LABEL: fir.global @_QMglobal_init_depending_on_each_other_addressExa
69+
! CHECK: fir.address_of(@_QMglobal_init_depending_on_each_other_addressExb)

0 commit comments

Comments
 (0)