<div dir="ltr">Hi folks, <div><br></div><div>I've discussed my desire to have multi location debug info support in LLVM IR with some of you before (and others - apologies if I forgot to CC you). I finally found some time to write down my thoughts about how this should work at the IR level and worked out the proposal below. Please let me know if this a) also does what you want out of it, b) seems like a sane way to encode things in the IR c) you see any obstacles in implementation.</div><div>This is a very rough draft, so I expect there may be several iterations, so I'll try to keep <a href="https://gist.github.com/Keno/480b8057df1b7c63c321">https://gist.github.com/Keno/480b8057df1b7c63c321</a> updated with the current iteration. Have at it:</div><div><br></div><div>===============================================================</div><div><br></div><div><div># What is it? / Why do we want this?</div><div><br></div><div>At any given time, the value of a source variable may be available in more than</div><div>one place. The most common case is that the variable is available in memory as</div><div>well as loaded in a register, but esp. in higher level languages where the</div><div>notion of a variable is more disconnected from the physical realities, there can</div><div>also be situations where you can find the same value in multiple places in</div><div>memory, or perhaps more commonly, their being multiple ways to get to the same</div><div>value in memory (e.g. through the GC frame and the argument register).</div><div><br></div><div>One can represent this in DWARF (i.e. ranges can overlap), but perhaps more</div><div>importantly one can avoid having to make a choice about which value to track in</div><div>mid level optimizations. The answer will generally depend on which value will</div><div>live longer, but at that stage we do not know that information yet. As a</div><div>concrete example, InstCombine will currently replace llvm.dbg.declare but</div><div>llvm.dbg.values on every load and store, expecting the alloca to get removed,</div><div>but if that assumption is wrong, we get worse debug info than we would have</div><div>without replacing the declare. With multiple location support, both locations</div><div>can be described and either both emitted to DWARF, or we can chose the one that</div><div>is live longer and emit that.</div><div><br></div><div>As such, there is two separate but related goals:</div><div><br></div><div>    1. Allow frontends to describe complex situations where variables may be</div><div>       available in more than one location.</div><div>    2. Provide a coherent framework for describing the locations of source</div><div>       variables in the optimization pipeline to improve debug info quality.</div><div><br></div><div>This proposal concerns the IR format for encoding this information. Separately,</div><div>getting this info into DWARF will require additional work, some of which has</div><div>already been done in D11933 and D11986. The backend work is outside the scope</div><div>of this proposal.</div><div><br></div><div># Goals of this design</div><div><br></div><div>I tried to come up with a scheme that is a minimal modification of the existing</div><div>mechanism to ease upgrading for both frontends and optimizers, but still</div><div>separates the three concerns I think are required for multiple locations support</div><div><br></div><div>    - Indicating that a value changed at source level (e.g. because an</div><div>      assignment occurred)</div><div>    - Indicating that the same value is now available in a new location</div><div>    - Indicating that a value is no longer available in some location</div><div><br></div><div>The last one is required in order to be able describe e.g. stack slot coloring,</div><div>where a memory location may cease to describe a variable even though the value</div><div>remained the same at source level.</div><div><br></div><div># The Proposal</div><div><br></div><div>I propose changing the llvm.dbg.value intrinsic from (note I'm ignoring the i64</div><div>offset argument which is already essentially dead and I expect it to be removed</div><div>soon)</div><div><br></div><div>    void @llvm.dbg.value(metadata, metadata, metadata)</div><div><br></div><div>to</div><div><br></div><div>    token @llvm.dbg.value(token, metadata, metadata, metadata)</div><div><br></div><div>with the semantics being the following:</div><div><br></div><div>    - A change of value of a variable is indicated by (pseudeo-IR)</div><div><br></div><div>        %first = call token @llvm.dbg.value(token undef, metadata %val,</div><div>                                            metadata !var, metadata !expr)</div><div><br></div><div>      for the purpose of this proposal, I'll denote such a call (with undef</div><div>      first argument) as a `key call`.</div><div><br></div><div>    - To add a location with the same value for the same variable, you pass the</div><div>      token of the FIRST llvm.dbg.value, as this llvm.dbg.value's first argument</div><div>      E.g. to add another location for the variable above:</div><div><br></div><div>        %second = call token @llvm.dbg.value(token %first, metadata %val2,</div><div>                                            metadata !var, metadata !expr2)</div><div><br></div><div>    - To indicate that a location will no longer hold a value, you do the</div><div>      following:</div><div><br></div><div>        call token @llvm.dbg.value(token %second, metadata token undef,</div><div>                                  metadata !var, metadata !())</div><div><br></div><div>    - The current set of locations for a variable at a given instruction are all</div><div>      those llvm.dbg.value instructions that dominate this location (</div><div>      equivalently all those llvm.dbg.value calls whose token you could use at</div><div>      that location without upsetting the Verifier), except that if more than</div><div>      one key call is dominating, only the most recent one and all calls</div><div>      associated to it by first argument count.</div><div><br></div><div>I think that should encapsulate the semantics, but here are some consequences</div><div>of and comments on the above that I think would be useful to discuss:</div><div><br></div><div>    - The upgrade path for existing IR is very simple and just consists of</div><div>      adding token undef as the first argument to any call in the IR.</div><div><br></div><div>    - In general, if a value gets removed by an optimization, the corresponding</div><div>      llvm.dbg.value call can be removed, unless that call is a key call, in</div><div>      which case the value should be undefed out. This is necessary both to be</div><div>      able to keep it around as the first argument to the other calls, and more</div><div>      importantly to mark the end point of a previous set of locations.</div><div><br></div><div>    - I'm, not sure I like the location removal incantation, since it doesn't</div><div>      seem super intuitive, however, I did not want to introduce an extra</div><div>      intrinsic just for this purpose. The second argument being a token</div><div>      guarantees that just undefing out an instruction will not turn a location</div><div>      add into a location remove of the key call.</div><div><br></div><div>    - It should be noted that for optimized (pseudo-C) source like:</div><div><br></div><div>        if (foo) {</div><div>            x = a;</div><div>        } else {</div><div>            x = b;</div><div>        }</div><div><br></div><div>      the IR would have to look like:</div><div><br></div><div>        if.true:</div><div>            %xtrue = ... (a)</div><div>            call token llvm.dbg.value(token undef, %xtrue, !var, !())</div><div>            br cont</div><div>        if.false:</div><div>            %xfalse = ... (b)</div><div>            call token llvm.dbg.value(token undef, %xfalse, !var, !())</div><div>            br cont</div><div>        cont:</div><div>            %x = phi [%xtrue, %if.true], [%xfalse, %if.false]</div><div>            call token llvm.dbg.value(token undef, %x, !var, !())</div><div><br></div><div>      as the live range of the debug value would end at the end of the</div><div>      respective basic block.</div><div><br></div><div>    - A related concern is what the following:</div><div><br></div><div>        call token llvm.dbg.value(token undef, %xold, !var, !())</div><div>        if.true:</div><div>            %xtrue = ... (a)</div><div>            call token llvm.dbg.value(token undef, %xtrue, !var, !())</div><div>            br cont</div><div>        if.false:</div><div>            %xfalse = ... (b)</div><div>            call token llvm.dbg.value(token undef, %xfalse, !var, !())</div><div>            br cont</div><div>        cont:</div><div>            %x = phi [%xtrue, %if.true], [%xfalse, %if.false]</div><div><br></div><div>      (i.e. the above but with a forgotten llvm.dbg.value in the cont block).</div><div>      By the semantics I have written above, `cont` would again have %xold as</div><div>      the value for %x, even though there was an intermediate assignment. I am</div><div>      not sure if this represents a problem, but it might at the very least be</div><div>      unexpected.</div><div><br></div><div>    - Do we run into problems in whatever MSVC's equivalent for debug info is.</div><div><br></div><div>    - I think llvm.dbg.declare can be deprecated and it's uses replaced by</div><div>      llvm.dbg.value with an DW_OP_deref. That would also clarify the semantics</div><div>      of the operation which have caused some confusion in the past.</div><div><br></div><div>    - We may want to add an extra pass that does debug info inference (some of</div><div>      which is done in InstCombine right now)</div><div><br></div><div>Here are some of the invariants, the verifier would enforce (included in the</div><div>hope that they can clarify anything in the above):</div><div><br></div><div>    1. If the first argument is not token undef, then</div><div>        a. If the second argument is not token undef,</div><div>            I. the first argument must be a call to llvm.dbg.value whose first</div><div>               argument is token undef</div><div>        b. If the second argument is token undef</div><div>            II.  the first argument must be a call to llvm.dbg.value whose second</div><div>                 argument is not token undef</div><div>            III. the expression argument must be empty</div><div>        c. In either case, the variable described must be the same as the one</div><div>           described by the call that is the first argument.</div><div>        d. There may not be another call to llvm.dbg.value with token undef</div><div>           that dominates this instruction, is not the one passed as the first</div><div>           argument and is dominated by the one passed as the first argument.</div><div>    2. All other invariants regarding calls to llvm.dbg.value carry over</div><div>       unchanged</div></div><div><br></div></div>