| Issue |
241269
|
| Summary |
[Coroutines] lifetime.end before a later lifetime.start in one block hides a suspend (miscompile at -O3)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
jeffchen006
|
After unrolling, one block has `lifetime.end(%a)` of iteration 0 and then `lifetime.start(%a)` of iteration 1, followed by a suspend. The block-level check treats the block as closing the lifetime, so the awaiter stays on the ramp stack and the resume function reads a dead slot.
**Reproducer** (`repro.cpp`)
```cpp
// a = {1, 10}. Step 0: Aw{1} is ready (odd), returns 1, a[0] = 2. Step 1: Aw{2} suspends;
// main resumes it; it returns 2, a[1] = 12. Correct output: 212 (2 * 100 + 12).
// clang++ -O3 prints 210: the awaiter of step 1 is kept on the ramp stack, not on the coroutine frame.
#include <coroutine>
extern "C" int printf(const char *, ...);
std::coroutine_handle<> pending;
unsigned cnt;
struct Aw {
unsigned v;
bool await_ready() noexcept { return v & 1; }
void await_suspend(std::coroutine_handle<> h) noexcept { pending = h; }
unsigned await_resume() noexcept { ++cnt; return v; }
};
struct Task {
struct promise_type {
unsigned long long val;
Task get_return_object() { return {std::coroutine_handle<promise_type>::from_promise(*this)}; }
std::suspend_never initial_suspend() noexcept { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void return_value(unsigned long long v) { val = v; }
void unhandled_exception() {}
};
std::coroutine_handle<promise_type> h;
};
Task f() {
unsigned long long a[2] = {1, 10};
for (int i = 0; i < 2; i++)
a[i] += co_await Aw{(unsigned)a[0]};
co_return a[0] * 100 + a[1];
}
int main() {
Task t = f();
pending.resume();
printf("%llu\n", t.h.promise().val);
t.h.destroy();
}
```
**Commands and output** (clang version 24.0.0git (https://github.com/llvm/llvm-project.git 8207e9143575), Target: x86_64-unknown-linux-gnu):
```console
$ clang++ -std=c++20 -O0 repro.cpp -o ./o0
$ ./o0
212
$ clang++ -std=c++20 -O3 repro.cpp -o ./o3
$ ./o3
210
```
Expected (as `-O0`): `212`. `-O3` prints `210`.
**Why the expected output is right**
One coroutine, one thread; resumed once while suspended at a non-final point; destroyed once after `final_suspend` (`suspend_always`). The awaiter temporary lives to the end of the full-_expression_ with `co_await`, so it is alive across the suspend. UBSan/ASan/MSan at -O0 are clean.
**Potential Root cause**
`llvm/lib/Transforms/Coroutines/SpillUtils.cpp`, `AllocaUseVisitor::computeShouldLiveOnFrame`: it works at block level with `isManyPotentiallyReachableFromMany(LifetimeStartBBs, CoroSuspendBBs, &LifetimeEndBBs, &DT)`. The start block is also in `LifetimeEndBBs` (it holds the first `lifetime.end`), and excluded blocks are skipped, so the search stops at once -> "no path to a suspend". The `lifetime.end` there is before the second `lifetime.start`, so it does not end that lifetime. (Gap in the fix of #86580, PRs #90265/#91372.)
**Possible Proposed fix**
Check the start block at instruction level: for each `lifetime.start` S, walk the rest of S's block; if an end of this alloca comes first, S is closed; if a suspend comes first, return true; else continue from S's successors:
```diff
- llvm::SmallVector<BasicBlock *> Worklist(LifetimeStartBBs);
+ SmallVector<BasicBlock *> Worklist;
+ for (IntrinsicInst *S : LifetimeStarts) {
+ bool Closed = false;
+ for (Instruction &I : make_range(std::next(S->getIterator()), S->getParent()->end()))
+ if (isLifetimeEndOfThisAlloca(I)) { Closed = true; break; }
+ else if (isa<AnyCoroSuspendInst>(I)) return true;
+ if (!Closed) append_range(Worklist, successors(S->getParent()));
+ }
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs