This is an archive of the discontinued LLVM Phabricator instance.

[LTO] Emit DebugLoc for dead function in optimization remarks
ClosedPublic

Authored by Enna1 on Sep 13 2021, 8:44 PM.

Details

Summary

Currently, the dead functions information getting from optimizations remarks does not contain debug location, but knowing where these dead functions locate could be useful for debugging or for detecting dead code.

Cause in LTO::addRegularLTO() we use BitcodeModule::getLazyModule() to read the bitcode module, when we pass Function F to ore::NV(), F is not materialized, so F->getSubprogram() returns nullptr, and there is no debug location information of dead functions in optimizations remarks.

This patch call F->materialize() before we pass Function F to ore::NV(), then debug location information will be emitted for dead functions in optimization remarks.

Diff Detail

Event Timeline

Enna1 created this revision.Sep 13 2021, 8:44 PM
Enna1 requested review of this revision.Sep 13 2021, 8:44 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 13 2021, 8:44 PM

Could you include a test case?

Enna1 updated this revision to Diff 372417.Sep 14 2021, 12:30 AM

Add test case.

Enna1 updated this revision to Diff 372419.Sep 14 2021, 12:36 AM
Enna1 updated this revision to Diff 372420.Sep 14 2021, 12:43 AM
tejohnson added inline comments.Sep 14 2021, 9:38 AM
llvm/lib/LTO/LTO.cpp
860

Were we eventually materializing these dead functions without this change? If not, this seems like a compile time degradation. Would it work to move the materialize call later, capture the OptimizationRemark in a variable, and guard the materialization by isEnabled() on the remark?

Enna1 added inline comments.Sep 15 2021, 12:46 AM
llvm/lib/LTO/LTO.cpp
860

Thanks for your comment!
Yes, this will cause a compile time degradation, without this change these dead function will never be materialized.
To emit DebugLoc for dead function in optimization remarks, we must call F->materialize before ORE(F, nullptr); and ore::NV("Function", F).
I'm looking for a variable which indicates whether we enable emit OptimizationRemark or not, we can use such variable to guard the materialization.
Here are some solutions came up to me:

  1. Check if Conf.RemarksFilename is empty, only materializing these dead functions when Conf.RemarksFilename is not empty.
  2. In LTO::linkRegularLTO collect all dead functions, and in lto::finalizeOptimizationRemarks call materialize and OptimizationRemarkEmitter for all dead functions. This will introduce a variable to store all dead functions.

Thanks again!

Enna1 updated this revision to Diff 372855.Sep 15 2021, 7:44 PM

Addressed review comment.

tejohnson accepted this revision.Sep 15 2021, 9:31 PM

lgtm

llvm/lib/LTO/LTO.cpp
860

Ah, it also looks like the Function will need to be materialized before the OptimizationRemark constructor call, which was what my suggested solution would have required. Your solution looks ok to me.

This revision is now accepted and ready to land.Sep 15 2021, 9:31 PM
Enna1 added a comment.Sep 16 2021, 6:49 PM

lgtm

I don't have commit access, could you help to commit this patch? Thanks!

This revision was landed with ongoing or failed builds.Sep 21 2021, 2:50 PM
This revision was automatically updated to reflect the committed changes.