Add support for consteval and constexpr functions in Clad

Description

In mathematics and computer algebra, automatic differentiation (AD) is a set of techniques to numerically evaluate the derivative of a function specified by a computer program. Automatic differentiation is an alternative technique to Symbolic differentiation and Numerical differentiation (the method of finite differences). Clad is based on Clang which provides the necessary facilities for code transformation. The AD library can differentiate non-trivial functions, to find a partial derivative for trivial cases and has good unit test coverage.

C++ provides the specifiers consteval and constexpr to allow compile time evaluation of functions. constexpr declares a possibility, i.e the function will be evaluated at compile time if possible, else at runtime; whereas consteval makes it mandatory, i.e every call to the function must produce a compile-time constant.

The aim of this project is to ensure that same semantics are followed by the generated derivative function, i.e if the primal function is evaluated at compile time (because of constexpr or consteval specifier), then the generated derivative code should also have the same specifier to be evaluatable at compile time.

This will enable clad to demonstrate the benefits of doing automatic differentiation directly on C++ frontend to utilize the benefits of clang’s infrastructure.

After successful completion of the project the code snippet should work as expected:

    #include <cstdio>
    #include "clad/Differentiator/Differentiator.h"
    
    constexpr double sq(double x) { return x*x; }
    consteval double fn(double x, double y, double z) {
      double res = sq(x) + sq(y) + sq(z);
      return res;
    }

    int main() {
      auto d_fn = clad::gradient(fn);
      double dx = 0, dy = 0, dz = 0;
      d_fn.execute(3, 4, 5, &dx, &dy, &dz);
      printf("Gradient vector: [%.2f, %.2f, %.2f]", dx, dy, dz);
      return 0;
    }

Project Milestones

Requirements

Mentors

Additional Information

Corresponding Project

Participating Organizations