Skip to content

[NvlinkWrapper] Add support for --undefined #113934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions clang/test/Driver/nvlink-wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ int bar() {
}
#else
extern int y;
int __attribute__((visibility("hidden"))) x = 999;
extern int x;
int baz() { return y + x; }
#endif

// Create various inputs to test basic linking and LTO capabilities. Creating a
// CUDA binary requires access to the `ptxas` executable, so we just use x64.
// RUN: %clang -cc1 %s -triple nvptx64-nvidia-cuda -emit-llvm-bc -o %t.o
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -DX -o %t-x.o
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -DY -o %t-y.o
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -DZ -o %t-z.o
Expand All @@ -36,6 +37,7 @@ int baz() { return y + x; }
// RUN: llvm-ar rcs %t-y.a %t-y.o
// RUN: llvm-ar rcs %t-z.a %t-z.o
// RUN: llvm-ar rcs %t-w.a %t-w.o
// RUN: llvm-ar rcs %t-u.a %t-u.o

//
// Check that we forward any unrecognized argument to 'nvlink'.
Expand All @@ -49,11 +51,16 @@ int baz() { return y + x; }
// `libx.a` and `liby.a` because extern weak symbols do not extract and `libz.a`
// is not used at all.
//
// RUN: clang-nvlink-wrapper --dry-run %t-x.a %t-u.o %t-y.a %t-z.a %t-w.a \
// RUN: clang-nvlink-wrapper --dry-run %t-x.a %t-u.a %t-y.a %t-z.a %t-w.a %t.o \
// RUN: -arch sm_52 -o a.out 2>&1 | FileCheck %s --check-prefix=LINK
// LINK: nvlink{{.*}} -arch sm_52 -o a.out [[INPUT:.+]].cubin {{.*}}-x-{{.*}}.cubin{{.*}}-y-{{.*}}.cubin

// RUN: %clang -cc1 %s -triple nvptx64-nvidia-cuda -emit-llvm-bc -o %t.o
//
// Same as above but we use '--undefined' to forcibly extract 'libz.a'
//
// RUN: clang-nvlink-wrapper --dry-run %t-x.a %t-u.a %t-y.a %t-z.a %t-w.a %t.o \
// RUN: -u z -arch sm_52 -o a.out 2>&1 | FileCheck %s --check-prefix=LINK
// UNDEFINED: nvlink{{.*}} -arch sm_52 -o a.out [[INPUT:.+]].cubin {{.*}}-x-{{.*}}.cubin{{.*}}-y-{{.*}}.cubin{{.*}}-z-{{.*}}.cubin

//
// Check that the LTO interface works and properly preserves symbols used in a
Expand Down
3 changes: 3 additions & 0 deletions clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ struct Symbol {
};

Symbol() : File(), Flags(None), UsedInRegularObj(false) {}
Symbol(Symbol::Flags Flags) : File(), Flags(Flags), UsedInRegularObj(true) {}

Symbol(MemoryBufferRef File, const irsymtab::Reader::SymbolRef Sym)
: File(File), Flags(0), UsedInRegularObj(false) {
Expand Down Expand Up @@ -535,6 +536,8 @@ Expected<SmallVector<StringRef>> getInput(const ArgList &Args) {

bool Extracted = true;
StringMap<Symbol> SymTab;
for (auto &Sym : Args.getAllArgValues(OPT_u))
SymTab[Sym] = Symbol(Symbol::Undefined);
SmallVector<std::unique_ptr<MemoryBuffer>> LinkerInput;
while (Extracted) {
Extracted = false;
Expand Down
7 changes: 5 additions & 2 deletions clang/tools/clang-nvlink-wrapper/NVLinkOpts.td
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,21 @@ def plugin : JoinedOrSeparate<["--", "-"], "plugin">,
Flags<[HelpHidden, WrapperOnlyOption]>;

def arch : Separate<["--", "-"], "arch">,
HelpText<"Specify the 'sm_' name of the target architecture.">;
HelpText<"Specify the 'sm_' name of the target architecture">;
def : Joined<["--", "-"], "plugin-opt=mcpu=">,
Flags<[HelpHidden, WrapperOnlyOption]>, Alias<arch>;

def g : Flag<["-"], "g">, HelpText<"Specify that this was a debug compile.">;
def g : Flag<["-"], "g">, HelpText<"Specify that this was a debug compile">;
def debug : Flag<["--"], "debug">, Alias<g>;

def lto_emit_llvm : Flag<["--"], "lto-emit-llvm">, Flags<[WrapperOnlyOption]>,
HelpText<"Emit LLVM-IR bitcode">;
def lto_emit_asm : Flag<["--"], "lto-emit-asm">, Flags<[WrapperOnlyOption]>,
HelpText<"Emit assembly code">;

def u : JoinedOrSeparate<["-"], "u">, HelpText<"Force undefined symbol during linking">;
def undefined : JoinedOrSeparate<["--"], "undefined">, Alias<u>;

def O : Joined<["--", "-"], "plugin-opt=O">,
Flags<[WrapperOnlyOption]>, MetaVarName<"<O0, O1, O2, or O3>">,
HelpText<"Optimization level for LTO">;
Expand Down
Loading