Stream: general

Topic: Bad x86 codegen for mem/reg adds?


view this post on Zulip Jan Katzer (Aug 15 2024 at 17:16):

Hi guys,
I'm writing a little toy brainf*ck compiler but I'm having some trouble with x86 codegen

Since brainf*ck primarily uses a big u8 array for storage, I'm emitting lots of

let old_val = b.ins().load(types::I8, mem_flags, real_ptr, static_offset_accum);
let new_val = b.ins().iadd_imm(old_val, insn_val as i64);
b.ins().store(mem_flags, new_val, real_ptr, static_offset_accum);

unfortunately these seem to get turned into manual loads and stores:

movzx  rax,BYTE PTR [rcx+0x8000]
add    eax,0x1
mov    BYTE PTR [rcx+0x8000],al

I would have expected these to get turned into ADD r/m8, r8, i.e. something like

add BYTE PTR [rcx+0x8000], 0x1

Does somebody have some insight into this? Seems odd to me that the x86 backend wouldn't clean that up

view this post on Zulip Chris Fallin (Aug 15 2024 at 17:21):

we have lowering rules for the 32 and 64 bit cases: https://github.com/bytecodealliance/wasmtime/blob/c7756bd2654fe1ac795f9dd9d13de41a31cafbac/cranelift/codegen/src/isa/x64/lower.isle#L3154-L3278

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 (Aug 15 2024 at 17:21):

it seems someone just needs to add this for the narrower cases; no reason why not other than "someone needs to do it" :-)

view this post on Zulip Jan Katzer (Aug 15 2024 at 17:35):

Ah, thanks for the pointer :D

Seems like a good opportunity to learn how ISLE works

view this post on Zulip Jan Katzer (Aug 15 2024 at 19:52):

This seems to be a bit of a bigger challenge than I hoped, since mem/imm instructions are not supported AT ALL by the x86 backend AFAICT

view this post on Zulip Chris Fallin (Aug 15 2024 at 19:57):

Ah, yes, we've added encodings as we've needed them, so that's a bit of work to fill that out

view this post on Zulip Jan Katzer (Aug 15 2024 at 19:58):

No worries, makes me do something and actually learn how it works :D

view this post on Zulip Jan Katzer (Aug 15 2024 at 19:59):

Quick question, what would be the best type for immediates in ISLE? I've seen i32 be used as a "general type" but I can't figure out how to get one from an iconst or whatever the second operand should be

view this post on Zulip Chris Fallin (Aug 15 2024 at 20:01):

we've tended to use u64s and should have an extractor that converts from iconst's parameter (u64_from_imm64 or somesuch? can't look at the moment)


Last updated: Nov 22 2024 at 16:03 UTC