llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[OpenMP] offload duplicate symbol issue when the same library appears twice on the link line

Open ye-luo opened this issue 6 months ago • 5 comments

It seems that the issue is not just in flang #195188 foo.cpp

#pragma omp declare target
int aa;
#pragma omp end declare target

void foo()
{
  aa = 1;
}

main.cpp

extern int aa;
void foo();
int main()
{
  aa = 1;
  foo();
}

run_clang.sh

clang++ -fopenmp --offload-arch=gfx906 foo.cpp -c
rm foo.a
ar qc foo.a foo.o
ranlib foo.a
clang++ -fopenmp --offload-arch=gfx906 main.cpp foo.a foo.a

failure

$ bash run_clang.sh
ld.lld: error: duplicate symbol: aa
>>> defined in /tmp/foo-amdgcn-amd-amdhsa-gfx906-18599b.o
>>> defined in /tmp/foo-amdgcn-amd-amdhsa-gfx906-c89a83.o
clang: error: ld.lld command failed with exit code 1 (use -v to see invocation)
/soft/llvm/main-20260413/bin/clang-linker-wrapper: error: 'clang' failed
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

ye-luo avatar May 15 '26 20:05 ye-luo

I'd like to work on this issue. I’ll take a look and submit a PR if I can reproduce and fix it.

syhhyl avatar May 16 '26 07:05 syhhyl

@llvm/issue-subscribers-openmp

Author: Ye Luo (ye-luo)

It seems that the issue is not just in flang #195188 foo.cpp ``` #pragma omp declare target int aa; #pragma omp end declare target

void foo() { aa = 1; }

main.cpp

extern int aa; void foo(); int main() { aa = 1; foo(); }

run_clang.sh 

clang++ -fopenmp --offload-arch=gfx906 foo.cpp -c rm foo.a ar qc foo.a foo.o ranlib foo.a clang++ -fopenmp --offload-arch=gfx906 main.cpp foo.a foo.a

failure
```bash
$ bash run_clang.sh
ld.lld: error: duplicate symbol: aa
>>> defined in /tmp/foo-amdgcn-amd-amdhsa-gfx906-18599b.o
>>> defined in /tmp/foo-amdgcn-amd-amdhsa-gfx906-c89a83.o
clang: error: ld.lld command failed with exit code 1 (use -v to see invocation)
/soft/llvm/main-20260413/bin/clang-linker-wrapper: error: 'clang' failed
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

I believe this is by design, not a bug. Archives in openmp don't have the same semantics as archives everywhere else - it should be behaving exactly like llvm-link on the members, which would give duplicate symbol errors. At least that's what the meeting minutes from a few years back should show.

If changing this, say to match usual archieve semantics, checking weak symbol override behaviour at the same time would be good. I think it'll show similar behaviour vs expectations to the duplicate symbol.

JonChesterfield avatar May 18 '26 11:05 JonChesterfield

I believe this is by design, not a bug. Archives in openmp don't have the same semantics as archives everywhere else - it should be behaving exactly like llvm-link on the members, which would give duplicate symbol errors. At least that's what the meeting minutes from a few years back should show.

If changing this, say to match usual archieve semantics, checking weak symbol override behaviour at the same time would be good. I think it'll show similar behaviour vs expectations to the duplicate symbol.

Yep, the fundamental issue is that offloading languages have additional expectations. Static libraries only extract when a symbol in the archive defines a currently undefined symbol. In the case of offloading, you have globals and kenrels that are only referenced on the CPU.

There is not a clean solution to this without implementing a lot of custom logic. The best I could do is to get a list of the offloading entries as all the symbols the host needs. Then just use a linker script with EXTERN or similar to state that these functions are needed. This wouldn't solve the problem in general however, because then you'd just get the same issue on multiply defined globals.

jhuber6 avatar May 18 '26 12:05 jhuber6

That makes sense. It sounds like the core problem is that we're trying to reuse traditional archive semantics for something that no longer has purely host-visible reachability.

One thing I'm wondering is whether the offloading pipeline eventually needs a more explicit device-side resolution phase instead of relying on host archive extraction semantics indirectly. Right now the archive behavior seems caught between two models: traditional lazy extraction driven by host undefined symbols whole-program style aggregation needed for device entities

and the current failure modes (duplicate symbols, weak override mismatches, missing device entries) are all symptoms of that mismatch.

I suppose the difficult part is defining ownership / visibility rules for device globals in a way that still preserves reasonable archive behavior without degenerating into full eager linking.

syhhyl avatar May 18 '26 12:05 syhhyl