[ORC] Clone module flags metadata into the globals module in the
authorLang Hames <lhames@gmail.com>
Sun, 4 Sep 2016 17:53:30 +0000 (17:53 +0000)
committerLang Hames <lhames@gmail.com>
Sun, 4 Sep 2016 17:53:30 +0000 (17:53 +0000)
CompileOnDemandLayer.

Also contains a tweak to the orc-lazy jit in LLI to enable the test case.

llvm-svn: 280632

llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h
llvm/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
llvm/lib/ExecutionEngine/Orc/IndirectionUtils.cpp
llvm/test/ExecutionEngine/OrcLazy/module-flags.ll [new file with mode: 0644]
llvm/tools/lli/OrcLazyJIT.cpp

index ca3f7ee..c670ad6 100644 (file)
@@ -364,10 +364,11 @@ private:
       assert(!EC && "Error generating stubs");
     }
 
-    // If this module doesn't contain any globals or aliases we can bail out
-    // early and avoid the overhead of creating and managing an empty globals
-    // module.
-    if (SrcM.global_empty() && SrcM.alias_empty())
+    // If this module doesn't contain any globals, aliases, or module flags then
+    // we can bail out early and avoid the overhead of creating and managing an
+    // empty globals module.
+    if (SrcM.global_empty() && SrcM.alias_empty() &&
+        !SrcM.getModuleFlagsMetadata())
       return;
 
     // Create the GlobalValues module.
@@ -387,6 +388,9 @@ private:
       if (!VMap.count(&A))
         cloneGlobalAliasDecl(*GVsM, A, VMap);
 
+    // Clone the module flags.
+    cloneModuleFlagsMetadata(*GVsM, SrcM, VMap);
+
     // Now we need to clone the GV and alias initializers.
 
     // Initializers may refer to functions declared (but not defined) in this
index 4592e74..6b38a24 100644 (file)
@@ -415,6 +415,10 @@ void moveGlobalVariableInitializer(GlobalVariable &OrigGV,
 GlobalAlias *cloneGlobalAliasDecl(Module &Dst, const GlobalAlias &OrigA,
                                   ValueToValueMapTy &VMap);
 
+/// @brief Clone module flags metadata into the destination module.
+void cloneModuleFlagsMetadata(Module &Dst, const Module &Src,
+                              ValueToValueMapTy &VMap);
+
 } // End namespace orc.
 } // End namespace llvm.
 
index 7e2e873..711b887 100644 (file)
@@ -241,5 +241,14 @@ GlobalAlias* cloneGlobalAliasDecl(Module &Dst, const GlobalAlias &OrigA,
   return NewA;
 }
 
+void cloneModuleFlagsMetadata(Module &Dst, const Module &Src,
+                              ValueToValueMapTy &VMap) {
+  auto *MFs = Src.getModuleFlagsMetadata();
+  if (!MFs)
+    return;
+  for (auto *MF : MFs->operands())
+    Dst.addModuleFlag(MapMetadata(MF, VMap));
+}
+
 } // End namespace orc.
 } // End namespace llvm.
diff --git a/llvm/test/ExecutionEngine/OrcLazy/module-flags.ll b/llvm/test/ExecutionEngine/OrcLazy/module-flags.ll
new file mode 100644 (file)
index 0000000..c1240a8
--- /dev/null
@@ -0,0 +1,13 @@
+; RUN: lli -jit-kind=orc-lazy -orc-lazy-debug=mods-to-stdout %s | FileCheck %s
+;
+; CHECK: module-flags.ll.globals
+; CHECK-NOT: Module End
+; CHECK: The Answer is {{.*}}42
+
+define i32 @main() {
+  ret i32 0
+}
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 1, !"The Answer is ", i32 42}
index d9cec0c..de69a26 100644 (file)
@@ -18,7 +18,7 @@ using namespace llvm;
 
 namespace {
 
-  enum class DumpKind { NoDump, DumpFuncsToStdOut, DumpModsToStdErr,
+  enum class DumpKind { NoDump, DumpFuncsToStdOut, DumpModsToStdOut,
                         DumpModsToDisk };
 
   cl::opt<DumpKind> OrcDumpKind("orc-lazy-debug",
@@ -30,9 +30,9 @@ namespace {
                                   clEnumValN(DumpKind::DumpFuncsToStdOut,
                                              "funcs-to-stdout",
                                              "Dump function names to stdout."),
-                                  clEnumValN(DumpKind::DumpModsToStdErr,
-                                             "mods-to-stderr",
-                                             "Dump modules to stderr."),
+                                  clEnumValN(DumpKind::DumpModsToStdOut,
+                                             "mods-to-stdout",
+                                             "Dump modules to stdout."),
                                   clEnumValN(DumpKind::DumpModsToDisk,
                                              "mods-to-disk",
                                              "Dump modules to the current "
@@ -71,9 +71,9 @@ OrcLazyJIT::TransformFtor OrcLazyJIT::createDebugDumper() {
       return M;
     };
 
-  case DumpKind::DumpModsToStdErr:
+  case DumpKind::DumpModsToStdOut:
     return [](std::unique_ptr<Module> M) {
-             dbgs() << "----- Module Start -----\n" << *M
+             outs() << "----- Module Start -----\n" << *M
                     << "----- Module End -----\n";
 
              return M;