Stream: t-compiler/wg-mir-opt

Topic: handling `Rvalue::Ref`


Wesley Wiser (May 31 2019 at 12:20, on Zulip):

I'm trying to add support for const propagating Rvalue::Refs but I'm struggling to write a test case that doesn't involve handling Indirects as they aren't supported yet. As a simple example:

fn main() {
    *(&1);
}

&1 is promoted to a static which transforms the line into *(&promoted[0]) where promoted[0] is an Indirect which points to the static.

Is there a kind of value I can use which can't/wont' be promoted to a static?

oli (May 31 2019 at 12:22, on Zulip):

The question is whether there is anything that won't get promoted that we can const prop ;)

oli (May 31 2019 at 12:22, on Zulip):

like... unions field accesses aren't promoted, but we should probably not const prop them either

Wesley Wiser (May 31 2019 at 12:22, on Zulip):

Heh that's true

oli (May 31 2019 at 12:23, on Zulip):

function calls I guess?

oli (May 31 2019 at 12:23, on Zulip):

but that would require implementing function calls first :P

Wesley Wiser (May 31 2019 at 12:23, on Zulip):

as in inlining?

oli (May 31 2019 at 12:23, on Zulip):

or .... maybe... let me try sth

oli (May 31 2019 at 12:24, on Zulip):

no, not inlining, just having let x = some_const_fn(42); compute the result of some_const_fn(42); and put that directly into x instead of leaving the function call to llvm

Wesley Wiser (May 31 2019 at 12:26, on Zulip):

I tried something like:

fn test<'a>(_: &'a ()) -> &'a u32 {
    &1
}

but promotion still happens

oli (May 31 2019 at 12:28, on Zulip):

no I mean fn test() -> u32 { 1 } and then having let x = *&test();

Wesley Wiser (May 31 2019 at 12:28, on Zulip):

Which makes sense because 'static >= 'a forall 'a

oli (May 31 2019 at 12:28, on Zulip):

So... slightly stupic idea: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6d3a2431a59f57a9aa068e01400b5402

oli (May 31 2019 at 12:28, on Zulip):

not sure if that acutally survives const prop

oli (May 31 2019 at 12:29, on Zulip):

Like we should be able to const prop the dereferencing of ZST references and raw pointers

oli (May 31 2019 at 12:29, on Zulip):

for that matter... why on earth is dereferencing ZST raw pointers unsafe?

oli (May 31 2019 at 12:29, on Zulip):

oh

oli (May 31 2019 at 12:29, on Zulip):

they may have private fields

Wesley Wiser (May 31 2019 at 12:29, on Zulip):

Doesn't that just fall out of deref of raw pointers being unsafe?

Wesley Wiser (May 31 2019 at 12:30, on Zulip):

Looks like the compiler errors out on that before we get to running the ConstProp pass

oli (May 31 2019 at 12:30, on Zulip):

yea

oli (May 31 2019 at 12:30, on Zulip):

but if you change the &'static to just &, then it doesn't error out ;)

Wesley Wiser (May 31 2019 at 12:30, on Zulip):

The last pass I see running is QualifyAndPromoteConstants

oli (May 31 2019 at 12:31, on Zulip):

that was just for demonstrating that promotion doesn't run on it

oli (May 31 2019 at 12:31, on Zulip):

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=dedbbd59d4cea4532e38e573dd437fee

oli (May 31 2019 at 12:31, on Zulip):

@Wesley Wiser this is a compiling example

oli (May 31 2019 at 12:31, on Zulip):

that will never ever get promoted

Wesley Wiser (May 31 2019 at 12:37, on Zulip):

Oh

Wesley Wiser (May 31 2019 at 12:37, on Zulip):

It looks like &A gets promoted which introduces an Indirect which prevents const propagation right now

Wesley Wiser (May 31 2019 at 12:38, on Zulip):

I think the answer here is "wait until we can handle indirects" :slight_smile:

oli (May 31 2019 at 12:43, on Zulip):

argml

Wesley Wiser (May 31 2019 at 13:08, on Zulip):

What's that?

oli (May 31 2019 at 13:23, on Zulip):

an expression of grumbling :D

Last update: Nov 17 2019 at 06:55UTC