<div dir="ltr"><div>> We could probably tolerate a certain amount of unused jump table entries. However, I just realized that all non-inline imported calls end up going through a jump table entry. Is that correct?<br></div><div><br></div><div>In fact it is all calls that go through a function pointer type that is used anywhere in the program for an indirect call, but depending on your program that could be very close to "yes".</div><div><br></div><div>> Moving the type check lowering pass further down the pipeline (after inlining) still does not solve the problem because CFI renaming happens early and symbols attached to the jump table do not have a matching type.<br></div><div><br></div><div>As far as I know, renaming happens during the LowerTypeTests pass, after the type checks are lowered.</div><div>Lowering: <a href="http://llvm-cs.pcc.me.uk/lib/Transforms/IPO/LowerTypeTests.cpp#1620">http://llvm-cs.pcc.me.uk/lib/Transforms/IPO/LowerTypeTests.cpp#1620</a></div><div>Renaming: <a href="http://llvm-cs.pcc.me.uk/lib/Transforms/IPO/LowerTypeTests.cpp#1642">http://llvm-cs.pcc.me.uk/lib/Transforms/IPO/LowerTypeTests.cpp#1642</a></div><div>Do you have an example of what you are seeing?</div><div><div><br></div><div>Peter</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Apr 26, 2018 at 4:54 PM,  <span dir="ltr"><<a href="mailto:dmitry.mikulin@sony.com" target="_blank">dmitry.mikulin@sony.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



<div style="word-wrap:break-word;line-break:after-white-space">
<div dir="auto" style="word-wrap:break-word;line-break:after-white-space">
Hi Peter,
<div><br>
</div>
<div>We could probably tolerate a certain amount of unused jump table entries. However, I just realized that all non-inline imported calls end up going through a jump table entry. Is that correct? Initially I thought you meant calls promoted from indirect.
 While this can be fixed by replacing direct calls to jump tables with direct calls to real targets, I found other cases where ThinLTO+CFI has issues.</div>
<div><br>
</div>
<div>In ThinLTO backend, type test lowering happens very early in the pipeline, before inlining. When the type check after the call to get_fptr() is lowered (in my original example, below), the compiler cannot see that both targets belong to the same
 type and that the type check will always return ‘true’ and can be eliminated. Moving the type check lowering pass further down the pipeline (after inlining) still does not solve the problem because CFI renaming happens early and symbols attached to the jump
 table do not have a matching type.</div>
<div><br>
</div>
<div>I’m trying to think if there’s a way to delay renaming until ThinLTO backend type check lowering pass. It would help with solving both problems.</div>
<div><br>
</div>
<div>Thanks.</div>
<div>Dmitry.</div><span class="">
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<div>
<blockquote type="cite">
<div class="gmail_extra">
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div style="word-wrap:break-word;line-break:after-white-space">
<div>
<div class="m_8895698708741900876h5">
<div>
<div>
<blockquote type="cite">
<div>
<div class="gmail_quote" style="font-family:CourierNewPSMT">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div style="word-wrap:break-word;line-break:after-white-space">
<div>
<div class="m_8895698708741900876m_8743137098318580593h5">
<div>
<div>
<blockquote type="cite">
<div>
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
> a.c<br>
> =============================<br>
> typedef int (*fptr_t) (void);<br>
> fptr_t get_fptr();<br>
> int main(int argc, char *argv[])<br>
> {<br>
>  fptr_t fp = get_fptr();<br>
>  return fp();<br>
> }<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
> b.c<br>
> =============================<br>
> typedef int (*fptr_t) (void);<br>
> int foo(void) { return 11; }<br>
> int bar(void) { return 22; }<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
> static fptr_t fptr = bar;<br>
> static int i = 53;<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
> fptr_t get_fptr(void)<br>
> {<br>
>  if (i >= 0)<br>
>    fptr = foo;<br>
>  else<br>
>    fptr = bar;<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
>  return fptr;<br>
> }<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
</blockquote>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
<br>
</div>
<div><br>
</div>
</span><div><div class="h5"><div><br>
<div><br>
<blockquote type="cite">
<div>On Apr 19, 2018, at 6:18 PM, Peter Collingbourne <<a href="mailto:peter@pcc.me.uk" target="_blank">peter@pcc.me.uk</a>> wrote:</div>
<br class="m_8895698708741900876Apple-interchange-newline">
<div>
<div dir="ltr">
<div>Regarding the orderfile, yes, I was thinking more about ordering the real functions.</div>
<div><br>
</div>
<div>In that case it sounds like your best option may be to implement the optimization pass to make direct calls go directly to the real function. From a performance perspective I don't think it would make much difference if there are unused jump table
 entries.</div>
<div><br>
</div>
<div>Peter</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Thu, Apr 19, 2018 at 6:09 PM, via llvm-dev <span dir="ltr">
<<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word;line-break:after-white-space">Teresa, Peter,
<div><br>
</div>
<div>Thanks for your help!</div>
<div>I need to re-run my experiments as the compiler I used did not have the latest changes like <span style="font-family:CourierNewPSMT">r327254.</span></div>
<div><font face="CourierNewPSMT">The fact that the decision about routing calls through jump table entries is made early may be problematic. In my experiments with FreeBSD kernel, ThinLTO produced thousands jump table entries compared to only dozens
 with full LTO. As for re-ordering jump table entries, I don’t think it’s going to work as they are placed in the same section. Including *.cfi names into a link order file will take care of re-ordering real functions routed through jump table entries, but
 in our case we need to force some functions to be on the same page. So not having jump table entries for the functions that don't really need them would be ideal.</font></div>
<div><font face="CourierNewPSMT"><br>
</font></div>
<div><font face="CourierNewPSMT">Thanks.</font></div>
<div><font face="CourierNewPSMT">Dmitry.</font></div>
<div>
<div class="m_8895698708741900876h5">
<div><font face="CourierNewPSMT"><br>
</font>
<div><br>
<blockquote type="cite">
<div>On Apr 18, 2018, at 6:11 PM, Teresa Johnson <<a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a>> wrote:</div>
<br class="m_8895698708741900876m_8743137098318580593Apple-interchange-newline">
<div><br class="m_8895698708741900876m_8743137098318580593Apple-interchange-newline">
<br style="font-family:CourierNewPSMT;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
<div class="gmail_quote" style="font-family:CourierNewPSMT;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
On Wed, Apr 18, 2018 at 4:49 PM,<span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><span dir="ltr"><<a href="mailto:dmitry.mikulin@sony.com" target="_blank">dmitry.mikulin@sony.com</a>></span><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"><wbr> </span>wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div style="word-wrap:break-word;line-break:after-white-space">Hi Teresa,
<div><br>
</div>
<div>Thanks for the info!</div>
<div>This example is my attempt to reduce FreeBSD kernel to something more manageable :)</div>
<div><br>
</div>
<div>I will take a look at why globals are not being imported in this case. What’s the best tool to look into ThinLTO objects and their summaries? Most dumping tools don’t seem to like ThinLTO bitcode files…</div>
</div>
</blockquote>
<div><br>
</div>
<div>Sadly there isn't a really great way to dump the summaries. =( There was a patch awhile back by a GSOC student to dump in YAML format, but there was resistance from some who preferred dumping to llvm assembly via llvm-dis and support reading in
 the summary from llvm assembly. It's been on my list of things to do, hasn't yet risen high enough in priority to work on that. For now, you have to use llvm-bcanalyzer -dump and look at the raw format.</div>
<div><br>
</div>
<div>Teresa</div>
<div><br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div style="word-wrap:break-word;line-break:after-white-space">
<div><br>
</div>
<div>Hopefully Peter can chime in regarding CFI related issues.</div>
<div><br>
</div>
<div>Thanks.</div>
<div>Dmitry. </div>
<div>
<div class="m_8895698708741900876m_8743137098318580593h5">
<div><br>
<div><br>
<blockquote type="cite">
<div>On Apr 17, 2018, at 9:37 AM, Teresa Johnson <<a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a>> wrote:</div>
<br class="m_8895698708741900876m_8743137098318580593m_5932420937475594305Apple-interchange-newline">
<div>
<div dir="ltr">Hi Dmitry,
<div><br>
</div>
<div>Sorry for the late reply. For CFI specific code generation, pcc is a better person to answer. But on the issue of global variables being optimized, that hasn't happened yet. That would be great if you wanted to pick that up!</div>
<div><br>
</div>
<div>In your original email example, it seems like the file static i=53 could be constant propagated since there are no other defs, and the code in get_fptr simplified during the compile step, but I assume this is part of a more complex example where
 it is not possible to do this? Also note that with r327254 we started importing global variables. Do you know why we don't import in your case? I wonder if it has to do with it being CFI inserted code?</div>
<div><br>
</div>
<div>Teresa</div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr">On Tue, Apr 17, 2018 at 9:17 AM <<a href="mailto:dmitry.mikulin@sony.com" target="_blank">dmitry.mikulin@sony.com</a>> wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
I watched  Teresa’s talk on ThinLTO from last year’s CppCon, and it sounded like adding global variable information to the summaries was in the works, or at least in planning. Can someone (Teresa?) please share the current status? If it’s part of future plans,
 are there any specific proposals that can be picked up and worked on?<br>
<br>
Thanks!<br>
<br>
<br>
> On Apr 9, 2018, at 6:51 PM, via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
> Hi,<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
> I’m working on setting up ThinLTO+CFI for a C application which uses a lot of function pointers. While functionally it appears stable, it’s performance is significantly degraded, to the tune of double digit percentage points compared to regular LTO+CFI.<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
> Looking into possible causes I see that under ThinLTO+CFI iCall type checks almost always generate jump table entries for indirect calls, which creates another level of indirection for every such call. On top of that it breaks the link order layout because
 real function names point to jump table entries. It appears that I’m hitting a limitation in ThinLTO on how much information it can propagate across modules, particularly information about constants. In the example below, the fact that “i” is effectively a
 constant, is lost under ThinLTO, and the inlined copy of b.c:get_fptr() in a.c does not eliminate the conditional, which, for CFI purposes requires to generate a type check/jump table.<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
> I was wondering if there was a way to mitigate this limitation.<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
> a.c<br>
> =============================<br>
> typedef int (*fptr_t) (void);<br>
> fptr_t get_fptr();<br>
> int main(int argc, char *argv[])<br>
> {<br>
>  fptr_t fp = get_fptr();<br>
>  return fp();<br>
> }<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
> b.c<br>
> =============================<br>
> typedef int (*fptr_t) (void);<br>
> int foo(void) { return 11; }<br>
> int bar(void) { return 22; }<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
> static fptr_t fptr = bar;<br>
> static int i = 53;<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
> fptr_t get_fptr(void)<br>
> {<br>
>  if (i >= 0)<br>
>    fptr = foo;<br>
>  else<br>
>    fptr = bar;<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
>  return fptr;<br>
> }<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
> ______________________________<wbr>_________________<br>
> LLVM Developers mailing list<br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
><span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bi<wbr>n/mailman/listinfo/llvm-dev</a><br>
<br>
</blockquote>
</div>
<br clear="all">
<div><br>
</div>
--<span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span><br>
<div dir="ltr" class="m_8895698708741900876m_8743137098318580593m_5932420937475594305m_-6854738011569243965gmail_signature" data-smartmail="gmail_signature">
<span style="font-family:Times;font-size:inherit">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small">
<td style="border-top-style:solid;border-top-color:rgb(213,15,37);border-top-width:2px">
Teresa Johnson |</td>
<td style="border-top-style:solid;border-top-color:rgb(51,105,232);border-top-width:2px">
 Software Engineer |</td>
<td style="border-top-style:solid;border-top-color:rgb(0,153,57);border-top-width:2px">
 <a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a> |</td>
<td style="border-top-style:solid;border-top-color:rgb(238,178,17);border-top-width:2px">
 408-460-2413</td>
</tr>
</tbody>
</table>
</span></div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br style="font-family:CourierNewPSMT;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
<br clear="all" style="font-family:CourierNewPSMT;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
<div style="font-family:CourierNewPSMT;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
<br>
</div>
<span style="font-family:CourierNewPSMT;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important">--<span class="m_8895698708741900876m_8743137098318580593Apple-converted-space"> </span></span><br style="font-family:CourierNewPSMT;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
<div class="m_8895698708741900876m_8743137098318580593gmail_signature" data-smartmail="gmail_signature" style="font-family:CourierNewPSMT;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
<span style="font-family:Times;font-size:inherit">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small">
<td nowrap style="border-top-style:solid;border-top-color:rgb(213,15,37);border-top-width:2px">
Teresa Johnson |</td>
<td nowrap style="border-top-style:solid;border-top-color:rgb(51,105,232);border-top-width:2px">
 Software Engineer |</td>
<td nowrap style="border-top-style:solid;border-top-color:rgb(0,153,57);border-top-width:2px">
 <a href="mailto:tejohnson@google.com" target="_blank">tejohnson@google.com</a> |</td>
<td nowrap style="border-top-style:solid;border-top-color:rgb(238,178,17);border-top-width:2px">
 408-460-2413</td>
</tr>
</tbody>
</table>
</span></div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</div>
<br>
______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br>
</blockquote>
</div>
<br>
<br clear="all">
<div><br>
</div>
-- <br>
<div class="m_8895698708741900876gmail_signature" data-smartmail="gmail_signature">
<div dir="ltr">-- 
<div>Peter</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div></div></div>
</div>

</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">-- <div>Peter</div></div></div>
</div>