[lld] r225767 - Remove InputGraph::registerObserver.

Rui Ueyama ruiu at google.com
Mon Jan 12 21:24:54 PST 2015


Author: ruiu
Date: Mon Jan 12 23:24:53 2015
New Revision: 225767

URL: http://llvm.org/viewvc/llvm-project?rev=225767&view=rev
Log:
Remove InputGraph::registerObserver.

PECOFF was the only user of the API, and the reason why we created
the API is because, although the driver creates a list of input files,
it has no knowledge on what files are being created. It was because
everything was hidden behind the InputGraph abstraction.

Now the driver knows what that's doing. We no longer need this
indirection to get the file list being processed.

Modified:
    lld/trunk/include/lld/Core/InputGraph.h
    lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
    lld/trunk/lib/Core/InputGraph.cpp
    lld/trunk/lib/Driver/WinLinkDriver.cpp
    lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h
    lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp
    lld/trunk/unittests/DriverTests/InputGraphTest.cpp

Modified: lld/trunk/include/lld/Core/InputGraph.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/InputGraph.h?rev=225767&r1=225766&r2=225767&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/InputGraph.h (original)
+++ lld/trunk/include/lld/Core/InputGraph.h Mon Jan 12 23:24:53 2015
@@ -57,11 +57,6 @@ public:
   /// nullptr is returned.
   File *getNextFile();
 
-  /// Adds an observer of getNextFile(). Each time a new file is about to be
-  /// returned from getNextFile(), registered observers are called with the file
-  /// being returned.
-  void registerObserver(std::function<void(File *)>);
-
   /// \brief Adds a node into the InputGraph
   void addInputElement(std::unique_ptr<InputElement>);
 
@@ -89,7 +84,6 @@ protected:
   // Index of the next element to be processed
   uint32_t _nextElementIndex;
   InputElement *_currentInputElement;
-  std::vector<std::function<void(File *)>> _observers;
 
 private:
   InputElement *getNextInputElement();

Modified: lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h?rev=225767&r1=225766&r2=225767&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/PECOFFLinkingContext.h Mon Jan 12 23:24:53 2015
@@ -30,6 +30,28 @@ static const uint8_t DEFAULT_DOS_STUB[12
 
 namespace lld {
 
+namespace pecoff {
+class ResolvableSymbols {
+public:
+  void add(File *file);
+
+  const std::set<std::string> &defined() {
+    readAllSymbols();
+    return _defined;
+  }
+
+private:
+  // Files are read lazily, so that it has no runtime overhead if
+  // no one accesses this class.
+  void readAllSymbols();
+
+  std::set<std::string> _defined;
+  std::set<File *> _seen;
+  std::set<File *> _queue;
+  std::mutex _mutex;
+};
+} // end namespace pecoff
+
 class PECOFFLinkingContext : public LinkingContext {
 public:
   PECOFFLinkingContext()
@@ -328,6 +350,10 @@ public:
 
   std::recursive_mutex &getMutex() { return _mutex; }
 
+  pecoff::ResolvableSymbols *getResolvableSymsFile() {
+    return &_resolvableSyms;
+  }
+
 protected:
   /// Method to create a internal file for the entry symbol
   std::unique_ptr<File> createEntrySymbolFile() const override;
@@ -442,6 +468,8 @@ private:
   // Name of the temporary file for lib.exe subcommand. For debugging
   // only.
   std::string _moduleDefinitionFile;
+
+  pecoff::ResolvableSymbols _resolvableSyms;
 };
 
 } // end namespace lld

Modified: lld/trunk/lib/Core/InputGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Core/InputGraph.cpp?rev=225767&r1=225766&r2=225767&view=diff
==============================================================================
--- lld/trunk/lib/Core/InputGraph.cpp (original)
+++ lld/trunk/lib/Core/InputGraph.cpp Mon Jan 12 23:24:53 2015
@@ -20,14 +20,9 @@ File *InputGraph::getNextFile() {
   // element points to an archive file, and there's a file left in the archive,
   // it will succeed. If not, try to get the next file in the input graph.
   for (;;) {
-    if (_currentInputElement) {
-      File *next = _currentInputElement->getNextFile();
-      if (next) {
-        for (const std::function<void(File *)> &observer : _observers)
-          observer(next);
+    if (_currentInputElement)
+      if (File *next = _currentInputElement->getNextFile())
         return next;
-      }
-    }
 
     InputElement *elt = getNextInputElement();
     if (!elt)
@@ -36,10 +31,6 @@ File *InputGraph::getNextFile() {
   }
 }
 
-void InputGraph::registerObserver(std::function<void(File *)> fn) {
-  _observers.push_back(fn);
-}
-
 void InputGraph::addInputElement(std::unique_ptr<InputElement> ie) {
   _inputArgs.push_back(std::move(ie));
 }

Modified: lld/trunk/lib/Driver/WinLinkDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/WinLinkDriver.cpp?rev=225767&r1=225766&r2=225767&view=diff
==============================================================================
--- lld/trunk/lib/Driver/WinLinkDriver.cpp (original)
+++ lld/trunk/lib/Driver/WinLinkDriver.cpp Mon Jan 12 23:24:53 2015
@@ -842,6 +842,7 @@ void addFiles(PECOFFLinkingContext &ctx,
   for (std::unique_ptr<File> &file : loadFile(ctx, path, false)) {
     if (ctx.logInputFiles())
       diag << file->path() << "\n";
+    ctx.getResolvableSymsFile()->add(file.get());
     files.push_back(std::move(file));
   }
 }

Modified: lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h?rev=225767&r1=225766&r2=225767&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h Mon Jan 12 23:24:53 2015
@@ -174,45 +174,6 @@ private:
   mutable llvm::BumpPtrAllocator _alloc;
 };
 
-class ResolvableSymbols {
-public:
-  void add(File *file) {
-    std::lock_guard<std::mutex> lock(_mutex);
-    if (_seen.count(file) > 0)
-      return;
-    _seen.insert(file);
-    _queue.insert(file);
-  }
-
-  const std::set<std::string> &defined() {
-    readAllSymbols();
-    return _defined;
-  }
-
-private:
-  // Files are read lazily, so that it has no runtime overhead if
-  // no one accesses this class.
-  void readAllSymbols() {
-    std::lock_guard<std::mutex> lock(_mutex);
-    for (File *file : _queue) {
-      if (auto *archive = dyn_cast<ArchiveLibraryFile>(file)) {
-        for (const std::string &sym : archive->getDefinedSymbols())
-          _defined.insert(sym);
-        continue;
-      }
-      for (const DefinedAtom *atom : file->defined())
-        if (!atom->name().empty())
-          _defined.insert(atom->name());
-    }
-    _queue.clear();
-  }
-
-  std::set<std::string> _defined;
-  std::set<File *> _seen;
-  std::set<File *> _queue;
-  std::mutex _mutex;
-};
-
 // A ExportedSymbolRenameFile is a virtual archive file for dllexported symbols.
 //
 // One usually has to specify the exact symbol name to resolve it. That's true
@@ -246,7 +207,7 @@ private:
 class ExportedSymbolRenameFile : public impl::VirtualArchiveLibraryFile {
 public:
   ExportedSymbolRenameFile(const PECOFFLinkingContext &ctx,
-                           std::shared_ptr<ResolvableSymbols> syms)
+                           ResolvableSymbols *syms)
       : VirtualArchiveLibraryFile("<export>"), _syms(syms),
         _ctx(const_cast<PECOFFLinkingContext *>(&ctx)) {
     for (PECOFFLinkingContext::ExportDesc &desc : _ctx->getDllExports())
@@ -258,7 +219,7 @@ public:
     if (_exportedSyms.count(sym) == 0)
       return nullptr;
     std::string replace;
-    if (!findDecoratedSymbol(_ctx, _syms.get(), sym.str(), replace))
+    if (!findDecoratedSymbol(_ctx, _syms, sym.str(), replace))
       return nullptr;
 
     for (ExportDesc &exp : _ctx->getDllExports())
@@ -271,7 +232,7 @@ public:
 
 private:
   std::set<std::string> _exportedSyms;
-  std::shared_ptr<ResolvableSymbols> _syms;
+  ResolvableSymbols *_syms;
   mutable llvm::BumpPtrAllocator _alloc;
   mutable PECOFFLinkingContext *_ctx;
 };
@@ -284,7 +245,7 @@ private:
 class EntryPointFile : public SimpleFile {
 public:
   EntryPointFile(const PECOFFLinkingContext &ctx,
-                 std::shared_ptr<ResolvableSymbols> syms)
+                 ResolvableSymbols *syms)
       : SimpleFile("<entry>"), _ctx(const_cast<PECOFFLinkingContext *>(&ctx)),
         _syms(syms), _firstTime(true) {}
 
@@ -316,7 +277,7 @@ private:
     StringRef opt = _ctx->getEntrySymbolName();
     if (!opt.empty()) {
       std::string mangled;
-      if (findDecoratedSymbol(_ctx, _syms.get(), opt, mangled))
+      if (findDecoratedSymbol(_ctx, _syms, opt, mangled))
         return mangled;
       return _ctx->decorateSymbol(opt);
     }
@@ -341,7 +302,7 @@ private:
       if (_syms->defined().count(sym))
         return true;
       std::string ignore;
-      return findDecoratedSymbol(_ctx, _syms.get(), sym, ignore);
+      return findDecoratedSymbol(_ctx, _syms, sym, ignore);
     };
 
     switch (_ctx->getSubsystem()) {
@@ -372,7 +333,7 @@ private:
   PECOFFLinkingContext *_ctx;
   atom_collection_vector<UndefinedAtom> _undefinedAtoms;
   std::mutex _mutex;
-  std::shared_ptr<ResolvableSymbols> _syms;
+  ResolvableSymbols *_syms;
   llvm::BumpPtrAllocator _alloc;
   bool _firstTime;
 };

Modified: lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp?rev=225767&r1=225766&r2=225767&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/PECOFFLinkingContext.cpp Mon Jan 12 23:24:53 2015
@@ -118,9 +118,7 @@ bool PECOFFLinkingContext::createImplici
       llvm::make_unique<pecoff::LocallyImportedSymbolFile>(*this));
   getInputGraph().addInputElement(std::move(impFileNode));
 
-  std::shared_ptr<pecoff::ResolvableSymbols> syms(
-      new pecoff::ResolvableSymbols());
-  getInputGraph().registerObserver([=](File *file) { syms->add(file); });
+  pecoff::ResolvableSymbols* syms = getResolvableSymsFile();
 
   // Create a file for dllexported symbols.
   auto exportNode = llvm::make_unique<SimpleFileNode>("<export>");
@@ -331,4 +329,27 @@ void PECOFFLinkingContext::addPasses(Pas
   pm.add(std::unique_ptr<Pass>(new pecoff::InferSubsystemPass(*this)));
 }
 
+void pecoff::ResolvableSymbols::add(File *file) {
+  std::lock_guard<std::mutex> lock(_mutex);
+  if (_seen.count(file) > 0)
+    return;
+  _seen.insert(file);
+  _queue.insert(file);
+}
+
+void pecoff::ResolvableSymbols::readAllSymbols() {
+  std::lock_guard<std::mutex> lock(_mutex);
+  for (File *file : _queue) {
+    if (auto *archive = dyn_cast<ArchiveLibraryFile>(file)) {
+      for (const std::string &sym : archive->getDefinedSymbols())
+	_defined.insert(sym);
+      continue;
+    }
+    for (const DefinedAtom *atom : file->defined())
+      if (!atom->name().empty())
+	_defined.insert(atom->name());
+  }
+  _queue.clear();
+}
+
 } // end namespace lld

Modified: lld/trunk/unittests/DriverTests/InputGraphTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/DriverTests/InputGraphTest.cpp?rev=225767&r1=225766&r2=225767&view=diff
==============================================================================
--- lld/trunk/unittests/DriverTests/InputGraphTest.cpp (original)
+++ lld/trunk/unittests/DriverTests/InputGraphTest.cpp Mon Jan 12 23:24:53 2015
@@ -105,18 +105,3 @@ TEST_F(InputGraphTest, Normalize) {
   EXPECT_EQ("file3", getNext());
   expectEnd();
 }
-
-TEST_F(InputGraphTest, Observer) {
-  std::vector<std::string> files;
-  _graph->registerObserver([&](File *file) { files.push_back(file->path()); });
-
-  _graph->addInputElement(createFile("file1"));
-  _graph->addInputElement(createFile("file2"));
-  EXPECT_EQ("file1", getNext());
-  EXPECT_EQ("file2", getNext());
-  expectEnd();
-
-  EXPECT_EQ(2U, files.size());
-  EXPECT_EQ("file1", files[0]);
-  EXPECT_EQ("file2", files[1]);
-}





More information about the llvm-commits mailing list