This is an archive of the discontinued LLVM Phabricator instance.

[llvm-reduce] Reduce metadata references.
ClosedPublic

Authored by Meinersbur on Sep 27 2021, 5:06 AM.

Details

Summary

The ReduceMetadata pass before this patch removed metadata on a per-MDNode (or NamedMDNode) basis. Either all references to an MDNode are kept, or all of them are removed. However, MDNodes are uniqued, meaning that references to MDNodes with the same data become references to the same MDNodes. As a consequence, e.g. tbaa references to the same type will all have the same MDNode reference and hence make it impossible to reduce only keeping metadata on those memory access for which they are interesting.
Moreover, MDNodes can also be referenced by some intrinsics or other MDNodes. These references were not considered for removal leading to the possibility that MDNodes are not actually removed even if selected to be removed by the oracle.

This patch changes ReduceMetadata to reduces based on removable metadata references instead. MDNodes without references implicitly dropped anyway. References by intrinsic calls should be removed by ReduceOperands or ReduceInstructions. References in other MDNodes cannot be removed as it would violate the immutability of MDNodes.

Additionally, ReduceMetadata pass before this patch used setMetadata(I, NULL) to remove references, where I is the index in the array returned by getAllMetadata. However, setMetadata expects a MDKind (such as MD_tbaa) as first argument. getAllMetadata does not return those in consecutive order (otherwise it would not need to be a std::pair with first representing the MDKind).

Diff Detail

Event Timeline

Meinersbur created this revision.Sep 27 2021, 5:06 AM
Meinersbur requested review of this revision.Sep 27 2021, 5:06 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 27 2021, 5:06 AM
aeubanks accepted this revision.Sep 27 2021, 9:31 AM

thanks!

llvm/test/tools/llvm-reduce/remove-metadata-args.ll
12

should add a couple things to check for just to make sure we didn't accidentally reduce everything

This revision is now accepted and ready to land.Sep 27 2021, 9:31 AM
llvm/test/tools/llvm-reduce/remove-metadata-args.ll
14

Just wondering: given the spaces around the matcher, will this have the wanted effect ? Aka, if you give the checker the original input, will it fail ?

Meinersbur edited the summary of this revision. (Show Details)

Test case refinement

  • Add --delta-passes=metadata
  • Remove required necessary space after/before "Boring"
  • Add RUN lines for test self-consistency
aeubanks added inline comments.Sep 27 2021, 10:37 AM
llvm/test/tools/llvm-reduce/remove-metadata-args.ll
14

llvm-reduce should automatically check that the input is interesting and fail out if it isn't, so I don't think we need the extra RUN lines. We just need to check if the output is correct.

llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
85

should this be auto &I rather than auto &&I?

Meinersbur added inline comments.Sep 27 2021, 10:43 AM
llvm/test/tools/llvm-reduce/remove-metadata-args.ll
12

The RUN at line 2 tests this by checking the existence of all the "EXCITING" lines. I started using --check-prefixes=EXCITING,REDUCED but then I would add NOT lines between all the INTERESTING lines. Would something like REDUCED-DAG-NOT or REDUCED-NOT-DAG work?

14

The NOT expressions would match the NOT lines themselves. To appropriately test the test itself, I ran it through opt (see lines 4 and 5) to remove comments.

Turns out you were right with your suspicion. Boring {{.*}} requires a space after Boring, which e.g. is not the case for @BoringGlobal. I had the concern with spaces as well, but only considered the !md !0 which indeed is always followed/proceeded with a space.

I left line 4 and 5 in the patch for illustration. I could remove them again before committing.

  • Replace auto with concrete types.
Meinersbur marked 2 inline comments as done.
  • Remove self-consistency RUN lines
Meinersbur marked an inline comment as done.Sep 27 2021, 10:52 AM
aeubanks added inline comments.Sep 27 2021, 10:58 AM
llvm/test/tools/llvm-reduce/remove-metadata-args.ll
12

Actually I changed my mind, I think this is good enough. If the output isn't passing the interestingness test there's something seriously wrong

And the RUN at line 2 is redundant with the internal llvm-reduce check that the input is interesting
Generally I think just the llvm-reduce RUN and the FileCheck for the reduced file RUN are good enough

  • Remove llvm-reduce final output interestingness check
Meinersbur marked an inline comment as done.Sep 27 2021, 11:04 AM
Meinersbur marked an inline comment as done.
llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
61

I was curious if we should switch the order of the for/if on !O.shouldKeep(), but if I'm reading this correctly, it seems that if O.shouldKeep() == true, then we do no work in this function and should just return early? Then we can remove all of the conditional checks on !O.shouldKeep()?

aeubanks added inline comments.Sep 27 2021, 11:35 AM
llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
61

O.shouldKeep() is not constant, the whole point of llvm-reduce is that it returns true/false for different calls so we can try reducing various subsets of whatever we're trying to reduce since we may not be able to remove everything all at once, or even one at a time, things may need to be reduced in batches.

Meinersbur marked 2 inline comments as done.Sep 27 2021, 12:59 PM
Meinersbur added inline comments.Sep 27 2021, 1:23 PM
llvm/tools/llvm-reduce/deltas/ReduceMetadata.cpp
61

To add to @aeubanks explanation, Oracle::shouldKeep() is comparable to Google Benchmark's State::KeepRunning() or LLVM's OptBisect::shouldRunPass(). It controls loop behaviour depending on when/how often it has been called already without explicitly passing which loop iteration we are in.

swamulism accepted this revision.Sep 27 2021, 8:21 PM
This revision was landed with ongoing or failed builds.Sep 29 2021, 9:25 AM
This revision was automatically updated to reflect the committed changes.