[clang] [C++20][Coroutines] Lambda-coroutine with operator new in promise_type (PR #84193)

Andreas Fertig via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 2 00:17:21 PDT 2024


andreasfertig wrote:

Hello all,

I did more investigation and found another shortcoming. In some cases, my initial implementation picked the wrong `this`- type, for example, https://github.com/andreasfertig/llvm-project/blob/9c60fec6881cca7e7fed9dca5cf24a0bd1924eaa/clang/test/SemaCXX/coroutine-promise-ctor-lambda.cpp#L45.

I pushed a new branch (https://github.com/andreasfertig/llvm-project/tree/fixGH84064take2), which also fixes this behavior and requires fewer changes.

Regarding the crash, that was improper testing on my side. All tests let Clang crash, even the tests I checked in as part of the PR. The difference is that my tests used only `static_assert` to verify that the lambda's size did not increase, while the `async_simple::Generator` example _executes_ the code. This step leads to code generation, the `static_assert`, I assume, is handled only on the constexpr evaluator.

This is where I'm stuck. The call stack for all cases is:

``` 
 #5 0x0000000105534f38 clang::CodeGen::CodeGenFunction::EmitCall(clang::CodeGen::CGFunctionInfo const&, clang::CodeGen::CGCallee const&, clang::CodeGen::ReturnValueSlot, clang::CodeGen::CallArgList const&, llvm::CallBase**, bool, clang::SourceLocation) (build/bin/clang-19+0x10262cf38)
 #6 0x00000001055e10d8 clang::CodeGen::CodeGenFunction::EmitCall(clang::QualType, clang::CodeGen::CGCallee const&, clang::CallExpr const*, clang::CodeGen::ReturnValueSlot, llvm::Value*) (build/bin/clang-19+0x1026d90d8)
 #7 0x00000001055e01b4 clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) (build/bin/clang-19+0x1026d81b4)
 #8 0x000000010561e5a8 (anonymous namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) (build/bin/clang-19+0x1027165a8)
 #9 0x000000010560edec clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (build/bin/clang-19+0x102706dec)
#10 0x000000010554e22c clang::CodeGen::CodeGenFunction::EmitCoroutineBody(clang::CoroutineBodyStmt const&) (build/bin/clang-19+0x10264622c)
```

The crash is within `CodeGenFunction::EmitCall` at https://github.com/llvm/llvm-project/blob/8bb9443333e0117ab61feecce9de339b11b924fc/clang/lib/CodeGen/CGCall.cpp#L5298

`I->getKnownRValue().getScalarVal()` is `nullptr`, which leads to the crash later when `V` is deferenced. I don't understand why `getKnownRValue` is empty. It looks to me like I had to mark `this` to be not optimized away. However, I could not see any coding doing this for the non-lambda case. Any thoughts?

https://github.com/llvm/llvm-project/pull/84193


More information about the cfe-commits mailing list