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

    <tr>
        <th>Summary</th>
        <td>
            [Clang] FE Crash in CGCoroutine: NRVO var emission didn't result in an AllocaInst
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          yuxuanchen1997
      </td>
    </tr>
</table>

<pre>
    Repro:
```
#include <coroutine>

struct Task {
    struct promise_type {
        Task get_return_object();
        
        std::suspend_always initial_suspend() { return {}; }
        std::suspend_always final_suspend() noexcept { return {}; }
        
        void return_void() {}
        void unhandled_exception() {}
    };
    
    Task() {}
    Task(const Task&) {};
};

// Example usage
const Task example() {
 co_return;
}
```

https://godbolt.org/z/9W6vEvvz4

Assertion failure here: https://github.com/llvm/llvm-project/blob/1a940bfff9176fb38a71b0045d7ae25df9a4ca7d/clang/lib/CodeGen/CGCoroutine.cpp#L712

This is due to that in `example()`'s return type is const qualified and therefore didn't pass [this check](https://github.com/llvm/llvm-project/blob/1a940bfff9176fb38a71b0045d7ae25df9a4ca7d/clang/lib/CodeGen/CGCoroutine.cpp#L683) for `DirectEmit`, and that it's NRVO optimized. 

The fix is either don't compare Qualified Types in `DirectEmit` or just don't attach the metadata when the return value is NRVO optimized, in which case the `AllocaInst` doesn't exist. 
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMVUuP4zYM_jXKhWggy6_xwQdPZrIoULToYtAeA1miY-0qkqtH5vHrC9meSbJdoO1tgwCSJfL7yI8kxL1XR4PYkvKeMPYaXyI3YkSTNU1NGCPlw4bHMFrX3t5teitf2884OUvyjtCOVHT9046wXBmho0Qg-U5YZ2NQBkn-mC5p54OLIsAT91-B1PeEdgAA6-nk7El5PITXCa9u0292OGI4OAzRmYPtv6AIhN0R1pD82vJ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