This is an archive of the discontinued LLVM Phabricator instance.

[LTO] Added flag to generate assembly file with after LTO passes
Needs ReviewPublic

Authored by bunty2020 on Aug 30 2016, 1:37 AM.

Details

Reviewers
tejohnson
Summary

save temps option currently doesn't generate assembly file with -flto flag.
To generate assembly file after LTO passes use -Wl,-plugin-opt=-save-temps

Diff Detail

Event Timeline

bunty2020 updated this revision to Diff 69648.Aug 30 2016, 1:37 AM
bunty2020 retitled this revision from to [LTO] Added flag to generate assembly file with after LTO passes.
bunty2020 updated this object.
bunty2020 added a reviewer: tejohnson.
bunty2020 added a subscriber: llvm-commits.
tejohnson edited edge metadata.Aug 30 2016, 6:46 AM

Thanks for the patch. Looks like there wasn't a way to dump assembly before codegen even before the restructuring of this out of gold-plugin.

Your patch does not follow LLVM coding conventions. Please clang-format it.

Please also add a test.

lib/LTO/LTOBackend.cpp
45

Just "lto-print-asm" (drop leading 'f' since this is an internal option)

46

s/in//

205

The -Wl,-plugin-opt is gold specific, and this LTO support is used by multiple clients (with more coming). Just remove the "-Wl,-plugin-opt=" (and update option name.

206

This should be a static function. But in any case I am suggesting a different approach below where this would go away.

211

Better to set this up in addSaveTemps (but keep optional) and use the same file naming used there (and remove the FLTOAsmFilename option). You could then do it via a new variant of setHook (e.g. setAsmHook) and create a new config hook like PrintAsmHook.

E.g. something like

auto setAsmHook = [&](ModuleHookFn &Hook) {

 // Keep track of the hook created earlier, which also needs to run.
 ModuleHookFn ExistingHook = Hook;
 Hook = [=](unsigned Task, const Module &M, const TargetMachine *TM) {
... create the filename as in setHook but with .s extension and invoke addPassesToEmitFile...

}
...
setAsmHook(PrintAsmHook);

Then you would replace where you are calling genAssemblyFile with an invocation of that new hook.

mehdi_amini added inline comments.Aug 30 2016, 9:10 AM
lib/LTO/LTOBackend.cpp
49

I rather not have any cl::opt controlling the behavior of the API.

211

I agree that this would be better, however usual hooks don't have access to the targetMachine, and more important, the CodeGen actually modifies the Module, which forces it to be cloned entirely before dumping the ASM.

bunty2020 updated this revision to Diff 69949.Sep 1 2016, 12:09 AM
bunty2020 updated this object.
bunty2020 edited edge metadata.

I have modified save temps to generate assembly file too. But, I had to clone the module for assembly generation. I would like to know if there is a work around to generate assembly without cloning module.

I have modified save temps to generate assembly file too. But, I had to clone the module for assembly generation. I would like to know if there is a work around to generate assembly without cloning module.

I already mentioned it in my previous comments: the codegen is modifying the IR. So running it two times on the same module is likely to give different results (Depending on your use-case, it may or may not matter to you, but it can be surprising).

include/llvm/LTO/Config.h
150

Comment says "before", name is "after".

Also you shouldn't describe what a hook is actually doing, this is up to the client.

lib/LTO/LTOBackend.cpp
123

This will be "costly", we don't want this by default with "addSaveTemps".
And since it hasn't been needed till now, it is likely a "rare" need. Most of the time (i.e. always till now), using clang -O2 -disable-llvm-optzns output.lto.bc -S is enough to reproduce the codegen and produce an assembly file out of the save-temps bitcode if needed.

tejohnson added inline comments.Sep 5 2016, 7:14 AM
include/llvm/LTO/Config.h
150

Part of the issue is that there is already a PreCodeGenModuleHook. That can't be used here though because of the need for passing the TM. So either name it something specific to asm generation like PreCodeGenAsmHook, or specific to taking the TM like PreCodeGenTMHook. Mehdi - WDYT?

248

Remove the whitespace change.

lib/LTO/LTOBackend.cpp
91

The block below here that computes the name and opens the file can be refactored out into a named lambda or helper function.

123

Right, I had suggested keeping this optional in my original suggestion about moving this to a hook. I think it should go back under the option from the original patch.

230

Remove whitespace change