YjyJeff opened issue #6437:
After reading the Overall Structure section in IR, I found the
sumvariable is explicitly placed in the stack withss0 = explicit_slot 32. However, the loop induction variableiis 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_storeandstack_loadinstructions, 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 ofStackSlotoverVariablelikeiin 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,
iis never stored on the stack; it exists only as SSA values. That is exactly the difference:StackSlotis an allocation of memory, and you can load and store to it whenever you like;Variableis an abstraction incranelift_frontendthat buildsValues, 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,
iis never stored on the stack; it exists only as SSA values. That is exactly the difference:StackSlotis an allocation of memory, and you can load and store to it whenever you like;Variableis an abstraction incranelift_frontendthat buildsValues, 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
sumvariable is explicitly placed in the stack withss0 = explicit_slot 32. However, the loop induction variableiis 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_storeandstack_loadinstructions, 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 ofStackSlotoverVariablelikeiin 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: Dec 13 2025 at 19:03 UTC