[flang] Missing dso_local on globals and functions?

I noticed that Flang does not set dso_local on global variables and functions. I believe this is the reason that it does not produce direct relocations when -fno-pic is used.

Is there a reason dso_local is not used for Fortran variables/functions or is it just a TODO (if yes, then do we have an issue for this)?

Definition of dso_local from LLVM LangRef:

Runtime Preemption Specifiers
Global variables, functions and aliases may have an optional runtime preemption specifier. If a preemption specifier isn’t given explicitly, then a symbol is assumed to be dso_preemptable.

dso_preemptable
Indicates that the function or variable may be replaced by a symbol from outside the linkage unit at runtime.

dso_local
The compiler may assume that a function or variable marked as dso_local will resolve to a symbol within the same linkage unit. Direct access will be generated even if the definition is not within this compilation unit.

I suppose we can use llvm-project/clang/lib/CodeGen/CodeGenModule.cpp at 7ca6490636e758ec44ed149029b91163087d798e · llvm/llvm-project · GitHub as a reference.

No, I think it was likely just not possible in the LLVM dialect at the beginning of flang and was never considered since then.

1 Like

I guess there’s probably some considerations here for Windows. I’m not an expert in this, but my understanding is that we’re currently essentially (in the absence of dso_local) treating everything on Windows as dllexport/dllimport, which is likely incorrect. However, if we want to add dso_local we’re going to need some sort of syntax for dllexport/dllimport that we don’t have at the moment, and we’re probably going to break users doing it. I haven’t had much time to look into that yet but will do if you’re going to go ahead adding dso_local support to flang.

Good point, David! I will let you know, if/when I start working on this.