This is an archive of the discontinued LLVM Phabricator instance.

[GVN] Adjust metadata for coerced load CSE
ClosedPublic

Authored by nikic on Apr 12 2023, 6:55 AM.

Details

Summary

When reusing a load in a way that requires coercion (i.e. casts or bit extraction) we currently fail to adjust metadata. Unfortunately, none of our existing tooling for this is really suitable, because combineMetadataForCSE() expects both loads to have the same type. In this case we may work on loads of different types and possibly and offset memory location.

As such, what this patch does is to simply drop all metadata, with the following exceptions:

  • Metadata for which violation is known to always cause UB.
  • If the load is !noundef, keep all metadata, as this will turn poison-generating metadata into UB as well.

This fixes the miscompile that was exposed by D146629.

Diff Detail

Event Timeline

nikic created this revision.Apr 12 2023, 6:55 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2023, 6:55 AM
nikic requested review of this revision.Apr 12 2023, 6:55 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2023, 6:55 AM

Is the behavior of violating !dereferencable or !dereferencable_or_null also IUB? If so, these metadata can also be retained.

nikic added a comment.Apr 13 2023, 2:04 AM

Is the behavior of violating !dereferencable or !dereferencable_or_null also IUB? If so, these metadata can also be retained.

I think so, but this is currently not spelled out in LangRef and combineMetadata() doesn't treat it as such. I've put up https://reviews.llvm.org/D148202 to clarify this.

nikic updated this revision to Diff 513510.Apr 14 2023, 2:54 AM

Preserve dereferenceable and dereferenceable_or_null.

nikic edited the summary of this revision. (Show Details)Apr 14 2023, 2:55 AM
StephenFan accepted this revision.Apr 16 2023, 1:03 AM
StephenFan added inline comments.
llvm/lib/Transforms/Scalar/GVN.cpp
1008

For !noalias or !alias.scope, I think we don't care about the size and type. But it is also dropped. Maybe leave a TODO which says we can do better for these two metadata.

This revision is now accepted and ready to land.Apr 16 2023, 1:03 AM
This revision was automatically updated to reflect the committed changes.