I learned about compiler usual contains a pass call mem2reg
recently , why I did see cranelift have this pass.
This pass should have high impact to performance.
This topic was moved here from #general > cranelift mem2reg by Till Schneidereit.
we sort of do some of this already in the alias analysis pass: https://github.com/bytecodealliance/wasmtime/blob/ec92f8e480f692bd32fd10723135445814601a05/cranelift/codegen/src/alias_analysis.rs#L1-L62
note that it is pretty hard in general to prove that some pointer is never aliased and can be promoted to register, it is a lot easier on the compiler (ie produces better code) for frontends to make as much a virtual register as they can, and allow register allocation to spill to the stack in memory as it needs to.
In addition to that, I'd note that at least in the context of Wasm compiled through Cranelift, Wasm has a semantic notion of "locals" that we do translate directly to SSA values. LLVM producing Wasm will normally do its own mem2reg and put as many values into Wasm locals as possible, so we get some benefit indirectly that way
I don't think a full mem2reg opt pass is possible without pointer provenance. Without pointer provenance amotherother thread is allowed to guess the address of the stack slot and access it directly, thus preventing removal of reads and writes to the stack.
@fitzgen (he/him) @Chris Fallin Thanks for the information.
bjorn3 said:
I don't think a full mem2reg opt pass is possible without pointer provenance. Without pointer provenance amotherother thread is allowed to guess the address of the stack slot and access it directly, thus preventing removal of reads and writes to the stack.
Is this means if a front compile think a pointer should on stack, And we can't promote it to register safety?
mem2reg
is a complex problem, the value on stack can pass to somewhere else by pointer to stack, So the store on stack is hard to omit,
Somehow there must be some static analyze to omit extra load from memory I think.
@yang yu yes, exactly, there is a thing called "escape analysis" that a compiler needs to do for that. If a pointer to the stack value exists, then we can't convert it into a reg. If escape analysis proves that no pointer ever is taken, then we can skip the store to memory. LLVM does this, and Cranelift would have to do this if it removed stores. Right now our alias analysis-based optimizations remove loads, but never stores, so the value always actually exists on the stack
@Chris Fallin Thanks for the details.
yang yu has marked this topic as resolved.
Last updated: Nov 22 2024 at 17:03 UTC