ilyas-mallah opened PR #15344 from ilyas-mallah:separate-constant-pool to bytecodealliance:main:
Part of #9402. With the new
separate_constant_poolsetting on (it's off by default), Cranelift returns each function's constants as a separate blob with relocations. Wasmtime puts them in a.wasmtime.constantssection between the page-aligned text and.eh_frame, as alexcrichton suggested. It's enabled with-C cranelift-separate_constant_pool=true.On AArch64 a constant load becomes an
adrpinto a fresh vreg and a load from it, as cfallin outlined, with a relocation for each.riscv64, s390x and Pulley return an error with the setting on, as do cranelift-jit and cranelift-object for a function with constants. Winch ignores it.
With the setting forced on locally, the wast suite and tests/all pass on x86-64, and the wast suite passes on AArch64 under qemu. I haven't run it on AArch64 hardware, macOS or Windows. With it off,
.textmatches main for the 28 Sightglass modules below. Most of the size change is page padding of the text.
Total .cwasmbytes, 28 Sightglass modulessetting off setting on change x86_64 Linux 14,229,528 14,219,568 -0.070% AArch64 Linux 18,525,504 18,461,288 -0.347% AArch64 macOS 14,265,664 14,266,984 +0.009%
ilyas-mallah removed the review request for wasmtime-compiler-reviewers from PR #15344.
ilyas-mallah requested fitzgen for a review on PR #15344.
ilyas-mallah requested wasmtime-compiler-reviewers for a review on PR #15344.
ilyas-mallah requested wasmtime-compiler-s390x-reviewers for a review on PR #15344.
ilyas-mallah requested wasmtime-core-reviewers for a review on PR #15344.
github-actions[bot] added the label cranelift:area:machinst on PR #15344.
github-actions[bot] added the label cranelift:area:x64 on PR #15344.
github-actions[bot] added the label cranelift:area:aarch64 on PR #15344.
github-actions[bot] added the label cranelift:module on PR #15344.
github-actions[bot] added the label cranelift:meta on PR #15344.
github-actions[bot] added the label wasmtime:api on PR #15344.
github-actions[bot] added the label cranelift on PR #15344.
:memo: fitzgen submitted PR review:
Overall seems reasonable to me, but I'm not super familiar with the details of the relocs involved. That's worth a separate once over from @cfallin or @alexcrichton
:speech_balloon: fitzgen created PR review comment:
Why is this PR changing the alignment of unwind info?
:speech_balloon: fitzgen created PR review comment:
This test doesn't really seem like it is carrying its weight.
ilyas-mallah updated PR #15344.
:memo: ilyas-mallah submitted PR review.
:speech_balloon: ilyas-mallah created PR review comment:
With the setting on,
.eh_framefollows the constants section instead of the page padding of.text, so it no longer starts on a page boundary and the old assert would fire. The constants section is padded to 16 bytes, so.eh_framestill starts 8 byte aligned, and I relaxed the assert to that. Nothing depends on the page here, sincepublishmakes the whole image read-only and only the text range executable. With the setting off the layout is unchanged.
cfallin commented on PR #15344:
Thanks for working this out!
That said, I'm not wild about the changes to
MachBuffer. We've tried to keep it as minimal as possible, and the "separate constant pool" feels like a glued-on duplication of some kinds of logic (e.g. fixups) and also does not update the informal-proof comments. We should try to avoid making changes toMachBufferif possible.Maybe instead we can have a separate buffer just for constants (perhaps another
MachBuffer) and then emit into that as well as part of theMachInstemit plumbing?We already have mechanisms for separate function bodies to have mutually-referring relocations that are resolved by the text-section builder; we could use the same mechanisms to resolve the relocs from the function-body buffers to the constant-pool buffer. That feels more in line with the current design, and less invasive / simplifies things overall.
ilyas-mallah commented on PR #15344:
Thanks for pointing at how calls between function bodies get resolved. You're right about the duplicated fixups and the missing proof comments, and constants as their own bodies fits the existing design better. I'll rework it that way, with each reference a relocation that the text section builder resolves like a call.
One correction on the second
MachBuffer:MachInstEmit::emitand x64'sAsmCodeSinkonly receive one&mut MachBuffer. Emit can't reach another without changing that signature or threading state through each backend. Constants also have no labels, fixups or islands, so a second buffer would hold only bytes. Sinceresolve_relochas no usable addend, each constant becomes its own labelled body.Would it be acceptable for
MachBufferto keep a small table of constant references (offset, reloc kind, addend, constant) next torelocsandtraps? It would have no fixup or island logic, and the proof comments stay as they are. The alternative is per-backend emit state. That leavesMachBufferalone but means plumbing it through each x64 and aarch64 emit site. I'd lean toward the table, but I'll do it your way.
cfallin commented on PR #15344:
One correction on the second MachBuffer: MachInstEmit::emit and x64's AsmCodeSink only receive one &mut MachBuffer. Emit can't reach another without changing that signature or threading state through each backend.
Yes, exactly, that's my suggestion: let's thread through another buffer (or an API wrapped around another buffer) to emit constants. In essence think of each
MachBufferas corresponding to one linker section; we now want to emit code+data into two different sections, so we need two of them.
ilyas-mallah commented on PR #15344:
Yes, exactly, that's my suggestion: let's thread through another buffer (or an API wrapped around another buffer) to emit constants. In essence think of each
MachBufferas corresponding to one linker section; we now want to emit code+data into two different sections, so we need two of them.Ah, yeah I didn't consider that. In that case, I'd carry it in each backend's
EmitState, which everyemitalready receives, so the signature stays.EmitStatemust beCloneandDebugandMachBufferisn't, so the state holds a small recorder andVCode::emitbuilds the real secondMachBufferat the end.MachBufferitself isn't edited, and code references are relocations the text builder resolves like calls between bodies.Does that match what you meant, or should I just change the
emitsignature to pass the buffer explicitly? Because from what I saw changing the signature would touch a few hundred call sites.
cfallin commented on PR #15344:
Let's not put the
MachBufferinEmitState: we don't want to make it clone because it could become large. Instead let's add the second&mut MachBufferto the signature foremit. Or alternately, perhaps wrap up the two buffers and the fixup state between them in some sort ofstruct MachBuffers<'a> { pub text: &'a mut MachBuffer, pub rodata: &'a mut MachBuffer, ... }with a method to associate a label in rodata with a relocation in text somehow.
ilyas-mallah commented on PR #15344:
Let's not put the
MachBufferinEmitState: we don't want to _make_ it clone because it could become large. Instead let's add the second&mut MachBufferto the signature foremit. Or alternately, perhaps wrap up the two buffers and the fixup state between them in some sort ofstruct MachBuffers<'a> { pub text: &'a mut MachBuffer, pub rodata: &'a mut MachBuffer, ... }with a method to associate a label in rodata with a relocation in text somehow.Sounds good, then I'll go with the
MachBuffersidea, so the emit calls stay as they are, since only the type ofsinkwould change. Does it work if the rodata buffer is only there when the setting is on, and Winch could just pass the text?
cfallin commented on PR #15344:
Ah, yes, maybe we keep the
MachBufferborrows private, make therodatafield anOption<&'a mut MachBuffer>, and then have methods on the wrapper to emit constants (which either uses the existing constant-pool feature in thetextbuffer or emits intorodata).
Last updated: Nov 19 2026 at 20:05 UTC