Skip to content

How to properly clean __enzyme_virtualreverse objects? #929

@ipcamit

Description

@ipcamit

My __enzyme_virtualreverse class used in differentiating for a virtual function contains several parameters arrays, therefore I would like to manage it manually. But when I call delete on a virtual reverse class with a virtual destructor, enzyme gives following warning:

freeing without malloc %class.BaseClass* %0
freeing without malloc %class.BaseClass* %0

And also it segfaults at runtime.

How to properly clean memory in such cases?

Below is the example code (I do not know how to enable warnings in compiler explorer therefore giving source as well)
Example code: https://fwd.gymni.ch/CkxEnB

#include <cmath>

template <typename T>
T __enzyme_virtualreverse(T);

class ParentClass{
    public:
        double number, power;
        virtual double compute(double) = 0;
        virtual ~ParentClass()=default;
};

class BaseClass: public ParentClass{
    public:
    BaseClass(){};

    double compute(double mult) override {
        return std::pow(mult * number, power);
    }
    ~BaseClass()=default;
};


int main(){
   BaseClass * d_bc = new BaseClass();
   *((void **) d_bc) = __enzyme_virtualreverse(*((void **) d_bc));
    delete d_bc;
    return 0;
}

compiled as:

clang++ example.cpp -Xclang -load -Xclang /opt/enzyme/enzyme/build/Enzyme/ClangEnzyme-12.so -O3 -Wall -o a.out

Outputs:

  1. With virtual destructor:
freeing without malloc %class.BaseClass* %0
freeing without malloc %class.BaseClass* %0

and segfaults.

  1. Without virtual destructor:
example.cpp:28:5: warning: delete called on non-final 'BaseClass' that has virtual functions but non-virtual destructor [-Wdelete-non-abstract-non-virtual-dtor]
    delete d_bc;

But incomplete destruction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions