<div dir="ltr">Slava,<div><br></div><div>I have two (clarification) questions in your proposed implementation:</div><div><br></div><div><b>Original Function:</b></div><div><span style="color:rgb(80,0,80);font-size:12.8px">@inline(never) internal func wrap_inc(a:SumProtocol, val:Int) -&gt; Int{</span><br style="color:rgb(80,0,80);font-size:12.8px"><span style="color:rgb(80,0,80);font-size:12.8px"> return a.increment(i:val)</span><br style="color:rgb(80,0,80);font-size:12.8px"><span style="color:rgb(80,0,80);font-size:12.8px">}</span><br></div><div><span style="color:rgb(80,0,80);font-size:12.8px"><b>Transformed code:</b></span></div><div><div style="font-size:12.8px"><div>@inline(always) internal func wrap_inc(a: SumProtocol, val: Int) -&gt; Int {</div><div>  // opening an existential cannot be expressed in Swift, but it can in SIL…</div><div>  let _a = a open as T</div><div><br></div><div>  return _wrap_inc(_a, val)</div><div>}</div><div><br></div></div><div style="font-size:12.8px"><div>@inline(never) internal func _wrap_inc&lt;T : SumProtocol&gt;(_a:T, val:Int) -&gt; Int{</div><div> return _a.increment(i:val)<br>}</div></div></div><div>****************************************************************************************</div><div>In the above code sequence, did you mean that &quot;<span style="font-size:12.8px">let _a = a open as T</span><span style="font-size:12.8px">&quot; opens &quot;a:SumProtocol&quot; using open_existential_ref  instruction as &quot;SumClass&quot; which is the concrete type of a or it is opened as the &quot;$@opened SumProtocol&quot;. In both cases, the open_existential_ref in the original function is still there and giving rise to opening the existential twice. Did you also intended that the _wrap_inc function is rewritten to eliminate the open_existential_ref as well (this is more complicated if the protocol is passed down a call chain)? So, I do not really understand what the &quot;</span><span style="font-size:12.8px">let _a = a open as T&quot; is suggesting. The other part of the confusion is</span><span style="font-size:12.8px"> about the peephole optimization which optimizes the code sequence consisting of the creation of object for SumClass and then the init_existential_ref and followed by the open_existential_ref. Can you clarify?</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Thanks.</span></div><div><span style="font-size:12.8px"><br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 29, 2017 at 1:43 PM, Slava Pestov <span dir="ltr">&lt;<a href="mailto:spestov@apple.com" target="_blank">spestov@apple.com</a>&gt;</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">Hi Raj,<div><br></div><div>The way I would approach this problem is first, turn a function taking a protocol value into one taking a protocol-constrained generic parameter. So</div><span class=""><div><br></div><div>@inline(never) internal func wrap_inc(a:SumProtocol, val:Int) -&gt; Int{<br> return a.increment(i:val)<br>}</div><div><br></div></span><div>Would become</div><div><br></div><div>@inline(always) internal func wrap_inc(a: SumProtocol, val: Int) -&gt; Int {</div><div>  // opening an existential cannot be expressed in Swift, but it can in SIL…</div><div>  let _a = a open as T</div><div><br></div><div>  return _wrap_inc(_a, val)</div><div>}</div><div><br></div><div>@inline(never) internal func _wrap_inc&lt;T : SumProtocol&gt;(_a:T, val:Int) -&gt; Int{</div><div> let a: SomeProtocol = _a<br> return a.increment(i:val)<br>}</div><div><div><br></div><div>(Note that the existing function signature specialization pass performs a similar transformation where it creates a new function with the same body as the old function but a different signature, and replaces the old function with a short thunk that transforms arguments and results and calls the new function.)</div><div><br></div><div>At this point, the existing “initialize existential with concrete type” peephole in the SILCombiner should eliminate the existential (but the peephole doesn’t work in 100% of cases yet):</div><div><br></div><div><div>@inline(always) internal func wrap_inc(a: SumProtocol, val: Int) -&gt; Int {</div><div>  // opening an existential cannot be expressed in Swift, but it can in SIL…</div><div>  let _a = a open as T</div><div><br></div><div>  return _wrap_inc(_a, val)</div><div>}</div><div><br></div></div><div><div>@inline(never) internal func _wrap_inc&lt;T : SumProtocol&gt;(_a:T, val:Int) -&gt; Int{</div><div> return _a.increment(i:val)<br>}</div></div><div><br></div><div>Now, if I have a call to wrap_inc somewhere, </div><span class=""><div><br></div><div>internal let magic:SumProtocol = SumClass(base:10)</div></span><div>_ = wrap_inc(magic)</div><div><br></div><div>Then the optimizer will inline the thunk, giving you a call to _wrap_inc. The existential value built from the SumClass instance is immediately opened so it will be peepholed away. At this point you have a call of a generic function _wrap_inc with a concrete type SumClass, and the generic specializer can produce a specialization of it.</div><div><br></div><div>Notice how this approach combines several existing optimizations and only requires adding a relatively simple new transformation, and possibly improving some of the existing optimizations to cover more cases.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Slava</div><div><br></div></font></span><div><blockquote type="cite"><span class=""><div>On Nov 29, 2017, at 11:30 AM, Raj Barik via swift-dev &lt;<a href="mailto:swift-dev@swift.org" target="_blank">swift-dev@swift.org</a>&gt; wrote:</div><br class="m_2460393608862820929Apple-interchange-newline"></span><div><div><div class="h5"><div dir="ltr"><div style="font-size:12.8px"><div>Hi,</div><div><br></div><div>I am thinking about writing a Protocol Devirtualizer Pass that specializes functions that take Protocols as arguments to transform them with concrete types instead of protocol types when the concrete types can be determined statically by some compiler analysis. This is the first step of the transformation that I am proposing. My goal is to extend this to eliminate the original function implementation and also to remove the corresponding protocol type (by deleting it from the witness table), if possible. For simple cases, where the protocol is only used for mocking for example and that there is just one class that conforms to it, we should be able to eliminate the protocol altogether. This is the second and final step of the transformation. Does anyone see any issues with both these steps? Arnold from Apple pointed out that there might be demangling issues when the protocol is eliminated. Any ideas on how to fix the demangling issues? Moreover, would such a pass be helpful to Swift folks?</div><div><br></div><div><div style="font-size:12.8px"><b>Original code:</b></div></div><div><b><br></b></div><div><br></div><div>protocol SumProtocol: class {</div><div>  func increment(i:Int)  -&gt; Int</div><div>}</div><div><br></div><div>internal class SumClass: SumProtocol {</div><div>  var a:Int</div><div>  init(base:Int) {</div><div>    self.a = base</div><div>  }</div><div>  func increment(i:Int) -&gt; Int {</div><div>   self.a += i</div><div>   return self.a</div><div>  }</div><div>}</div><div><br></div><div>@inline(never) internal func wrap_inc(a:SumProtocol, val:Int) -&gt; Int{</div><div> return a.increment(i:val)</div><div>}</div><div><br></div><div>internal let magic:SumProtocol = SumClass(base:10)</div><div>print(&quot;c=\(wrap_inc(a:magic,va<wbr>l:10))&quot;)</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><b>After Step 1:</b></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div>protocol SumProtocol: class {</div><div>  func increment(i:Int)  -&gt; Int</div><div>}</div><div><br></div><div>internal class SumClass: SumProtocol {</div><div>  var a:Int</div><div>  init(base:Int) {</div><div>    self.a = base</div><div>  }</div><div>  func increment(i:Int) -&gt; Int {</div><div>   self.a += i</div><div>   return self.a</div><div>  }</div><div>}</div><div><br></div><div><div style="font-size:12.8px">@inline(never) internal func wrap_inc(a:SumProtocol, val:Int) -&gt; Int{</div><div style="font-size:12.8px"> return a.increment(i:val)</div><div style="font-size:12.8px">}</div></div><div><br></div><div>@inline(never) internal func wrap_inc_1(a:SumClass, val:Int) -&gt; Int{</div><div> return a.increment(i:val)</div><div>}</div><div><br></div><div>internal let magic:SumClass = SumClass(base:10)</div><div>print(&quot;c=\(wrap_inc_1(a:magic,<wbr>val:10))&quot;)</div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px"><div><b>After Step 2:</b></div><div><b><br></b></div><div><div>internal class SumClass {</div><div>  var a:Int</div><div>  init(base:Int) {</div><div>    self.a = base</div><div>  }</div><div>  func increment(i:Int) -&gt; Int {</div><div>   self.a += i</div><div>   return self.a</div><div>  }</div><div>}</div><div><br></div><div>@inline(never) <span style="font-size:12.8px">internal </span><span style="font-size:12.8px">func wrap_inc(a:SumClass, val:Int) -&gt; Int{</span></div><div> return a.increment(i:val)</div><div>}</div><div><br></div><div>internal let magic:SumClass = SumClass(base:10)</div><div>print(&quot;c=\(wrap_inc(a:magic,va<wbr>l:10))&quot;)</div></div></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Any comments/thought on this transformation?</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Best,</div><div style="font-size:12.8px">Raj </div></div></div></div><span class="">
______________________________<wbr>_________________<br>swift-dev mailing list<br><a href="mailto:swift-dev@swift.org" target="_blank">swift-dev@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-dev" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-dev</a><br></span></div></blockquote></div><br></div></div></blockquote></div><br></div>