Stream: t-compiler/wg-learning

Topic: lecture on LLVM/codegen


mark-i-m (Nov 26 2019 at 20:32, on Zulip):

Hi @nagisa

nagisa (Nov 26 2019 at 20:32, on Zulip):

Hi.

mark-i-m (Nov 26 2019 at 20:32, on Zulip):

@WG-learning was hoping you could do a lecture about LLVM/codegen so we could add it to the rustc-guide

mark-i-m (Nov 26 2019 at 20:33, on Zulip):

It is one of the biggest long-standing gaps in the guide

mark-i-m (Nov 26 2019 at 20:33, on Zulip):

Alternately, we could do some sort of discussion here on zulip if you prefer that

mark-i-m (Nov 26 2019 at 20:33, on Zulip):

like we did with macros/hygiene with petrochenkov:

nagisa (Nov 26 2019 at 20:33, on Zulip):

I don’t have the means to record a lecture, sadly.

mark-i-m (Nov 26 2019 at 20:34, on Zulip):

https://rust-lang.zulipchat.com/#narrow/stream/196385-t-compiler.2Fwg-learning/topic/macros.20discussion

nagisa (Nov 26 2019 at 20:34, on Zulip):

Also LLVM and MIR→LLVM-IR codegen are two very distinct things and would deserve their separate two lectures.

mark-i-m (Nov 26 2019 at 20:34, on Zulip):

Hmm

mark-i-m (Nov 26 2019 at 20:35, on Zulip):

that's good to know

mark-i-m (Nov 26 2019 at 20:35, on Zulip):

Would you be able to do something like the macros discussion here on zulip? (like the link above)

mark-i-m (Nov 26 2019 at 20:35, on Zulip):

?

mark-i-m (Nov 26 2019 at 20:36, on Zulip):

We also understand if you are not able to

nagisa (Nov 26 2019 at 20:37, on Zulip):

I can write down some broad outline and point out where to look for more specifics. But also, again, codegen is very deceptively prone to look small. Do you have lectures on backend (linking, etc.)?

mark-i-m (Nov 26 2019 at 20:38, on Zulip):

That would be an excellent start!

mark-i-m (Nov 26 2019 at 20:38, on Zulip):

Unfortunately, we have nothing past the MIR opt

mark-i-m (Nov 26 2019 at 20:38, on Zulip):

nothing on monomorphization, LLVM IR generation, and very little on LLVM itself

mark-i-m (Nov 26 2019 at 20:44, on Zulip):

I have to go now. We would appreciate any information you can along in whatever format is most convenient for you. Please feel free to DM me

nagisa (Nov 26 2019 at 20:50, on Zulip):

Not gonna get anything earlier than some weekend.

Santiago Pastorino (Nov 27 2019 at 13:23, on Zulip):

I've been talking with @Alex Crichton (can do some LLVM) and @oli who can do some of monomorphization and type layout

Santiago Pastorino (Nov 27 2019 at 13:23, on Zulip):

could be good to coordinate, what would you cover @nagisa ?

Santiago Pastorino (Nov 27 2019 at 13:25, on Zulip):

also, @mw do you have time and can cover some part?

Santiago Pastorino (Nov 27 2019 at 13:27, on Zulip):

there's also https://github.com/rust-lang/rust/pull/54012 some people from there may know and be able to do something

nagisa (Nov 30 2019 at 21:38, on Zulip):

So, first, where do I see what we already have? I was planning to make a list of things that we should probably split into separate categories first.

nagisa (Nov 30 2019 at 21:39, on Zulip):

starting with "monomorpization collector" and ending with "backend/linker invocation/lto".

nagisa (Nov 30 2019 at 21:39, on Zulip):

There are few things I cannot cover in there, collector being one of those things.

mark-i-m (Nov 30 2019 at 21:48, on Zulip):

@nagisa Thanks!
This is all we have: https://rust-lang.github.io/rustc-guide/codegen.html
so any info you have would be wonderful

mark-i-m (Nov 30 2019 at 21:48, on Zulip):

I guess we should create a placeholder chapter for each of the categories you come up with

nagisa (Dec 01 2019 at 13:58, on Zulip):

https://gist.github.com/nagisa/a311a0dab09851397f266076130eefb6 some braindumping

Edd Barrett (Dec 05 2019 at 15:51, on Zulip):

Hi!

I had a thread where I was pretty much asking about the same kind of lecture:
https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/Lecture.20Series.20Suggesion.3A.20Monomorphisation

I think it'd be good to explain why monomorphisation occurs in the codegen, and not at the MIR level. This surprised me.

Thanks

Santiago Pastorino (Dec 05 2019 at 15:56, on Zulip):

cc @nagisa

centril (Dec 05 2019 at 16:23, on Zulip):

relevant: https://github.com/rust-lang/rust/issues/66969

mark-i-m (Dec 05 2019 at 17:17, on Zulip):

@Edd Barrett My understanding is that it sort of does happen at the MIR level. But first, we have to find out what to codegen (i.e. how do you codegen a Vec<T> without first knowing what T is?). So after the MIR is produced/checked/optimized, we monomorphize, then we generate code for the monomorphized versions.

Edd Barrett (Dec 05 2019 at 17:19, on Zulip):

@mark-i-m But you don't generate a new MIR body for each monomorphisation, do you?

mark-i-m (Dec 05 2019 at 17:43, on Zulip):

Not sure, but I don't think so. My understanding is that we only collect a list of concrete instantiations of each generic MIR body. We then iterate through the list and generate code with that concrete instance. Not 100% sure though

Edd Barrett (Dec 05 2019 at 17:44, on Zulip):

That's in line with my understanding.

nagisa (Dec 05 2019 at 18:12, on Zulip):

We figure out the necessary concrete instantiations as part of collector and codegen unit distribution (occurs right before we lower MIR into LLVM) and then for each such instantiation we run a separate lowering pass. Lowering then monomorphises various generic statements etc. within MIR as it goes.

nagisa (Dec 05 2019 at 18:17, on Zulip):

So yes, the descriptions above seem fairly on-point :slight_smile:

nagisa (Dec 05 2019 at 18:19, on Zulip):

Perhaps it is also good to mention why this decision has been made, because it is a tradeoff – we anticipate MIR to be optimised as far as it being generic allows first, so that we end up spending less time on lowering monomorphic forms and reducing the workload LLVM has to handle. That being said there’s nothing preventing us from having what @eddyb calls LIR which is essentially MIR but monomorphic.

nagisa (Dec 10 2019 at 13:48, on Zulip):

Turns out the collector has some nice documentation https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/monomorphize/collector/index.html already

Edd Barrett (Dec 10 2019 at 14:31, on Zulip):

It might also be useful to state the difference between "declaring" and "defining".

Last update: Dec 12 2019 at 01:40UTC