diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp --- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp +++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp @@ -162,7 +162,10 @@ }; namespace llvm { -/// Helper that allows DeviceFile to be used as a key in a DenseMap. +/// Helper that allows DeviceFile to be used as a key in a DenseMap. For now we +/// assume device files with matching architectures and triples but different +/// offloading kinds should be handlded together, this may not be true in the +/// future. template <> struct DenseMapInfo { static DeviceFile getEmptyKey() { return {DenseMapInfo::getEmptyKey(), @@ -193,10 +196,12 @@ SmallVectorImpl &DeviceFiles); static StringRef getDeviceFileExtension(StringRef DeviceTriple, - bool IsBitcode = false) { + file_magic magic) { Triple TheTriple(DeviceTriple); - if (TheTriple.isAMDGPU() || IsBitcode) + if (magic == file_magic::bitcode) return "bc"; + if (TheTriple.isNVPTX() && magic == file_magic::unknown) + return "s"; if (TheTriple.isNVPTX()) return "cubin"; return "o"; @@ -298,8 +303,8 @@ if (Expected Contents = Sec.getContents()) { SmallString<128> TempFile; - StringRef DeviceExtension = getDeviceFileExtension( - DeviceTriple, identify_magic(*Contents) == file_magic::bitcode); + StringRef DeviceExtension = + getDeviceFileExtension(DeviceTriple, identify_magic(*Contents)); if (Error Err = createOutputFile(Prefix + "-" + Kind + "-" + DeviceTriple + "-" + Arch, DeviceExtension, TempFile)) @@ -409,8 +414,8 @@ StringRef Contents = CDS->getAsString(); SmallString<128> TempFile; - StringRef DeviceExtension = getDeviceFileExtension( - DeviceTriple, identify_magic(Contents) == file_magic::bitcode); + StringRef DeviceExtension = + getDeviceFileExtension(DeviceTriple, identify_magic(Contents)); if (Error Err = createOutputFile(Prefix + "-" + Kind + "-" + DeviceTriple + "-" + Arch, DeviceExtension, TempFile)) @@ -630,7 +635,8 @@ // Add extracted input files. for (StringRef Input : InputFiles) - CmdArgs.push_back(Input); + if (Input.endswith(".cubin")) + CmdArgs.push_back(Input); if (sys::ExecuteAndWait(*NvlinkPath, CmdArgs)) return createStringError(inconvertibleErrorCode(), "'nvlink' failed"); @@ -906,7 +912,22 @@ return createFileError(File, EC); file_magic Type = identify_magic((*BufferOrErr)->getBuffer()); - if (Type != file_magic::bitcode) { + switch (Type) { + case file_magic::bitcode: { + Expected> InputFileOrErr = + llvm::lto::InputFile::create(**BufferOrErr); + if (!InputFileOrErr) + return InputFileOrErr.takeError(); + + // Save the input file and the buffer associated with its memory. + BitcodeFiles.push_back(std::move(*InputFileOrErr)); + SavedBuffers.push_back(std::move(*BufferOrErr)); + continue; + } + case file_magic::elf_relocatable: + case file_magic::elf_shared_object: + case file_magic::macho_object: + case file_magic::coff_object: { Expected> ObjFile = ObjectFile::createObjectFile(**BufferOrErr, Type); if (!ObjFile) @@ -924,15 +945,10 @@ else UsedInSharedLib.insert(Saver.save(*Name)); } - } else { - Expected> InputFileOrErr = - llvm::lto::InputFile::create(**BufferOrErr); - if (!InputFileOrErr) - return InputFileOrErr.takeError(); - - // Save the input file and the buffer associated with its memory. - BitcodeFiles.push_back(std::move(*InputFileOrErr)); - SavedBuffers.push_back(std::move(*BufferOrErr)); + continue; + } + default: + continue; } }