Search code examples
c++lambdaconstexprfunctorcompile-time

Members in constexpr functors causing runtime execution


I am using functors to generate compile time calculated code in the following way (I apologize for the long code, but it is the only way I have found to reproduce the behavior):

#include <array>
#include <tuple>

template <int order>
constexpr auto compute (const double h)
{
  std::tuple<std::array<double,order>,
         std::array<double,order> > paw{};

  auto xtab = std::get<0>(paw).data();
  auto weight = std::get<1>(paw).data();

  if constexpr ( order == 3 )
              {
            xtab[0] =  - 1.0E+00;
            xtab[1] =    0.0E+00;
            xtab[2] =    1.0E+00;

            weight[0] =  1.0 / 3.0E+00;
            weight[1] =  4.0 / 3.0E+00;
            weight[2] =  1.0 / 3.0E+00;
              }
  else if constexpr ( order == 4 )
              {
            xtab[0] =  - 1.0E+00;
            xtab[1] =  - 0.447213595499957939281834733746E+00;
            xtab[2] =    0.447213595499957939281834733746E+00;
            xtab[3] =    1.0E+00;

            weight[0] =  1.0E+00 / 6.0E+00;
            weight[1] =  5.0E+00 / 6.0E+00;
            weight[2] =  5.0E+00 / 6.0E+00;
            weight[3] =  1.0E+00 / 6.0E+00;
              }

  for (auto & el : std::get<0>(paw))
      el = (el + 1.)/2. * h ;

  for (auto & el : std::get<1>(paw))
    el = el/2. * h ;

  return paw;
}


template <std::size_t n>
class Basis
{
public:

  constexpr Basis(const double h_) :
    h(h_),
    paw(compute<n>(h)),
    coeffs(std::array<double,n>())
  {}

  const double h ;
  const std::tuple<std::array<double,n>,
           std::array<double,n> > paw ;
  const std::array<double,n> coeffs ;

  constexpr double operator () (int i, double x) const
  {
    return 1. ;
  }

}; 

template <std::size_t n,std::size_t p,typename Ltype,typename number=double>
class Functor
{
 public:
  constexpr Functor(const Ltype L_):
    L(L_)
  {}

  const Ltype L ;

  constexpr auto operator()(const auto v) const 
  {
    const auto l = L;
    // const auto l = L();
    std::array<std::array<number,p+1>,p+1> CM{},CM0{},FM{};
    const auto basis = Basis<p+1>(l);
    typename std::remove_const<typename std::remove_reference<decltype(v)>::type>::type w{};

    for (auto i = 0u; i < p + 1; ++i)
      CM0[i][0] += l;
    for (auto i = 0u ; i < p+1 ; ++i)
      for (auto j = 0u ; j < p+1 ; ++j)
        {
          w[i] += CM0[i][j]*v[j];
        }
    for (auto b = 1u ; b < n-1 ; ++b)
      for (auto i = 0u ; i < p+1 ; ++i)
        for (auto j = 0u ; j < p+1 ; ++j)
          {
            w[b*(p+1)+i] += CM[i][j]*v[b*(p+1)+j];
            w[b*(p+1)+i] += FM[i][j]*v[(b+1)*(p+1)+j];
          }
    return w ;
  }
};

int main(int argc,char *argv[])
{
  const auto nel = 4u;
  const auto p = 2u;
  std::array<double,nel*(p+1)> x{} ;
  constexpr auto L = 1.;
  // constexpr auto L = [](){return 1.;};
  const auto A = Functor<nel,p,decltype(L)>(L);
  const volatile auto y = A(x);
  return 0;
}

I compile using GCC 8.2.0 with the flags:

-march=native -std=c++1z -fconcepts -Ofast -Wa,-adhln

And when looking at the generated assembly, the calculation is being executed at runtime.

If I change the two lines that are commented for the lines immediately below, I find that the code is indeed being executed at compile time and just the value of the volatile variable is placed in the assembly.

I tried to generate a smaller example that reproduces the behavior but small changes in the code indeed calculate at compile time.

I somehow understand why providing constexpr lambdas helps, but I would like to understand why providing a double would not work in this case. Ideally I wouldn't like to provide lambdas because it makes my frontend messier.

This code is part of a very large code base, so please disregard what the code is actually calculating, I created this example to show the behavior and nothing more.

What would be the right way to provide a double to the functor and store it as a const member variable without changing the compile-time behavior?

Why do small modifications in the compute() function (for instance, other small changes do so as well) do indeed produce compile time code?

I would like to understand what are the actual conditions for GCC to provide these compile-time calculations, as the actual application I am working in requires it.

Thanks!


Solution

  • Non sure to understand when your code is executed run-time and when is executed compile-time, anyway the rule of the C++ language (not only g++ and ignoring the as-if rule) is that a constexpr function

    • can be executed run-time and must be executed run-time when compute values know run-time (by example: values coming from standard input)
    • can be executed compile-time and must be executed compile-time when the result goes where a compile-time know value is strictly required (by example: initialization of constexpr variable, not-type template arguments, C-style arrays dimensions, static_assert() tests)
    • there is a grey area -- when the compiler know the value involved in computation compile time but the computed value doesn't goes where a compile-time value is strictly required -- where the compiler can choose if compute compile-time or run-time.

    If you're interested in

    const volatile auto y = A(x);
    

    it seems to me we are in the grey area and the compiler can choose if compute the initial value for y compile time or run-time.

    If you want a y initialized compile-time, I suppose you can obtain this defining it (and also preceding variables) constexpr

      constexpr auto nel = 4u;
      constexpr auto p = 2u;
      constexpr std::array<double,nel*(p+1)> x{} ;
      constexpr auto L = 1.;
      // constexpr auto L = [](){return 1.;};
      constexpr auto A = Functor<nel,p,decltype(L)>(L);
      constexpr volatile auto y = A(x);