<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
        {mso-style-name:msonormal;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'>We are seeing the same. Cloning is failing to add newly created  DICompileUnit to llvm.dbg.cu in the module resulting in verifier assert.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'>Sergei<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><b><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'>From:</span></b><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'> llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org] <b>On Behalf Of </b>Matthias Bernad via llvm-dev<br><b>Sent:</b> Thursday, June 15, 2017 10:32 AM<br><b>To:</b> llvm-dev@lists.llvm.org<br><b>Subject:</b> [llvm-dev] CloneFunctionInto produces invalid debug info<o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal>Hi!<o:p></o:p></p><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>We are currently working on a science project and implemented a FunctionPass that clones a function (more precisely a constructor of a struct/class) and adds a parameter.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>First, we create a new function with a new function type, which includes the newly added parameter:<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><blockquote style='margin-left:30.0pt;margin-right:0in'><div><p class=MsoNormal>Function *NF = Function::Create(NewFTy, F.getLinkage(), F.getName() + "Cloned", F.getParent());<o:p></o:p></p></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>and after setting up the ValueToValueMapTy, we use the CloneFunctionInto method to clone the function body<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><blockquote style='margin-left:30.0pt;margin-right:0in'><div><div><p class=MsoNormal>CloneFunctionInto(NF, &F, Map, true, Returns, "Cloned");<o:p></o:p></p></div></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal style='margin-bottom:12.0pt'>The code seems to work as intended, but when we try to emit debug symbols (clang -g flag) the pass fails with following message:<o:p></o:p></p></div><blockquote style='margin-left:30.0pt;margin-right:0in'><div><p class=MsoNormal>"All DICompileUnits must be listed in <a href="http://llvm.dbg.cu">llvm.dbg.cu</a>"<o:p></o:p></p></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Nevertheless, we can dump the Module and therefore can print out the annotated IR.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>This is what the function to be cloned looks like:<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><blockquote style='margin-left:30.0pt;margin-right:0in'><div><div><p class=MsoNormal>; Function Attrs: noinline nounwind uwtable<o:p></o:p></p></div></div><div><div><p class=MsoNormal>define linkonce_odr void @_ZN12MyFunnyClassC2Ev(%struct.MyFunnyClass* %this) unnamed_addr #4 comdat align 2 !dbg !46 {<o:p></o:p></p></div></div><div><div><p class=MsoNormal>entry:<o:p></o:p></p></div></div><div><div><p class=MsoNormal>  %this.addr = alloca %struct.MyFunnyClass*, align 8<o:p></o:p></p></div></div><div><div><p class=MsoNormal>  store %struct.MyFunnyClass* %this, %struct.MyFunnyClass** %this.addr, align 8<o:p></o:p></p></div></div><div><div><p class=MsoNormal>  call void @llvm.dbg.declare(metadata %struct.MyFunnyClass** %this.addr, metadata !49, metadata !31), !dbg !50<o:p></o:p></p></div></div></blockquote><blockquote style='margin-left:30.0pt;margin-right:0in'><div><div><p class=MsoNormal>... rest of function code<o:p></o:p></p></div></div></blockquote><blockquote style='margin-left:30.0pt;margin-right:0in'><div><div><p class=MsoNormal>}<o:p></o:p></p></div></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><div><p class=MsoNormal>!46 = distinct !DISubprogram(name: "MyFunnyClass", linkageName: "_ZN12MyFunnyClassC2Ev", scope: !15, file: !1, line: 1, type: !25, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagArtificial | DIFlagPrototyped, isOptimized: false, unit: !0, declaration: !47, variables: !2)<o:p></o:p></p></div></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>and the cloned function:<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><blockquote style='margin-left:30.0pt;margin-right:0in'><div><div><p class=MsoNormal>; Function Attrs: noinline nounwind uwtable<o:p></o:p></p></div></div><div><div><p class=MsoNormal>define linkonce_odr void @_ZN12MyFunnyClassC2EvCloned(%struct.MyFunnyClass* %this, { [6 x i8*] }* %newparam) unnamed_addr #4 align 2 !dbg !73 {<o:p></o:p></p></div></div><div><div><p class=MsoNormal>entry:<o:p></o:p></p></div></div><div><div><p class=MsoNormal>  %this.addr = alloca %struct.MyFunnyClass*, align 8<o:p></o:p></p></div></div><div><div><p class=MsoNormal>  store %struct.MyFunnyClass* %this, %struct.MyFunnyClass** %this.addr, align 8<o:p></o:p></p></div></div><div><div><p class=MsoNormal>  call void @llvm.dbg.declare(metadata %struct.MyFunnyClass** %this.addr, metadata !89, metadata !31), !dbg !91<o:p></o:p></p></div></div></blockquote><blockquote style='margin-left:30.0pt;margin-right:0in'><div><div><p class=MsoNormal>... rest of function code<o:p></o:p></p></div></div></blockquote><blockquote style='margin-left:30.0pt;margin-right:0in'><div><div><p class=MsoNormal>}<o:p></o:p></p></div></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><div><p class=MsoNormal>!73 = distinct !DISubprogram(name: "MyFunnyClass", linkageName: "_ZN12MyFunnyClassC2Ev", scope: !74, file: !1, line: 1, type: !81, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagArtificial | DIFlagPrototyped, isOptimized: false, unit: !87, declaration: !88, variables: !2)<o:p></o:p></p></div></div><div><p class=MsoNormal><o:p> </o:p></p></div></blockquote><p class=MsoNormal>So the cloned function gets annotated with debug symbols as expected. We noticed that the linkageName of the cloned function is the same as the original one's. Could that cause the error mentioned above? If so, how can we fix that error?<o:p></o:p></p><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Best regards and thanks in advance,<o:p></o:p></p></div><div><p class=MsoNormal>Matthias<o:p></o:p></p></div></div></div></body></html>