[Orc] Teach the CompileOnDemand layer to clone aliases.
authorLang Hames <lhames@gmail.com>
Tue, 6 Oct 2015 22:55:05 +0000 (22:55 +0000)
committerLang Hames <lhames@gmail.com>
Tue, 6 Oct 2015 22:55:05 +0000 (22:55 +0000)
This allows modules containing aliases to be lazily jit'd. Previously these
failed with missing symbol errors because the aliases weren't cloned from the
original module.

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

include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h
include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
lib/ExecutionEngine/Orc/IndirectionUtils.cpp
test/ExecutionEngine/OrcLazy/global_aliases.ll [new file with mode: 0644]

index 9694b80d19289a9691b7d92e7562e3e64235f78c..714ca2374dc359a3343307dacb8edf2e9994f4e0 100644 (file)
@@ -217,6 +217,10 @@ private:
       if (!GV.isDeclaration())
         cloneGlobalVariableDecl(*GVsAndStubsM, GV, &VMap);
 
       if (!GV.isDeclaration())
         cloneGlobalVariableDecl(*GVsAndStubsM, GV, &VMap);
 
+    // And the aliases.
+    for (auto &Alias : SrcM->aliases())
+      cloneGlobalAlias(*GVsAndStubsM, Alias, VMap, &GDMat);
+
     // Then clone the initializers.
     for (auto &GV : SrcM->globals())
       if (!GV.isDeclaration())
     // Then clone the initializers.
     for (auto &GV : SrcM->globals())
       if (!GV.isDeclaration())
index 4b7fc5e84b9c62950bf7df9efedbf3825dbe3929..e5cd0d2559c6c6268caa470f5f83b20b15288ac7 100644 (file)
@@ -289,6 +289,10 @@ void moveGlobalVariableInitializer(GlobalVariable &OrigGV,
                                    ValueMaterializer *Materializer = nullptr,
                                    GlobalVariable *NewGV = nullptr);
 
                                    ValueMaterializer *Materializer = nullptr,
                                    GlobalVariable *NewGV = nullptr);
 
+GlobalAlias* cloneGlobalAlias(Module &Dst, const GlobalAlias &OrigA,
+                              ValueToValueMapTy &VMap,
+                              ValueMaterializer *Materializer = nullptr);
+
 } // End namespace orc.
 } // End namespace llvm.
 
 } // End namespace orc.
 } // End namespace llvm.
 
index b439810ed330ee43f9e12609d422145398fcb3b4..ddd1921657b3324fe5f5d7a03b9b764150b3dfe0 100644 (file)
@@ -177,5 +177,19 @@ void moveGlobalVariableInitializer(GlobalVariable &OrigGV,
                                  nullptr, Materializer));
 }
 
                                  nullptr, Materializer));
 }
 
+GlobalAlias* cloneGlobalAlias(Module &Dst, const GlobalAlias &OrigA,
+                              ValueToValueMapTy &VMap,
+                              ValueMaterializer *Materializer) {
+  assert(OrigA.getAliasee() && "Original alias doesn't have an aliasee?");
+  auto *NewA = GlobalAlias::create(OrigA.getValueType(),
+                                   OrigA.getType()->getPointerAddressSpace(),
+                                   OrigA.getLinkage(), OrigA.getName(), &Dst);
+  NewA->copyAttributesFrom(&OrigA);
+  VMap[&OrigA] = NewA;
+  NewA->setAliasee(cast<Constant>(MapValue(OrigA.getAliasee(), VMap, RF_None,
+                                           nullptr, Materializer)));
+  return NewA;
+}
+
 } // End namespace orc.
 } // End namespace llvm.
 } // End namespace orc.
 } // End namespace llvm.
diff --git a/test/ExecutionEngine/OrcLazy/global_aliases.ll b/test/ExecutionEngine/OrcLazy/global_aliases.ll
new file mode 100644 (file)
index 0000000..61fde4b
--- /dev/null
@@ -0,0 +1,21 @@
+; RUN: lli -jit-kind=orc-lazy %s
+;
+; Test handling of global aliases for function and variables.
+
+@x = global i32 42, align 4
+@y = alias i32, i32* @x
+
+define i32 @foo() {
+entry:
+  %0 = load i32, i32* @y, align 4
+  ret i32 %0
+}
+
+@bar = alias i32(), i32()* @foo
+
+define i32 @main(i32 %argc, i8** %argv) {
+entry:
+  %0 = call i32() @bar()
+  %1 = sub i32 %0, 42
+  ret i32 %1
+}