[swift-dev] [RFC] SIL syntax for debug information Part 1: Variables

Adrian Prantl aprantl at apple.com
Wed Dec 9 18:15:50 CST 2015


In order to write textual SIL -> SIL testcases that exercise the handling of debug information by SIL passes, we need to make a couple of additions to the textual SIL language. In memory, the debug information attached to SIL instructions references information from the AST. If we want to create debug info from parsing a textual .sil file, these bits need to be made explicit.

Let me illustrate this with an example. The function

> func foo(x : Int) -> Int {
>   return bar(x)
> }

is compiled to SIL as 

> // main.foo (Swift.Int) -> Swift.Int
> sil hidden @_TF4main3fooFSiSi : $@convention(thin) (Int) -> Int {
> // %0                                             // users: %1, %2, %4
> bb0(%0 : $Int):
>   debug_value %0 : $Int  // let x, argno: 1       // id: %1 line:1:10:in_prologue
>   return %4 : $Int                                // id: %5 line:2:3:return
> }

Note that there is a bunch of information available in comments that will be lost once we parse that textual SIL again. I’d like to add syntax to SIL for the information in the comments. This proposal deals with lifting the debug variable information (the first comment) into actual SIL syntax. A similar proposal for locations will be coming soon.
With the proposed syntax, this could like like:

> sil hidden @_TF4main3fooFSiSi : $@convention(thin) (Int) -> Int {
> bb0(%0 : $Int):
>   debug_value %0 : $Int, !dbg_var(name: "x", type: "_TTSi", argno: 1)
>   return %4 : $Int
> }

More formally, debug variable info may be attached to debug_value, debug_value_addr, alloc_box, and alloc_stack instructions.

  sil-instruction ::= 'alloc_stack' sil-type dbg-var
  sil-instruction ::= 'alloc_stack' sil-type dbg-var
  sil-instruction ::= debug_value sil-operand dbg-var
  sil-instruction ::= debug_value_addr sil-operand dbg-var
  dbg-var ::= ‘!dbg_var’ ‘(‘ var-attr (',' var-attr)*) ‘)'
  var-attr ::= ‘name:’ string-literal
  var-attr ::= ’type:’ string-literal
  var-attr ::= ‘argno:’ integer-literal

This syntax for `dbg-var` is borrowed straight from LLVM IR and thus invokes a familiar feeling. Since the primary use-case of it will be in test cases, the verbose dictionary-like syntax is really helpful.

Syntax alternatives I’ve considered and rejected include:
1. debug_value %0 : $Int, “x”, “_TtSi”, 1
   Why: Hard to read, potentially ambiguous because some fields are optional.

2. debug_value [name “x”] [type “_TtSi”] [argno 1] %0 : $Int
   Why: Attributes in square brackets don’t typically have arguments and come before the entity they are modifying.

3. debug_value @var(name: “x”, type: “_TtSi”, argno: 1) %0 : $Int
   Why: The ‘@‘ sigil is used not just for attributes but also for global symbols and thus creates an ambiguity.

-- adrian




More information about the swift-dev mailing list