<p dir="ltr">Knew I was forgetting something. And yeah maybe we can use Quentin's new diagnostics to warn on this also since we could be doing cross module inlining and the front end would have no idea. </p>
<div class="gmail_quote">On Apr 16, 2014 2:21 AM, "Nick Lewycky" <<a href="mailto:nicholas@mxc.ca">nicholas@mxc.ca</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hal Finkel wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
----- Original Message -----<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
From: "Eric Christopher"<<a href="mailto:echristo@gmail.com" target="_blank">echristo@gmail.<u></u>com</a>><br>
To: "Gerolf Hoflehner"<<a href="mailto:ghoflehner@apple.com" target="_blank">ghoflehner@apple.<u></u>com</a>><br>
Cc: "llvm-commits"<<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@<u></u>cs.uiuc.edu</a>><br>
Sent: Monday, April 14, 2014 6:11:06 PM<br>
Subject: Re: patch for functions with indirect calls and always_inline  attribute<br>
<br>
On Mon, Apr 14, 2014 at 4:09 PM, Gerolf Hoflehner<br>
<<a href="mailto:ghoflehner@apple.com" target="_blank">ghoflehner@apple.com</a>>  wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Please see below.<br>
<br>
On Apr 14, 2014, at 2:56 PM, Eric Christopher<<a href="mailto:echristo@gmail.com" target="_blank">echristo@gmail.com</a><u></u>><br>
wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I could be down for this...<br>
<br>
Do you have any numbers?<br>
</blockquote>
No. I ran into this when creating a test case for another bug fix.<br>
</blockquote>
<br>
OK, would you mind running some performance numbers on this? It<br>
passes<br>
my sniff test as well, but I'd like to not be wrong :)<br>
<br>
If the numbers come out neutral/ahead/no change feel free to commit.<br>
</blockquote>
<br>
And if they don't? always_inline means *always inline* -- this is a correctness issue, and we should do it regardless of performance considerations.<br>
<br>
I am curious, however, why we have this restriction at all. Indirect dispatch is expensive, compounding that expense with the call overhead of a small function seems suboptimal; plus, for hardware that has indirect branch prediction (Intel, ARM, and others), inlining should give the branch predictor a better chance at doing the right thing.<br>

</blockquote>
<br>
I thought this restriction was there for correctness? Does the function cloning actually update blockaddress to point to the new target function? Even if the blockaddress is hidden inside an arbitrarily complex constant expression instead of being a direct operand of an instruction? (Possible, but you may want to visit all users of the BBs instead.)<br>

<br>
Also, always_inline is defeated when applied to varargs functions, because the inliner can't inline those either.<br>
<br>
Nick<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Thanks!<br>
<br>
-eric<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Nits on the patch:<br>
<br>
     // Disallow inlining of functions which contain an indirect<br>
     branch.<br>
-    if (isa<IndirectBrInst>(BI-><u></u>getTerminator()))<br>
+    if (isa<IndirectBrInst>(BI-><u></u>getTerminator())&&<br>
+        !F.hasFnAttribute(Attribute::<u></u>AlwaysInline))<br>
<br>
Update the comment.<br>
</blockquote>
Ok.<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'm assuming this can't be hoisted because of the<br>
returns_twice aspect yes?<br>
</blockquote>
<br>
Yes, for now I only wanted to address indirect branches and leave<br>
remaining checks as they are.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
-define i32 @inner5(i8* %addr) alwaysinline {<br>
+define i32 @inner5(i8* %addr) {<br>
<br>
Why the change?<br>
</blockquote>
This is to keep the old test behavior which relied on *not*<br>
inlining inner5 because of an indirect branch.<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
-eric<br>
<br>
On Mon, Apr 14, 2014 at 2:49 PM, Gerolf Hoflehner<br>
<<a href="mailto:ghoflehner@apple.com" target="_blank">ghoflehner@apple.com</a>>  wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi<br>
<br>
llvm currently does not inline functions with indirect branches.<br>
We had some<br>
internal discussion and think the always_inline attribute should<br>
overrule<br>
that restriction.<br>
<br>
Gerolf<br>
<br>
<br>
<br>
<br>
<br>
Index: lib/Analysis/IPA/InlineCost.<u></u>cpp<br>
==============================<u></u>==============================<u></u>=======<br>
--- lib/Analysis/IPA/InlineCost.<u></u>cpp     (revision 206204)<br>
+++ lib/Analysis/IPA/InlineCost.<u></u>cpp     (working copy)<br>
@@ -1301,7 +1301,8 @@<br>
                                    Attribute::ReturnsTwice);<br>
   for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE;<br>
   ++BI) {<br>
     // Disallow inlining of functions which contain an indirect<br>
     branch.<br>
-    if (isa<IndirectBrInst>(BI-><u></u>getTerminator()))<br>
+    if (isa<IndirectBrInst>(BI-><u></u>getTerminator())&&<br>
+        !F.hasFnAttribute(Attribute::<u></u>AlwaysInline))<br>
       return false;<br>
<br>
<br>
<br>
     for (BasicBlock::iterator II = BI->begin(), IE = BI->end();<br>
     II != IE;<br>
Index: test/Transforms/Inline/always-<u></u>inline-attribute.ll<br>
==============================<u></u>==============================<u></u>=======<br>
--- test/Transforms/Inline/always-<u></u>inline-attribute.ll   (revision<br>
0)<br>
+++ test/Transforms/Inline/always-<u></u>inline-attribute.ll   (working<br>
copy)<br>
@@ -0,0 +1,26 @@<br>
+; RUN: opt<  %s -O3 -S | FileCheck %s<br>
+@gv = external global i32<br>
+<br>
+define i32 @main() nounwind {<br>
+; CHECK-NOT: call i32 @foo<br>
+  %1 = load i32* @gv, align 4<br>
+  %2 = tail call i32 @foo(i32 %1)<br>
+  unreachable<br>
+}<br>
+<br>
+define internal i32 @foo(i32) alwaysinline {<br>
+  br label %2<br>
+<br>
+;<label>:2                                       ; preds = %8,<br>
%1<br>
+  %3 = phi i32 [ %0, %1 ], [ %10, %8 ]<br>
+  %4 = phi i8* [ blockaddress(@foo, %2), %1 ], [ %6, %8 ]<br>
+  %5 = icmp eq i32 %3, 1<br>
+  %6 = select i1 %5, i8* blockaddress(@foo, %8), i8* %4<br>
+  %7 = add nsw i32 %3, -1<br>
+  br label %8<br>
+<br>
+;<label>:8                                       ; preds = %8,<br>
%2<br>
+  %9 = phi i32 [ %7, %2 ], [ %10, %8 ]<br>
+  %10 = add nsw i32 %9, -1<br>
+  indirectbr i8* %6, [label %2, label %8]<br>
+}<br>
Index: test/Transforms/Inline/always-<u></u>inline.ll<br>
==============================<u></u>==============================<u></u>=======<br>
--- test/Transforms/Inline/always-<u></u>inline.ll     (revision 206203)<br>
+++ test/Transforms/Inline/always-<u></u>inline.ll     (working copy)<br>
@@ -78,7 +78,7 @@<br>
   ret i32 %add<br>
}<br>
<br>
<br>
<br>
-define i32 @inner5(i8* %addr) alwaysinline {<br>
+define i32 @inner5(i8* %addr) {<br>
entry:<br>
   indirectbr i8* %addr, [ label %one, label %two ]<br>
<br>
<br>
______________________________<u></u>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/llvm-commits</a><br>
<br>
</blockquote></blockquote>
<br>
</blockquote>
______________________________<u></u>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/llvm-commits</a><br>
<br>
</blockquote>
<br>
</blockquote>
<br>
</blockquote></div>