Skip to content

cranelift: restore explicit no-region for alias analysis - #14373

Merged
fitzgen merged 5 commits into
bytecodealliance:mainfrom
agourakis82:users/agourakis/cranelift-regionless-load-alias-fence
Nov 25, 2026
Merged

fitzgen merged 5 commits into
bytecodealliance:mainfrom
agourakis82:users/agourakis/cranelift-regionless-load-alias-fence

Conversation

@agourakis82

@agourakis82 agourakis82 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Regionless stores were being treated as fences over every named region. That was a behavior change from the older alias analysis, where no-region was its own disjoint bucket for both loads and stores.

This restores that older shape:

  • regionless store → other only
  • regionless load → other only
  • true fences still touch every bucket

Also updates the docs/memflags wording and the alias tests that had encoded the fence-for-stores behavior (issue-13508, no-region, etc.).

For #14323: under explicit no-region, mixing a regionless access and a named-region access on the same address is outside the frontend invariant. Happy to close that issue once this lands, or rework again if the meeting picks the other rule.

Fixes #14323

Assisted-by: Claude

@agourakis82
agourakis82 requested a review from a team as a code owner September 22, 2026 02:32
@agourakis82
agourakis82 requested review from fitzgen and removed request for a team September 22, 2026 02:32
@github-actions github-actions Bot added the cranelift Issues related to the Cranelift code generator label Sep 22, 2026
@agourakis82

agourakis82 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks. Parking this on the meeting.

#14373 follows the issue report’s reading (no-region load can see a named-region store). If the call is explicit no-region, I’ll rework/close it.

@agourakis82

agourakis82 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Wrote up the store/load asymmetry on the issue.

@agourakis82

agourakis82 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Better summary of the two options is here: #14323 (comment)

@agourakis82
agourakis82 force-pushed the users/agourakis/cranelift-regionless-load-alias-fence branch from a482899 to db762f6 Compare September 22, 2026 15:22
@agourakis82

agourakis82 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased on main.

Tried the explicit-no-region store change locally; notes on the issue.

@agourakis82

agourakis82 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor Author

#14323 (comment)

@agourakis82

agourakis82 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Parking on the meeting: explicit no-region vs no-region as fence. Fine either way.

Regionless stores had been treated as fences over every named region.
That disagreed with the older alias-analysis behavior, where no-region
was its own disjoint bucket for both loads and stores, and it disagreed
with how regionless loads already worked.

Put regionless stores back in the "other" bucket, keep loads on that
same bucket, and leave true fences as the only thing that touches every
region. Docs and alias tests updated to match.

Assisted-by: Claude
@agourakis82
agourakis82 force-pushed the users/agourakis/cranelift-regionless-load-alias-fence branch from db762f6 to 962fcf8 Compare September 22, 2026 17:11
@agourakis82 agourakis82 changed the title cranelift: treat regionless loads as aliasing all regions cranelift: restore explicit no-region for alias analysis Sep 22, 2026
Preserve the existing PR commits and integrate the pinned upstream changes
without rewriting the branch history.

Validate merged tree c76532a on Linux x86-64
with Rust 1.98.0: locked clif-util build, cargo fmt, the 50 alias filetests,
and the six backend filetests added or changed since the previous base.
Additional private regionless join and loop probes also passed; these are
not part of the upstream test suite. This is scoped validation, not full
cross-platform CI or maintainer approval.

Assisted-by: OpenAI Codex
Preserve volatile accesses as fences while integrating upstream alias-region extent handling. Align the issue-13508 initializer with its named-region contract and correct regionless-slot comments.

Native validation: 52 alias fixtures, three focused regionless join/loop/DSE fixtures, selected volatile and readonly cases, and 235 codegen unit tests. A volatile-guard mutant is rejected; restoring the source reproduces the original compiler artifact.

Assisted-by: Codex:GPT-6.1-Sol
@github-actions

This comment was marked as outdated.

@github-actions github-actions Bot closed this Nov 20, 2026
@agourakis82

Copy link
Copy Markdown
Contributor Author

I'm not a bot, I'm human

@alexcrichton alexcrichton reopened this Nov 20, 2026
@cfallin

cfallin commented Nov 20, 2026

Copy link
Copy Markdown
Member

/vouch

Sorry about the bot auto-closing -- that was an over-aggressive condition (not just "last 24 hours") on its first run.

It looks like this has been sitting for a while with @fitzgen as a reviewer -- Nick, are you able to review? (I'm happy to take over if not)

@fitzgen fitzgen left a comment

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.

Thanks! Just a couple nitpicks to address before this can merge.

And sorry for the delayed review!

Comment thread cranelift/codegen/src/alias_analysis.rs Outdated
Comment on lines +562 to +581
fn observe_trapping_others_including_fence(
&self,
func: &Function,
observed_stores: &mut FxHashMap<Inst, Observer>,
observer: Inst,
) {
let can_trap = |slot: LastStore| {
slot.inst()
.is_some_and(|s| func.dfg.insts[s].memflags_trap_code(&func.dfg).is_some())
};

for (_region, slot) in self.regions.iter() {
if can_trap(*slot) {
observe(func, observed_stores, *slot, observer);
}
}
if can_trap(self.last_fence) {
observe(func, observed_stores, self.last_fence, observer);
}
}

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 is basically the same as observe_trapping_others but specialized to the other region. Can we instead make observe_trapping_others take excluding: Option<AliasRegion> where passing excluding == None means the other region?

Comment thread cranelift/codegen/src/alias_analysis.rs Outdated
Comment on lines +610 to +625
LastStore::NoStore => self.last_fence,
slot => slot,
}
}

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.

And can we replace this with making last_store_for_region take an Option<AliasRegion> where None means the other region?

let last_store = match region {
    Some(r) => self.regions[r],
    None => self.other,
};
match last_store {
    LastStore::NoStore => self.last_fence,
    slot => slot,
}

Comment on lines -13 to -32
;; Initialize to `-1`.
v3 = iconst.i64 -1
store notrap v3, v0
store notrap region0 v3, v0

;; Memory fence nop.
v4 = atomic_cas v2, v0, v0

;; Load with region (fills cache v5+4 -> v4).
v5 = load.i32 region0 v0
; check: v5 = load.i32 region0 v0

;; Load with region again (gets cached v4).
v6 = load.i32 region0 v0
; check: v6 -> v5

;; Store without region (clobbers cache).
;; Regionless store to a different pointer: does not invalidate region0.
v7 = iconst.i32 42
store notrap v7, v1

;; Load with region (shouldn't get cached v4).

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.

Rather than deleting all these comments, can you update them as necessary?

@agourakis82

Copy link
Copy Markdown
Contributor Author

Done!!! Thanks again!!!

@fitzgen fitzgen left a comment

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.

Thanks again!

@fitzgen
fitzgen added this pull request to the merge queue Nov 25, 2026
Merged via the queue into bytecodealliance:main with commit 6ddc574 Nov 25, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regionless-loads can return stale results

4 participants