Closed
Description
Some intrinsic functions can be effectively implemented in pure MIR code. Notably, the wrapping and checked arithmetic functions, which can turn into regular BinaryOp
and CheckedBinaryOp
(when implemented) rvalues. Similarly intrinsics like move_val_init
can be exactly represented in the MIR.
While relatively low impact right now, due to most intrinsics being used via wrapper functions, if/when we get MIR-level inlining, this would allow those intrinsics to better participate in optimisations.
Activity
luqmana commentedon Apr 17, 2016
I had a branch that did exactly this a while back for transmute and a few others.
https://github.com/luqmana/rust/commits/mir-intrinsics
Aatch commentedon Apr 17, 2016
@luqmana I have a branch that I was experimenting with that did the lowering while building, which I think makes more sense in some situations, though an intrinsic lowering pass in other cases may also be useful.
aochagavia commentedon Apr 1, 2017
I would like to work on this, but don't know where to begin. Would it make sense to rebase @luqmana's branch first?
nagisa commentedon Apr 1, 2017
Rebasing probably makes little sense. It’s a very old branch approaching the problem from a direction that would likely not work anymore, due to how much MIR has changed over time. IMO it would be easier to begin from scratch.
aochagavia commentedon Apr 1, 2017
Thanks! I took a quick look at
core::intrinsics
andrustc_trans::intrinsic
to get a bit of background information. Just to ensure I got the basics right, let me know if the following statements are correct:mem::size_of
could be implemented as a normal function, so it is generated by the compiler.mem::transmute
seems like a good example, as it shouldn't generate any code at all. EDIT: I see it doesn't even exist inrustc_trans::intrinsic
).I have more questions, but I think I better make sure we are on the same line before I go on 😉
nagisa commentedon Apr 1, 2017
Rather than not benefiting, they inhibit them.
Everything else sounds about right.
aochagavia commentedon Apr 1, 2017
Right, so now on with more questions. When you say that @luqmana's direction won't work anymore, do you mean that it would be a bad idea to write a MIR transformation that lowers intrinsics? I cannot imagine a different way of implementing this.
nagisa commentedon Apr 1, 2017
MIR transformation will still work for most of the interesting intrinsics I think; although there may be a saner approach. What I mean is that the transformation in question was written a year ago, and MIR has changed so significantly since, that rebasing it properly would be equivalent (or even harder) than redoing it from scratch.
Also: when implementing this keep in mind the eventual move to EBBs.
aochagavia commentedon Apr 2, 2017
Loosely based on @luqmana's code I wrote the following: https://github.com/aochagavia/rust/commit/0f9a70b3567b76b8d06ac3fd5f38426512d26128
There are a couple of FIXMEs, though. I left comments on some lines. @nagisa could you take a look at them and point me in the right direction? That would be great!
steveklabnik commentedon May 17, 2019
Triage: is this bug still useful?