`__nvqpp_vectorCopyCtor` gets external linkage, breaking links with >1 device translation unit

Open
#5,199 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

  • #5200 by @cabreraam — closed without merging

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
cpp
Domain
compilers

Research direction

Start with the intrinsic handling in ConvertStmt.cpp and the definitions in Intrinsics.cpp, then reproduce the duplicate symbol by compiling the two provided kernel translation units and linking them together. Check how the generated __nvqpp_vectorCopyCtor linkage is lowered; done means the multi-translation-unit link succeeds and the helper is no longer emitted as a public strong symbol.

Written by the indexing model from the issue text.

Description

stale-notified
Summary

Since #5044 (a2f58465), __nvqpp_vectorCopyCtor is marked no_inline. It therefore
survives codegen into every object file that needs it, but it is emitted with
external linkage — so linking two or more separately-nvq++-compiled translation
units into one library or executable now fails with duplicate symbols.

Any __qpu__ kernel returning a std::vector pulls in the intrinsic
(ConvertStmt.cpp loads it for span-like return types), so this is easy to hit.

Reproducer
// a.cpp
#include <cudaq.h>
__qpu__ std::vector<cudaq::measure_result> kern_a() {
  cudaq::qvector q(2);
  h(q[0]);
  return mz(q);
}

b.cpp is identical with kern_a renamed to kern_b.

$ nvq++ -c -fPIC a.cpp -o a.o
$ nvq++ -c -fPIC b.cpp -o b.o
$ nm -g a.o | grep vectorCopyCtor
00000000000000e0 T __nvqpp_vectorCopyCtor        # strong global, in both objects

$ nvq++ -shared a.o b.o -o libtwo.so
ld.lld: error: duplicate symbol: __nvqpp_vectorCopyCtor
>>> defined at a.cpp
>>>            a.o:(__nvqpp_vectorCopyCtor)
>>> defined at b.cpp
>>>            b.o:(.text+0xe0)
clang++: error: linker command failed with exit code 1

(GNU ld reports the same as multiple definition of '__nvqpp_vectorCopyCtor'.)

Linking either object alone succeeds. Note that the helper is also exported from the
resulting library's dynamic symbol table:

$ nvq++ -shared a.o -o libone.so && nm -D libone.so | grep vectorCopyCtor
000000000000f800 T __nvqpp_vectorCopyCtor

Before #5044 the helper was inlined and DCE'd, so no
object carried a definition and the link succeeded.

The emitted definition is dead code

In the object above, the duplicated definition is never referenced — the call site is
inlined anyway, and only the external linkage forces the body to be emitted:

$ nm a.o | grep vectorCopyCtor
00000000000000e0 T __nvqpp_vectorCopyCtor     # defined ...
$ objdump -r a.o | grep -c vectorCopyCtor
0                                             # ... but zero relocations reference it
$ objdump -dr a.o --disassemble='__nvqpp__mlirgen__function_kern_a._Z6kern_av' | grep -o malloc
malloc                                        # helper was inlined into the kernel

So on this path no_inline does not prevent LLVM from inlining the helper; it only
forces an unreferenced copy into every object, which then collides at link time.
(The attribute presumably still does useful work on the Quake/QIR device path that
#5044 targeted.)

Expected

Compiler-generated per-TU helpers should be mergeable or TU-local, and should not
land in a library's public ABI. nm should show W (weak/linkonce_odr) or t
(internal), not T.

Candidate fixes (all verified to fix the link)

Rebuilt main locally with each of these and re-ran the reproducer:

variant attribute on @__nvqpp_vectorCopyCtor link
A (current main) {no_inline} fails
B (attribute removed) passes
C {no_inline, llvm.linkage = #llvm.linkage<linkonce_odr>} passes
D {no_inline, llvm.linkage = #llvm.linkage<internal>} passes

C and D keep no_inline and so preserve whatever #5044 needs on the device path,
which makes them preferable to B. In all three the symbol disappears from the object
entirely (inlined, then the discardable definition dropped).

Worth applying at the mechanism level: other intrinsics in Intrinsics.cpp are
exposed to the same hazard the moment they stop being inlined, since func.func private there does not lower to internal LLVM linkage.

Notes

In-tree tests likely miss this because they link one translation unit at a time.
CUDA-QX is a downstream example: libcudaq-qec.so links four device objects and has
failed to build against CUDA-Q main since 2026-08-18.

Environment
  • CUDA-Q main, first bad commit a2f58465 (#5044, merged 2026-08-17)
  • Last known good: 383c736f
  • Reproducer verified on main @ 3d450c3e73, amd64; CI shows the same on arm64 and CUDA 12.6/13.0
Dominant language
C++
Stars
1.2k
Forks
508
Avg merge
1d 19h
Merged PRs (30d)
100

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NVIDIA/cuda-quantum

All issues in NVIDIA/cuda-quantum

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.