<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>Hello,</p>
<p>I use CloneFunctionInto to clone a function from one module to a
function declaration in other module. When the original function
contains debug info, the clone fails verification.</p>
<p>The source code is as follows:<br>
</p>
<pre>// Helper routine wrapping CloneFunctionInto.
static void CloneFuncIntoDecl(Function *NewFunc, Function *OldFunc,
ValueToValueMapTy &VMap) {
auto InjectionArgIt = OldFunc->arg_begin();
auto ExistingArgIt = NewFunc->arg_begin();
while (ExistingArgIt != NewFunc->arg_end())
VMap[&*InjectionArgIt++] = &*ExistingArgIt++;
SmallVector<ReturnInst *, 8> Returns;
CloneFunctionInto(NewFunc, OldFunc, VMap, true, Returns);
}
TEST(CloneFunction, ToDifferentModule) {
StringRef Assembly1 = R"(
define void @foo() {
ret void, !dbg !5
}
!llvm.module.flags = !{!0}
!llvm.dbg.cu = !{!2}
!0 = !{i32 1, !"Debug Info Version", i32 3}
!1 = distinct !DISubprogram(unit: !2)
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3)
!3 = !DIFile(filename: "foo.c", directory: "/tmp")
!4 = distinct !DISubprogram(unit: !2)
!5 = !DILocation(line: 4, scope: !1)
)";
StringRef Assembly2 = R"(
declare void @foo()
)";
LLVMContext Context;
SMDiagnostic Error;
auto M1 = parseAssemblyString(Assembly1, Error, Context);
auto* F1 = M1->getFunction("foo");
EXPECT_TRUE(F1 != nullptr);
auto M2 = parseAssemblyString(Assembly2, Error, Context);
auto* F2 = M2->getFunction("foo");
EXPECT_TRUE(F2 != nullptr);
ValueToValueMapTy VMap;
VMap[F1] = F2;
::CloneFuncIntoDecl(F2, F1, VMap);
EXPECT_FALSE(verifyModule(*M1, &errs()));
EXPECT_FALSE(verifyModule(*M2, &errs()));
}
</pre>
<p>The result is:</p>
<pre>DICompileUnit not listed in llvm.dbg.cu
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
/local/opliss/dolphin-llvm/unittests/Transforms/Utils/CloningTest.cpp:617: Failure
Value of: verifyModule(*M2, &errs())
Actual: true
Expected: false
</pre>
<p>Is this a bug or an unintended use of CloneFunctionInt? What is
the right way to clone a function to a different module?</p>
<p>Thanks,</p>
<p>-Oleg<br>
</p>
</body>
</html>