Dematerializing functions during opt

I'm writing an opt pass that adds a lot of new functions to a module. In some
extreme cases, this causes opt to fail with out-of-memory errors.

Since all the created functions quickly become unneeded for my pass, I am
trying to find a way to discard them from memory (i.e., write them to disk).

I noticed there is a method to do just this: GlobalValue::Dematerialize.
However, there does not seem to be an appropriate GVMaterializer to do the

So, I was wondering if there is an existing way to dematerialize functions
during opt. And if there's not, could someone give some pointers on how to add

Thanks in advance,