YjyJeff opened issue #6437:
After reading the Overall Structure section in IR, I found the
sum
variable is explicitly placed in the stack withss0 = explicit_slot 32
. However, the loop induction variablei
is not placed explicitly placed in the stack. The document said:Such variables can also be presented to Cranelift as [stack slot]s. Stack slots are accessed with the
stack_store
andstack_load
instructions, and can have their address taken withstack_addr
, which supports C-like programming languages where local variables can have their address taken.I am confused about when should we create a
StackSlot
. What is the advantage ofStackSlot
overVariable
likei
in the above? In my view, all of them are in the stack, there is no difference here. I searched a lot and did not find the answer. Could anyone explain it?Thanks in advance.
cfallin commented on issue #6437:
I think the main point of confusion is
In my view, all of them are in the stack
which is not quite true: in the example you linked,
i
is never stored on the stack; it exists only as SSA values. That is exactly the difference:StackSlot
is an allocation of memory, and you can load and store to it whenever you like;Variable
is an abstraction incranelift_frontend
that buildsValue
s, which are values computed in your program that may only live in registers. You can load a stack-slot to get a value, and you can store a value to a stack-slot; but a stack-slot itself is not a value.
cfallin edited a comment on issue #6437:
I think the main point of confusion is
In my view, all of them are in the stack
which is not quite true: in the example you linked,
i
is never stored on the stack; it exists only as SSA values. That is exactly the difference:StackSlot
is an allocation of memory, and you can load and store to it whenever you like;Variable
is an abstraction incranelift_frontend
that buildsValue
s, which are values computed in your program that might only live in registers (the register allocator might also "spill" them to the stack but that's transparent to you). You can load a stack-slot to get a value, and you can store a value to a stack-slot; but a stack-slot itself is not a value.
YjyJeff closed issue #6437:
After reading the Overall Structure section in IR, I found the
sum
variable is explicitly placed in the stack withss0 = explicit_slot 32
. However, the loop induction variablei
is not placed explicitly placed in the stack. The document said:Such variables can also be presented to Cranelift as [stack slot]s. Stack slots are accessed with the
stack_store
andstack_load
instructions, and can have their address taken withstack_addr
, which supports C-like programming languages where local variables can have their address taken.I am confused about when should we create a
StackSlot
. What is the advantage ofStackSlot
overVariable
likei
in the above? In my view, all of them are in the stack, there is no difference here. I searched a lot and did not find the answer. Could anyone explain it?Thanks in advance.
Last updated: Nov 22 2024 at 16:03 UTC