Author: Sergey Dmitriev
Date: 2021-05-18T08:44:41-07:00
New Revision: 8998a8aa97f81c758403615ec897ce79c1ccdcad

URL: 
https://github.com/llvm/llvm-project/commit/8998a8aa97f81c758403615ec897ce79c1ccdcad
DIFF: 
https://github.com/llvm/llvm-project/commit/8998a8aa97f81c758403615ec897ce79c1ccdcad.diff

LOG: [clang-offload-bundler] Add sections and set section flags using one 
llvm-objcopy invocation

llvm-objcopy has been changed to support adding a section and updating section 
flags
in one run (D90438), so we can now change clang-offload-bundler to run 
llvm-objcopy
tool only once when creating fat object.

Reviewed By: ABataev

Differential Revision: https://reviews.llvm.org/D102670

Added: 
    

Modified: 
    clang/test/Driver/clang-offload-bundler.c
    clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp

Removed: 
    


################################################################################
diff  --git a/clang/test/Driver/clang-offload-bundler.c 
b/clang/test/Driver/clang-offload-bundler.c
index 32fe6dad1f3f3..f77a240e79b51 100644
--- a/clang/test/Driver/clang-offload-bundler.c
+++ b/clang/test/Driver/clang-offload-bundler.c
@@ -274,8 +274,7 @@
 
 // RUN: clang-offload-bundler -type=o 
-targets=host-%itanium_abi_triple,openmp-powerpc64le-ibm-linux-gnu,openmp-x86_64-pc-linux-gnu
 -inputs=%t.o,%t.tgt1,%t.tgt2 -outputs=%t.bundle3.o -### 2>&1 \
 // RUN: | FileCheck %s -DHOST=%itanium_abi_triple -DINOBJ1=%t.o 
-DINOBJ2=%t.tgt1 -DINOBJ3=%t.tgt2 -DOUTOBJ=%t.bundle3.o --check-prefix 
CK-OBJ-CMD
-// CK-OBJ-CMD: llvm-objcopy{{(.exe)?}}" 
"--add-section=__CLANG_OFFLOAD_BUNDLE__host-[[HOST]]={{.*}}" 
"--add-section=__CLANG_OFFLOAD_BUNDLE__openmp-powerpc64le-ibm-linux-gnu=[[INOBJ2]]"
 "--add-section=__CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu=[[INOBJ3]]" 
"[[INOBJ1]]" "[[TEMPOBJ:.*]]"
-// CK-OBJ-CMD: llvm-objcopy{{(.exe)?}}" 
"--set-section-flags=__CLANG_OFFLOAD_BUNDLE__host-[[HOST]]=readonly,exclude" 
"--set-section-flags=__CLANG_OFFLOAD_BUNDLE__openmp-powerpc64le-ibm-linux-gnu=readonly,exclude"
 
"--set-section-flags=__CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu=readonly,exclude"
 "[[TEMPOBJ]]" "[[OUTOBJ]]"
+// CK-OBJ-CMD: llvm-objcopy{{(.exe)?}}" 
"--add-section=__CLANG_OFFLOAD_BUNDLE__host-[[HOST]]={{.*}}" 
"--set-section-flags=__CLANG_OFFLOAD_BUNDLE__host-[[HOST]]=readonly,exclude" 
"--add-section=__CLANG_OFFLOAD_BUNDLE__openmp-powerpc64le-ibm-linux-gnu=[[INOBJ2]]"
 
"--set-section-flags=__CLANG_OFFLOAD_BUNDLE__openmp-powerpc64le-ibm-linux-gnu=readonly,exclude"
 "--add-section=__CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu=[[INOBJ3]]" 
"--set-section-flags=__CLANG_OFFLOAD_BUNDLE__openmp-x86_64-pc-linux-gnu=readonly,exclude"
 "[[INOBJ1]]" "[[OUTOBJ]]"
 
 // RUN: clang-offload-bundler -type=o 
-targets=host-%itanium_abi_triple,openmp-powerpc64le-ibm-linux-gnu,openmp-x86_64-pc-linux-gnu
 -inputs=%t.o,%t.tgt1,%t.tgt2 -outputs=%t.bundle3.o
 // RUN: clang-offload-bundler -type=o -inputs=%t.bundle3.o -list | FileCheck 
-check-prefix=CKLST %s

diff  --git a/clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp 
b/clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp
index 0b56d19e616c1..74b7a4539aff5 100644
--- a/clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp
+++ b/clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp
@@ -578,11 +578,7 @@ class ObjectFileHandler final : public FileHandler {
     // We will use llvm-objcopy to add target objects sections to the output
     // fat object. These sections should have 'exclude' flag set which tells
     // link editor to remove them from linker inputs when linking executable or
-    // shared library. llvm-objcopy currently does not support adding new
-    // section and changing flags for the added section in one invocation, and
-    // because of that we have to run it two times. First run adds sections and
-    // the second changes flags.
-    // TODO: change it to one run once llvm-objcopy starts supporting that.
+    // shared library.
 
     // Find llvm-objcopy in order to create the bundle binary.
     ErrorOr<std::string> Objcopy = sys::findProgramByName(
@@ -600,14 +596,8 @@ class ObjectFileHandler final : public FileHandler {
     // Temporary files that need to be removed.
     TempFileHandlerRAII TempFiles;
 
-    // Create an intermediate temporary file to save object after the first
-    // llvm-objcopy run.
-    Expected<StringRef> IntermediateObjOrErr = TempFiles.Create(None);
-    if (!IntermediateObjOrErr)
-      return IntermediateObjOrErr.takeError();
-    StringRef IntermediateObj = *IntermediateObjOrErr;
-
-    // Compose llvm-objcopy command line for add target objects' sections.
+    // Compose llvm-objcopy command line for add target objects' sections with
+    // appropriate flags.
     BumpPtrAllocator Alloc;
     StringSaver SS{Alloc};
     SmallVector<StringRef, 8u> ObjcopyArgs{"llvm-objcopy"};
@@ -627,20 +617,11 @@ class ObjectFileHandler final : public FileHandler {
       ObjcopyArgs.push_back(SS.save(Twine("--add-section=") +
                                     OFFLOAD_BUNDLER_MAGIC_STR + TargetNames[I] 
+
                                     "=" + InputFile));
-    }
-    ObjcopyArgs.push_back(InputFileNames[HostInputIndex]);
-    ObjcopyArgs.push_back(IntermediateObj);
-
-    if (Error Err = executeObjcopy(*Objcopy, ObjcopyArgs))
-      return Err;
-
-    // And run llvm-objcopy for the second time to update section flags.
-    ObjcopyArgs.resize(1);
-    for (unsigned I = 0; I < NumberOfInputs; ++I)
       ObjcopyArgs.push_back(SS.save(Twine("--set-section-flags=") +
                                     OFFLOAD_BUNDLER_MAGIC_STR + TargetNames[I] 
+
                                     "=readonly,exclude"));
-    ObjcopyArgs.push_back(IntermediateObj);
+    }
+    ObjcopyArgs.push_back(InputFileNames[HostInputIndex]);
     ObjcopyArgs.push_back(OutputFileNames.front());
 
     if (Error Err = executeObjcopy(*Objcopy, ObjcopyArgs))


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to