This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine][DebugInfo] Fold constants wrapped in metadata
ClosedPublic

Authored by dstenb on Jan 29 2020, 7:51 AM.

Details

Summary

When constant folding, constants that are wrapped in metadata were not
folded. This could lead to dbg.values being the only user of a constant
expression, due to the non-dbg uses having been rewritten, resulting in
the constant later on being removed by some other pass. This occurred
with the attached test case, in which the non-rewritten GEP in the
dbg.value intrinsic was later on removed by globalopt.

This patch makes the code look through metadata and fold such constants.

I guess that we in the future may want to allow dbg.values using GEPs and
other constant expressions to be emittable even if there are no non-dbg
uses, but for example SelectionDAG does not support that.

Diff Detail

Event Timeline

dstenb created this revision.Jan 29 2020, 7:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 29 2020, 7:51 AM
aprantl accepted this revision.Jan 29 2020, 9:01 AM

I think this makes sense. Thanks!

This revision is now accepted and ready to land.Jan 29 2020, 9:01 AM
vsk accepted this revision.Jan 29 2020, 9:04 AM
vsk added inline comments.
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
3571

I think this is the right way to implement this change at the moment. If a similar need comes up in other contexts, we might consider adding a helper API to Use e.g. std::pair<Value *, bool /* IsMetadataAsValue */> Use::getUnderlyingValue().

llvm/test/Transforms/InstCombine/constant-fold-metadata-wrapped.ll
16

Could you mention that the constant fold here is to turn an i32 GEP operand into i64? It's slightly tricky to spot.

davide accepted this revision.Jan 29 2020, 9:26 AM

LGTM with Vedant's comment addressed.

dstenb updated this revision to Diff 241407.Jan 30 2020, 4:16 AM

Address review comment.

dstenb marked 3 inline comments as done.Jan 30 2020, 4:19 AM
dstenb added inline comments.
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
3571

That sounds like a good future improvement! I now skimmed through the MetadataAsValue uses in lib/ to find some similar code pattern, but I did not find any.

This revision was automatically updated to reflect the committed changes.
dstenb marked an inline comment as done.
dstenb added a comment.EditedFeb 10 2020, 9:01 AM

I reverted this in 982944525c7706c4dee00042d5b7cf2f0d87804f. Downstream we encountered debug-invariance (different code with/without -g), which seems to be due to a pre-existing issue in GlobalOpt. I'll find a good upstream reproducer and write a PR for that.

EDIT: I want to clarify that I have been able to reproduce this upstream with an x86 reproducer; I just need to reduce the reproducer.

I reverted this in 982944525c7706c4dee00042d5b7cf2f0d87804f. Downstream we encountered debug-invariance (different code with/without -g), which seems to be due to a pre-existing issue in GlobalOpt. I'll find a good upstream reproducer and write a PR for that.

I wrote https://bugs.llvm.org/show_bug.cgi?id=44880 for that.