[flang-commits] [flang] [flang] Represent use statement in fir. (PR #168106)

Abid Qadeer via flang-commits flang-commits at lists.llvm.org
Fri Nov 21 13:19:38 PST 2025


abidh wrote:

> Hi @abidh, thanks for working on this.
> 
> I wonder if this representation should better be outside of the func.func.
> 
> There is a case that is not addressed here I think (at least in the test), which are the indirect accesses when a module itself has use statements. In some modern application, there may be many of these indirect accesses, and putting all the use as operation in the func.func would pollute the IR quite a lot.

Hi @jeanPerier 

Thanks for taking a look. I was aware of module level use statements. I intentionally ignored them for this PR for the reason I list below. With this PR, we generate `fir.use_stmt` only for use statements that are at function level. For example, for the following code, we only get one `fir.use_stmt` in `_QQmain`

```
module base_module
  integer :: base_var_a = 10
end module base_module

module middle_module
  use base_module
  integer :: middle_var = 100
contains
  subroutine middle_sub()
    print *, "middle_sub:", base_var_a, middle_var
  end subroutine middle_sub
end module middle_module

program test
  use middle_module
  print *, "main:", base_var_a, middle_var
  call middle_sub()
end program test
```

My reasons for not generating `fir.use_stmt` at module level were the following:

1. We don't have any FIR module level construct to nest them in so where exactly they should be generated.
2. Even if we generate them, we will not be able use them to generate `DWARF`. The LLVM debug metadata like `DIModule` will require changes.

So I took the simpler approach of generating FIR for only those use statements that are in functions for this PR. That is the common use case and we can generate `DWARF` for them.

We can generate `fir.use_stmt` for module level use statement if we want in a future PR. The information about them is now available in lowering code. 

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


More information about the flang-commits mailing list