llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.81k stars 11.45k forks source link

Gradient of an accumulation obtained by a for-loop #98449

Open de-sholl opened 1 month ago

de-sholl commented 1 month ago

The code


double f(double const* const x, std::size_t const n)
{
    double acc{};
    for (std::size_t i = 0; i < n; ++i)
        acc += x[i] * x[i];
    return acc;
}

int main()
{
    auto g = clad::gradient(f, "x[0:1]");

    std::vector<double> x = { 3, 4 },
        dx(x.size());
    g.execute(x.data(), x.size(), dx.data());

    std::cout << dx[0] << std::endl;
    return 0;
}

produces the following error:

member reference base type 'double' is not a structure or union.

When I change the definition of f to

double f(double const* const x, std::size_t const n)
{
    double acc = x[0] * x[0];
    for (std::size_t i = 1; i < n; ++i)
        acc += x[i] * x[i];
    return acc;
}

then everything works fine. Can this problem be solved somehow?

llvmbot commented 1 month ago

@llvm/issue-subscribers-clang-frontend

Author: None (de-sholl)

The code ``` double f(double const* const x, std::size_t const n) { double acc{}; for (std::size_t i = 0; i < n; ++i) acc += x[i] * x[i]; return acc; } int main() { auto g = clad::gradient(f, "x[0:1]"); std::vector<double> x = { 3, 4 }, dx(x.size()); g.execute(x.data(), x.size(), dx.data()); std::cout << dx[0] << std::endl; return 0; } ``` produces the following error: > member reference base type 'double' is not a structure or union. When I change the definition of `f` to ``` double f(double const* const x, std::size_t const n) { double acc = x[0] * x[0]; for (std::size_t i = 1; i < n; ++i) acc += x[i] * x[i]; return acc; } ``` then everything works fine. Can this problem be solved somehow?