<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p><br>
</p>
<blockquote type="cite" cite="mid:CAAe5K+VbZPvghBSLRYRdY_1BZwqARK5wv-g7FLdb=kCqFn91mQ@mail.gmail.com">
<div dir="ltr">
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px
            0.8ex;border-left:1px solid
            rgb(204,204,204);padding-left:1ex">
Hey,<br>
<br>
I am trying to use the BlockFrequencyInfoWrapperPass to obtain hotness <br>
information in my LLVM pass. Attached is a minimal example of code which <br>
creates a SIGSEGV. The pass calls <br>
AU.addRequired<BlockFrequencyInfoWrapperPass>(); in <br>
getAnalysisUsage(..). The problem exists with changed and unchanged IR.<br>
<br>
The binary is instrumented like this: clang input.bc -fprofile-generate <br>
-o output<br>
<br>
The binary runs with selected inputs and the profiles are merged: <br>
llvm-profdata merge input.profraw -output=output.prof<br>
<br>
Then opt runs with arguments: opt input.bc -o output.bc -load mypass.so <br>
-block-freq -pgo-instr-use -pgo-test-profile-file=output.prof <br>
-profile-sample-accurate -mypass<br>
<br>
Is this a bug or am can someone provide an example on how to use <br>
BlockFrequencyInfo correctly?<br>
<br>
<br>
Example code:<br>
<br>
<br>
for (auto& F : M) {<br>
     if (F.isDeclaration()) {<br>
         continue;<br>
     }<br>
     auto& bfiPass = getAnalysis<BlockFrequencyInfoWrapperPass>(F);<br>
     llvm::BlockFrequencyInfo* BFI = &bfiPass.getBFI();<br>
<br>
     //Works - shows all info I want<br>
     BFI->print(llvm::dbgs());<br>
</blockquote>
<div><br>
</div>
<div>I don't know offhand why your code below is segfaulting, but note that BFI->print() will dereference the F pointer just as *getFunction below will. I would look in the debugger to see what the value of F is during print() since it seems to be ok there,
 and then see if you can figure out where it is changing if it is returning a bad value below.</div>
<div>Teresa</div>
</div>
</div>
</blockquote>
<p>Thanks for the feedback! I've tried debugging but this didn't get me far. I found a different solution. First, you're right that my code is correct. The problem existed elsewhere.<br>
I've upgraded to the latest LLVM 8 commit and - after recompiling my projects - the above code executed without segfaults. Going back to my LLVM 7.01 source build, and again I could no longer observe any segfaults. I know that one of my projects was compiled
 with clang 7, while LLVM build from source was done with the system's default gcc/g++ 8. Not sure if combining differently compiled libraries is considered UB in C++, but after using the same compiler for both parts errors stopped occurring.
</p>
<blockquote type="cite" cite="mid:CAAe5K+VbZPvghBSLRYRdY_1BZwqARK5wv-g7FLdb=kCqFn91mQ@mail.gmail.com">
<div dir="ltr">
<div class="gmail_quote">
<div><br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
            0.8ex;border-left:1px solid
            rgb(204,204,204);padding-left:1ex">
<br>
     for (const llvm::BasicBlock& B : F) {<br>
         //Fails with SIGSEGV -> *getFunction() returns 0xa<br>
         dbgs() << BFI->getBlockProfileCount(&B).getValueOr(0) << "\n";<br>
     }<br>
}<br>
<br>
<br>
lib/Analysis/BlockFrequencyInfo.cpp#L211<br>
<br>
return BFI->getBlockProfileCount(*getFunction(), BB);<br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" moz-do-not-send="true">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank" moz-do-not-send="true">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote>
</div>
<br clear="all">
<div><br>
</div>
-- <br>
<div dir="ltr" class="gmail_signature">
<div dir="ltr">
<div><span style="font-family:Times;font-size:medium">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small">
<td style="border-top:2px solid rgb(213,15,37)" nowrap="nowrap">Teresa Johnson |</td>
<td style="border-top:2px solid rgb(51,105,232)" nowrap="nowrap"> Software Engineer |</td>
<td style="border-top:2px solid rgb(0,153,57)" nowrap="nowrap"> <a href="mailto:tejohnson@google.com" target="_blank" moz-do-not-send="true">tejohnson@google.com</a> |</td>
<td style="border-top:2px solid rgb(238,178,17)" nowrap="nowrap"><br>
</td>
</tr>
</tbody>
</table>
</span></div>
</div>
</div>
</div>
</blockquote>
</body>
</html>