►
From YouTube: Making MIR Fly
Description
Mid-level Intermediate Representation (MIR) was introduced into the Rust compiler in early August 2016. One of the many benefits of MIR is it makes writing optimizations easier. In this presentation, Scott Carr describes his summer 2016 intern project on MIR optimizations.
See the Additional Links tab for more information.
Help us caption & translate this video!
http://amara.org/v/2FiE/
A
A
A
A
Okay,
so
I
need
to
do
some
background
before
I
jump
into
what
I
did
this
summer,
but
it's
gonna
be
pretty
brief
in
high
level,
so
I'll
go
fast,
just
so
that
we're
all
on
the
same
page.
So
we
start
from
the
rust
source,
which
Cameron
talked
about
rust
and
his
previous
presentation.
Basically,
it's
a
high
level
systems,
programming,
language
and
the
compiler
doesn't
just
compile
ross
line-by-line
into
machine
code.
It
goes
through
intermediate
steps.
A
A
So
what
did
we
do
recently?
Actually,
today,
the
Russ
team
flipped
the
switch
and
now
Mir
is
part
of
the
rest.
Compiler
and
Mir
stands
for
middle
intermediate
representation
and
it's
coincidentally,
the
same
name
of
a
spaceship.
That's
the
spaceship.
So
what
did
we
do?
There's
now
a
tiny
spaceship
in
the
compiler
and
it's
between
the
high-level
and
Elvia
Mir.
A
So
why
would
we
do
this?
What's
what's
Mir
good
for?
Basically,
it's
the
simple
core
of
Russ
it
getsu.
It
makes
things
explicit
that
were
not
explicit
in
breast
code
because
you
wouldn't
want
the
programmer
to
type
these
things
out,
always
like
types
of
variables
can
be
alighted
in
certain
cases
and
rest
and
the
mere
makes
them
all
explicitly
stated.
A
It
also
has
simplified
control
flow,
so
in
rust,
there's
a
few
different
ways
of
doing
loops.
There's
lots
of
different
kinds
of
expressions
for
control
flow
and
the
mere
changes
that
all
into
sort
of
mostly
go-to
based
control,
flow
graph
and
panics
and
drops
are
explicit
in
mirror.
So
a
panic
is
sort
of
like
when
something
goes
wrong
in
the
code
and
we
should
like
abort
the
program
and
drops
are
sort
of
like
this
variable
is
no
longer
used.
A
We
can
reclaim
its
memory
and
the
mirror
is
rather
than
like,
representing
the
syntax
and
characters
of
the
code.
It's
representing
things
in
a
control
flow
graph
that
you
can
follow
this
graph
of
how
the
code
is
going
to
flow
and
the
data
is
going
to
flow
in
the
program
logically,
and
the
real
reason
for
mirror
or
for
my
purposes,
is
at
least
it's
easier
to
write,
optimizations
in
a
middle
intermediate
representation,
then
on
the
source
language.
So
that's
why
I
use
it
for
my
project.
A
So
let's
I'll
show
you
an
example
that
gives
you
an
idea
of
an
optimization
a
compiler
might
do
so.
The
top
box
is
some
theoretical,
rust
code
and
it
defines
the
kind
of
silly
function,
but
the
bottom
would
be
is
sort
of
like
the
mirror
generated
for
that
code
and
in
a
mirror
and
in
rust.
Edition
is
checked
for
integer
overflow.
A
So
if
you
add
two
numbers
that
are
too
big
and
can't
be
held
in
the
representation
of
the
integer
that
you're
adding
it'll
give
your
air
and
it'll
detect
that
so
the
plus
signs
in
rust
code
are
translated
into
checked,
ads
and
mirror,
and
this
is
the
bottom
generated
code
is
one
way
that
you
could
do
it.
But
there's
not
only
one
mapping
right,
there's,
there's
other
ways
that
you
could.
You
could
define
this
the
same
function
and
so
to
jump
to
the
conclusion.
You
can
actually
optimize
this
code.
A
We
already
had
the
value
of
C
available
to
us,
which
is
actually
the
same
value
as
a
plus
B,
so
you
can
do
check
that
C
plus
C
and
you
get
rid
of
a
whole
check
tab
and
we
saved
one
optimization
or
one
instruction,
and
so
our
code
got
a
little
bit
faster
and
it
took
a
little
bit
less
energy
to
execute
and
also
we
potentially
saved
a
stack
allocation
for
the
temporary
variable.
So
we
save
some
memory
too.
So
that's
like
what
we
want
to
do
as
compiler
optimization
writers.
A
So
we
did
it
yay
now
that
was
an
example
of
an
optimization
I'm,
going
to
talk
more
concretely
about
the
things
that
I
really
did
in
my
internship.
So
I
worked
on
generating
mirror
for
switch
arms.
I
worked
on
some
generic
graph,
algorithms
that
are
useful
in
a
lot
of
different
compiler.
Optimizations
I
worked
on
a
test
framework
for
testing
these
optimizations
and
I'll
talk
about
one
optimization.
A
So
the
first
step
would
be
like
you
want
if
you're
gonna
check
both.
Maybe
you
check
the
first
one
first,
so
you
check
if
X
dot,
0
the
first
field
of
that
car.
The
first
component
of
X
is
a
and
that's
what
BB
0
is
on
the
right.
So
then,
once
we've
done
2
BB
0.
We
need
to
generate
code
in
it
and
we
say:
ok
if
X
is
really
a
I
need
to
check.
A
A
Now
I'll
talk
about
some
of
the
graph
algorithms,
so
my
mentor
Nico
had
a
graph
algorithm
library
that
he
had
implemented
and
I
sort
of
ported
it
over
to
use
the
compilers
internal
data
structures,
and
this
is
just
showing
a
common
example
of
a
graph
algorithm
that
you
wanted
a
compiler
for
dominators.
Basically,
a
basic
black
dominates
another
basic
black.
If
the
control
hole
will
have
to
go
through
this
other
black,
it's
it's
just
a
general
piece
of
information.
You
want
to
know
about
code
that
you're
optimizing
in
a
lot
of
different
compiler
contexts.
A
So
now
I
was
working
on
these
optimizations
and
I
thought.
How
do
we
know
that
they're
right
and
one
way
is
to
just
like
assume
that
I'm
really
smart
or
whoever
is
writing?
That
position
is
super
smart
and
it's
just
gonna
do
the
right
thing
and
I
was
like
well,
maybe
that's
not
the
best
idea.
I,
don't
really
trust
myself,
so
we
decided
to
do
some
testing
framework
for
mirror
optimizations
and
the
cool
thing
about
contributing
to
rust.
Is
that
it's
all
in
the
open?
So
you
can
just
send
in
your
pull
request
and
it'll.
A
Merge
your
pull
request
if
it
can
and
run
the
whole
continuous
integration
testing
suite
against
your
pull
request
and
see
if
it's
actually
gonna
pass
all
the
tests
and
if
it
does
pass
all
the
tests,
then
that
gives
the
maintainer
or
the
internal
team
like
pretty
good
confidence
that
your
code
is
at
least
sort
of
okay.
So
we
want
to
extend
this
to
optimizations
too
and
say
you
know,
give
you
a
way
of
testing
your
optimizations
so
that
when
some
stranger
on
the
internet
comes
and
says,
I
have
a
great
idea
for
this
optimization.
A
You
can
say:
okay,
write
a
test,
that's
sort
of
like
makes
us
believe
that
it's
good
and
it's
a
really
cool
aspect
of
contributing
to
rest
and
open
source
in
general
that
you
can
get
your
full
across
in
and
see
if
they
really
work
on
the
real
codebase
right
away.
So
the
last
thing
that
I'm
going
to
talk
about
is
move
up
propagation,
which
is
a
optimization
that
I
implemented.
This
is
gonna,
both,
hopefully
eliminate
temporary
variables,
which
will
save
us
memory
and
eliminate
some
copying.
A
So
the
the
general
idea
is
sort
of
like
you
have
a
temporary
variable
temp
to
0
and
you
assign
something
to
at
any
expression
and
then
you
don't
use
temporary
0
ever
and
you
assign
it
to
some
other
variable.
So
really
you
never
needed
this
temporary.
You
could
have
just
assigned
directly
to
the
destination
variable
first,
and
this
seems
pretty
straightforward
when
you
just
have
a
few
statements,
but
the
hard
part
is
sort
of
like
figuring
out
when
this
opposition
is
really
safe
so
with
like.
A
A
You
have
it
actually
strengthens
your
other
optimizations,
so
we'll
I'm
working
right
now
on
diego's
agarose,
HD
aggregation
which
breaks
a
struct
in
two
parts
instead
of
assigning
to
the
whole
struct
in
one
big
chunk,
and
so
that
gives
you
the
opportunity
to
optimize
the
temporaries
that
are
the
fields
of
the
struct
and
more
complex
expressions
that
are
assigned
to
dust.
So
this
is
my
call
to
action
slide
if
you
feel
inspired
by
my
talk.
Well,
maybe
not,
but
maybe
you
just
like
rust.
A
In
spite
of
my
mediocre
talk,
you
should
go
to
these
websites
and
you
can
get
involved.
You
can
go
on
IRC.
You
can
submit
a
pull
request
against
the
you,
can
fork,
rustling,
slash,
rust
and
then
submit
your
pull
crest
and
the
Travis
will
test
the
whole
thing
and
tell
you
if
it
works
or
that
discussion
is
on
internals
wrestling.
That's
a
pretty
cool
place
to
hang
out
and
discuss
things
about
rest
and
my
contact
information
is
there.
Thank
you,
everyone
at
Mozilla,
it's
been
awesome
and
I'll.
Do
questions.