[CIR] Avoid illegal copies of return values
Because ABI calling convention lowering is deferred in CIR, we do not use sret parameters for functions that return a structure that can't be trivially copied. This might be OK except that we currently also generate a store to the __retval alloca and a reload from there to return the value. The following example shows this behavior:
struct S {
S();
S(const S&) = delete;
~S();
};
S foo();
S bar() {
return foo();
}
That compiles to the following CIR:
cir.func private @_Z3foov() -> !rec_S
cir.func dso_local @_Z3barb() -> !rec_S {
%0 = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["__retval"] {alignment = 1 : i64}
%1 = cir.call @_Z3foov() : () -> !rec_S
cir.store align(1) %1, %0 : !rec_S, !cir.ptr<!rec_S>
%2 = cir.load %0 : !cir.ptr<!rec_S>, !rec_S
cir.return %2 : !rec_S
}
We need to clean up the CIR generation to avoid such copies.
Note that this issue also concerns classes with non-trivial, but non deleted copy constructors.
While the store and reload shown in the example above will almost certainly be eliminated later in the compilation pipeline, the intent of this issue is to avoid having this semantically incorrect representation in any intermediate state.
Hi @andykaylor ! I'd be interested in working on this issue as a first contribution if nobody else is already handling it. I'd love to begin contributing to ClangIR but I'm not sure if this would be a good issue to dive into; please let me know!
Hi, @robpatterson13. Thanks for your offer to help!
I don't think this is a particularly good first issue. It involves some design decisions that I don't think we're ready to make just yet and will require some synchronization with the calling convention ABI lowering, which is still in progress. I filed this issue to track work that needs to be done, but I intended to leave it unstarted until we have a more complete ABI lowering implementation.
If you want to get involved with ClngIR, a good way to begin would be to look through the code and find a place where we're calling errorNYI, then find a small C or C++ test case that triggers that diagnostic (this may require specific command-line inputs), and then implement the code based on the corresponding function in the LLVM IR codegen implementation.
It's very important to verify that your proposed test case triggers the diagnostic before you begin implementing the solution, but you should probably read and understand the corresponding LLVM IR handling before you go too far, in order to get a good idea of the scope of work you're taking on.
Thanks for the explanation.
I'm interested in getting started with ClangIR. Do you have any recommendations for smaller errorNYI cases that would make good first contributions? I'm currently looking through the codebase and would appreciate any pointers to areas that are ready for contribution.
@AjaySingh8923 As a general procedure, I recommend the following:
- Find a place in the code where we're calling
errorNYI. - Find the corresponding code in classic codegen and decide if this looks like something you're willing to take on.
- Create a test case that triggers the NYI diagnostic when compiled with CIR.
- Implement it, using the classic codegen implementation as a guide to what is needed.
It's very important that you complete step 3 before starting step 4. Many of the remaining cases are subtle variations of things that are fully implemented elsewhere, so unless you've seen the diagnostic produced you can't be confident that your test will exercise the new code. Finding a test case that hits the diagnostic can be difficult, though this is something the coding agents have proven to be very good at if you have access to something like Cursor or Claude. (BTW, be sure to read the LLVM AI policy if you're going to use a coding agent. We strongly discourage the use of AI to write the code for your early contributions, but feel free to use AI to help you understand the code base. The key point is that we want new contributors to learn from what they're doing, and to fully understand the code they are contributing.)
There are some very large code paths that we haven't begun yet (such as Objective-C or Windows support). Those are not generally good places to look when you're just getting started with CIR. Feel free to reach out to me (andykaylor on Discord or [email protected]) if you'd like my input on a task that you want to attempt.
Thank you for the guidance. I have been working through the steps you recommended and wanted to share my progress.
Investigation of emitReturnOfRValue: complex return type NYI in CIR I investigated the CIR NYI emitReturnOfRValue: complex return type. It is triggered in CIRGenStmt.cpp, where emitReturnOfRValue() supports only scalar and aggregate returns, while complex types fall into an unimplemented path. Clang CodeGen (CGStmt.cpp) handles complex returns using EmitStoreOfComplex(...), suggesting CIR is missing equivalent support. Test Cases Case 1: Direct complex return _Complex double someFunc(); _Complex double bar() { return someFunc(); } Result: No NYI (different return codegen path) Case 2: Lambda returning scalar int someFunc(); auto lam = -> int { return someFunc(); }; int (*fp)() = lam; fp(); Result: No NYI (handled successfully via emitReturnOfRValue()) *Case 3: Lambda returning complex _Complex double someFunc(); auto lam = -> _Complex double { return someFunc(); }; _Complex double (fp)() = lam; fp();
Compile:clang -fclangir -emit-cir test.cpp Result: NYI triggered. Conclusion The NYI is triggered during lambda-to-function-pointer conversion when a forwarding function passes a _Complex double return into emitReturnOfRValue(). This type is not handled as scalar or aggregate and hits the unimplemented path. Direct complex returns follow a different codegen path, indicating CIR is missing complex return handling similar to Clang’s EmitStoreOfComplex().
Would appreciate feedback on whether this analysis and understanding of the execution path are correct.
@AjaySingh8923 Yes, that is the correct approach, and I agree that your fragment (with syntax corrected) does reproduce this diagnostic. This looks like an easy first issue.
Thanks for confirming. Should I work on this issue, or would you prefer I pick up a higher-priority one?
Thanks for confirming. Should I work on this issue, or would you prefer I pick up a higher-priority one?
Go ahead with this. We need to implement all of the NYIs eventually, so every one helps.