Skip to content

Fix type inference for enum variants#73

Merged
mmcloughlin merged 1 commit intoverify-mainfrom
mbm/fix-type-inference-enum-variant
Oct 20, 2023
Merged

Fix type inference for enum variants#73
mmcloughlin merged 1 commit intoverify-mainfrom
mbm/fix-type-inference-enum-variant

Conversation

@mmcloughlin
Copy link
Collaborator

@mmcloughlin mmcloughlin commented Oct 18, 2023

This PR proposes a fix for the type inference problem that was preventing verification of the Amode lowering with the expression (Amode.ImmRegRegShift offset x y 0 flags).

(rule 0 (amode_imm_reg_reg_shift flags x y offset)
(Amode.ImmRegRegShift offset x y 0 flags)) ;; 0 == y<<0 == "no shift"

The specific problem was a failure to deduce the type for the shift argument (set to the constant 0). I believe the root cause was with add_isle_constraints which was only considering declarations, not enum variants. I saw that the Sema layer in ISLE handles this already, so both declarations and enum variants are merged in the TermEnv. Therefore this PR changes add_isle_constraints to operate on Sema Terms rather than AST Decls. This has the (I think) nice side effect that type inference no longer uses any AST types. That also meant that the change had a bit of a larger blast radius, with various function signatures changing etc.

@mmcloughlin mmcloughlin force-pushed the mbm/fix-type-inference-enum-variant branch from bf06d1f to 53ef6f5 Compare October 18, 2023 18:29
@mmcloughlin mmcloughlin requested a review from avanhatt October 18, 2023 18:32
@mmcloughlin
Copy link
Collaborator Author

Please advise on how to test this PR.

@mmcloughlin mmcloughlin marked this pull request as ready for review October 18, 2023 18:33
@mmcloughlin mmcloughlin changed the base branch from verify-main to mbm/veri-fmt October 18, 2023 18:37
@mmcloughlin mmcloughlin force-pushed the mbm/fix-type-inference-enum-variant branch from 53ef6f5 to 4443cad Compare October 18, 2023 20:04
@mmcloughlin mmcloughlin changed the base branch from mbm/veri-fmt to verify-main October 18, 2023 20:04
Comment on lines +2395 to +2396
/// Lookup term by name.
pub fn get_term_by_name(&self, tyenv: &TypeEnv, sym: &ast::Ident) -> Option<TermId> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Had to make this public.

Is the intention that some of the verify code would be moved inside this package in future?

Copy link
Owner

Choose a reason for hiding this comment

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

Likely not in the short term, no.

Copy link
Owner

@avanhatt avanhatt left a comment

Choose a reason for hiding this comment

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

Looks good, thanks! Some non-blocking comments inline

Comment on lines +2395 to +2396
/// Lookup term by name.
pub fn get_term_by_name(&self, tyenv: &TypeEnv, sym: &ast::Ident) -> Option<TermId> {
Copy link
Owner

Choose a reason for hiding this comment

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

Likely not in the short term, no.

#[derive(Clone, Debug)]
pub struct AnnotationEnv {
pub annotation_map: HashMap<String, TermAnnotation>,
pub annotation_map: HashMap<TermId, TermAnnotation>,
Copy link
Owner

Choose a reason for hiding this comment

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

Nice

pub fn spec_to_annotation_bound_var(i: &Ident) -> BoundVar {
BoundVar {
name: string_from_ident(env, i),
name: i.0.clone(),
Copy link
Owner

Choose a reason for hiding this comment

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

Nice


if unresolved_widths.contains(&width_name) {
// println!("\tResolved width: {}, {}", width_name, width_int);
println!("\tResolved width: {}, {}", width_name, width_int);
Copy link
Owner

Choose a reason for hiding this comment

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

For a later change: I (or we, if you get there first) should probably add a --verbose flag so we can toggle these more easily.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we should be using the debug and trace logging macros?

trace!("iter: value {:?}", value);

log::debug!("Number of CLIF blocks to lower: {}", f.dfg.num_blocks());


// Print term for debugging
print!(" {}", t);
print!(" {}", term_name);
Copy link
Owner

Choose a reason for hiding this comment

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

We probably don't want this on by default long term, but fine for now.

@avanhatt
Copy link
Owner

Please advise on how to test this PR.

The integration rule tests exercise this code (including for specific types), so we have some assurance that this doesn't break everything. But feel free to add more unit tests as you go, too.

@mmcloughlin
Copy link
Collaborator Author

The integration rule tests exercise this code (including for specific types)

Yes, I did rerun the test suite on this PR too.

It would be nice to add type-inference specific tests. Perhaps I'll look into that.

@mmcloughlin mmcloughlin merged commit 6578776 into verify-main Oct 20, 2023
@mmcloughlin mmcloughlin deleted the mbm/fix-type-inference-enum-variant branch October 20, 2023 19:04
mmcloughlin added a commit that referenced this pull request Nov 2, 2023
Updates some of the "broken" test cases to ensure they have full enum types.

These were failing with errors such as:

```
---- test_broken_iadd_shift2 stdout ----
Verifying iadd rules in file: ./examples/broken/iadd/broken_shift2.isle
thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', cranelift/isle/veri/veri_engine/src/annotations.rs:322:85
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'test_broken_iadd_shift2' panicked at 'called `Result::unwrap()` on an `Err` value: Error { error: "Test thread panicked Any { .. }", total_delay: 0ns, tries: 1 }', cranelift/isle/veri/veri_engine/tests/utils/mod.rs:109:12
```

Following the change in #73, we now error if an enum model does not have a corresponding type declaration. This PR fixes the problem by adding full enum type declarations alongside the models.
avanhatt pushed a commit to wellesley-prog-sys/wasmtime that referenced this pull request Oct 9, 2024
Generates the remaining opcodes for `AluRRRR`.

Updates #35
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