Stream: cranelift

Topic: ✔ cranelift mem2reg


view this post on Zulip yang yu (May 09 2023 at 07:21):

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.

view this post on Zulip Notification Bot (May 09 2023 at 13:06):

This topic was moved here from #general > cranelift mem2reg by Till Schneidereit.

view this post on Zulip fitzgen (he/him) (May 09 2023 at 15:26):

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.

A fast and secure runtime for WebAssembly. Contribute to bytecodealliance/wasmtime development by creating an account on GitHub.

view this post on Zulip Chris Fallin (May 09 2023 at 15:54):

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

view this post on Zulip bjorn3 (May 09 2023 at 18:27):

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.

view this post on Zulip yang yu (May 10 2023 at 02:39):

@fitzgen (he/him) @Chris Fallin Thanks for the information.

view this post on Zulip yang yu (May 10 2023 at 02:44):

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?

view this post on Zulip yang yu (May 10 2023 at 06:23):

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.

view this post on Zulip Chris Fallin (May 10 2023 at 15:58):

@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

view this post on Zulip yang yu (May 10 2023 at 22:57):

@Chris Fallin Thanks for the details.

view this post on Zulip Notification Bot (May 10 2023 at 22:57):

yang yu has marked this topic as resolved.


Last updated: Oct 23 2024 at 20:03 UTC