Is dso_local set in LLVM IR output?

https://github.com/llvm/llvm-project/commit/2047c10c22b071cccc57a7e2779d6603512e9113 was to clean up llvm::TargetMachine::shouldAssumeDSOLocal. For Clang, that code is unneeded because Clang sets dso_local https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CodeGenModule.cpp#L910

I am thinking whether https://github.com/llvm/llvm-project/commit/2047c10c22b071cccc57a7e2779d6603512e9113 should be reverted (or be replaced with if (RM == Reloc::Static && !GV->isDeclarationForLinker()) return true) as a workaround before IR producers set dso_local properly.

But for Julia, it’d be nice to explicitly set dso_local like Clang.

About dso_local in LLVM IR, it was introduced to model “whether references to a function/global variable (either definition or declaration) can be assumed to be resolved within the linkage unit”.

On ELF, STV_DEFAULT STB_GLOBAL/STB_WEAK symbols are preemptible (interposable) for a shared object (ld -shared). Such -fpic produced symbols are modeled with dso_preemptable. (Yes, “preemptible” is used more frequently than “preemptable” in ELF)

On COFF, only extern_weak or dllimport symbols can be dso_preemptable.

On Mach-O, the current rule is

  if (TT.isOSBinFormatMachO()) {
    if (RM == llvm::Reloc::Static)
      return true;
    return GV->isStrongDefinitionForLinker();
  }

It’d be nice if someone can clarify whether the GV->isStrongDefinitionForLinker() condition can be generalized to !GV->isDeclaractionForLinker in non-static relocation models (llc -relocation-model=pic or llc -relocation-model=dynamic-no-pic)

2 Likes

Thanks for the info! I am working on this now. I think I missed this comment initially, but Google found it for me now, so it turned out to be helpful 2 years later anyways! For Mach-O I found the main challenge is that clang neither permits visibility(protected) in the clang parser nor allows aliases, so it is rather difficult to get clang to mark an exported symbol as dso_local. Do you have any thoughts on what that should do instead?

1 Like