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

Allow for selection of diversly contextualized main functions in Bir #184

Merged
merged 19 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
41235c5
Expand Bir.program and Bir interface to expose both bare main_functio…
mdurero Sep 12, 2022
bf82d9d
Add context_with_reset_function in Bir.program similar to context_fun…
mdurero Sep 12, 2022
94d7448
Make contex_function_with_reset and context_function diverge
mdurero Sep 12, 2022
d967caf
Swap behaviour of Bir.main_statements and its first derivative with/n…
mdurero Sep 12, 2022
095ce46
Remove the Bir-built context functions from the program function map …
mdurero Sep 12, 2022
340ce1a
Replace context functions by an optional context record of 3 statemen…
mdurero Sep 15, 2022
7eabd5b
Remove Test_interpreter.add_test_conds_to_combined_program, the condi…
mdurero Sep 16, 2022
5eaac4f
Remove Bir_interface.context_agnostic_mpp_functions as program mpp_fu…
mdurero Sep 16, 2022
c79a4ce
Add context statements in Oir.program to pass it from the initial Bir…
mdurero Sep 16, 2022
f596448
Revert "Add context statements in Oir.program to pass it from the ini…
mdurero Sep 16, 2022
c0204da
Merge branch 'master' into main-function-selection-in-bir
mdurero Sep 19, 2022
f52cc2f
Transmit context statements transparently through Oir.program
mdurero Sep 20, 2022
adbdfa5
Merge remote-tracking branch 'origin/master' at release 98 into main-…
mdurero Sep 20, 2022
5b66e73
Delete outdated comment
mdurero Sep 21, 2022
5bcebcc
Merge branch 'master' at rev100 into main-function-selection-in-bir
mdurero Sep 22, 2022
d9bf2c9
Merge branch 'master' at release 102 into main-function-selection-in-bir
mdurero Sep 26, 2022
29db93e
Merge remote-tracking branch 'origin/master' at release 103 into main…
mdurero Sep 30, 2022
5168b57
Rename main_statements_with_reset to main_statements_with_context_and…
mdurero Sep 30, 2022
b8e0f56
Documentation comment on program context
mdurero Sep 30, 2022
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
10 changes: 2 additions & 8 deletions src/mlang/backend_compilers/bir_to_dgfip_c.ml
Original file line number Diff line number Diff line change
Expand Up @@ -759,10 +759,7 @@ let generate_mpp_function (dgfip_flags : Dgfip_options.flags)
let generate_mpp_functions (dgfip_flags : Dgfip_options.flags)
(program : Bir.program) (oc : Format.formatter)
(var_indexes : Dgfip_varid.var_id_map) =
let funcs =
Bir.FunctionMap.bindings
(Bir_interface.context_agnostic_mpp_functions program)
in
let funcs = Bir.FunctionMap.bindings program.Bir.mpp_functions in
List.iter
(fun (fname, { mppf_is_verif; _ }) ->
generate_mpp_function
Expand All @@ -772,10 +769,7 @@ let generate_mpp_functions (dgfip_flags : Dgfip_options.flags)

let generate_mpp_functions_signatures (oc : Format.formatter)
(program : Bir.program) =
let funcs =
Bir.FunctionMap.bindings
(Bir_interface.context_agnostic_mpp_functions program)
in
let funcs = Bir.FunctionMap.bindings program.Bir.mpp_functions in
Format.fprintf oc "@[<v 0>%a@]@,"
(Format.pp_print_list (fun ppf (func, { mppf_is_verif; _ }) ->
generate_mpp_function_protoype true mppf_is_verif ppf func))
Expand Down
6 changes: 2 additions & 4 deletions src/mlang/backend_compilers/bir_to_java.ml
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ let generate_calculateTax_method (calculation_vars_len : int)
@,"
print_double_cut () calculation_vars_len locals_size print_double_cut ()
print_double_cut () print_double_cut () (generate_stmts program)
(Bir.main_statements program)
(Bir.main_statements_with_context program)

let generate_mpp_function (program : program) (oc : Format.formatter)
(f : function_name) =
Expand All @@ -424,9 +424,7 @@ let generate_mpp_function (program : program) (oc : Format.formatter)
f (generate_stmts program) mppf_stmts

let generate_mpp_functions (oc : Format.formatter) (program : program) =
let functions =
FunctionMap.bindings (Bir_interface.context_agnostic_mpp_functions program)
in
let functions = FunctionMap.bindings program.Bir.mpp_functions in
let function_names, _ = List.split functions in
Format.pp_print_list ~pp_sep:print_double_cut
(generate_mpp_function program)
Expand Down
24 changes: 24 additions & 0 deletions src/mlang/backend_ir/bir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,17 @@ module FunctionMap = Map.Make (struct
let compare = String.compare
end)

type program_context = {
constant_inputs_init_stmts : stmt list;
adhoc_specs_conds_stmts : stmt list;
unused_inputs_init_stmts : stmt list;
}

type program = {
mpp_functions : mpp_function FunctionMap.t;
rules_and_verifs : rule_or_verif ROVMap.t;
main_function : function_name;
context : program_context option;
idmap : Mir.idmap;
mir_program : Mir.program;
outputs : unit VariableMap.t;
Expand All @@ -137,6 +144,23 @@ let main_statements (p : program) : stmt list =
with Not_found ->
Errors.raise_error "Unable to find main function of Bir program"

let main_statements_with_context (p : program) : stmt list =
match p.context with
| Some context ->
context.constant_inputs_init_stmts @ main_statements p
@ context.adhoc_specs_conds_stmts
| None ->
Errors.raise_error
"This Bir program has no context constants and conditions stored"

let main_statements_with_reset (p : program) : stmt list =
match p.context with
| Some context ->
context.unused_inputs_init_stmts @ main_statements_with_context p
| None ->
Errors.raise_error
"This Bir program has no context input reset statements stored"

let rec get_block_statements (p : program) (stmts : stmt list) : stmt list =
List.fold_left
(fun stmts stmt ->
Expand Down
11 changes: 11 additions & 0 deletions src/mlang/backend_ir/bir.mli
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@ type mpp_function = { mppf_stmts : stmt list; mppf_is_verif : bool }

module FunctionMap : Map.S with type key = function_name

type program_context = {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm ok with the change but you should put the rationale about why we need this context as a documentation comment here (you can start from your PR description which is quite informative).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Documentation comment proposed in commit b8e0f56.

constant_inputs_init_stmts : stmt list;
adhoc_specs_conds_stmts : stmt list;
unused_inputs_init_stmts : stmt list;
}

type program = {
mpp_functions : mpp_function FunctionMap.t;
rules_and_verifs : rule_or_verif ROVMap.t;
main_function : function_name;
context : program_context option;
idmap : Mir.idmap;
mir_program : Mir.program;
outputs : unit VariableMap.t;
Expand All @@ -84,6 +91,10 @@ val rule_or_verif_as_statements : rule_or_verif -> stmt list

val main_statements : program -> stmt list

val main_statements_with_context : program -> stmt list

val main_statements_with_reset : program -> stmt list

val get_all_statements : program -> stmt list

val squish_statements : program -> int -> string -> program
Expand Down
2 changes: 1 addition & 1 deletion src/mlang/backend_ir/bir_instrumentation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ and get_code_locs_stmts (p : Bir.program) (stmts : Bir.stmt list)
locs

let get_code_locs (p : Bir.program) : code_locs =
get_code_locs_stmts p (Bir.main_statements p) []
get_code_locs_stmts p (Bir.main_statements_with_reset p) []
28 changes: 8 additions & 20 deletions src/mlang/backend_ir/bir_interface.ml
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,6 @@ let read_inputs_from_stdin (f : bir_function) : Mir.literal Bir.VariableMap.t =
with Mparser.Error -> Errors.raise_error "Lexer error in input!")
f.func_variable_inputs

let context_function = "contextualize"

let context_agnostic_mpp_functions (p : Bir.program) :
Bir.mpp_function Bir.FunctionMap.t =
Bir.FunctionMap.remove context_function p.Bir.mpp_functions

(** Add varibles, constants, conditions and outputs from [f] to [p] *)
let adapt_program_to_function (p : Bir.program) (f : bir_function) :
Bir.program * int =
Expand Down Expand Up @@ -327,22 +321,16 @@ let adapt_program_to_function (p : Bir.program) (f : bir_function) :
Pos.same_pos_as (Bir.SVerif cond) cond.cond_expr :: acc)
f.func_conds []
in
let mpp_functions =
Bir.FunctionMap.add context_function
Bir.
{
mppf_stmts =
unused_input_stmts @ const_input_stmts
@ Bir.[ (SFunctionCall (p.main_function, []), Pos.no_pos) ]
@ conds_stmts;
mppf_is_verif = false;
}
p.mpp_functions
in
( {
p with
mpp_functions;
main_function = context_function;
context =
Some
Bir.
{
constant_inputs_init_stmts = const_input_stmts;
adhoc_specs_conds_stmts = conds_stmts;
unused_inputs_init_stmts = unused_input_stmts;
};
outputs = f.func_outputs;
},
List.length unused_input_stmts + List.length const_input_stmts )
5 changes: 0 additions & 5 deletions src/mlang/backend_ir/bir_interface.mli
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,3 @@ val read_inputs_from_stdin : bir_function -> Mir.literal Bir.VariableMap.t
val adapt_program_to_function : Bir.program -> bir_function -> Bir.program * int
(** [adapt_program_to_function program io] modifies [program] according to the
input-output specification of [io]*)

val context_agnostic_mpp_functions :
Bir.program -> Bir.mpp_function Bir.FunctionMap.t
(** Returns the mpp functions of the specification without contextualization
from [adapt_proram_to_function] *)
4 changes: 3 additions & 1 deletion src/mlang/backend_ir/bir_interpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,9 @@ module Make (N : Bir_number.NumberInterface) = struct
(code_loc_start_value : int) : ctx =
try
let ctx =
evaluate_stmts p ctx (Bir.main_statements p) [] code_loc_start_value
evaluate_stmts p ctx
(Bir.main_statements_with_reset p)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here a little comment about why we reset would be good

[] code_loc_start_value
in
ctx
with RuntimeError (e, ctx) ->
Expand Down
2 changes: 1 addition & 1 deletion src/mlang/backend_ir/format_bir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ let format_rules fmt rules =

let format_program fmt (p : program) =
Format.fprintf fmt "%a%a" format_rules p.rules_and_verifs format_stmts
(Bir.main_statements p)
(Bir.main_statements_with_reset p)
1 change: 1 addition & 0 deletions src/mlang/mpp_ir/mpp_ir_to_bir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ let create_combined_program (m_program : Mir_interface.full_program)
rules_and_verifs;
mpp_functions;
main_function = mpp_function_to_extract;
context = None;
idmap = m_program.program.program_idmap;
mir_program = m_program.program;
outputs = Bir.VariableMap.empty;
Expand Down
6 changes: 6 additions & 0 deletions src/mlang/optimizing_ir/bir_to_oir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ let bir_program_to_oir (p : Bir.program) : Oir.program =
mir_program = p.mir_program;
outputs = p.outputs;
main_function = p.main_function;
context = p.context;
}

let rec re_translate_statement (s : Oir.stmt)
Expand Down Expand Up @@ -177,6 +178,10 @@ and re_translate_block (block_id : Oir.block_id)
let cfg_to_bir_stmts (cfg : Oir.cfg) : Bir.stmt list =
re_translate_blocks_until cfg.entry_block cfg.blocks None

(*WARNING : OIR is not tested, but changes in Bir interface to "context" of the
program (Bir_interface.adapt_program_to_function) could have broken it. In any
cases : its behavior is modified as the context is no more included in the
optimisations.*)
mdurero marked this conversation as resolved.
Show resolved Hide resolved
let oir_program_to_bir (p : Oir.program) : Bir.program =
let mpp_functions =
Bir.FunctionMap.map
Expand Down Expand Up @@ -208,4 +213,5 @@ let oir_program_to_bir (p : Oir.program) : Bir.program =
mir_program = p.mir_program;
outputs = p.outputs;
main_function = p.main_function;
context = p.context;
}
2 changes: 2 additions & 0 deletions src/mlang/optimizing_ir/oir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type program = {
mir_program : Mir.program;
outputs : unit Bir.VariableMap.t;
main_function : Bir.function_name;
context : Bir.program_context option;
}

let map_program_cfgs (f : cfg -> cfg) (p : program) : program =
Expand Down Expand Up @@ -78,6 +79,7 @@ let map_program_cfgs (f : cfg -> cfg) (p : program) : program =
mir_program = p.mir_program;
outputs = p.outputs;
main_function = p.main_function;
context = p.context;
}

let count_instr (p : program) : int =
Expand Down
1 change: 1 addition & 0 deletions src/mlang/optimizing_ir/oir.mli
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type program = {
mir_program : Mir.program;
outputs : unit Bir.VariableMap.t;
main_function : Bir.function_name;
context : Bir.program_context option;
}

val map_program_cfgs : (cfg -> cfg) -> program -> program
Expand Down
48 changes: 0 additions & 48 deletions src/mlang/test_framework/test_interpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -120,51 +120,6 @@ let to_MIR_function_and_inputs (program : Bir.program) (t : test_file)
( { func_variable_inputs; func_constant_inputs; func_outputs; func_conds },
input_file )

let add_test_conds_to_combined_program (p : Bir.program)
(conds : Bir.condition_data Bir.VariableMap.t) : Bir.program =
(* because evaluate_program redefines everything each time, we have to make
sure that the redefinitions of our constant inputs are removed from the
main list of statements *)
mdurero marked this conversation as resolved.
Show resolved Hide resolved
let filter_stmts stmts =
List.filter_map
(fun stmt ->
match Pos.unmark stmt with
| Bir.SAssign (var, var_data) -> (
let new_var_data =
{
var_data with
var_io = Regular;
var_definition =
(match var_data.var_definition with
| InputVar ->
SimpleVar
(Pos.same_pos_as (Mir.Literal Undefined)
(Bir.var_to_mir var).Mir.Variable.name)
| SimpleVar old -> SimpleVar old
| TableVar (size, old) -> TableVar (size, old));
}
in
match new_var_data.var_definition with
| InputVar -> None
| _ -> Some (Pos.same_pos_as (Bir.SAssign (var, new_var_data)) stmt)
)
| _ -> Some stmt)
stmts
in
let new_stmts = filter_stmts (Bir.main_statements p) in
let conditions_stmts =
Bir.VariableMap.fold
(fun _ cond stmts ->
(Bir.SVerif cond, Pos.get_position cond.cond_expr) :: stmts)
conds []
in
let mpp_functions =
Bir.FunctionMap.add p.Bir.main_function
Bir.{ mppf_stmts = new_stmts @ conditions_stmts; mppf_is_verif = false }
p.mpp_functions
in
{ p with mpp_functions }

let check_test (combined_program : Bir.program) (test_name : string)
(optimize : bool) (code_coverage : bool)
(value_sort : Bir_interpreter.value_sort) (test_error_margin : float) :
Expand All @@ -179,9 +134,6 @@ let check_test (combined_program : Bir.program) (test_name : string)
let combined_program, code_loc_offset =
Bir_interface.adapt_program_to_function combined_program f
in
let combined_program =
add_test_conds_to_combined_program combined_program f.func_conds
in
(* Cli.debug_print "Combined Program (w/o verif conds):@.%a@."
Format_bir.format_program combined_program; *)
let combined_program =
Expand Down