• V
    Retaining temporary IL slots allocated for passing rvalues as lvalues for the... · 03d0ec83
    vsadov 提交于
    Retaining temporary IL slots allocated for passing rvalues as lvalues for the duration of the whole encompassing expression.
    
    Passing rvalues to "in" parameters require allocation of temporary IL slots.
    
    ```C#
    var result M1(42).ToString();
    
     // where  M is declared as
    ref readonly int M1(in int x) {...}
    
    // needs to be emitted as:
    int temp = 42;
    var result M1(ref temp) .ToString();   // passed by reference
    ```
    
    This pattern bring an issue of the lifetime of such temporaries. Note that `M1` can return its argument back by reference, so the variable must exist as long as the return variable exists. - longer then the call to M1 itself.
    
    The situation where we would have to pass an rvalue by reference via a copy was possible before, but it was very rare, so the solution was to just "leak" the temp. I.E - do not release such temps back to the temp pool. This is not a good solution when the situation becomes more common.
    
    In this change we introduce a mechanism that collects temps of this kind and keeps them as for the duration of the most encompassing expression.
    We will use the same mechanism for the preexisting cases as well.
    
    Why "encompassing expression" is a sufficient extent for the temps. Consider the following concerns -
    
    1) It is not possible to store a result of a `ref readonly` call, so the temp does not need to be retained at block level. - we do not allow `ref readonly` locals in source.
    2) Internally compiler can create long-lived `ref readonly` temps for the purpose of returning from exception regions. This is not causing a concern since rvalues are not returnable and can never be stored insuch refs.
    3) We sometimes call struct methods on a temp, there is no concern with the life time of such temps since struct `this` it cannot be ref-returned.
    4) Sometimes short-term ref temps become mapped to long-term temps as a result of async spilling. We do not need to handle that specially here since those already have appropriate life times when extracted into  block variables in lowering.
    03d0ec83
CodeGenerator.cs 17.3 KB