If I create a block but don't use it, or it becomes unused later (maybe some optimization merges blocks), what happens to the actual block? Is it ever "collected"? I think not, as otherwise mapping a Block to the actual block details won't be as easy (an array indexing).
The Block itself (aka the index) is not reused. Optimizations can remove the block itself from the layout though, in which case there all instructions are removed from it.
OK so in the vector that maps Blocks (indices) to the actual Block data, the slot for the block is left unused?
Yes. The Layout struct is defined in https://github.com/bytecodealliance/wasmtime/blob/51f9ac2150bd880dc7b92952ba92e72ea3ab60b7/cranelift/codegen/src/ir/layout.rs by the way.
Thanks for the link, really helpful.
I'm curious, is this ever a problem in practice? That is, the unused slots in secondary maps every become large enough to be problematic?
Probably not in the common case. Cranelift is made to compile one function at a time, so peak memory usage is relatively small compared to for example LLVM anyway.
Last updated: Dec 06 2025 at 06:05 UTC