[llvm-dev] CloneFunctionInto of a function with debug info to a different module

Oleg Pliss via llvm-dev llvm-dev at lists.llvm.org
Thu Jun 27 17:31:28 PDT 2019


Hello,

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.

The source code is as follows:

// 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()));
}

The result is:

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

Is this a bug or an unintended use of CloneFunctionInt? What is the 
right way to clone a function to a different module?

Thanks,

-Oleg

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190627/82d11bc0/attachment.html>


More information about the llvm-dev mailing list