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

Use the new update-field AIR support to simplify assignment though a path in AIR #1359

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

utaal
Copy link
Collaborator

@utaal utaal commented Dec 2, 2024

This replaces code that takes a snapshot of the pre-state of the assignment, assumes the new value for the changed field and then assumes that all other fields (on each datatype of the path) are unchanged:

struct C {
    d: i64,
    e: u64,
}

struct B {
    c: C,
    e: u64,
}

struct A {
    b1: B,
    e: u64,
}

fn main (){
    let c1 = C{d: 3, e: 3};
    let b1 = B{c: c1, e: 3};
    let mut a = A{b1, e: 3};

    a.b1.c.d = 5;
}
  (snapshot snap%ASSIGN)
  (havoc a@)
  (assume
   (= (nestedstructs!C./C/d (%Poly%nestedstructs!C. (Poly%nestedstructs!C. (nestedstructs!B./B/c
        (%Poly%nestedstructs!B. (Poly%nestedstructs!B. (nestedstructs!A./A/b1 (%Poly%nestedstructs!A.
            (Poly%nestedstructs!A. a@)
     )))))))
    ) 5
  ))
  (assume
   (and
    (= (nestedstructs!C./C/e (nestedstructs!B./B/c (nestedstructs!A./A/b1 (old snap%ASSIGN
         a@
      )))
     ) (nestedstructs!C./C/e (nestedstructs!B./B/c (nestedstructs!A./A/b1 a@)))
    )
    (= (nestedstructs!B./B/e (nestedstructs!A./A/b1 (old snap%ASSIGN a@))) (nestedstructs!B./B/e
      (nestedstructs!A./A/b1 a@)
    ))
    (= (nestedstructs!A./A/e (old snap%ASSIGN a@)) (nestedstructs!A./A/e a@))
))

All this can be replaced with a single assignment using update-field:

(assign a@ ((_ update-field nestedstructs!A./A/?b1) (%Poly%nestedstructs!A. (Poly%nestedstructs!A. a@))
  ((_ update-field nestedstructs!B./B/?c) (%Poly%nestedstructs!B. (Poly%nestedstructs!B.
       (nestedstructs!A./A/b1 (%Poly%nestedstructs!A. (Poly%nestedstructs!A. a@)))
      )
     ) ((_ update-field nestedstructs!C./C/?d) (%Poly%nestedstructs!C. (Poly%nestedstructs!C.
        (nestedstructs!B./B/c (%Poly%nestedstructs!B. (Poly%nestedstructs!B. (nestedstructs!A./A/b1
            (%Poly%nestedstructs!A. (Poly%nestedstructs!A. a@))
       )))))
      ) 5
))))

This was primarily developed by @tim-rohde.

Copy link
Collaborator

@Chris-Hawblitzel Chris-Hawblitzel left a comment

Choose a reason for hiding this comment

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

Overall, it looks good. I just have a few changes that I've requested.

return Ok(t1);
if t1 != *s {
Err(format!(
"in field-update, argument type {:?} does not match struct type {:?}",
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe "datatype type" instead of "struct type"?

@@ -0,0 +1,61 @@
#![feature(rustc_private)]
#[macro_use]
Copy link
Collaborator

Choose a reason for hiding this comment

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

These look like copies of tests from other test files. If the tests are already in some other place, we don't have to have a second copy here. For refactoring an existing feature (like field update) for which tests already exist, we don't necessarily have to have a new test file.

);
let bop = air::ast::BinaryOp::FieldUpdate(acc);
if typ_is_poly(ctx, field_typ) && !typ_is_poly(ctx, &value_typ) {
value = try_box(ctx, value, &value_typ).expect("box field update");
Copy link
Collaborator

Choose a reason for hiding this comment

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

If possible, I'd like to see boxing/unboxing coercions inserted by poly.rs rather than by sst_to_air.rs. The ongoing monomorphize branch, for example, would have to add special cases to sst_to_air.rs whenever sst_to_air.rs performs boxing/unboxing operations directly, whereas everything in poly.rs will automatically be skipped by monomorphize.

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.

3 participants