@@ -639,6 +639,9 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
639639 CmdArgs.push_back (
640640 Args.MakeArgString (" --plugin-opt=-mattr=" + llvm::join (Features, " ," )));
641641
642+ // Enable ctor / dtor lowering for the direct / freestanding NVPTX target.
643+ CmdArgs.append ({" -mllvm" , " --nvptx-lower-global-ctor-dtor" });
644+
642645 // Add paths for the default clang library path.
643646 SmallString<256 > DefaultLibPath =
644647 llvm::sys::path::parent_path (TC.getDriver ().Dir );
@@ -723,8 +726,9 @@ void NVPTX::getNVPTXTargetFeatures(const Driver &D, const llvm::Triple &Triple,
723726// / toolchain.
724727NVPTXToolChain::NVPTXToolChain (const Driver &D, const llvm::Triple &Triple,
725728 const llvm::Triple &HostTriple,
726- const ArgList &Args)
727- : ToolChain(D, Triple, Args), CudaInstallation(D, HostTriple, Args) {
729+ const ArgList &Args, bool Freestanding = false )
730+ : ToolChain(D, Triple, Args), CudaInstallation(D, HostTriple, Args),
731+ Freestanding(Freestanding) {
728732 if (CudaInstallation.isValid ())
729733 getProgramPaths ().push_back (std::string (CudaInstallation.getBinPath ()));
730734 // Lookup binaries into the driver directory, this is used to
@@ -736,7 +740,8 @@ NVPTXToolChain::NVPTXToolChain(const Driver &D, const llvm::Triple &Triple,
736740// / system's default triple if not provided.
737741NVPTXToolChain::NVPTXToolChain (const Driver &D, const llvm::Triple &Triple,
738742 const ArgList &Args)
739- : NVPTXToolChain(D, Triple, llvm::Triple(LLVM_HOST_TRIPLE), Args) {}
743+ : NVPTXToolChain(D, Triple, llvm::Triple(LLVM_HOST_TRIPLE), Args,
744+ /* Freestanding=*/ true) {}
740745
741746llvm::opt::DerivedArgList *
742747NVPTXToolChain::TranslateArgs (const llvm::opt::DerivedArgList &Args,
@@ -777,7 +782,13 @@ NVPTXToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
777782
778783void NVPTXToolChain::addClangTargetOptions (
779784 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
780- Action::OffloadKind DeviceOffloadingKind) const {}
785+ Action::OffloadKind DeviceOffloadingKind) const {
786+ // If we are compiling with a standalone NVPTX toolchain we want to try to
787+ // mimic a standard environment as much as possible. So we enable lowering
788+ // ctor / dtor functions to global symbols that can be registered.
789+ if (Freestanding && !getDriver ().isUsingLTO ())
790+ CC1Args.append ({" -mllvm" , " --nvptx-lower-global-ctor-dtor" });
791+ }
781792
782793bool NVPTXToolChain::supportsDebugInfoOption (const llvm::opt::Arg *A) const {
783794 const Option &O = A->getOption ();
0 commit comments