OSDN Git Service

[ORC] Verify modules when running LLLazyJIT in LLI, and deal with fallout.
authorLang Hames <lhames@gmail.com>
Mon, 2 Jul 2018 22:30:18 +0000 (22:30 +0000)
committerLang Hames <lhames@gmail.com>
Mon, 2 Jul 2018 22:30:18 +0000 (22:30 +0000)
The verifier identified several modules that were broken due to incorrect
linkage on declarations. To fix this, CompileOnDemandLayer2::extractFunction
has been updated to change decls to external linkage.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@336150 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ExecutionEngine/Orc/CompileOnDemandLayer.cpp
tools/lli/lli.cpp

index 27a4ad6..f7ff40c 100644 (file)
@@ -322,11 +322,15 @@ std::unique_ptr<Module> CompileOnDemandLayer2::extractFunctions(
   ValueToValueMapTy VMap;
 
   auto Materializer = createLambdaValueMaterializer([&](Value *V) -> Value * {
+    GlobalValue *NewGV = nullptr;
     if (auto *F = dyn_cast<Function>(V))
-      return cloneFunctionDecl(*ExtractedFunctionsModule, *F);
+      NewGV = cloneFunctionDecl(*ExtractedFunctionsModule, *F);
     else if (auto *GV = dyn_cast<GlobalVariable>(V))
-      return cloneGlobalVariableDecl(*ExtractedFunctionsModule, *GV);
-    return nullptr;
+      NewGV = cloneGlobalVariableDecl(*ExtractedFunctionsModule, *GV);
+
+    if (NewGV)
+      NewGV->setLinkage(GlobalValue::ExternalLinkage);
+    return NewGV;
   });
 
   std::vector<std::pair<Function *, Function *>> OrigToNew;
index b7c821b..74ab2f3 100644 (file)
@@ -35,6 +35,7 @@
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Type.h"
 #include "llvm/IR/TypeBuilder.h"
+#include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
 #include "llvm/Object/Archive.h"
 #include "llvm/Object/ObjectFile.h"
@@ -767,7 +768,16 @@ int runOrcLazyJIT(LLVMContext &Ctx, std::vector<std::unique_ptr<Module>> Ms,
   auto J =
       ExitOnErr(orc::LLLazyJIT::Create(std::move(ES), std::move(TM), DL, Ctx));
 
-  J->setLazyCompileTransform(createDebugDumper());
+  auto Dump = createDebugDumper();
+
+  J->setLazyCompileTransform(
+    [&](std::unique_ptr<Module> M) {
+      if (verifyModule(*M, &dbgs())) {
+        dbgs() << "Bad module: " << *M << "\n";
+        exit(1);
+      }
+      return Dump(std::move(M));
+    });
   J->getMainVSO().setFallbackDefinitionGenerator(
       orc::DynamicLibraryFallbackGenerator(
           std::move(LibLLI), DL, [](orc::SymbolStringPtr) { return true; }));
@@ -776,8 +786,10 @@ int runOrcLazyJIT(LLVMContext &Ctx, std::vector<std::unique_ptr<Module>> Ms,
   orc::LocalCXXRuntimeOverrides2 CXXRuntimeOverrides;
   ExitOnErr(CXXRuntimeOverrides.enable(J->getMainVSO(), Mangle));
 
-  for (auto &M : Ms)
+  for (auto &M : Ms) {
+    orc::makeAllSymbolsExternallyAccessible(*M);
     ExitOnErr(J->addLazyIRModule(std::move(M)));
+  }
 
   ExitOnErr(J->runConstructors());