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

Fix bug in finding arity for verify attributes #2812

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bakaq
Copy link
Contributor

@bakaq bakaq commented Feb 4, 2025

This fixes a bug in the current attributed variables implementation that creates a lot of strange bugs that seem like miscompilations.

Closes #2706, closes #2809, closes #2632.

Copy link
Contributor

@adri326 adri326 left a comment

Choose a reason for hiding this comment

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

I don't have much experience with the logic behind verify_attributes, but I couldn't find any issue with this fix.

I wonder how hard it would be to store somewhere else the number of temporary variables, so that the WAM instructions don't need to be scanned for every attributed variable unification.


I do believe that the current method of modifying self.code to eventually call verify_attr_interrupt is iffy. It feels flimsy and it will make JIT impossible in its current form.

Comment on lines +584 to +585
//println!("{}: {:?}", p, &self.code[p]);
//println!("{} {:?}", arity, self.code[p]);
Copy link
Contributor

Choose a reason for hiding this comment

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

You forgot these in :)

Comment on lines +598 to +599
while p < self.code.len()
&& current_pred_limit.map(|x| p < x).unwrap_or(true)
Copy link
Contributor

Choose a reason for hiding this comment

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

This can also work:

Suggested change
while p < self.code.len()
&& current_pred_limit.map(|x| p < x).unwrap_or(true)
let current_pred_limit = current_pred_limit.unwrap_or(self.code.len());
while p < self.code.len() && p < current_pred_limit {

I also think that both loops could be joined together, simplifying the logic a bit (or at least making it less imperative in nature):

  • grab a slice of self.code: let predicate_instrs = &self.code[p..current_pred_limit.min(self.code.len())];
  • find p_interrupt: let p_interrupt = p + predicate_instrs.partition_point(Instruction::is_head_instr);
  • find the arity:
let arity = predicate_instrs
    .flat_map(Instruction::registers)
    .flat_map(|r| {
        match r {
            RegType::Temp(t) => Some(t),
            _ => None
        }
    })
    .max()
    .unwrap_or(0);

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I'm not sure if partition_point can be used, since it does a binary search.

@bakaq
Copy link
Contributor Author

bakaq commented Feb 4, 2025

I wonder how hard it would be to store somewhere else the number of temporary variables, so that the WAM instructions don't need to be scanned for every attributed variable unification.

I do believe that the current method of modifying self.code to eventually call verify_attr_interrupt is iffy. It feels flimsy and it will make JIT impossible in its current form.

I think ideally this should all be done in the compilation step, although maybe the verify attributes interrupt really needs to be this dynamic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants