Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crucible-mir: Improve pretty-printing of ZST constants #1312

Merged
merged 1 commit into from
Feb 20, 2025

Conversation

RyanGlScott
Copy link
Contributor

@RyanGlScott RyanGlScott commented Feb 20, 2025

ConstZST (i.e., constant values with zero-sized types) do not include any information about what the zero-sized type is. In particular, function constants are represented as ConstZSTs, and the only way to determine what function the ConstZST corresponds to is to look at its type (FnDef), which stores the DefId of the function name itself.

Previously, the crucible-mir pretty-printer was printing ConstZSTs as <ZST> because it did not have access to any type information when pretty-printing ConstVals. This results in very suboptimal pretty-printed output (see the example below). This is also true for other forms of ConstZSTs, although currently mir-json only turns function constants into ConstZSTs. (In the future, this could conceivably change.)

To fix this issue, we add special cases in the Pretty Constant instance (which does have access to type information for ConstVals) such that ConstZSTs are printed using their type information. In particular, if the ConstZST has type TyFnDef, then the function DefId stored in the TyFnDef is printed. All other forms of ConstZSTs are printed like <ZST: [ty]>, where [ty] is the pretty-printed type.

As an example of the improvements in this patch, consider this program:

pub fn foo() {}
pub fn bar() { foo() }

Before the changes in this patch, the MIR for bar() would be pretty-printed as:

fn test/f3b25c00::bar[0]() -> () {
   let mut _0 : ();
   bb0: {
      call(_0 = <ZST>(), bb1)
   }
   bb1: {
      return;
   }
}

(Note the call to <ZST>(), which gives no indication whatsoever about what function was called.)

After the changes in this patch, it is pretty-printed as:

fn test/4717ccf0::bar[0]() -> () {
   let mut _0 : ();
   bb0: {
      call(_0 = test/4717ccf0::foo[0](), bb1)
   }
   bb1: {
      return;
   }
}

Fixes GaloisInc/mir-json#79.

`ConstZST` (i.e., constant values with zero-sized types) do not include any
information about what the zero-sized type is. In particular, function
constants are represented as `ConstZST`s, and the only way to determine what
function the `ConstZST` corresponds to is to look at its type (`FnDef`), which
stores the `DefId` of the function name itself.

Previously, the `crucible-mir` pretty-printer was printing `ConstZST`s as
`<ZST>` because it did not have access to any type information when
pretty-printing `ConstVal`s. This results in very suboptimal pretty-printed
output (see the example below). This is also true for other forms of
`ConstZST`s, although currently `mir-json` only turns function constants into
`ConstZST`s. (In the future, this could conceivably change.)

To fix this issue, we add special cases in the `Pretty Constant` instance
(which does have access to type information for `ConstVal`s) such that
`ConstZST`s are printed using their type information. In particular, if the
`ConstZST` has type `TyFnDef`, then the function `DefId` stored in the
`TyFnDef` is printed. All other forms of `ConstZST`s are printed like `<ZST:
[ty]>`, where `[ty]` is the pretty-printed type.

As an example of the improvements in this patch, consider this program:

```rs
pub fn foo() {}
pub fn bar() { foo() }
```

Before the changes in this patch, the MIR for `bar()` would be pretty-printed
as:

```rs
fn test/f3b25c00::bar[0]() -> () {
   let mut _0 : ();
   bb0: {
      call(_0 = <ZST>(), bb1)
   }
   bb1: {
      return;
   }
}
```

(Note the call to `<ZST>()`, which gives no indication whatsoever about what
function was called.)

After the changes in this patch, it is pretty-printed as:

```rs
fn test/4717ccf0::bar[0]() -> () {
   let mut _0 : ();
   bb0: {
      call(_0 = test/4717ccf0::foo[0](), bb1)
   }
   bb1: {
      return;
   }
}
```

Fixes GaloisInc/mir-json#79.
@RyanGlScott RyanGlScott changed the title crucible-mir: Improve pretty-printing of function constants crucible-mir: Improve pretty-printing of ZST constants Feb 20, 2025
@RyanGlScott RyanGlScott merged commit d491c2e into master Feb 20, 2025
32 checks passed
@RyanGlScott RyanGlScott deleted the mir-json-T79 branch February 20, 2025 21:55
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.

Record a FnDef's DefId when serializing to JSON
2 participants