<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 11, 2017, at 10:16 AM, Johannes Weiß <<a href="mailto:johannesweiss@apple.com" class="">johannesweiss@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Hi Michael,<br class=""><br class=""><blockquote type="cite" class="">[...]<br class=""><blockquote type="cite" class="">Now you advise to run the '-debug-only=sil-redundant-load-elim' so I tried<br class=""><br class=""> sil-opt [...] -debug-only=sil-redundant-load-elim<br class=""><br class="">but it doesn't seem happy with that. Did I misunderstand how to pass this option?<br class=""></blockquote><br class="">What do you mean by it doesn't seem happy?<br class=""></blockquote><br class="">I was using a too old/wrong version of sil-opt (the one from Xcode 9 beta N) which didn't have that option. Resolved by using the one from my own swift build.<br class=""></div></div></blockquote><div><br class=""></div><div>Ok.</div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class=""><br class=""><blockquote type="cite" class="">[...]<br class=""><blockquote type="cite" class="">is this roughly what you had in mind?<br class=""></blockquote><br class="">Nope. I was suggesting that you write the SIL by hand. It will be much easier.<br class=""></blockquote><br class="">Ha, that's a much better idea, thanks for your help, much appreciated! No idea why I didn't think of that. This is today's update:<br class=""><br class="">--- SNIP (whole file attached as test-load-forwarding.sil) ---<br class="">// bar()<br class="">sil hidden @bar : $@convention(thin) () -> () {<br class="">bb0:<br class=""> alloc_global @MyIntStorage<br class=""> %int_storage = global_addr @MyIntStorage : $*Int<br class=""> %value_raw = integer_literal $Builtin.Int64, 42<br class=""> %value = struct $Int (%value_raw : $Builtin.Int64)<br class=""> store %value to %int_storage : $*Int<br class=""><br class=""> %f_buz = function_ref @buz : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""> %r1 = apply %f_buz(%int_storage) : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""><br class=""> %value_again = load %int_storage : $*Int<br class=""> %f_test = function_ref @testNumber12345 : $@convention(thin) (Int) -> ()<br class=""> %r2 = apply %f_test(%value_again) : $@convention(thin) (Int) -> ()<br class=""><br class=""> // just to test an illegal function<br class=""> %f_bad = function_ref @bad : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""> %r3 = apply %f_bad(%int_storage) : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""><br class=""> %value_again2 = load %int_storage : $*Int<br class=""> %r4 = apply %f_test(%value_again2) : $@convention(thin) (Int) -> ()<br class=""><br class=""> return %r2 : $()<br class="">} // end sil function 'bar'<br class="">--- SNAP ---<br class=""><br class="">So I make a global Int storage, store the number 42 to it, invoke buz() which takes an @in_guaranteed Int, then load it again (this is redundant as @in_guaranteed isn't allowed to modify it). I also pass the loaded number to the function testNumber12345() which just tests that it's the number <span class="Apple-tab-span" style="white-space:pre">        </span>12345 (which it isn't but I wanted to be sure it's actually consumed)<br class=""><br class="">Then (because I believe the RLE actually works) I also introduced a function `bad` which is a bit like `buz` but it does actually write to the *Int which I believe is illegal (right?).<br class=""><br class="">Now let's see what we get with sil-opt<br class=""><br class="">I run the following command:<br class=""><br class=""> sil-opt -verify -assume-parsing-unqualified-ownership-sil -redundant-load-elim -debug-only=sil-redundant-load-elim -debug test-load-forwarding.sil<br class=""><br class="">which gives me the following output (my annotations marked as [JW: ... ])<br class=""><br class=""><br class="">--- SNIP (whole file attached as test-load-forwarding.sil-opt) ---<br class="">*** RLE on function: testNumber12345 ***<br class="">*** RLE on function: buz ***<br class="">*** RLE on function: bad ***<br class="">*** RLE on function: bar ***<br class="">LSLocation #0 %1 = global_addr @MyIntStorage : $*Int // users: %12, %11, %7, %6, %4<br class="">Projection Path [$*Int<br class=""> Field: var _value: Int64 of: $*Builtin.Int64]<br class="">PROCESS bb0 for RLE.<br class="">WRITE alloc_global @MyIntStorage // id: %0<br class="">WRITE %6 = apply %5(%1) : $@convention(thin) (@in_guaranteed Int) -> ()<br class="">FORWARD %3 = struct $Int (%2 : $Builtin.Int64) // user: %4<br class=""> to %7 = load %1 : $*Int // user: %9<br class="">WRITE %9 = apply %8(%7) : $@convention(thin) (Int) -> () // user: %14<br class="">WRITE %11 = apply %10(%1) : $@convention(thin) (@in_guaranteed Int) -> ()<br class="">WRITE %13 = apply %8(%12) : $@convention(thin) (Int) -> ()<br class="">Replacing %7 = load %1 : $*Int // user: %9<br class="">With %3 = struct $Int (%2 : $Builtin.Int64) // user: %4<br class="">[JW: ^^^^ this looks pretty promising to me, don't understand all the output but looks like it's replacing a load :) ]<br class="">*** RLE on function: main ***<br class="">sil_stage canonical<br class=""><br class="">[...]<br class=""><br class="">// bar<br class="">sil hidden @bar : $@convention(thin) () -> () {<br class="">bb0:<br class=""> alloc_global @MyIntStorage // id: %0<br class=""> %1 = global_addr @MyIntStorage : $*Int // users: %11, %10, %6, %4<br class=""> %2 = integer_literal $Builtin.Int64, 42 // user: %3<br class=""> %3 = struct $Int (%2 : $Builtin.Int64) // users: %8, %4<br class=""> store %3 to %1 : $*Int // id: %4<br class=""> // function_ref buz<br class=""> %5 = function_ref @buz : $@convention(thin) (@in_guaranteed Int) -> () // user: %6<br class=""> %6 = apply %5(%1) : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""> // function_ref testNumber12345<br class=""> %7 = function_ref @testNumber12345 : $@convention(thin) (Int) -> () // users: %12, %8<br class=""> [JW: Indeed, it seems to have optimised out the redundant load. So that works! ]<br class=""> %8 = apply %7(%3) : $@convention(thin) (Int) -> () // user: %13<br class=""> // function_ref bad<br class=""> %9 = function_ref @bad : $@convention(thin) (@in_guaranteed Int) -> () // user: %10<br class=""> %10 = apply %9(%1) : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""> [JW: Wow, very clever, it didn't optimise out this load despite me giving the wrong guarantee that it's not actually modified (or am I misunderstanding something]<br class=""> %11 = load %1 : $*Int // user: %12<br class=""> %12 = apply %7(%11) : $@convention(thin) (Int) -> ()<br class=""> return %8 : $() // id: %13<br class="">} // end sil function 'bar'<br class=""><br class="">[...]<br class="">--- SNAP ---<br class=""><br class="">Long story short, I think the RLE actually works for the test case I created. It's even clever enough to see through my invalid function bad() which modified the storage despite its claim that it doesn't. I might also be misunderstanding something.<br class=""></div></div></blockquote><div><br class=""></div><div>When something is marked as in_guaranteed, it should be immutable. If the callee violates that, then the SIL is malformed. Perhaps, we can add some sort of verification check.</div><div><br class=""></div><div>That being said, I have a good feeling that there is some sort of analysis occuring here since you provided enough information to the optimizer. The optimization here is regardless of whether or not we can see the body of a function, we know that it is safe to optimize this just based off the @in_guaranteed. This implies using a declaration, not a definition of the bad function.</div><div><br class=""></div><div>When I said write the SIL by hand, what I meant was writing the whole program by hand. In general, we prefer SIL programs that do not have extraneous stuff that is added by the compiler (for instance debug_value). Additionally, it is important for SIL files to not have dependencies on the stdlib unless absolutely necessary (i.e. your usage of Int). This prevents the stdlib maintainers from having to update these tests given chances to the stdlib. Below is a cleaned up version that shows the problem. Look at how small it is and how it tests /exactly/ what we are trying to test and via the use of declarations and the like we are able to exclude other optimizations:</div><div><br class=""></div><div>----</div><div><font face="Monaco" class="">sil_stage canonical<br class=""><br class="">import Builtin<br class=""><br class="">struct Int {<br class=""> var _value : Builtin.Int64<br class="">}<br class=""><br class="">sil @test : $@convention(thin) (Int) -> ()<br class="">sil @buz : $@convention(thin) (@in_guaranteed Int) -> ()<br class="">sil @bad : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""><br class="">sil @bar : $@convention(thin) (@in Int) -> () {<br class="">bb0(%0 : $*Int):<br class=""> %value_raw = integer_literal $Builtin.Int64, 42<br class=""> %value = struct $Int (%value_raw : $Builtin.Int64)<br class=""> store %value to %0 : $*Int<br class=""><br class=""> %f_buz = function_ref @buz : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""> %r1 = apply %f_buz(%0) : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""><br class=""> %value_again = load %0 : $*Int<br class=""> %f_test = function_ref @test : $@convention(thin) (Int) -> ()<br class=""> %r2 = apply %f_test(%value_again) : $@convention(thin) (Int) -> ()<br class=""><br class=""> %f_bad = function_ref @bad : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""> %r3 = apply %f_bad(%0) : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""><br class=""> %value_again2 = load %0 : $*Int<br class=""> %r4 = apply %f_test(%value_again2) : $@convention(thin) (Int) -> ()<br class=""><br class=""> %9999 = tuple()<br class=""> return %9999 : $()<br class="">}</font><br class="">----</div><div><br class=""></div><div>When I run this test file through rle, I get:</div><div><br class=""></div><div>----</div><div><font face="Monaco" class="">sil_stage canonical<br class=""><br class="">import Builtin<br class="">import Swift<br class="">import SwiftShims<br class=""><br class="">struct Int {<br class=""> @sil_stored var _value: Builtin.Int64<br class=""> init(_value: Builtin.Int64)<br class="">}<br class=""><br class="">// test<br class="">sil @test : $@convention(thin) (Int) -> ()<br class=""><br class="">// buz<br class="">sil @buz : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""><br class="">// bad<br class="">sil @bad : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""><br class="">// bar<br class="">sil @bar : $@convention(thin) (@in Int) -> () {<br class="">// %0 // users: %11, %10, %6, %5, %3<br class="">bb0(%0 : $*Int):<br class=""> %1 = integer_literal $Builtin.Int64, 42 // user: %2<br class=""> %2 = struct $Int (%1 : $Builtin.Int64) // user: %3<br class=""> store %2 to %0 : $*Int // id: %3<br class=""> // function_ref buz<br class=""> %4 = function_ref @buz : $@convention(thin) (@in_guaranteed Int) -> () // user: %5<br class=""> %5 = apply %4(%0) : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""> %6 = load %0 : $*Int // user: %8<br class=""> // function_ref test<br class=""> %7 = function_ref @test : $@convention(thin) (Int) -> () // users: %12, %8<br class=""> %8 = apply %7(%6) : $@convention(thin) (Int) -> ()<br class=""> // function_ref bad<br class=""> %9 = function_ref @bad : $@convention(thin) (@in_guaranteed Int) -> () // user: %10<br class=""> %10 = apply %9(%0) : $@convention(thin) (@in_guaranteed Int) -> ()<br class=""> %11 = load %0 : $*Int // user: %12<br class=""> %12 = apply %7(%11) : $@convention(thin) (Int) -> ()<br class=""> %13 = tuple () // user: %14<br class=""> return %13 : $() // id: %14<br class="">} // end sil function 'bar'<br class=""></font></div><div>----</div><div><br class=""></div><div>Michael</div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">Does that all make sense?<br class=""><br class="">Thanks,<br class=""> Johannes<br class=""><span id="cid:685EE83B-6317-40DC-8492-D844CCFF7BA0"><test-load-forwarding.sil></span><span id="cid:BDAEF09D-C73E-4537-B437-1131ECC4CAC0"><test-load-forwarding.sil-opt></span><br class=""><br class=""><br class=""><blockquote type="cite" class=""><br class=""><blockquote type="cite" class=""><br class="">Thanks,<br class="">Johannes<br class=""><br class="">[1]: <a href="https://bugs.swift.org/browse/SR-5403" class="">https://bugs.swift.org/browse/SR-5403</a><br class=""></blockquote></blockquote><br class=""></div></div></blockquote></div><br class=""></body></html>