<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/56008>56008</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [RISCV] Stack space ins't de-allocate immediately might cause stack overflow
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:RISC-V
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          kito-cheng
      </td>
    </tr>
</table>

<pre>
    In this https://reviews.llvm.org/D126861 patch we try to fixed a mis-compile for a run-time failed https://github.com/llvm/llvm-project/issues/55442 , however there still another issue: the stack isn't de-allocate after exception handling immediately, that might cause stack overflow the exception is happened within a loop:  

Reduced case: 
```
void foo(int, int, int, int, int, int, int, int, int, int);

bool check_frame_variant(int n) {
  int exception_value = 1;
  int j;
  for (j=0;j < n;j++) {
    try {
        // Allocate stack for out-going argument
        foo(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        // De-allocate stack for out-going argument, that won't release if we go exception from foo.
    } catch (int i) {
      exception_value += i;
    }
  }
  // Restore sp from fp
  return exception_value;
}

``` 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJylVEFvqzAM_jXhYlEBpbQ9cNhWPWnXTdp1MmAgW0hQEtrt3z-Htq_tJk16GooAO479-bOdyjSf5aMG30sHvfejE8s7kf3hZWkv6eAWSu2HhbEdq3ZpVmyKFEb0dQ8HAm8_wRto5Qc1gDBIF9dmGKUiaI1ljZ107OXAIrKy-RKik76fqgUfYSHEOX3i0Zo3qj2L0rmJHP-sVnmegcgeoDcH2pNlzGQJnJdKAWoTRJjN2X_Y5C2s31mlRbb20FCMSpkaPQG2no3po6bRS6OhR90oqTuQw0CNZBP1GUL5Hj1n1fUeapzc2aXh8K0yhznKxUugEMeRNCd64NSkZgaUMWMABCLZieTu-H6iZqrZqkY3oz3tFclpzeLeyIZpNCLbSO0DnF99tmJ5f42hMkZB3VP9_tpaHOh1j1ZisAzhgEnbglifjkDwcUmVbdVEIJY7SP-5Pdq8XcmhB9gdq3YJq9_4wAM75j-R3c_rJgTM_XSjCM-xV-DuXLxjDYJvM_m4M6FuaLtpIAZ_c_JIXhIo-I_X9iqDGwS7qw76EcS5cw7m2HqWFHGlQbZhaDpz1TOtNUPAubhEFOsdN0aYsFMp5Dee4HspmE2uhrzBzp7OwvXvMZsnct6EARpPIMazgSU_Wf01xKV9zq6-dC1ETblstsstRl56RaVY3T89Pj-8iNUOnme-3Ig106Ddt4m8GrwfBi6arCp_cYcUSbKJ-rLANWaUbvKK0nyFlKZrXKeY5_kyR8qLSGFFyoUERJZVjIB0wxFDNvELqzijSJZZkmVJkS6TIl-l-aKtCItNtamyJllt20TkCQ187f27QSNbzsiqqXO8qaTzl-s1Qudkp2mmLfjHyffGlu_Sm5jHVHfRnEc5J_EXJ0q-hQ">