Skip to content

re-enable code coverage + enzyme #1481

@chapman39

Description

@chapman39

Enzyme does not work well with code coverage.

#include <iostream>
extern "C" double __enzyme_autodiff(void*, double);

double square(double x) {
    return x * x;
}

double dsquare(double x) {
    // This returns the derivative of square, i.e., 2 * x
    return __enzyme_autodiff((void*)square, x);
}

int main() {
    for (double i = 1; i < 5; ++i) {
        std::cout << "square(" << i << ") = " << square(i)
                  << ", dsquare(" << i << ") = " << dsquare(i) << std::endl;
    }
    return 0;
}

Link with Enzyme and --coverage CXX flags, and you will get the following error:

root@50e20fa2bc58:/home/serac/serac_repo/enzyme_cov# clang++   -fplugin=../Enzyme/enzyme/build1/Enzyme/ClangEnzyme-19.so   -fprofile-instr-generate   -fcoverage-mapping   ./enzyme_smoketest.cpp 
error: Enzyme: ; Function Attrs: mustprogress noinline nounwind optnone willreturn uwtable
define dso_local noundef double @preprocess__Z6squared(double noundef %0) #5 {
  %2 = load i64, ptr @__profc__Z6squared, align 8
  %3 = add i64 %2, 1
  store i64 %3, ptr @__profc__Z6squared, align 8
  %4 = fmul double %0, %0
  ret double %4
}

 constantarg[double %0] = 0 type: {[-1]:Float@double} - vals: {}
 constantinst[  %2 = load i64, ptr @__profc__Z6squared, align 8] = 0 val:0 type: {}
 constantinst[  %3 = add i64 %2, 1] = 0 val:0 type: {}
 constantinst[  store i64 %3, ptr @__profc__Z6squared, align 8] = 0 val:1 type: {}
 constantinst[  %4 = fmul double %0, %0] = 0 val:0 type: {[-1]:Float@double}
 constantinst[  ret double %4] = 1 val:1 type: {}
cannot handle unknown binary operator:   %3 = add i64 %2, 1

1 error generated.

According to an issue in Enzyme, if you re-build Enzyme with coverage CXX flags, it'll work. However, after doing so and creating a small CMake reproducer, I was not able to get it working.

Related issue: EnzymeAD/Enzyme#2411

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions