Compile using whole module optimisation with emit-ir to separate files

I want to compile a module, using WMO but output each swift file as LLVM IR in a separate file. I will then compile each of these to binary using a separate process.

I can't figure out what flags to use to do this.

I'm attempting to do it like this...

swiftc -emit-ir -O -parse-as-library ... -whole-module-optimization -num-threads 4 -output-file-map outputMap.json -module-name XXX A.swift B.swift C.swift...

And my output file map looks like this...

{
"A.swift":{ "object":"A.ll" },
"B.swift":{ "object":"B.ll" },
"C.swift":{ "object":"C.ll" },
...
}

I was expecting the optimisation to be done together, then each file lowered to IR and emitted producing A.ll, B.ll, C.ll in the directory.

This is not what is happening. Instead the whole LLVM IR is being dumped to stdout. I can't use a -o flag or it will just dump the whole LLVM IR to that file.

My understanding of WMO is it optimises the whole module using SIL transforms, but then lowers to separate LLVM IR for each compilation unit separately, so they can undergo target lowering in LLVM separately (usually multi threaded). So I don't understand why this isn't working.

Is there some magic flag I can pass to get the result I want? I need WMO and also separate LLVM IR text files for the final phase of compilation.

Thanks for any help or advice you can give.

Carl

I think I might have just answered my own question with a lot of searching through the source code.

The output map should be like "A.swift",{"llvm-ir":"A.ll"},.... Is that right?

If so, we should probably document it to make life easier for people building toolchains and build tools? I saw the list of possible file types are in include/swift/Basic/FileTypes.def I think?

PRs improving the documentation are very welcome. :slight_smile: For compiler flag examples and precise formats for ancillary files, in my experience, it is often faster to look through test cases rather than trying to understand the source code.

Cool. I'll put it on my todo list of PRs to submit when I've finished this big push to get a working swift stdlib for my microcontroller. Getting close! :slight_smile:

1 Like