[cfe-dev] Detecting (N)RVO

Kim Gräsman via cfe-dev cfe-dev at lists.llvm.org
Tue Nov 3 13:34:56 PST 2015


On Tue, Nov 3, 2015 at 9:49 PM, Kim Gräsman <kim.grasman at gmail.com> wrote:
>
> I'm experimenting with a patch to let users mark functions with an attribute
>
> [[clang::requires_rvo]]
> std::string f() {
>     return std::string("Hello world");
> }
>
> and in the presence of this attribute, warn if (N)RVO didn't happen.
> This would be useful to prevent small code changes from disabling RVO
> in critical paths.
>
> I have the attribute in place, and I've seen that I can check if it
> exists with FunctionDecl::hasAttr. But finding where NRVO is actually
> decided for a given function turns out to be more difficult...
>
> SemaInit appears to do something for NRVO/copy elision more generally
> when constructors are called. But it only marks the CXXConstructExpr
> as elidable, as far as I can see, and I'm not sure if this is used to
> effect RVO later (I found something in
> CodeGenFunction::EmitCXXConstructExpr, but there seems to be more
> playing into the decision of whether to construct at caller or not.
>
> Also, I'm not sure if my terminology is right; I haven't found any
> references to RVO-sans-the-N, is that refered to as copy elision? NRVO
> only seems to address VarDecls and their lifetime.

I guess the more direct question is: is there a single place where I
can detect if copy elision/NRVO/RVO has happened or will definitely
happen for the return value of a function?

If not, I guess I'm thinking about the problem the wrong way; is there
a more correct way to ask this question?

Thanks,
- Kim



More information about the cfe-dev mailing list