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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/machine/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,38 @@ impl Machine {
let mut p = self.machine_st.p;
let mut arity = 0;

self.indices.code_dir.sort_by(|_, a, _, b| a.cmp(b));

let predicate_idx = self
.indices
.code_dir
.binary_search_by_key(&p, |_, x| x.get().p() as usize)
.unwrap_or_else(|x| x - 1);

let current_pred_limit = self
.indices
.code_dir
.get_index(predicate_idx + 1)
.map(|x| x.1.p() as usize);

while self.code[p].is_head_instr() {
//println!("{}: {:?}", p, &self.code[p]);
//println!("{} {:?}", arity, self.code[p]);
Comment on lines +584 to +585
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 :)

for r in self.code[p].registers() {
//println!("reg {:?}", r);
if let RegType::Temp(t) = r {
arity = std::cmp::max(arity, t);
}
}

p += 1;
}

let p_interrupt = p;

while p < self.code.len()
&& current_pred_limit.map(|x| p < x).unwrap_or(true)
Comment on lines +598 to +599
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.

{
for r in self.code[p].registers() {
if let RegType::Temp(t) = r {
arity = std::cmp::max(arity, t);
Expand All @@ -577,14 +608,15 @@ impl Machine {
}

let instr = std::mem::replace(
&mut self.code[p],
&mut self.code[p_interrupt],
Instruction::VerifyAttrInterrupt(arity),
);

self.code[VERIFY_ATTR_INTERRUPT_LOC] = instr;
self.machine_st.attr_var_init.cp = p;
self.machine_st.attr_var_init.cp = p_interrupt;
}
&Instruction::VerifyAttrInterrupt(arity) => {
//println!("VerifyAttr arity: {arity}");
// let (_, arity) = self.code[VERIFY_ATTR_INTERRUPT_LOC].to_name_and_arity();
// let arity = std::cmp::max(arity, self.machine_st.num_of_args);
self.run_verify_attr_interrupt(arity);
Expand Down
17 changes: 17 additions & 0 deletions tests/scryer/cli/issues/compilation_bug.in/a.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
% Issue 2706
:- use_module(library(atts)).
:- use_module(library(lists)).
:- use_module(library(iso_ext)).

:- attribute a/1.

verify_attributes(_,_, []).

asdf([_|Xs], N) :-
true,
N1 is N - 1,
asdf(Xs, N1).

test_a :-
put_atts(A, a(1)),
call_with_inference_limit(asdf(A, 1), 1000, _).
30 changes: 30 additions & 0 deletions tests/scryer/cli/issues/compilation_bug.in/b.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
% Issue 2632

% Repro for cycle detection crash
:- use_module(library(lists)).
:- use_module(library(clpz)).
:- use_module(library(error)).
:- use_module(library(lambda)).
:- use_module(library(debug)).

clpz:monotonic.

q_r(T/N, T:U) :- 0 #=< #T, 0 #=< #U, #N #= T + U.

qs_Ts_Us(Qs, ΣTs, ΣUs) :-
maplist(\Q^T^U^(q_r(Q, T:U)), Qs, Ts, Us),
intlist_partsums(Ts, ΣTs),
intlist_partsums(Us, ΣUs).

%% Utility predicates used above:

intlist_partsums([X|Xs], [X|Ss]) :-
intlist_partsums_acc(Xs, Ss, X).

intlist_partsums_acc([], [], _).
intlist_partsums_acc([X|Xs], [S|Ss], A) :-
#S #= #X + #A,
intlist_partsums_acc(Xs, Ss, S).

test_b :-
once(qs_Ts_Us(_, [1,3], [5,9])).
19 changes: 19 additions & 0 deletions tests/scryer/cli/issues/compilation_bug.in/c.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
% Issue 2809

:- use_module(library(freeze)).

main1 :-
freeze(Minor,true),
cbor_minor_value1(Minor, []).

main2 :-
freeze(Minor,true),
cbor_minor_value2(Minor, []).

cbor_minor_value1(24, S0) :- numbytes_number(1, S0).

cbor_minor_value2(24, S0) :- S0=S1, numbytes_number(1, S1).

numbytes_number(_, []).

test_c :- main1.
2 changes: 2 additions & 0 deletions tests/scryer/cli/issues/compilation_bug.stdin
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
use_module(a), use_module(b), use_module(c).
\+ \+ (test_a, test_b, test_c).
2 changes: 2 additions & 0 deletions tests/scryer/cli/issues/compilation_bug.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
true.
true.
2 changes: 2 additions & 0 deletions tests/scryer/cli/issues/compilation_bug.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# issue 2706
args = ["-f", "--no-add-history"]
Loading