Skip to content

Commit

Permalink
Remove some unnecessary infos from BIR (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
denismerigoux authored Feb 8, 2023
2 parents f5f1492 + 73a3770 commit 03e2fd2
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 107 deletions.
6 changes: 3 additions & 3 deletions src/mlang/backend_compilers/bir_to_dgfip_c.ml
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ let generate_m_assign (dgfip_flags : Dgfip_options.flags)
(Dgfip_varid.gen_access_pos_from_start var_indexes var)

let generate_var_def (dgfip_flags : Dgfip_options.flags)
(var_indexes : Dgfip_varid.var_id_map) (var : variable)
(data : variable_data) (fmt : Format.formatter) : unit =
match data.var_definition with
(var_indexes : Dgfip_varid.var_id_map) (var : variable) (def : variable_def)
(fmt : Format.formatter) : unit =
match def with
| SimpleVar e ->
let se = generate_c_expr e var_indexes in
generate_m_assign dgfip_flags var_indexes var None fmt se
Expand Down
4 changes: 2 additions & 2 deletions src/mlang/backend_compilers/bir_to_java.ml
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ let format_local_vars_defs (oc : Format.formatter)
Format.fprintf fmt "localVariables[%d] = %s;" lvar.Mir.LocalVariable.id se)
oc defs

let generate_var_def (var : variable) (data : variable_data)
let generate_var_def (var : variable) (def : variable_def)
(oc : Format.formatter) =
match data.var_definition with
match def with
| SimpleVar e ->
let se, defs = generate_java_expr e in
Format.fprintf oc "%a%s = %s;" format_local_vars_defs defs
Expand Down
8 changes: 3 additions & 5 deletions src/mlang/backend_ir/bir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ type condition_data = variable Mir.condition_data_

type variable_def = variable Mir.variable_def_

type variable_data = variable Mir.variable_data_

type function_name = string

type rule_or_verif_code = Rule of stmt list | Verif of stmt
Expand All @@ -106,7 +104,7 @@ and rule_or_verif = {
and stmt = stmt_kind Pos.marked

and stmt_kind =
| SAssign of variable * variable_data
| SAssign of variable * variable_def
| SConditional of expression * stmt list * stmt list
| SVerif of condition_data
| SRovCall of rov_id
Expand Down Expand Up @@ -291,8 +289,8 @@ let get_local_variables (p : program) : unit Mir.LocalVariableMap.t =
(fun acc stmt ->
match Pos.unmark stmt with
| SVerif cond -> get_local_vars_expr acc cond.Mir.cond_expr
| SAssign (_, data) -> (
match data.Mir.var_definition with
| SAssign (_, def) -> (
match def with
| Mir.SimpleVar e -> get_local_vars_expr acc e
| Mir.TableVar (_, defs) -> (
match defs with
Expand Down
4 changes: 1 addition & 3 deletions src/mlang/backend_ir/bir.mli
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ type condition_data = variable Mir.condition_data_

type variable_def = variable Mir.variable_def_

type variable_data = variable Mir.variable_data_

type function_name = string

type rule_or_verif_code = Rule of stmt list | Verif of stmt
Expand All @@ -47,7 +45,7 @@ and rule_or_verif = {
and stmt = stmt_kind Pos.marked

and stmt_kind =
| SAssign of variable * variable_data
| SAssign of variable * variable_def
| SConditional of expression * stmt list * stmt list
| SVerif of condition_data
| SRovCall of rov_id
Expand Down
52 changes: 17 additions & 35 deletions src/mlang/backend_ir/bir_interface.ml
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,7 @@ let adapt_program_to_function (p : Bir.program) (f : bir_function) :
let const_input_stmts =
Bir.VariableMap.fold
(fun var e acc ->
Pos.same_pos_as
(Bir.SAssign
( var,
{
Mir.var_typ = None;
Mir.var_io = Regular;
Mir.var_definition = Mir.SimpleVar e;
} ))
e
:: acc)
Pos.same_pos_as (Bir.SAssign (var, Mir.SimpleVar e)) e :: acc)
f.func_constant_inputs []
in
let unused_input_stmts =
Expand All @@ -285,31 +276,22 @@ let adapt_program_to_function (p : Bir.program) (f : bir_function) :
let pos = Pos.no_pos in
( Bir.SAssign
( Bir.(var_from_mir default_tgv) var,
{
Mir.var_typ = None;
Mir.var_io = Regular;
Mir.var_definition =
begin
match var.Mir.Variable.is_table with
| None ->
Mir.SimpleVar (Mir.Literal Mir.Undefined, pos)
| Some size ->
let idxmap =
let rec loop i acc =
if i < 0 then acc
else
loop (i - 1)
(Mir.IndexMap.add i
(Pos.same_pos_as
(Mir.Literal Mir.Undefined)
var.Mir.Variable.name)
acc)
in
loop (size - 1) Mir.IndexMap.empty
in
Mir.TableVar (size, Mir.IndexTable idxmap)
end;
} ),
match var.Mir.Variable.is_table with
| None -> Mir.SimpleVar (Mir.Literal Mir.Undefined, pos)
| Some size ->
let idxmap =
let rec loop i acc =
if i < 0 then acc
else
loop (i - 1)
(Mir.IndexMap.add i
(Pos.same_pos_as (Mir.Literal Mir.Undefined)
var.Mir.Variable.name)
acc)
in
loop (size - 1) Mir.IndexMap.empty
in
Mir.TableVar (size, Mir.IndexTable idxmap) ),
pos )
:: acc
| _ -> acc)
Expand Down
4 changes: 2 additions & 2 deletions src/mlang/backend_ir/bir_interpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -710,15 +710,15 @@ struct
let rec evaluate_stmt (p : Bir.program) (ctx : ctx) (stmt : Bir.stmt)
(loc : code_location) =
match Pos.unmark stmt with
| Bir.SAssign (var, vdata) ->
| Bir.SAssign (var, vdef) ->
let value =
try Bir.VariableMap.find var ctx.ctx_vars
with Not_found -> (
match (Bir.var_to_mir var).is_table with
| Some size -> TableVar (size, Array.make size Undefined)
| None -> SimpleVar Undefined)
in
let res = evaluate_variable p ctx value vdata.var_definition in
let res = evaluate_variable p ctx value vdef in
!assign_hook var (fun _ -> var_value_to_var_literal res) loc;
{ ctx with ctx_vars = Bir.VariableMap.add var res ctx.ctx_vars }
| Bir.SConditional (b, t, f) -> (
Expand Down
4 changes: 2 additions & 2 deletions src/mlang/backend_ir/format_bir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ let format_variable_def fmt (vdef : variable_def) =

let rec format_stmt fmt (stmt : stmt) =
match Pos.unmark stmt with
| SAssign (v, vdata) ->
| SAssign (v, vdef) ->
Format.fprintf fmt "%s = %a"
(Pos.unmark (var_to_mir v).Mir.Variable.name)
format_variable_def vdata.var_definition
format_variable_def vdef
| SConditional (cond, t, []) ->
Format.fprintf fmt "if(%a):@\n@[<h 2> %a@]@\n" format_expression cond
format_stmts t
Expand Down
32 changes: 5 additions & 27 deletions src/mlang/mpp_ir/mpp_ir_to_bir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,8 @@ let translate_m_code (m_program : Mir_interface.full_program)
match var_definition with
| InputVar -> None
| TableVar _ | SimpleVar _ ->
let vdef = { vdef with var_definition } in
Some
( Bir.SAssign (Bir.(var_from_mir default_tgv) var, vdef),
( Bir.SAssign (Bir.(var_from_mir default_tgv) var, var_definition),
var.Mir.Variable.execution_number.pos )
with Not_found -> None)
vars
Expand Down Expand Up @@ -351,13 +350,7 @@ and translate_mpp_stmt (mpp_program : Mpp_ir.mpp_compute list)
[
Pos.same_pos_as
(Bir.SAssign
( new_l,
{
var_definition =
SimpleVar (translate_mpp_expr m_program ctx expr, pos);
var_typ = None;
var_io = Regular;
} ))
(new_l, SimpleVar (translate_mpp_expr m_program ctx expr, pos)))
stmt;
] )
| Mpp_ir.Assign (Mbased (var, _), expr) ->
Expand All @@ -370,12 +363,7 @@ and translate_mpp_stmt (mpp_program : Mpp_ir.mpp_compute list)
Pos.same_pos_as
(Bir.SAssign
( Bir.(var_from_mir default_tgv) var,
{
var_definition =
SimpleVar (translate_mpp_expr m_program ctx expr, pos);
var_typ = None;
var_io = Mir.Input;
} ))
SimpleVar (translate_mpp_expr m_program ctx expr, pos) ))
stmt;
] )
| Mpp_ir.Conditional (e, t, f) ->
Expand All @@ -394,25 +382,15 @@ and translate_mpp_stmt (mpp_program : Mpp_ir.mpp_compute list)
Pos.same_pos_as
(Bir.SAssign
( Bir.(var_from_mir default_tgv) var,
{
var_definition = SimpleVar (Mir.Literal Undefined, pos);
var_typ = None;
var_io = Mir.Input;
} ))
SimpleVar (Mir.Literal Undefined, pos) ))
stmt;
] )
| Mpp_ir.Delete (Local l) ->
let var = StringMap.find l ctx.new_variables in
( ctx,
[
Pos.same_pos_as
(Bir.SAssign
( var,
{
var_definition = SimpleVar (Mir.Literal Undefined, pos);
var_typ = None;
var_io = Regular;
} ))
(Bir.SAssign (var, SimpleVar (Mir.Literal Undefined, pos)))
stmt;
] )
| Mpp_ir.Expr (Call (MppFunction f, args), pos) ->
Expand Down
2 changes: 1 addition & 1 deletion src/mlang/optimizing_ir/dead_code_removal.ml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ let remove_dead_statements (stmts : block) (id : block_id)
used_blocks
then
let stmt_used_vars =
match var_def.Mir.var_definition with
match var_def with
| Mir.SimpleVar e -> Bir.get_used_variables e
| Mir.TableVar (_, def) -> (
match def with
Expand Down
4 changes: 2 additions & 2 deletions src/mlang/optimizing_ir/format_oir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ open Oir

let rec format_stmt fmt (stmt : stmt) =
match Pos.unmark stmt with
| SAssign (v, vdata) ->
| SAssign (v, vdef) ->
Format.fprintf fmt "%s = %a@,"
(Pos.unmark (Bir.var_to_mir v).Mir.Variable.name)
Format_bir.format_variable_def vdata.var_definition
Format_bir.format_variable_def vdef
| SConditional (cond, b1, b2, _) ->
Format.fprintf fmt "if(%a) then goto %d else goto %d@,"
Format_bir.format_expression cond b1 b2
Expand Down
22 changes: 5 additions & 17 deletions src/mlang/optimizing_ir/inlining.ml
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,15 @@ let rec inline_in_expr (e : Bir.expression) (ctx : ctx)
let inline_in_stmt (stmt : stmt) (ctx : ctx) (current_block : block_id)
(current_stmt_pos : int) : stmt * ctx * int =
match Pos.unmark stmt with
| SAssign (var, data) -> (
match data.var_definition with
| SAssign (var, def) -> (
match def with
| InputVar -> (stmt, ctx, current_stmt_pos)
| SimpleVar def ->
let new_def =
inline_in_expr (Pos.unmark def) ctx current_block current_stmt_pos
in
let new_def = Mir.SimpleVar (Pos.same_pos_as new_def def) in
let new_stmt =
Pos.same_pos_as
(SAssign (var, { data with var_definition = new_def }))
stmt
in
let new_stmt = Pos.same_pos_as (SAssign (var, new_def)) stmt in
let new_ctx =
add_var_def_to_ctx var new_def current_block current_stmt_pos ctx
in
Expand All @@ -279,11 +275,7 @@ let inline_in_stmt (stmt : stmt) (ctx : ctx) (current_block : block_id)
Mir.TableVar
(size, IndexGeneric (v, Pos.same_pos_as new_def def))
in
let new_stmt =
Pos.same_pos_as
(SAssign (var, { data with var_definition = new_def }))
stmt
in
let new_stmt = Pos.same_pos_as (SAssign (var, new_def)) stmt in
let new_ctx =
add_var_def_to_ctx var new_def current_block current_stmt_pos
ctx
Expand All @@ -300,11 +292,7 @@ let inline_in_stmt (stmt : stmt) (ctx : ctx) (current_block : block_id)
defs
in
let new_defs = Mir.TableVar (size, IndexTable new_defs) in
let new_stmt =
Pos.same_pos_as
(SAssign (var, { data with var_definition = new_defs }))
stmt
in
let new_stmt = Pos.same_pos_as (SAssign (var, new_defs)) stmt in
let new_ctx =
add_var_def_to_ctx var new_defs current_block current_stmt_pos
ctx
Expand Down
2 changes: 1 addition & 1 deletion src/mlang/optimizing_ir/oir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module BlockMap = Map.Make (Int)
type stmt = stmt_kind Pos.marked

and stmt_kind =
| SAssign of Bir.variable * Bir.variable_data
| SAssign of Bir.variable * Bir.variable_def
| SConditional of Bir.expression * block_id * block_id * block_id
(** The first two block ids are the true and false branch, the third is
the join point after *)
Expand Down
2 changes: 1 addition & 1 deletion src/mlang/optimizing_ir/oir.mli
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module BlockMap : Map.S with type key = block_id
type stmt = stmt_kind Pos.marked

and stmt_kind =
| SAssign of Bir.variable * Bir.variable_data
| SAssign of Bir.variable * Bir.variable_def
| SConditional of Bir.expression * block_id * block_id * block_id
| SVerif of Bir.condition_data
| SGoto of block_id
Expand Down
8 changes: 2 additions & 6 deletions src/mlang/optimizing_ir/partial_evaluation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ let partially_evaluate_stmt (stmt : stmt) (block_id : block_id)
List.mem (Pos.unmark (Bir.var_to_mir var).name) !Cli.var_info_debug
in
let new_def, new_ctx =
match def.var_definition with
match def with
| InputVar -> (Mir.InputVar, ctx)
| SimpleVar e ->
if peval_debug then
Expand Down Expand Up @@ -698,11 +698,7 @@ let partially_evaluate_stmt (stmt : stmt) (block_id : block_id)
(TableVar (size, IndexTable (Mir.IndexMap.map fst es')), new_ctx)
)
in
let new_stmt =
Pos.same_pos_as
(SAssign (var, { def with var_definition = new_def }))
stmt
in
let new_stmt = Pos.same_pos_as (SAssign (var, new_def)) stmt in
(new_stmt :: new_block, new_ctx)
| SConditional (e, b1, b2, join) -> (
let new_e, d =
Expand Down

0 comments on commit 03e2fd2

Please sign in to comment.