PSA: a Compiler Explorer ('Godbolt') tip when viewing SIL (or other IR) output

FYI if you are are a Compiler Explorer user, i recently learned something that may be of interest to you, in particular if you (like me) enjoy using the tool for viewing Swift's intermediate language output (SIL).

by default, if outputting the SIL for a snippet like this by using the -emit-sil option:

func f() -> Int { 42 }
func g() { _ = f() }

the SIL for the g() function would be rendered as something like:

sil hidden @$s6output1gyyF : $@convention(thin) () -> () {
bb0:
  %0 = function_ref @$s6output1fSiyF : $@convention(thin) () -> Int, loc "/app/example.swift":2:16, scope 4 // user: %1
  %1 = apply %0() : $@convention(thin) () -> Int, loc "/app/example.swift":2:16, scope 4
  %2 = tuple (), loc "/app/example.swift":2:20, scope 4 // user: %3
  return %2, loc "/app/example.swift":2:20, scope 4 // id: %3
} // end sil function '$s6output1gyyF'

however, this is missing some extra debug output that you get when compiling the code locally, or using other similar sites. the reason why appears to be that the compiler explorer site by default strips out 'comment only' lines. to disable this behavior you have to deselect the 'Comments' filter from the site's UI (there's a little funnel-shaped-thing button in the compiler panes). after doing this, the same SIL function looks like:

// g(), loc "/app/example.swift":2:6, scope 4
// Isolation: unspecified
sil hidden @$s6output1gyyF : $@convention(thin) () -> () {
bb0:
  // function_ref f()
  %0 = function_ref @$s6output1fSiyF : $@convention(thin) () -> Int, loc "/app/example.swift":2:16, scope 4 // user: %1
  %1 = apply %0() : $@convention(thin) () -> Int, loc "/app/example.swift":2:16, scope 4
  %2 = tuple (), loc "/app/example.swift":2:20, scope 4 // user: %3
  return %2, loc "/app/example.swift":2:20, scope 4 // id: %3
} // end sil function '$s6output1gyyF'

much nicer! no need to manually demangle function ref symbols to determine what they are! you can see an example comparing the outputs here.


p.s. i always feel slightly unsure where compiler explorer-related topics should go on these forums. 'Using Swift' probably gets the most visibility, but it's maybe a bit niche... would a 'Related Projects' subcategory be appropriate?

12 Likes