Running passes added by addPassesToEmitFile() mutates a module

I’m generating assembly from a module like this:

TM->addPassesToEmitFile(pass, os, nullptr, CGFT_AssemblyFile, false);
pass.run(M);

I get the assembly output, as expected, but this is also mutating my module – eliminating some dead code and optimizing away a (not-dead) zext instruction – in ways that are undesirable for my use case. Assuming that this is a feature and not a bug, can I prevent these transformations from happening? (If not, I can always make a copy of the module first, but that seems clumsy.) Thanks.

It is not possible generically I believe, this involved hooks on each target to inject their IR transformations as a “codegen preparation” phase before getting into instruction selection, like here for example: llvm-project/AArch64TargetMachine.cpp at 0963833a194331a8b8d6775dcd1c3025a8154751 · llvm/llvm-project · GitHub

1 Like

thanks Mehdi!