jhuber6 created this revision.
jhuber6 added reviewers: jdoerfert, JonChesterfield.
Herald added subscribers: guansong, yaxunl.
jhuber6 requested review of this revision.
Herald added subscribers: cfe-commits, sstefan1.
Herald added a project: clang.

After basic support for embedding and handling CUDA files was added to
the new driver, we should be able to call CUDA functions from OpenMP
code. This patch makes the necessary changes to successfuly link in CUDA
programs that were compiled using the new driver. With this patch it
should be possible to compile device-only CUDA code (no kernels) and
call it from OpenMP as follows:

  $ clang++ cuda.cu -fopenmp-new-driver -offload-arch=sm_70 -c
  $ clang++ openmp.cpp cuda.o -fopenmp-new-driver -fopenmp 
-fopenmp-targets=nvptx64 -Xopenmp-target=nvptx64 -march=sm_70

Currently this requires using a host variant to suppress the generation
of a CPU-side fallback call.

Depends on D120272 <https://reviews.llvm.org/D120272>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120273

Files:
  clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===================================================================
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -163,7 +163,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<DeviceFile> {
   static DeviceFile getEmptyKey() {
     return {DenseMapInfo<StringRef>::getEmptyKey(),
@@ -178,13 +181,11 @@
             DenseMapInfo<StringRef>::getTombstoneKey()};
   }
   static unsigned getHashValue(const DeviceFile &I) {
-    return DenseMapInfo<StringRef>::getHashValue(I.OffloadKind) ^
-           DenseMapInfo<StringRef>::getHashValue(I.TheTriple) ^
+    return DenseMapInfo<StringRef>::getHashValue(I.TheTriple) ^
            DenseMapInfo<StringRef>::getHashValue(I.Arch);
   }
   static bool isEqual(const DeviceFile &LHS, const DeviceFile &RHS) {
-    return LHS.OffloadKind == RHS.OffloadKind &&
-           LHS.TheTriple == RHS.TheTriple && LHS.Arch == RHS.Arch;
+    return LHS.TheTriple == RHS.TheTriple && LHS.Arch == RHS.Arch;
   }
 };
 } // namespace llvm
@@ -196,10 +197,12 @@
                   SmallVectorImpl<DeviceFile> &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";
@@ -300,8 +303,8 @@
 
     if (Expected<StringRef> 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))
@@ -411,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))
@@ -908,7 +911,22 @@
       return createFileError(File, EC);
 
     file_magic Type = identify_magic((*BufferOrErr)->getBuffer());
-    if (Type != file_magic::bitcode) {
+    switch (Type) {
+    case file_magic::bitcode: {
+      Expected<std::unique_ptr<lto::InputFile>> 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<std::unique_ptr<ObjectFile>> ObjFile =
           ObjectFile::createObjectFile(**BufferOrErr, Type);
       if (!ObjFile)
@@ -926,15 +944,10 @@
         else
           UsedInSharedLib.insert(Saver.save(*Name));
       }
-    } else {
-      Expected<std::unique_ptr<lto::InputFile>> 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;
     }
   }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to