Skip to content

Use a type visitor to collect type metadata, remove layout #68

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

Merged
merged 16 commits into from
Apr 29, 2025

Conversation

jberthold
Copy link
Member

Fixes #67

As discussed, using a type visitor to collect type information is more robust than the prior collect_ty.
Layout information causes issues with polymorphic functions again, so we do not collect it any more for now as we aren't currently using it at all in mir-semantics.

@jberthold jberthold requested a review from dkcumming April 22, 2025 05:14
@jberthold jberthold marked this pull request as ready for review April 22, 2025 05:18
@jberthold jberthold requested a review from gtrepta April 22, 2025 05:18
src/printer.rs Outdated
impl Visitor for TyCollector {
type Break = ();
fn visit_ty(&mut self, ty: &stable_mir::ty::Ty) -> ControlFlow<Self::Break> {
self.types.insert(*ty, (ty.kind(), None));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually thinking about this again, I don't think this is a good idea, because the attempt to get a Layout failing is because we are still collecting Tys on unmonomorphised function like items. I think the vistor / collector needs to be corrected before we merge this on second thoughts.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agree this is indicative of a problem we have (quite probably in stdlib code only, but still).
We can maybe also just fix the collect_ty function to consider types within TyConst (that was the original motivation to look into this).

Copy link
Contributor

Choose a reason for hiding this comment

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

I updated the visitor to resolve the unmonomorphised items like before.

Now, this visitor does a way better job of visiting every possible thing in these types. This is good, obviously, but it also means we hit some stuff we weren't previously that we don't know how to handle just yet. So, I've arranged the visitor to break on these items to avoid any panics. These are:

  • Bound regions (RegionKind::ReBound). Some of these have escaping vars that cause a message saying they can't be wrapped in a dummy binder. There may be some bound regions that don't have this issue, but at the moment I'm just skipping all of them.
  • RigidTy::CoroutineWitness. Trying get the layout for these causes a panic. Maybe we just skip calling layout and use None, but I have it skipping them altogether right now.

This passes the ui tests now. I think this is in a good place now and should make it much easier to special case specific things and track what we aren't supporting.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this intended as a temporary fix? I am hesitant to move forward merging in code knowing that we are accessing MIR that is not resolved. But I understand that if we don't know where it is originating from and we need the Tys collected immediately we need to weigh that in too.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Or am I misunderstanding what we are avoiding with breaking the control flow?

Copy link
Contributor

Choose a reason for hiding this comment

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

The two bullet points I listed above are cases where we don't add the type to the metadata output. It's intended to prevent any panics at runtime, and it's temporary in the sense that we (I) don't understand why the panic happens or how to resolve it.

As far as whether or not we're still hitting some MIR, maybe we can determine somehow if we are with breakpoints/cases/print statements. Not sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am a bit puzzled by the differences in the expectation files.
The code does not collect any function types any more ? That's OK I guess, we don't use them, but... we also don't get I8 any more (in this example)? Why not?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I also can't understand why i8 is now removed from the map

Copy link
Contributor

@gtrepta gtrepta Apr 28, 2025

Choose a reason for hiding this comment

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

I noticed functions aren't in the map as well. I think it's because they get inserted by Stephen's code before monomorphization:

    if val_collector
.visited_tys
.insert(val, (val.kind(), maybe_layout_shape))
.is_some()
{
match val.kind() {   

The monomorphization gets done inside the match statement, but the function is already added to the map! In my code, the argument and return types for the function are added only. But, we should probably figure out how to get the resolved function signature and add it as well.

Not sure why the I8 disappears, idk why it's there in the first place either. The integers in the source don't have any type annotations, rust defaults to I32 (which is in the type map), so there shouldn't be any issue with that.

@jberthold
Copy link
Member Author

Some IDs seem to be unstable (type info records change location in the expectation files on different runs). We could sort the type infos by something that stabilises the order (the name is the obvious option for the failure I saw in CI - jq should have a stable sort function so things without name don't change order).

dkcumming and others added 3 commits April 27, 2025 08:46
I think this is the correct way to access the function signature args
for a function pointer, even though it goes through internal. I checked
kani kani-compiler/src/codegen_cprover_gotoc/codegen/statement.rs and
this is what they are doing.

There was one failure `rust/tests/ui/issues/issue-26641.rs (exit 101)`
that uses a `RigidTy::Dynamic(Binder...)` but we haven't really looked
at details for `dyn` and `vtable` yet so this is moved to failing for
now.

Also improved the UI testing output but adding verbosity and some
metrics
@gtrepta gtrepta requested a review from dkcumming April 29, 2025 18:57
Copy link
Collaborator

@dkcumming dkcumming left a comment

Choose a reason for hiding this comment

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

I think great improvement, you mentioned some more things to do but that can be another PR

@gtrepta gtrepta merged commit 8c178ef into master Apr 29, 2025
3 checks passed
@gtrepta gtrepta deleted the guy/ty_visitor branch April 29, 2025 19:09
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.

Collect type metadata using a type table visitor instead of searching items
3 participants