llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[C++] NRVO doesn't elide move when return type is a placeholder in a function template

Open MitalAshok opened this issue 2 years ago • 8 comments

Looking at the assembly output for this https://godbolt.org/z/6eG7TWvvd:

struct test {
    test();
    test(test&&);
};

auto f1() {
    test t;
    return t;
}

template<typename = void>
auto f2() {
    test t;
    return t;
}

template<typename = void>
test f3() {
    test t;
    return t;
}

template auto f2();
template test f3();

Shows that the move is elided in f1 and f3, but not in f2. This also happens if the placeholder is decltype(auto)

MitalAshok avatar Jun 12 '24 17:06 MitalAshok

Apparently this is just unimplemented

https://github.com/llvm/llvm-project/blob/abedb3b8356d5d56f1c575c4f7682fba2cb19787/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp#L1254-L1256

One fix I can think of is to "speculatively infer" what an auto type will be. When isNRVOVariable(), we know that it is of the form return x; or return (x);. With a plain (possibly cv-qualified) auto placeholder, this will be deduced to the correct type and be eligible for NRVO. With decltype(auto), only return x; would be (and return (x); would be a reference type, so not eligible). Any other form (auto*, auto&) would not be a class type.

Would that approach work?

Another fix could be to keep a list of possible NRVO variables re-check them after the placeholder type is deduced. This list can not be larger than the number of return statements I think

MitalAshok avatar Jun 12 '24 19:06 MitalAshok

@llvm/issue-subscribers-clang-frontend

Author: Mital Ashok (MitalAshok)

Looking at the assembly output for this <https://godbolt.org/z/6eG7TWvvd>:
struct test {
    test();
    test(test&amp;&amp;);
};

auto f1() {
    test t;
    return t;
}

template&lt;typename = void&gt;
auto f2() {
    test t;
    return t;
}

template&lt;typename = void&gt;
test f3() {
    test t;
    return t;
}

template auto f2();
template test f3();

Shows that the move is elided in f1 and f3, but not in f2. This also happens if the placeholder is decltype(auto)

llvmbot avatar Jun 13 '24 20:06 llvmbot

@llvm/issue-subscribers-clang-codegen

Author: Mital Ashok (MitalAshok)

Looking at the assembly output for this <https://godbolt.org/z/6eG7TWvvd>:
struct test {
    test();
    test(test&amp;&amp;);
};

auto f1() {
    test t;
    return t;
}

template&lt;typename = void&gt;
auto f2() {
    test t;
    return t;
}

template&lt;typename = void&gt;
test f3() {
    test t;
    return t;
}

template auto f2();
template test f3();

Shows that the move is elided in f1 and f3, but not in f2. This also happens if the placeholder is decltype(auto)

llvmbot avatar Jun 13 '24 20:06 llvmbot

I think the best here would be to change NRVO from a Scope based approach, into an AST-based approach.

mizvekov avatar Jun 13 '24 20:06 mizvekov

Another hard case that doesn't involve return type deduction:

struct X {
  X();
  X(const X&);
  ~X();
  char data[32];
};
template<bool NRVO> X f() {
  X x;
  if constexpr (NRVO) { return x; }
  else { return X(); }
}
int main() {
  f<true>();
}

This kind of situation seems basically impossible to handle with the scope-based approach. We would need to do the NRVO analysis during or after instantiation to get this right.

zygoloid avatar Nov 11 '25 18:11 zygoloid

There's also this case where copy is not elided if the return type is deduced:

#include <vector>

auto f(auto&& input) //-> std::remove_cvref_t<decltype(input)> // why does uncommenting the trailing return type fix -Wnrvo error?
{
    auto out = std::forward<decltype(input)>(input);
    return out;
}

int main() {
    auto dummy = f(std::vector<int>{1,2,3});
}

Output:

<source>:6:12: error: not eliding copy on return [-Werror,-Wnrvo]
    6 |     return out;
      |            ^
<source>:10:18: note: in instantiation of function template specialization 'f<std::vector<int>>' requested here
   10 |     auto dummy = f(std::vector<int>{1,2,3});
      |                  ^
1 error generated.
Compiler returned: 1

And here's the godbolt link: https://godbolt.org/z/TjPjM3dex GCC seems to have no issues with NRVO here.

macleivo avatar Oct 22 '26 09:10 macleivo

@macleivo That is the same as f2 in the original comment in this thread, the auto parameter makes your function a template.

LB-- avatar Oct 22 '26 17:10 LB--

21. @macleivo That is the same as f2 in the original comment in this thread, the auto parameter makes your function a template.

@LB-- You're right, good catch!

macleivo avatar Oct 23 '26 12:10 macleivo