[llvm-branch-commits] [llvm] 267a57a - [llvm-link] Fix for an assertion when linking global with appending linkage

2021-01-23 Thread Sergey Dmitriev via llvm-branch-commits

Author: Sergey Dmitriev
Date: 2021-01-23T00:10:42-08:00
New Revision: 267a57a64572cffbb74599878bdcc9f3b678ffa3

URL: 
https://github.com/llvm/llvm-project/commit/267a57a64572cffbb74599878bdcc9f3b678ffa3
DIFF: 
https://github.com/llvm/llvm-project/commit/267a57a64572cffbb74599878bdcc9f3b678ffa3.diff

LOG: [llvm-link] Fix for an assertion when linking global with appending linkage

This patch fixes llvm-link assertion when linking external variable
declaration with a definition with appending linkage.

Reviewed By: jdoerfert

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

Added: 
llvm/test/Linker/Inputs/appending-global.ll
llvm/test/Linker/appending-global-err1.ll
llvm/test/Linker/appending-global-err2.ll
llvm/test/Linker/appending-global-err3.ll
llvm/test/Linker/appending-global-err4.ll
llvm/test/Linker/appending-global-err5.ll
llvm/test/Linker/appending-global-proto.ll

Modified: 
llvm/lib/Linker/IRMover.cpp
llvm/lib/Linker/LinkModules.cpp

Removed: 




diff  --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp
index 5eeba0c0c3e7..6a2f84bb48a0 100644
--- a/llvm/lib/Linker/IRMover.cpp
+++ b/llvm/lib/Linker/IRMover.cpp
@@ -843,6 +843,38 @@ static void getArrayElements(const Constant *C,
 Expected
 IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,
 const GlobalVariable *SrcGV) {
+  // Check that both variables have compatible properties.
+  if (DstGV && !DstGV->isDeclaration() && !SrcGV->isDeclaration()) {
+if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
+  return stringErr(
+  "Linking globals named '" + SrcGV->getName() +
+  "': can only link appending global with another appending "
+  "global!");
+
+if (DstGV->isConstant() != SrcGV->isConstant())
+  return stringErr("Appending variables linked with 
diff erent const'ness!");
+
+if (DstGV->getAlignment() != SrcGV->getAlignment())
+  return stringErr(
+  "Appending variables with 
diff erent alignment need to be linked!");
+
+if (DstGV->getVisibility() != SrcGV->getVisibility())
+  return stringErr(
+  "Appending variables with 
diff erent visibility need to be linked!");
+
+if (DstGV->hasGlobalUnnamedAddr() != SrcGV->hasGlobalUnnamedAddr())
+  return stringErr(
+  "Appending variables with 
diff erent unnamed_addr need to be linked!");
+
+if (DstGV->getSection() != SrcGV->getSection())
+  return stringErr(
+  "Appending variables with 
diff erent section name need to be linked!");
+  }
+
+  // Do not need to do anything if source is a declaration.
+  if (SrcGV->isDeclaration())
+return DstGV;
+
   Type *EltTy = cast(TypeMap.get(SrcGV->getValueType()))
 ->getElementType();
 
@@ -868,37 +900,13 @@ IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,
   }
 
   uint64_t DstNumElements = 0;
-  if (DstGV) {
+  if (DstGV && !DstGV->isDeclaration()) {
 ArrayType *DstTy = cast(DstGV->getValueType());
 DstNumElements = DstTy->getNumElements();
 
-if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
-  return stringErr(
-  "Linking globals named '" + SrcGV->getName() +
-  "': can only link appending global with another appending "
-  "global!");
-
 // Check to see that they two arrays agree on type.
 if (EltTy != DstTy->getElementType())
   return stringErr("Appending variables with 
diff erent element types!");
-if (DstGV->isConstant() != SrcGV->isConstant())
-  return stringErr("Appending variables linked with 
diff erent const'ness!");
-
-if (DstGV->getAlignment() != SrcGV->getAlignment())
-  return stringErr(
-  "Appending variables with 
diff erent alignment need to be linked!");
-
-if (DstGV->getVisibility() != SrcGV->getVisibility())
-  return stringErr(
-  "Appending variables with 
diff erent visibility need to be linked!");
-
-if (DstGV->hasGlobalUnnamedAddr() != SrcGV->hasGlobalUnnamedAddr())
-  return stringErr(
-  "Appending variables with 
diff erent unnamed_addr need to be linked!");
-
-if (DstGV->getSection() != SrcGV->getSection())
-  return stringErr(
-  "Appending variables with 
diff erent section name need to be linked!");
   }
 
   SmallVector SrcElements;
@@ -928,9 +936,10 @@ IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,
 
   Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
 
-  Mapper.scheduleMapAppendingVariable(*NG,
-  DstGV ? DstGV->getInitializer() : 
nullptr,
-  IsOldStructor, SrcElements);
+  Mapper.scheduleMapAppendingVariable(
+  *NG,
+  (DstGV && !DstGV->isDeclaration()) ? DstGV->getInitializer() : nullptr,
+  IsOldStructor, SrcElements);
 
   // Replace 

[llvm-branch-commits] [llvm] 2331062 - [llvm-link] Improve link time for bitcode archives [NFC]

2021-01-19 Thread Sergey Dmitriev via llvm-branch-commits

Author: Sergey Dmitriev
Date: 2021-01-19T16:41:28-08:00
New Revision: 233106269db6af64f9eff7db0bdf119593f822b1

URL: 
https://github.com/llvm/llvm-project/commit/233106269db6af64f9eff7db0bdf119593f822b1
DIFF: 
https://github.com/llvm/llvm-project/commit/233106269db6af64f9eff7db0bdf119593f822b1.diff

LOG: [llvm-link] Improve link time for bitcode archives [NFC]

Linking large bitcode archives currently takes a lot of time with llvm-link,
this patch adds couple improvements which reduce link time for archives
- Use one Linker instance for archive instead of recreating it for each member
- Lazy load archive members

Reviewed By: tra, jdoerfert

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

Added: 


Modified: 
llvm/tools/llvm-link/llvm-link.cpp

Removed: 




diff  --git a/llvm/tools/llvm-link/llvm-link.cpp 
b/llvm/tools/llvm-link/llvm-link.cpp
index 158b168107f1..eed49c438335 100644
--- a/llvm/tools/llvm-link/llvm-link.cpp
+++ b/llvm/tools/llvm-link/llvm-link.cpp
@@ -153,6 +153,7 @@ static std::unique_ptr loadArFile(const char *Argv0,
   Error Err = Error::success();
   object::Archive Archive(*Buffer, Err);
   ExitOnErr(std::move(Err));
+  Linker L(*Result);
   for (const object::Archive::Child &C : Archive.children(Err)) {
 Expected Ename = C.getName();
 if (Error E = Ename.takeError()) {
@@ -186,7 +187,12 @@ static std::unique_ptr loadArFile(const char 
*Argv0,
   return nullptr;
 }
 
-std::unique_ptr M = parseIR(MemBuf.get(), ParseErr, Context);
+std::unique_ptr M;
+if (DisableLazyLoad)
+  M = parseIR(MemBuf.get(), ParseErr, Context);
+else
+  M = getLazyIRModule(MemoryBuffer::getMemBuffer(MemBuf.get(), false),
+  ParseErr, Context);
 
 if (!M.get()) {
   errs() << Argv0 << ": ";
@@ -197,7 +203,7 @@ static std::unique_ptr loadArFile(const char *Argv0,
 }
 if (Verbose)
   errs() << "Linking member '" << ChildName << "' of archive library.\n";
-if (Linker::linkModules(*Result, std::move(M)))
+if (L.linkInModule(std::move(M)))
   return nullptr;
   } // end for each child
   ExitOnErr(std::move(Err));



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


[llvm-branch-commits] [llvm] 761aca1 - [llvm-link] fix linker behavior when linking archives with --only-needed option

2021-01-05 Thread Sergey Dmitriev via llvm-branch-commits

Author: Sergey Dmitriev
Date: 2021-01-05T10:02:51-08:00
New Revision: 761aca1e2e393da62ddaf7c42b61196be2466571

URL: 
https://github.com/llvm/llvm-project/commit/761aca1e2e393da62ddaf7c42b61196be2466571
DIFF: 
https://github.com/llvm/llvm-project/commit/761aca1e2e393da62ddaf7c42b61196be2466571.diff

LOG: [llvm-link] fix linker behavior when linking archives with --only-needed 
option

This patch fixes linker behavior when archive is linked with other inputs
as a library (i.e. when --only-needed option is specified). In this case library
is expected to be normally linked first into a separate module and only after
that linker should import required symbols from the linked library module.

Reviewed By: tra

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

Added: 
llvm/test/tools/llvm-link/Inputs/i.ll
llvm/test/tools/llvm-link/archive-only-needed.ll

Modified: 
llvm/tools/llvm-link/llvm-link.cpp

Removed: 




diff  --git a/llvm/test/tools/llvm-link/Inputs/i.ll 
b/llvm/test/tools/llvm-link/Inputs/i.ll
new file mode 100644
index ..d23df3792a60
--- /dev/null
+++ b/llvm/test/tools/llvm-link/Inputs/i.ll
@@ -0,0 +1,8 @@
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+define void @i() {
+  call void @f()
+  ret void
+}
+
+declare void @f()

diff  --git a/llvm/test/tools/llvm-link/archive-only-needed.ll 
b/llvm/test/tools/llvm-link/archive-only-needed.ll
new file mode 100644
index ..d997e6c71b9c
--- /dev/null
+++ b/llvm/test/tools/llvm-link/archive-only-needed.ll
@@ -0,0 +1,15 @@
+; RUN: llvm-as %S/Inputs/f.ll -o %t.f.bc
+; RUN: llvm-as %S/Inputs/g.ll -o %t.g.bc
+; RUN: llvm-as %S/Inputs/i.ll -o %t.i.bc
+; RUN: rm -f %t.lib
+; RUN: llvm-ar cr %t.lib %t.f.bc %t.g.bc %t.i.bc
+; RUN: llvm-link %s %t.lib -o %t.linked.bc --only-needed
+; RUN: llvm-nm %t.linked.bc | FileCheck %s
+
+; CHECK:  T f
+; CHECK:  T i
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+@i = external global i8*
+@llvm.used = appending global [1 x i8*] [i8* bitcast (i8** @i to i8*)], 
section "llvm.metadata"

diff  --git a/llvm/tools/llvm-link/llvm-link.cpp 
b/llvm/tools/llvm-link/llvm-link.cpp
index 6996c5bfbe76..61d0c1561054 100644
--- a/llvm/tools/llvm-link/llvm-link.cpp
+++ b/llvm/tools/llvm-link/llvm-link.cpp
@@ -142,9 +142,9 @@ static std::unique_ptr loadFile(const char *argv0,
   return Result;
 }
 
-static std::unique_ptr
-loadArFile(const char *Argv0, std::unique_ptr Buffer,
-   LLVMContext &Context, unsigned OrigFlags, unsigned ApplicableFlags) 
{
+static std::unique_ptr loadArFile(const char *Argv0,
+  std::unique_ptr Buffer,
+  LLVMContext &Context) {
   std::unique_ptr Result(new Module("ArchiveModule", Context));
   StringRef ArchiveName = Buffer->getBufferIdentifier();
   if (Verbose)
@@ -197,9 +197,8 @@ loadArFile(const char *Argv0, std::unique_ptr 
Buffer,
 }
 if (Verbose)
   errs() << "Linking member '" << ChildName << "' of archive library.\n";
-if (Linker::linkModules(*Result, std::move(M), ApplicableFlags))
+if (Linker::linkModules(*Result, std::move(M)))
   return nullptr;
-ApplicableFlags = OrigFlags;
   } // end for each child
   ExitOnErr(std::move(Err));
   return Result;
@@ -354,8 +353,7 @@ static bool linkFiles(const char *argv0, LLVMContext 
&Context, Linker &L,
 
 std::unique_ptr M =
 identify_magic(Buffer->getBuffer()) == file_magic::archive
-? loadArFile(argv0, std::move(Buffer), Context, Flags,
- ApplicableFlags)
+? loadArFile(argv0, std::move(Buffer), Context)
 : loadFile(argv0, std::move(Buffer), Context);
 if (!M.get()) {
   errs() << argv0 << ": ";



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


[llvm-branch-commits] [llvm] 55f8c2f - [llvm-link] use file magic when deciding if input should be loaded as archive

2020-12-02 Thread Sergey Dmitriev via llvm-branch-commits

Author: Sergey Dmitriev
Date: 2020-12-02T16:29:41-08:00
New Revision: 55f8c2fdfbc5eda1be946e97ecffa2dea44a883e

URL: 
https://github.com/llvm/llvm-project/commit/55f8c2fdfbc5eda1be946e97ecffa2dea44a883e
DIFF: 
https://github.com/llvm/llvm-project/commit/55f8c2fdfbc5eda1be946e97ecffa2dea44a883e.diff

LOG: [llvm-link] use file magic when deciding if input should be loaded as 
archive

llvm-link should not rely on the '.a' file extension when deciding if input file
should be loaded as archive. Archives may have other extensions (f.e. .lib) or 
no
extensions at all. This patch changes llvm-link to use llvm::file_magic to check
if input file is an archive.

Reviewed By: RaviNarayanaswamy

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

Added: 


Modified: 
llvm/test/tools/llvm-link/archive-bad.ll
llvm/test/tools/llvm-link/archive.ll
llvm/tools/llvm-link/llvm-link.cpp

Removed: 




diff  --git a/llvm/test/tools/llvm-link/archive-bad.ll 
b/llvm/test/tools/llvm-link/archive-bad.ll
index 80ce6fc1fe0d..7a4bddb43c0a 100644
--- a/llvm/test/tools/llvm-link/archive-bad.ll
+++ b/llvm/test/tools/llvm-link/archive-bad.ll
@@ -1,7 +1,7 @@
-# RUN: cp %S/Inputs/f.ll %t.fg.a
+# RUN: echo -e '!\nwith invalid contents' > %t.fg.a
 # RUN: not llvm-link %S/Inputs/h.ll %t.fg.a -o %t.linked.bc 2>&1 | FileCheck %s
 
 # RUN: rm -f %t.fg.a
 # RUN: rm -f %t.linked.bc
 
-# CHECK: file too small to be an archive
+# CHECK: truncated or malformed archive

diff  --git a/llvm/test/tools/llvm-link/archive.ll 
b/llvm/test/tools/llvm-link/archive.ll
index 10ab83a3d5be..b027db0753d9 100644
--- a/llvm/test/tools/llvm-link/archive.ll
+++ b/llvm/test/tools/llvm-link/archive.ll
@@ -1,8 +1,8 @@
 # RUN: llvm-as %S/Inputs/f.ll -o %t.f.bc
 # RUN: llvm-as %S/Inputs/g.ll -o %t.g.bc
 # RUN: llvm-ar cr %t.fg.a %t.f.bc %t.g.bc
-# RUN: llvm-ar cr %t.empty.a
-# RUN: llvm-link %S/Inputs/h.ll %t.fg.a %t.empty.a -o %t.linked.bc
+# RUN: llvm-ar cr %t.empty.lib
+# RUN: llvm-link %S/Inputs/h.ll %t.fg.a %t.empty.lib -o %t.linked.bc
 
 # RUN: llvm-nm %t.linked.bc | FileCheck %s
 

diff  --git a/llvm/tools/llvm-link/llvm-link.cpp 
b/llvm/tools/llvm-link/llvm-link.cpp
index 7141bd1ca7a1..0f851a3e7c38 100644
--- a/llvm/tools/llvm-link/llvm-link.cpp
+++ b/llvm/tools/llvm-link/llvm-link.cpp
@@ -11,8 +11,8 @@
 //
 
//===--===//
 
-#include "llvm/Object/Archive.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/BinaryFormat/Magic.h"
 #include "llvm/Bitcode/BitcodeReader.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
 #include "llvm/IR/AutoUpgrade.h"
@@ -24,6 +24,7 @@
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
 #include "llvm/Linker/Linker.h"
+#include "llvm/Object/Archive.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/InitLLVM.h"
@@ -115,17 +116,18 @@ static ExitOnError ExitOnErr;
 // link path for the specified file to try to find it...
 //
 static std::unique_ptr loadFile(const char *argv0,
-const std::string &FN,
+std::unique_ptr Buffer,
 LLVMContext &Context,
 bool MaterializeMetadata = true) {
   SMDiagnostic Err;
   if (Verbose)
-errs() << "Loading '" << FN << "'\n";
+errs() << "Loading '" << Buffer->getBufferIdentifier() << "'\n";
   std::unique_ptr Result;
   if (DisableLazyLoad)
-Result = parseIRFile(FN, Err, Context);
+Result = parseIR(*Buffer, Err, Context);
   else
-Result = getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata);
+Result =
+getLazyIRModule(std::move(Buffer), Err, Context, !MaterializeMetadata);
 
   if (!Result) {
 Err.print(argv0, errs());
@@ -141,19 +143,17 @@ static std::unique_ptr loadFile(const char *argv0,
 }
 
 static std::unique_ptr loadArFile(const char *Argv0,
-  const std::string &ArchiveName,
+  std::unique_ptr Buffer,
   LLVMContext &Context, Linker &L,
   unsigned OrigFlags,
   unsigned ApplicableFlags) {
   std::unique_ptr Result(new Module("ArchiveModule", Context));
+  StringRef ArchiveName = Buffer->getBufferIdentifier();
   if (Verbose)
 errs() << "Reading library archive file '" << ArchiveName
<< "' to memory\n";
-  ErrorOr> Buf =
-MemoryBuffer::getFile(ArchiveName, -1, false);
-  ExitOnErr(errorCodeToError(Buf.getError()));
   Error Err = Error::success();
-  object::Archive Archive(Buf.get()->getMemBufferRef(), Err);
+  object::Archive Archive(*Buffer, Err);
   ExitOnErr(std::move(Err));
   for (const object::Archive::Child &C : Archive.children(Err)) {
 Exp

[llvm-branch-commits] [llvm] 9c955b7 - Revert "[llvm-link] use file magic when deciding if input should be loaded as archive"

2020-12-02 Thread Sergey Dmitriev via llvm-branch-commits

Author: Sergey Dmitriev
Date: 2020-12-02T16:53:57-08:00
New Revision: 9c955b79fb2b86c5acdd0665e5ea2866cbc309c2

URL: 
https://github.com/llvm/llvm-project/commit/9c955b79fb2b86c5acdd0665e5ea2866cbc309c2
DIFF: 
https://github.com/llvm/llvm-project/commit/9c955b79fb2b86c5acdd0665e5ea2866cbc309c2.diff

LOG: Revert "[llvm-link] use file magic when deciding if input should be loaded 
as archive"

This reverts commit 55f8c2fdfbc5eda1be946e97ecffa2dea44a883e.

Added: 


Modified: 
llvm/test/tools/llvm-link/archive-bad.ll
llvm/test/tools/llvm-link/archive.ll
llvm/tools/llvm-link/llvm-link.cpp

Removed: 




diff  --git a/llvm/test/tools/llvm-link/archive-bad.ll 
b/llvm/test/tools/llvm-link/archive-bad.ll
index 7a4bddb43c0a..80ce6fc1fe0d 100644
--- a/llvm/test/tools/llvm-link/archive-bad.ll
+++ b/llvm/test/tools/llvm-link/archive-bad.ll
@@ -1,7 +1,7 @@
-# RUN: echo -e '!\nwith invalid contents' > %t.fg.a
+# RUN: cp %S/Inputs/f.ll %t.fg.a
 # RUN: not llvm-link %S/Inputs/h.ll %t.fg.a -o %t.linked.bc 2>&1 | FileCheck %s
 
 # RUN: rm -f %t.fg.a
 # RUN: rm -f %t.linked.bc
 
-# CHECK: truncated or malformed archive
+# CHECK: file too small to be an archive

diff  --git a/llvm/test/tools/llvm-link/archive.ll 
b/llvm/test/tools/llvm-link/archive.ll
index b027db0753d9..10ab83a3d5be 100644
--- a/llvm/test/tools/llvm-link/archive.ll
+++ b/llvm/test/tools/llvm-link/archive.ll
@@ -1,8 +1,8 @@
 # RUN: llvm-as %S/Inputs/f.ll -o %t.f.bc
 # RUN: llvm-as %S/Inputs/g.ll -o %t.g.bc
 # RUN: llvm-ar cr %t.fg.a %t.f.bc %t.g.bc
-# RUN: llvm-ar cr %t.empty.lib
-# RUN: llvm-link %S/Inputs/h.ll %t.fg.a %t.empty.lib -o %t.linked.bc
+# RUN: llvm-ar cr %t.empty.a
+# RUN: llvm-link %S/Inputs/h.ll %t.fg.a %t.empty.a -o %t.linked.bc
 
 # RUN: llvm-nm %t.linked.bc | FileCheck %s
 

diff  --git a/llvm/tools/llvm-link/llvm-link.cpp 
b/llvm/tools/llvm-link/llvm-link.cpp
index 0f851a3e7c38..7141bd1ca7a1 100644
--- a/llvm/tools/llvm-link/llvm-link.cpp
+++ b/llvm/tools/llvm-link/llvm-link.cpp
@@ -11,8 +11,8 @@
 //
 
//===--===//
 
+#include "llvm/Object/Archive.h"
 #include "llvm/ADT/STLExtras.h"
-#include "llvm/BinaryFormat/Magic.h"
 #include "llvm/Bitcode/BitcodeReader.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
 #include "llvm/IR/AutoUpgrade.h"
@@ -24,7 +24,6 @@
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
 #include "llvm/Linker/Linker.h"
-#include "llvm/Object/Archive.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/InitLLVM.h"
@@ -116,18 +115,17 @@ static ExitOnError ExitOnErr;
 // link path for the specified file to try to find it...
 //
 static std::unique_ptr loadFile(const char *argv0,
-std::unique_ptr Buffer,
+const std::string &FN,
 LLVMContext &Context,
 bool MaterializeMetadata = true) {
   SMDiagnostic Err;
   if (Verbose)
-errs() << "Loading '" << Buffer->getBufferIdentifier() << "'\n";
+errs() << "Loading '" << FN << "'\n";
   std::unique_ptr Result;
   if (DisableLazyLoad)
-Result = parseIR(*Buffer, Err, Context);
+Result = parseIRFile(FN, Err, Context);
   else
-Result =
-getLazyIRModule(std::move(Buffer), Err, Context, !MaterializeMetadata);
+Result = getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata);
 
   if (!Result) {
 Err.print(argv0, errs());
@@ -143,17 +141,19 @@ static std::unique_ptr loadFile(const char *argv0,
 }
 
 static std::unique_ptr loadArFile(const char *Argv0,
-  std::unique_ptr Buffer,
+  const std::string &ArchiveName,
   LLVMContext &Context, Linker &L,
   unsigned OrigFlags,
   unsigned ApplicableFlags) {
   std::unique_ptr Result(new Module("ArchiveModule", Context));
-  StringRef ArchiveName = Buffer->getBufferIdentifier();
   if (Verbose)
 errs() << "Reading library archive file '" << ArchiveName
<< "' to memory\n";
+  ErrorOr> Buf =
+MemoryBuffer::getFile(ArchiveName, -1, false);
+  ExitOnErr(errorCodeToError(Buf.getError()));
   Error Err = Error::success();
-  object::Archive Archive(*Buffer, Err);
+  object::Archive Archive(Buf.get()->getMemBufferRef(), Err);
   ExitOnErr(std::move(Err));
   for (const object::Archive::Child &C : Archive.children(Err)) {
 Expected Ename = C.getName();
@@ -287,9 +287,7 @@ static bool importFunctions(const char *argv0, Module 
&DestModule) {
 
   auto ModuleLoader = [&DestModule](const char *argv0,
 const std::string &Identifier) {
-std::unique_ptr Buffer =
-ExitOnE

[llvm-branch-commits] [llvm] 715ba18 - [llvm-link] use file magic when deciding if input should be loaded as archive

2020-12-02 Thread Sergey Dmitriev via llvm-branch-commits

Author: Sergey Dmitriev
Date: 2020-12-02T17:21:34-08:00
New Revision: 715ba18d3e11bc3636a9d0f701d8e5a2fdf66852

URL: 
https://github.com/llvm/llvm-project/commit/715ba18d3e11bc3636a9d0f701d8e5a2fdf66852
DIFF: 
https://github.com/llvm/llvm-project/commit/715ba18d3e11bc3636a9d0f701d8e5a2fdf66852.diff

LOG: [llvm-link] use file magic when deciding if input should be loaded as 
archive

llvm-link should not rely on the '.a' file extension when deciding if input file
should be loaded as archive. Archives may have other extensions (f.e. .lib) or 
no
extensions at all. This patch changes llvm-link to use llvm::file_magic to check
if input file is an archive.

Reviewed By: RaviNarayanaswamy

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

Added: 


Modified: 
llvm/test/tools/llvm-link/archive-bad.ll
llvm/test/tools/llvm-link/archive.ll
llvm/tools/llvm-link/CMakeLists.txt
llvm/tools/llvm-link/llvm-link.cpp

Removed: 




diff  --git a/llvm/test/tools/llvm-link/archive-bad.ll 
b/llvm/test/tools/llvm-link/archive-bad.ll
index 80ce6fc1fe0d..7a4bddb43c0a 100644
--- a/llvm/test/tools/llvm-link/archive-bad.ll
+++ b/llvm/test/tools/llvm-link/archive-bad.ll
@@ -1,7 +1,7 @@
-# RUN: cp %S/Inputs/f.ll %t.fg.a
+# RUN: echo -e '!\nwith invalid contents' > %t.fg.a
 # RUN: not llvm-link %S/Inputs/h.ll %t.fg.a -o %t.linked.bc 2>&1 | FileCheck %s
 
 # RUN: rm -f %t.fg.a
 # RUN: rm -f %t.linked.bc
 
-# CHECK: file too small to be an archive
+# CHECK: truncated or malformed archive

diff  --git a/llvm/test/tools/llvm-link/archive.ll 
b/llvm/test/tools/llvm-link/archive.ll
index 10ab83a3d5be..b027db0753d9 100644
--- a/llvm/test/tools/llvm-link/archive.ll
+++ b/llvm/test/tools/llvm-link/archive.ll
@@ -1,8 +1,8 @@
 # RUN: llvm-as %S/Inputs/f.ll -o %t.f.bc
 # RUN: llvm-as %S/Inputs/g.ll -o %t.g.bc
 # RUN: llvm-ar cr %t.fg.a %t.f.bc %t.g.bc
-# RUN: llvm-ar cr %t.empty.a
-# RUN: llvm-link %S/Inputs/h.ll %t.fg.a %t.empty.a -o %t.linked.bc
+# RUN: llvm-ar cr %t.empty.lib
+# RUN: llvm-link %S/Inputs/h.ll %t.fg.a %t.empty.lib -o %t.linked.bc
 
 # RUN: llvm-nm %t.linked.bc | FileCheck %s
 

diff  --git a/llvm/tools/llvm-link/CMakeLists.txt 
b/llvm/tools/llvm-link/CMakeLists.txt
index 051489f94bc9..c0f286e8fd10 100644
--- a/llvm/tools/llvm-link/CMakeLists.txt
+++ b/llvm/tools/llvm-link/CMakeLists.txt
@@ -1,4 +1,5 @@
 set(LLVM_LINK_COMPONENTS
+  BinaryFormat
   BitReader
   BitWriter
   Core

diff  --git a/llvm/tools/llvm-link/llvm-link.cpp 
b/llvm/tools/llvm-link/llvm-link.cpp
index 7141bd1ca7a1..0f851a3e7c38 100644
--- a/llvm/tools/llvm-link/llvm-link.cpp
+++ b/llvm/tools/llvm-link/llvm-link.cpp
@@ -11,8 +11,8 @@
 //
 
//===--===//
 
-#include "llvm/Object/Archive.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/BinaryFormat/Magic.h"
 #include "llvm/Bitcode/BitcodeReader.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
 #include "llvm/IR/AutoUpgrade.h"
@@ -24,6 +24,7 @@
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
 #include "llvm/Linker/Linker.h"
+#include "llvm/Object/Archive.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/InitLLVM.h"
@@ -115,17 +116,18 @@ static ExitOnError ExitOnErr;
 // link path for the specified file to try to find it...
 //
 static std::unique_ptr loadFile(const char *argv0,
-const std::string &FN,
+std::unique_ptr Buffer,
 LLVMContext &Context,
 bool MaterializeMetadata = true) {
   SMDiagnostic Err;
   if (Verbose)
-errs() << "Loading '" << FN << "'\n";
+errs() << "Loading '" << Buffer->getBufferIdentifier() << "'\n";
   std::unique_ptr Result;
   if (DisableLazyLoad)
-Result = parseIRFile(FN, Err, Context);
+Result = parseIR(*Buffer, Err, Context);
   else
-Result = getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata);
+Result =
+getLazyIRModule(std::move(Buffer), Err, Context, !MaterializeMetadata);
 
   if (!Result) {
 Err.print(argv0, errs());
@@ -141,19 +143,17 @@ static std::unique_ptr loadFile(const char *argv0,
 }
 
 static std::unique_ptr loadArFile(const char *Argv0,
-  const std::string &ArchiveName,
+  std::unique_ptr Buffer,
   LLVMContext &Context, Linker &L,
   unsigned OrigFlags,
   unsigned ApplicableFlags) {
   std::unique_ptr Result(new Module("ArchiveModule", Context));
+  StringRef ArchiveName = Buffer->getBufferIdentifier();
   if (Verbose)
 errs() << "Reading library archive file '" << ArchiveName
<< "' to memory\n";
-  ErrorOr> Buf

[llvm-branch-commits] [llvm] 025d4fa - [llvm-link][NFC] Minor cleanup

2020-12-09 Thread Sergey Dmitriev via llvm-branch-commits

Author: Sergey Dmitriev
Date: 2020-12-09T23:16:13-08:00
New Revision: 025d4faadb680925ccf5fdc957a36edd2fbb07ce

URL: 
https://github.com/llvm/llvm-project/commit/025d4faadb680925ccf5fdc957a36edd2fbb07ce
DIFF: 
https://github.com/llvm/llvm-project/commit/025d4faadb680925ccf5fdc957a36edd2fbb07ce.diff

LOG: [llvm-link][NFC] Minor cleanup

llvm::Linker::linkModules() is a static member, so there is no need
to pass reference to llvm::Linker instance to loadArFile() function.

Reviewed By: tra

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

Added: 


Modified: 
llvm/tools/llvm-link/llvm-link.cpp

Removed: 




diff  --git a/llvm/tools/llvm-link/llvm-link.cpp 
b/llvm/tools/llvm-link/llvm-link.cpp
index 0f851a3e7c38..6996c5bfbe76 100644
--- a/llvm/tools/llvm-link/llvm-link.cpp
+++ b/llvm/tools/llvm-link/llvm-link.cpp
@@ -142,11 +142,9 @@ static std::unique_ptr loadFile(const char *argv0,
   return Result;
 }
 
-static std::unique_ptr loadArFile(const char *Argv0,
-  std::unique_ptr Buffer,
-  LLVMContext &Context, Linker &L,
-  unsigned OrigFlags,
-  unsigned ApplicableFlags) {
+static std::unique_ptr
+loadArFile(const char *Argv0, std::unique_ptr Buffer,
+   LLVMContext &Context, unsigned OrigFlags, unsigned ApplicableFlags) 
{
   std::unique_ptr Result(new Module("ArchiveModule", Context));
   StringRef ArchiveName = Buffer->getBufferIdentifier();
   if (Verbose)
@@ -163,7 +161,7 @@ static std::unique_ptr loadArFile(const char *Argv0,
   << " failed to read name of archive member"
   << ArchiveName << "'\n";
   return nullptr;
-};
+}
 std::string ChildName = Ename.get().str();
 if (Verbose)
   errs() << "Parsing member '" << ChildName
@@ -199,7 +197,7 @@ static std::unique_ptr loadArFile(const char *Argv0,
 }
 if (Verbose)
   errs() << "Linking member '" << ChildName << "' of archive library.\n";
-if (L.linkModules(*Result, std::move(M), ApplicableFlags))
+if (Linker::linkModules(*Result, std::move(M), ApplicableFlags))
   return nullptr;
 ApplicableFlags = OrigFlags;
   } // end for each child
@@ -356,7 +354,7 @@ static bool linkFiles(const char *argv0, LLVMContext 
&Context, Linker &L,
 
 std::unique_ptr M =
 identify_magic(Buffer->getBuffer()) == file_magic::archive
-? loadArFile(argv0, std::move(Buffer), Context, L, Flags,
+? loadArFile(argv0, std::move(Buffer), Context, Flags,
  ApplicableFlags)
 : loadFile(argv0, std::move(Buffer), Context);
 if (!M.get()) {



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