[llvm] r314374 - [ORC] Update the GlobalMappingLayer interface to fit the error-ized layer

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 27 19:17:35 PDT 2017


Author: lhames
Date: Wed Sep 27 19:17:35 2017
New Revision: 314374

URL: http://llvm.org/viewvc/llvm-project?rev=314374&view=rev
Log:
[ORC] Update the GlobalMappingLayer interface to fit the error-ized layer
concept.

Add a unit-test to make sure we don't backslide, and tweak the MockBaseLayer
utility to make it easier to test this kind of thing in the future.

Modified:
    llvm/trunk/include/llvm/ExecutionEngine/Orc/GlobalMappingLayer.h
    llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp
    llvm/trunk/unittests/ExecutionEngine/Orc/GlobalMappingLayerTest.cpp
    llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.h

Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/GlobalMappingLayer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/GlobalMappingLayer.h?rev=314374&r1=314373&r2=314374&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/GlobalMappingLayer.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/GlobalMappingLayer.h Wed Sep 27 19:17:35 2017
@@ -46,13 +46,14 @@ public:
 
   /// @brief Add the given module to the JIT.
   /// @return A handle for the added modules.
-  ModuleHandleT addModule(std::shared_ptr<Module> M,
-                          std::shared_ptr<JITSymbolResolver> Resolver) {
+  Expected<ModuleHandleT>
+  addModule(std::shared_ptr<Module> M,
+            std::shared_ptr<JITSymbolResolver> Resolver) {
     return BaseLayer.addModule(std::move(M), std::move(Resolver));
   }
 
   /// @brief Remove the module set associated with the handle H.
-  void removeModule(ModuleHandleT H) { BaseLayer.removeModule(H); }
+  Error removeModule(ModuleHandleT H) { return BaseLayer.removeModule(H); }
 
   /// @brief Manually set the address to return for the given symbol.
   void setGlobalMapping(const std::string &Name, JITTargetAddress Addr) {
@@ -96,8 +97,8 @@ public:
   /// @brief Immediately emit and finalize the module set represented by the
   ///        given handle.
   /// @param H Handle for module set to emit/finalize.
-  void emitAndFinalize(ModuleHandleT H) {
-    BaseLayer.emitAndFinalize(H);
+  Error emitAndFinalize(ModuleHandleT H) {
+    return BaseLayer.emitAndFinalize(H);
   }
 
 private:

Modified: llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp?rev=314374&r1=314373&r2=314374&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp (original)
+++ llvm/trunk/unittests/ExecutionEngine/Orc/CompileOnDemandLayerTest.cpp Wed Sep 27 19:17:35 2017
@@ -49,21 +49,18 @@ public:
 };
 
 TEST(CompileOnDemandLayerTest, FindSymbol) {
-  auto MockBaseLayer = createMockBaseLayer<int>(
-      DoNothingAndReturn<int>(0),
-      [](int Handle) { return Error::success(); },
-      [](const std::string &Name, bool) {
-        if (Name == "foo")
-          return JITSymbol(1, JITSymbolFlags::Exported);
-        return JITSymbol(nullptr);
-      },
-      ReturnNullJITSymbol());
+  MockBaseLayer<int, std::shared_ptr<Module>> TestBaseLayer;
+  TestBaseLayer.findSymbolImpl =
+    [](const std::string &Name, bool) {
+      if (Name == "foo")
+        return JITSymbol(1, JITSymbolFlags::Exported);
+      return JITSymbol(nullptr);
+    };
 
-  typedef decltype(MockBaseLayer) MockBaseLayerT;
   DummyCallbackManager CallbackMgr;
 
-  llvm::orc::CompileOnDemandLayer<MockBaseLayerT> COD(
-      MockBaseLayer, [](Function &F) { return std::set<Function *>{&F}; },
+  llvm::orc::CompileOnDemandLayer<decltype(TestBaseLayer)> COD(
+      TestBaseLayer, [](Function &F) { return std::set<Function *>{&F}; },
       CallbackMgr, [] { return llvm::make_unique<DummyStubsManager>(); }, true);
 
   auto Sym = COD.findSymbol("foo", true);

Modified: llvm/trunk/unittests/ExecutionEngine/Orc/GlobalMappingLayerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/Orc/GlobalMappingLayerTest.cpp?rev=314374&r1=314373&r2=314374&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/Orc/GlobalMappingLayerTest.cpp (original)
+++ llvm/trunk/unittests/ExecutionEngine/Orc/GlobalMappingLayerTest.cpp Wed Sep 27 19:17:35 2017
@@ -8,6 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ExecutionEngine/Orc/GlobalMappingLayer.h"
+#include "OrcTestCommon.h"
 #include "gtest/gtest.h"
 
 using namespace llvm;
@@ -15,21 +16,26 @@ using namespace llvm::orc;
 
 namespace {
 
-struct MockBaseLayer {
-
-  typedef int ModuleHandleT;
-
-  JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
-    if (Name == "bar")
-      return llvm::JITSymbol(0x4567, JITSymbolFlags::Exported);
-    return nullptr;
-  }
-
-};
-
 TEST(GlobalMappingLayerTest, Empty) {
-  MockBaseLayer M;
-  GlobalMappingLayer<MockBaseLayer> L(M);
+  MockBaseLayer<int, std::shared_ptr<Module>> TestBaseLayer;
+
+  TestBaseLayer.addModuleImpl =
+    [](std::shared_ptr<Module> M, std::shared_ptr<JITSymbolResolver> R) {
+      return 42;
+    };
+
+  TestBaseLayer.findSymbolImpl =
+    [](const std::string &Name, bool ExportedSymbolsOnly) -> JITSymbol {
+      if (Name == "bar")
+        return llvm::JITSymbol(0x4567, JITSymbolFlags::Exported);
+      return nullptr;
+    };
+
+  GlobalMappingLayer<decltype(TestBaseLayer)> L(TestBaseLayer);
+
+  // Test addModule interface.
+  int H = cantFail(L.addModule(nullptr, nullptr));
+  EXPECT_EQ(H, 42) << "Incorrect result from addModule";
 
   // Test fall-through for missing symbol.
   auto FooSym = L.findSymbol("foo", true);

Modified: llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.h?rev=314374&r1=314373&r2=314374&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.h (original)
+++ llvm/trunk/unittests/ExecutionEngine/Orc/OrcTestCommon.h Wed Sep 27 19:17:35 2017
@@ -105,73 +105,63 @@ public:
   }
 };
 
-template <typename HandleT,
-          typename AddModuleFtor,
-          typename RemoveModuleFtor,
-          typename FindSymbolFtor,
-          typename FindSymbolInFtor>
+template <typename HandleT, typename ModuleT>
 class MockBaseLayer {
 public:
 
-  typedef HandleT ModuleHandleT;
+  using ModuleHandleT = HandleT;
 
-  MockBaseLayer(AddModuleFtor &&AddModule,
-                RemoveModuleFtor &&RemoveModule,
-                FindSymbolFtor &&FindSymbol,
-                FindSymbolInFtor &&FindSymbolIn)
-      : AddModule(std::move(AddModule)),
-        RemoveModule(std::move(RemoveModule)),
-        FindSymbol(std::move(FindSymbol)),
-        FindSymbolIn(std::move(FindSymbolIn))
-  {}
-
-  template <typename ModuleT, typename MemoryManagerPtrT,
-            typename SymbolResolverPtrT>
-  Expected<ModuleHandleT> addModule(ModuleT Ms, MemoryManagerPtrT MemMgr,
-                                    SymbolResolverPtrT Resolver) {
-    return AddModule(std::move(Ms), std::move(MemMgr), std::move(Resolver));
+  using AddModuleSignature =
+    Expected<ModuleHandleT>(ModuleT M,
+                            std::shared_ptr<JITSymbolResolver> R);
+
+  using RemoveModuleSignature = Error(ModuleHandleT H);
+  using FindSymbolSignature = JITSymbol(const std::string &Name,
+                                        bool ExportedSymbolsOnly);
+  using FindSymbolInSignature = JITSymbol(ModuleHandleT H,
+                                          const std::string &Name,
+                                          bool ExportedSymbolsONly);
+  using EmitAndFinalizeSignature = Error(ModuleHandleT H);
+
+  std::function<AddModuleSignature> addModuleImpl;
+  std::function<RemoveModuleSignature> removeModuleImpl;
+  std::function<FindSymbolSignature> findSymbolImpl;
+  std::function<FindSymbolInSignature> findSymbolInImpl;
+  std::function<EmitAndFinalizeSignature> emitAndFinalizeImpl;
+
+  Expected<ModuleHandleT> addModule(ModuleT M,
+                                    std::shared_ptr<JITSymbolResolver> R) {
+    assert(addModuleImpl &&
+           "addModule called, but no mock implementation was provided");
+    return addModuleImpl(std::move(M), std::move(R));
   }
 
   Error removeModule(ModuleHandleT H) {
-    return RemoveModule(H);
+    assert(removeModuleImpl &&
+           "removeModule called, but no mock implementation was provided");
+    return removeModuleImpl(H);
   }
 
   JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
-    return FindSymbol(Name, ExportedSymbolsOnly);
+    assert(findSymbolImpl &&
+           "findSymbol called, but no mock implementation was provided");
+    return findSymbolImpl(Name, ExportedSymbolsOnly);
   }
 
   JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
                          bool ExportedSymbolsOnly) {
-    return FindSymbolIn(H, Name, ExportedSymbolsOnly);
+    assert(findSymbolInImpl &&
+           "findSymbolIn called, but no mock implementation was provided");
+    return findSymbolInImpl(H, Name, ExportedSymbolsOnly);
   }
 
-private:
-  AddModuleFtor AddModule;
-  RemoveModuleFtor RemoveModule;
-  FindSymbolFtor FindSymbol;
-  FindSymbolInFtor FindSymbolIn;
+  Error emitAndFinaliez(ModuleHandleT H) {
+    assert(emitAndFinalizeImpl &&
+           "emitAndFinalize called, but no mock implementation was provided");
+    return emitAndFinalizeImpl(H);
+  }
 };
 
-template <typename ModuleHandleT,
-          typename AddModuleFtor,
-          typename RemoveModuleFtor,
-          typename FindSymbolFtor,
-          typename FindSymbolInFtor>
-MockBaseLayer<ModuleHandleT, AddModuleFtor, RemoveModuleFtor,
-              FindSymbolFtor, FindSymbolInFtor>
-createMockBaseLayer(AddModuleFtor &&AddModule,
-                    RemoveModuleFtor &&RemoveModule,
-                    FindSymbolFtor &&FindSymbol,
-                    FindSymbolInFtor &&FindSymbolIn) {
-  return MockBaseLayer<ModuleHandleT, AddModuleFtor, RemoveModuleFtor,
-                       FindSymbolFtor, FindSymbolInFtor>(
-                         std::forward<AddModuleFtor>(AddModule),
-                         std::forward<RemoveModuleFtor>(RemoveModule),
-                         std::forward<FindSymbolFtor>(FindSymbol),
-                         std::forward<FindSymbolInFtor>(FindSymbolIn));
-}
-
-
 class ReturnNullJITSymbol {
 public:
   template <typename... Args>




More information about the llvm-commits mailing list