[ORC] SimpleCompiler and module transformations

Hi devs, hi Lang,

I use ORC’s SimpleCompiler to compile modules into object files and it works just great, however I’m having an issue:
a module gets changed a bit when I compile it (some transformations/optimizations applied).
I tried to set optimization level of a TargetMachine to None, but the module gets changed anyway.

The question is: how can I prevent a module from being modified during compilation?
I can make a clone of a module, but I’m curious if there are other ways to achieve the same goal.

More context:

When I run a program I try to get object file for a module from a cache.
If the object file exists, then I just proceed to a module analysis phase.
If the object does not exist, then I compile the module, put object file into a cache, and then proceed to a module analysis phase.
In other words:

  let object = cache.getObject(module);
  if object == null {
    object = compile(module)
    cache.putObject(object, module)
  }
  processModule(module)

When I run my program two times, then I basically analyze two different modules, even though they are loaded from the same bitcode file.

Please, let me know if more information/clarification is needed.

Any hints, suggestions, and recommendations are more than welcome :slight_smile:

Naively, I suspect you’d have to clone it - there are some optimizations that must be done for correctness on the IR (such as the always inlining pass that inlines any functions that specify they /must/ be inlined).

Naively, I suspect you’d have to clone it - there are some optimizations that must be done for correctness on the IR (such as the always inlining pass that inlines any functions that specify they /must/ be inlined).

That could be solved by "pre-optimizing”.
However that wouldn’t be enough: there are IR transformations that are done as part of the codegen (codegenprepare for instance).

Right now in LLVM there is no other safe way (that I know of) than cloning before processing a module.

Thank you guys, now the picture is much cleaner :slight_smile:
I decided to go with clone of an original module and everything works just great.