Stream: t-compiler/wg-mir-opt

Topic: what mir-opts matter?


Xavier Denis (Jul 22 2020 at 10:25, on Zulip):

Writing optimizations is always fun, i love seeing test cases shrink but for rustc it seems like the real objective is to pre-chew MIR so that LLVM will have less to do. In that context, do we have any idea what actually matters? or are we just hoping that doing enough optimizations in rustc will cut down the emitted IR enough to make compilation faster?

Sorry if this has been answered but I didn't find anything on this in the compiler-team repo

Xavier Denis (Jul 22 2020 at 10:25, on Zulip):

P.S. if there are more issues, I'd be happy to work on them

oli (Jul 22 2020 at 13:06, on Zulip):

well, we aren't just hoping, we're seeing this happening, but we don't have a structured approach for finding optimizations that will have an effect

oli (Jul 22 2020 at 13:07, on Zulip):

in some cases it's not even about llvm, but about shrinking MIR so later mir passes call fewer queries, which definitely has had an effect in the past

Xavier Denis (Jul 22 2020 at 14:43, on Zulip):

i noticed that we don't currently do the optimizations described in Stacked Borrows and LLVM doesn't seem to pick up on them either. Is this something on the roadmap? specifically thinking of things like *x = 5; *y = 10; do_something_with_x(*x)

Félix Fischer (Jul 22 2020 at 14:44, on Zulip):

ohh, that's very interesting if LLVM doesn't do that

Xavier Denis (Jul 22 2020 at 14:44, on Zulip):

https://rust.godbolt.org/z/s4od4W

Félix Fischer (Jul 22 2020 at 14:45, on Zulip):

do you have a playground link with that example?

Félix Fischer (Jul 22 2020 at 14:45, on Zulip):

oh

Félix Fischer (Jul 22 2020 at 14:45, on Zulip):

nice

Jonas Schievink (Jul 22 2020 at 14:45, on Zulip):

Pretty sure that's just the noalias issue

Félix Fischer (Jul 22 2020 at 14:45, on Zulip):

(PS: sometimes I use playgrounds because you can directly ask for the MIR output :D)

Xavier Denis (Jul 22 2020 at 14:45, on Zulip):

im guessing this case could be picked up by NRVO but it can be generalized as well

Xavier Denis (Jul 22 2020 at 14:45, on Zulip):

yea but I wanted to see if LLVM was doing something clever afterwards

simulacrum (Jul 22 2020 at 14:46, on Zulip):

you need -Zmutable-noalias, yeah

simulacrum (Jul 22 2020 at 14:46, on Zulip):

LLVM is buggy with it sometimes though which is why it's (still) off by default

Félix Fischer (Jul 22 2020 at 14:46, on Zulip):

yeah, without noalias there's not much that can be done I reckon

Félix Fischer (Jul 22 2020 at 14:46, on Zulip):

so fwiw LLVM is working on this atm

Félix Fischer (Jul 22 2020 at 14:46, on Zulip):

some people in the community have picked up the slack there and have made a sorts of working group for noalias

Félix Fischer (Jul 22 2020 at 14:47, on Zulip):

there are fortran folks there, which is super interesting. Fortran of course is one of the most prominent use cases for noalias, together with Rust

Félix Fischer (Jul 22 2020 at 14:47, on Zulip):

imma send you links

Xavier Denis (Jul 22 2020 at 14:47, on Zulip):

simulacrum said:

LLVM is buggy with it sometimes though which is why it's (still) off by default

that makes sense. i guess we could also do this in MIR though

Félix Fischer (Jul 22 2020 at 14:48, on Zulip):

Okay so they get together around once a month

Félix Fischer (Jul 22 2020 at 14:48, on Zulip):

https://docs.google.com/document/d/1ybwEKDVtIbhIhK50qYtwKsL50K-NvB6LfuBsfepBZ9Y/edit#heading=h.vpxs8lkuxy79

Félix Fischer (Jul 22 2020 at 14:48, on Zulip):

that document tracks everything more or less

Jonas Schievink (Jul 22 2020 at 14:48, on Zulip):

This doesn't look like a typical NRVO pattern

Félix Fischer (Jul 22 2020 at 14:48, on Zulip):

you can scroll to see the details of every meeting

Félix Fischer (Jul 22 2020 at 14:48, on Zulip):

one can join them as well

Félix Fischer (Jul 22 2020 at 14:50, on Zulip):

If you look at the mailing list once a week you can find new posts regarding this: http://lists.llvm.org/pipermail/llvm-dev/2020-July/143388.html (that's the doodle poll for the last meeting, which happened about a week ago)

Xavier Denis (Jul 22 2020 at 14:50, on Zulip):

Jonas Schievink said:

This doesn't look like a typical NRVO pattern

that's what I thought but it is described as the first example in Stacked Borrows, so it seems like it should be valid for Rust

Félix Fischer (Jul 22 2020 at 14:51, on Zulip):

Anywho, I'm super excited for this work. Having noalias actually work in LLVM could bring Rust code beyond C performance in many, many use cases

Félix Fischer (Jul 22 2020 at 14:51, on Zulip):

(just like Fortran! XD)

Jonas Schievink (Jul 22 2020 at 14:52, on Zulip):

Yeah this is definitely optimizable, but not via NRVO

Félix Fischer (Jul 22 2020 at 14:52, on Zulip):

Xavier Denis said:

Jonas Schievink said:

This doesn't look like a typical NRVO pattern

that's what I thought but it is described as the first example in Stacked Borrows, so it seems like it should be valid for Rust

It is! In fact if you think about it, Rust can guarantee lack of aliasing in like, most parts of all programs. Thanks to the borrow checker and all that

Xavier Denis (Jul 22 2020 at 14:53, on Zulip):

@Félix Fischer yes but to justify the optimization we must also consider how unsafe code could potentially break it

Félix Fischer (Jul 22 2020 at 14:54, on Zulip):

It's just that we (re?)discovered a noalias optimization bug in LLVM with the amount of non aliasing pairs of references that can be created in Rust. And after that discovery, it was disabled in rustc

Félix Fischer (Jul 22 2020 at 14:54, on Zulip):

Xavier Denis said:

Félix Fischer yes but to justify the optimization we must also consider how unsafe code could potentially break it

ahhhh I see

Félix Fischer (Jul 22 2020 at 14:55, on Zulip):

Okay since I don't know enough about this, and considering that we're already talking about Stacked Borrows, imma summon @RalfJ to the conversation :3

Félix Fischer (Jul 22 2020 at 14:56, on Zulip):

He probably knows what we can do wrt unsafe here

Xavier Denis (Jul 22 2020 at 14:56, on Zulip):

there's no real need

Xavier Denis (Jul 22 2020 at 14:57, on Zulip):

the paper already shows how this transformation is safe for programs that respect Stacked Borrows

Félix Fischer (Jul 22 2020 at 14:57, on Zulip):

But don't you mean that interior aliasing could break the optimization in unsafe code?

Félix Fischer (Jul 22 2020 at 14:57, on Zulip):

oh

Félix Fischer (Jul 22 2020 at 14:57, on Zulip):

OHHH

Félix Fischer (Jul 22 2020 at 14:57, on Zulip):

omg, that is amazing

Félix Fischer (Jul 22 2020 at 14:57, on Zulip):

holy crap

Xavier Denis (Jul 22 2020 at 14:57, on Zulip):

I was just curious if it we had implemented it / were implemnting it

Félix Fischer (Jul 22 2020 at 14:57, on Zulip):

I see

Félix Fischer (Jul 22 2020 at 14:57, on Zulip):

mmm... well I would honestly wait for LLVM to complete their move

Félix Fischer (Jul 22 2020 at 14:58, on Zulip):

From what I've seen in that "working group", they have a lot of momentum

Félix Fischer (Jul 22 2020 at 14:59, on Zulip):

but also I wonder if we can optimize some cases where LLVM could not, even if their noalias opt were sound

Félix Fischer (Jul 22 2020 at 14:59, on Zulip):

do you reckon we can?

Félix Fischer (Jul 22 2020 at 14:59, on Zulip):

(with regards to non-aliasing reference optimizations)

Wesley Wiser (Jul 22 2020 at 15:01, on Zulip):

I suspect one of the issues here is that while Stacked Borrows is a model of the borrowing system that has desirable properties, it's not the officially blessed one (yet).

Félix Fischer (Jul 22 2020 at 15:01, on Zulip):

@Wesley Wiser because the official semantics haven't been defined (yet)?

Wesley Wiser (Jul 22 2020 at 15:02, on Zulip):

Yeah AFAIK

Félix Fischer (Jul 22 2020 at 15:02, on Zulip):

mm, that makes sense

Wesley Wiser (Jul 22 2020 at 15:02, on Zulip):

I mean, it's very promising and I think it probably will be the eventual model

Wesley Wiser (Jul 22 2020 at 15:02, on Zulip):

But there's been no RFC yet to make that official

Wesley Wiser (Jul 22 2020 at 15:03, on Zulip):

There's also a much bigger focus on getting compile time improvements from mir-opts than runtime performance (in general).

Wesley Wiser (Jul 22 2020 at 15:03, on Zulip):

We generally don't get a lot of complaints about runtime performance especially in comparison to compile time performance.

Félix Fischer (Jul 22 2020 at 15:04, on Zulip):

Right, right

Félix Fischer (Jul 22 2020 at 15:05, on Zulip):

I mean I've heard from people running debug builds in production, happy with runtime performance, only to be blown away by the actual runtime performance of their program after recompiling to release mode

Wesley Wiser (Jul 22 2020 at 15:06, on Zulip):

Yeah, that's always amusing.

Wesley Wiser (Jul 22 2020 at 15:06, on Zulip):

Some of us come from ecosystems where Release mode is +30% or something. Not 100x faster.

lzutao (Jul 22 2020 at 15:33, on Zulip):

Félix Fischer said:

Anywho, I'm super excited for this work. Having noalias actually work in LLVM could bring Rust code beyond C performance in many, many use cases

Not without fixing #74183. Pattern matching is used quite a lot in Rust code.

Félix Fischer (Jul 22 2020 at 16:11, on Zulip):

Oh wow. Fair enough.

Although you could say that both are needed :P

RalfJ (Jul 23 2020 at 12:01, on Zulip):

there's still some very interesting theoretical open questions here -- what exactly are LLVM's new noalias semantics (once they materialize), and can we relate them formally to Stacked Borrows?

RalfJ (Jul 23 2020 at 12:03, on Zulip):

looks like at some point I'll have to look for coauthors with LLVM experience to make that a paper ;)

Last update: Sep 28 2020 at 15:00UTC