Skip to content

(rewrite) const_prop: soundness & case support - #1006

Open
dc-mak wants to merge 3 commits into
rems-project:masterfrom
dc-mak:copy-prop-sound
Open

(rewrite) const_prop: soundness & case support#1006
dc-mak wants to merge 3 commits into
rems-project:masterfrom
dc-mak:copy-prop-sound

Conversation

@dc-mak

@dc-mak dc-mak commented May 29, 2026

Copy link
Copy Markdown
Collaborator

The previous analysis of which pexpr can be propagated was poorly explained and also unsound: values which could potentially not terminate, error, or cause UB, or depend on the memory model, in Core's strict setting should be left alone.

This shouldn't affect the upcoming mem2reg because that only relies on symbols being propagated correctly.

The PR also adds support for const-prop across case branches.

@dc-mak
dc-mak requested a review from kmemarian as a code owner May 29, 2026 13:02
| PEcatch_exceptional_condition _ (* may raise UB *)
| PEconv_int _ (* may raise impl-defined error *)
| PEcfunction _ (* function pointer lookup may fail *)
| PEcall _ (* may not terminate *) ->

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I dont see how DeriveCap can UB, it's not in the memory monad and the reduction in Core_eval simply returns the integer value.

Other than that, I double checked the rest and I think you have it all covered. Just some nitpick if we care about the comments being more precise:

  • PEcall may even also UB out.
  • for PEcfunction, looking at Core_eval I think all the fail $ Illformed_program ... should be UBs (there are also already a UB, and there will still remain an error for an illtyped expression, but that's irrelevant here). I'll make a PR to fix that in parallel.

@dc-mak dc-mak May 29, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

So is it CapAssignValue that may UB here?

type pure_memop =
    (* CHERI *) (* (integer, integer) -> integer *)
  | DeriveCap of derivecap_op * bool (* is_signed *)
    (* CHERI*) (* (integer, integer) -> integer (may result in UB) *)
  | CapAssignValue
    (* CHERI *) (* integer -> integer *)
  | Ptr_tIntValue
    (* Bytes *)
  | ByteFromInt  (* integer -> byte *)
  | IntFromByte  (* byte -> integer *)

@kmemarian

Copy link
Copy Markdown
Collaborator

The CI failure is weird, I thought it was because the opam cache had an old cmdliner that might not do the argument shortening but it's using 2.1.0 and I can't replicate the issue with that version (or in fact older ones) on my machine.

@dc-mak
dc-mak force-pushed the copy-prop-sound branch 2 times, most recently from 2876c3b to 8eb270f Compare May 29, 2026 18:51
dc-mak added 2 commits July 6, 2026 16:26
The previous analysis of which pexpr can be propagated was poorly
explained and also unsound: values which could potentially not
terminate, error, or cause UB  in Core's strict setting should be left
alone.

This shouldn't affect the upcoming mem2reg because that only relies on
symbols being propagated correctly.
This commit modifies the copy-prop to also propagate under case
branches. It does so by adding a new switch to analyze_pat_pexpr so that
it _doesn't_ change the type of the pattern to unit, and replace the
scrutinee with Unit, to maintain consitency across branches. Woohoo!

Might be nice to simplify the pattern but this could affect source
location and annotations.
@dc-mak
dc-mak force-pushed the copy-prop-sound branch from 8eb270f to 3db3c9b Compare July 6, 2026 15:27
It started off as a copy-propagation pass just for symbols but it's long
passed that now so it's been renamed as such to minimise further
confusion.
@dc-mak
dc-mak force-pushed the copy-prop-sound branch from 2a6b596 to e774c23 Compare July 6, 2026 15:40
@dc-mak dc-mak changed the title (rewrite) copy_prop: soundness & case support (rewrite) const_prop: soundness & case support Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants