Skip to content

cranelift: Turn AliasRegion into an entity stored in the DFG#13354

Open
fitzgen wants to merge 1 commit into
bytecodealliance:mainfrom
fitzgen:alias-region-entity
Open

cranelift: Turn AliasRegion into an entity stored in the DFG#13354
fitzgen wants to merge 1 commit into
bytecodealliance:mainfrom
fitzgen:alias-region-entity

Conversation

@fitzgen
Copy link
Copy Markdown
Member

@fitzgen fitzgen commented May 13, 2026

Generalize from

enum AliasRegion {
    Heap,
    Table,
    Vmctx,
}

into a proper entity, allowing users to define arbitrary numbers of custom alias
regions.

MemFlagsData grows a region: PackedOption<AliasRegion> field instead of
bitpacking the alias region with the rest of its flags.

The CLIF text format is also updated to support alias region declarations in the
function header, e.g. region0 = 0 "heap", with memflags referencing regions by
entity name instead of the old hardcoded keywords.

Depends on #13353

@fitzgen fitzgen requested review from a team as code owners May 13, 2026 18:34
@fitzgen fitzgen requested review from cfallin and removed request for a team May 13, 2026 18:34
@fitzgen fitzgen force-pushed the alias-region-entity branch from b094d0f to 29b3c7d Compare May 13, 2026 18:46
@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:meta Everything related to the meta-language. isle Related to the ISLE domain-specific language labels May 13, 2026
@github-actions
Copy link
Copy Markdown

Subscribe to Label Action

cc @cfallin, @fitzgen

Details This issue or pull request has been labeled: "cranelift", "cranelift:area:aarch64", "cranelift:meta", "isle"

Thus the following users have been cc'd because of the following labels:

  • cfallin: isle
  • fitzgen: isle

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

Copy link
Copy Markdown
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Some thoughts below but overall shape looks good. Happy this worked out w.r.t. compile-time overheads!

// We need to mutate `self` if this instruction accepts a value list, will construct
// BlockCall values, or has memflags operands (which need DFG insertion).
// We need to mutate `self` if this instruction accepts a value list, will construct
// BlockCall values, or has memflags operands (which need DFG insertion).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated comment (maybe a merge or rebase issue?)

Comment thread cranelift/codegen/src/ir/memflags.rs Outdated
// * 3 - big endian flag
// * 4 - checked flag
// * 5/6 - alias region
// * 5/6 - (unused, formerly alias region)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can just say "unused" here I guess -- no need to embed the history of the bits in the comment?

Comment thread cranelift/codegen/src/ir/memflags.rs Outdated
bits: u16,

/// The alias region for this memory operation, if any.
region: Option<AliasRegion>,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PackedOption maybe so None can fit in the reserved_value niche? I know this won't be as memory-critical today because it's dedup'd but if we end up with a lot of these it might still matter...

dedupe_map: HashMap<MemFlagsData, MemFlags>,
}

impl Hash for MemFlagsSet {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as we're manually implementing Hash we could do so for PartialEq/Eq as well for efficiency, since we can skip dedupe_map for that too (it's purely derived data), right?

Comment thread cranelift/codegen/src/ir/memflags.rs Outdated
/// Insert new mem flags into this set.
///
/// Returns an existing `MemFlags` if the data already exists.
pub fn insert(&mut self, data: MemFlagsData) -> MemFlags {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be the point in the API that's fallible, so we can propagate the u16-index-space allocation failure upward (e.g. to wasmtime-cranelift when it uses these for a user-controlled number of regions)?

&self.frame_layout
}

fn set_mem_flags(&mut self, mem_flags: crate::ir::MemFlagsSet) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a fairly awkward and inefficient bit of plumbing to get the MemFlagsSet down to the point of emission -- maybe we could pass a borrow into MachInst::emit instead?

Also, separately, it's a somewhat conspicuous layering violation: we have an IR type all the way down in the VCode. I'm not completely categorically opposed to that in all cases, but I wonder if we could lower MemFlagData to a "core" memflags actually semantically meaningful at the VCode layer (so, no alias regions any more, just trap info and the like) and use that in the backends?

Comment thread cranelift/codegen/src/alias_analysis.rs Outdated
// Store with no memflags: must clobber everything.
for (_ar, last) in self.regions.iter_mut() {
*last = inst.into();
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the last-fence becomes the fallback, could we clear regions here instead?

; block1:
; xone x0
; f1 = fload64 Slot(0) // flags = notrap aligned
; f1 = fload64 Slot(0) // flags =memflags0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's another example where we might benefit from having a VCode-specific "core memflags": the printed form here is relatively useless (refers to an index of an entity that we don't print anywhere else in the disassembly) whereas ideally we'd print the same as before, after lowering.

Generalize AliasRegion from a fixed enum (Heap/Table/Vmctx) into an entity
index with AliasRegionData containing a description string for printing.

MemFlagsData grows a region: PackedOption<AliasRegion> field instead of
bitpacking the alias region with the rest of its flags.

Alias regions are stored in a PrimaryMap<AliasRegion, AliasRegionData> in the
DFG. There is no deduplication: each push() creates a new region entity.
Users (e.g. Wasmtime) are responsible for caching and reusing their own alias
region entities.

The CLIF text format is updated to support region declarations in the function
header, e.g. region0 = "heap", with memflags referencing regions by entity
name instead of the old hardcoded keywords.
@fitzgen fitzgen force-pushed the alias-region-entity branch from 29b3c7d to 574b4dd Compare May 20, 2026 23:40
@fitzgen
Copy link
Copy Markdown
Member Author

fitzgen commented May 20, 2026

@cfallin okay I fixed the rebase, cleaned up the region clearing, and introduced MachMemFlags. Think this is ready for re-review!

@github-actions github-actions Bot added the cranelift:area:x64 Issues related to x64 codegen label May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:area:x64 Issues related to x64 codegen cranelift:meta Everything related to the meta-language. cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants