[flang-commits] [flang] [flang] only instantiate referenced use associated variables (PR #222318)

via flang-commits flang-commits at lists.llvm.org
Wed Sep 9 07:04:21 PDT 2026


================
@@ -0,0 +1,158 @@
+! Test that use associated module variables are only instantiated
+! in a subprogram when they are referenced in it.
+
+! RUN: %flang_fc1 -emit-hlfir %s -o - | FileCheck %s
+
+module test_use_instantiation
+  integer :: var1, var2, var3
+  integer :: a, b, c
+  equivalence (a, b)
+  integer :: n = 10, l = 3
+  integer, target :: pointee_target
+  real, target :: tgt(10)
+  integer, pointer :: p => null()
+  procedure(real), pointer :: proc_ptr
+  namelist /nml/ var3
+end module
+
+subroutine foo()
+  use test_use_instantiation
+  call bar(var1)
+end subroutine
+
+subroutine foo_equiv()
+  use test_use_instantiation
+  b = 1
+end subroutine
+
+subroutine foo_spec_expr()
+  use test_use_instantiation
+  real :: x(n)
+  call bar2(x)
+end subroutine
+
+subroutine foo_char_len()
+  use test_use_instantiation
+  character(l) :: ch
+  call bar3(ch)
+end subroutine
+
+subroutine foo_equiv_spec_expr()
+  use test_use_instantiation
+  real :: x(a)
+  call bar2(x)
+end subroutine
+
+subroutine foo_init_target()
+  use test_use_instantiation
+  real, pointer :: ptr(:) => tgt
+  call bar4(ptr)
+end subroutine
+
+subroutine foo_init_target_scalar()
+  use test_use_instantiation
+  integer, pointer :: iptr => pointee_target
+  call bar5(iptr)
+end subroutine
+
+subroutine foo_namelist()
+  use test_use_instantiation
+  read(*, nml)
+end subroutine
+
+subroutine foo_internal()
+  use test_use_instantiation
+  call internal()
+contains
+  subroutine internal()
+    var2 = 42
+  end subroutine
+end subroutine
+
+subroutine foo_unused()
+  use test_use_instantiation
+end subroutine
+
+! CHECK-LABEL: func.func @_QPfoo(
----------------
jeanPerier wrote:

Done.

https://github.com/llvm/llvm-project/pull/222318


More information about the flang-commits mailing list