More ptr metadata gvn by scottmcm · Pull Request #126541 · rust-lang/rust (original) (raw)

There's basically 3 parts to this PR.

  1. Allow references as arguments to UnOp::PtrMetadata

This is a MIR semantics addition, so
r? mir

Rather than just raw pointers, also allow references to be passed to PtrMetadata. That means the length of a slice can be just PtrMetadata(_1) instead of also needing a ref-to-pointer statement (_2 = &raw *_1 + PtrMetadata(_2)).

AFAIK there should be no provenance or tagging implications of looking at the metadata of a pointer, and the code in the backends actually already supported it (other than a debug assert, given that they don't care about ptr vs reference, really), so we might as well allow it.

  1. Simplify the argument to PtrMetadata in GVN

Because the specific kind of pointer-like thing isn't that important, GVN can simplify all those details away. Things like *const-to-*mut casts and &mut-to-& reborrows are irrelevant, and skipping them lets it see more interesting things.

cc @cjgillot

Notably, unsizing casts for arrays. GVN supported that for Len, and now it sees it for PtrMetadata as well, allowing PtrMetadata(pointer) to become a constant if that pointer came from an array-to-slice unsizing, even through a bunch of other possible steps.

  1. Replace NormalizeArrayLen with GVN

The NormalizeArrayLen pass hasn't been running even in optimized builds for well over a year, and it turns out that GVN -- which is on in optimized builds -- can do everything it was trying to do.

So the code for the pass is deleted, but the tests are kept, just changed to the different pass.

As part of this, LowerSliceLen was changed to emit PtrMetadata(_1) instead of Len(*_1), a small step on the road to eventually eliminating Rvalue::Len.