This is an archive of the discontinued LLVM Phabricator instance.

[llvm-profdata] Check for all duplicate entries in MemOpSize table
ClosedPublic

Authored by ormris on Oct 18 2022, 4:40 PM.

Details

Summary

Previously, we only checked for duplicate zero entries when merging a MemOPSize table (see D92074), but a user recently provided a reproducer demonstrating that other entries can also be duplicated. As demonstrated by the test in this patch, PGOMemOPSizeOpt can potentially generate invalid IR for non-zero, non-consecutive duplicate entries. This seems to be a rare case, since the duplicate entry is often below the threshold, but possible. This patch extends the existing warning to check for any duplicate values in the table, both in the optimization and in llvm-profdata. We're hoping that this expanded warning will catch more examples of this behavior so we can diagnose the root cause of these duplicate entries more easily.

Diff Detail

Event Timeline

ormris created this revision.Oct 18 2022, 4:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 18 2022, 4:40 PM
ormris requested review of this revision.Oct 18 2022, 4:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 18 2022, 4:40 PM
ellis added inline comments.Oct 25 2022, 1:50 PM
llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp
318–319

Why not make this a full diagnostic error/warning? Is this something a user could encounter in the wild?

318–319
llvm/test/tools/llvm-profdata/invalid-profile-gen-dup-entries.proftext
4 ↗(On Diff #468739)

It seems strange that this is emitted as a warning rather than an error. Is there a case where this warning can be safely ignored?

ormris added inline comments.Oct 25 2022, 2:47 PM
llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp
318–319

Why not make this a full diagnostic error/warning?

Well, my understanding is that invalid profile data is usually skipped with a diagnostic. I don't want this error to be out of line with other profile data diagnostics. If something more severe is usually emitted, then we should change this error as well.

Is this something a user could encounter in the wild?

This does occur in the wild, though it's very rare. We originally discovered this issue when a user sent us invalid bitcode which we determined had been generated by this pass.

llvm/test/tools/llvm-profdata/invalid-profile-gen-dup-entries.proftext
4 ↗(On Diff #468739)

My original intention was to make this an error (see the earlier patch), but my experiments seem to indicate that very few users 1) encounter the bug that generates this invalid profile data and 2) have the correct conditions to force the MemOpSize pass to use that invalid profile data. If the 3 largest counts aren't invalid, it seems like generating invalid bitcode is unlikely. We also haven't seen many users report this diagnostic to us.

That said, while I'm comfortable leaving it as a warning, but I would still prefer this to be an error. If it's an error, we would likely get more data to reproduce this issue. @xur, do you have any thoughts on this?

ormris updated this revision to Diff 470621.Oct 25 2022, 3:07 PM

Changelist:

  • Roll insertion and existence check into one line.
ellis added inline comments.Oct 25 2022, 3:52 PM
llvm/lib/ProfileData/InstrProfWriter.cpp
536

Oh I missed this the first time.

llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp
318–319

This does occur in the wild, though it's very rare. We originally discovered this issue when a user sent us invalid bitcode which we determined had been generated by this pass.

If the compiler generated invalid bitcode then IMO we should definitely emit an error. Or maybe we can turn this into an assert at least? Maybe there is something I'm missing because I'm not familiar with MemOP.

ormris added inline comments.Oct 27 2022, 10:39 AM
llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp
318–319

OK, that makes sense to me. I'll change it in the next update.

ormris updated this revision to Diff 471619.Oct 28 2022, 11:58 AM
  • Correct existence check
  • Change debug note to an error
  • Update tests to look for error
llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp
318–319

I've changed this to a simple error, but there is an argument that it should be an optimization remark. Thoughts?

ellis accepted this revision.Nov 4 2022, 4:33 PM

Seems fine to me!

This revision is now accepted and ready to land.Nov 4 2022, 4:33 PM
ormris added a comment.Nov 4 2022, 5:03 PM

Thanks! I'll commit this.

This revision was landed with ongoing or failed builds.Nov 4 2022, 5:19 PM
This revision was automatically updated to reflect the committed changes.