Skip to content

Commit

Permalink
Merge pull request #1351 from stan-dev/fix/partial-eval-mod-0
Browse files Browse the repository at this point in the history
Fix: Optimizations crash on mod 0 partial evaluation
  • Loading branch information
WardBrian authored Aug 28, 2023
2 parents d5c25b6 + 743d0dd commit 2fea142
Show file tree
Hide file tree
Showing 12 changed files with 16,789 additions and 31,340 deletions.
2 changes: 1 addition & 1 deletion src/analysis_and_optimization/Partial_evaluator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ let rec eval_expr ?(preserve_stability = false) (e : Expr.Typed.t) =
| "PPlus__" | "PMinus__" ->
apply_prefix_operator_real op (Float.of_string r)
| _ -> FunApp (kind, l) )
| ( ("Divide__" | "IntDivide__")
| ( ("Divide__" | "IntDivide__" | "Modulo__")
, [{meta= {type_= UInt; _}; _}; {pattern= Lit (Int, i2); _}] )
when Int.of_string i2 = 0 ->
raise (Rejected (e.meta.loc, "Integer division by zero"))
Expand Down
18 changes: 9 additions & 9 deletions src/stanc/stanc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ let get_ast_or_exit ?printed_filename ?(print_warnings = true)
Errors.pp ?printed_filename Fmt.stderr err ;
exit 1

let print_sexp sexp =
let ppf = Format.std_formatter in
Format.pp_set_margin ppf 90 ;
Sexp.pp_hum ppf sexp

let type_ast_or_exit ?printed_filename ast =
match Typechecker.check_program ast with
| Result.Ok (p, warns) ->
Expand Down Expand Up @@ -337,8 +342,7 @@ let use_file filename =
Debugging.typed_ast_logger typed_ast ;
if not !pretty_print_program then (
let mir = Ast_to_Mir.trans_prog filename typed_ast in
if !dump_mir then
Sexp.pp_hum Format.std_formatter [%sexp (mir : Middle.Program.Typed.t)] ;
if !dump_mir then print_sexp [%sexp (mir : Middle.Program.Typed.t)] ;
if !dump_mir_pretty then Program.Typed.pp Format.std_formatter mir ;
if !warn_pedantic then
Pedantic_analysis.warn_pedantic mir
Expand All @@ -347,8 +351,7 @@ let use_file filename =
Pedantic_analysis.warn_uninitialized mir
|> pp_stderr (Warnings.pp_warnings ?printed_filename) ;
let tx_mir = Transform_Mir.trans_prog mir in
if !dump_tx_mir then
Sexp.pp_hum Format.std_formatter [%sexp (tx_mir : Middle.Program.Typed.t)] ;
if !dump_tx_mir then print_sexp [%sexp (tx_mir : Middle.Program.Typed.t)] ;
if !dump_tx_mir_pretty then Program.Typed.pp Format.std_formatter tx_mir ;
let opt_mir =
let set_optims =
Expand All @@ -359,14 +362,11 @@ let use_file filename =
Optimize.optimization_suite ~settings:set_optims tx_mir in
if !dump_mem_pattern then
Memory_patterns.pp_mem_patterns Format.std_formatter opt_mir ;
if !dump_opt_mir then
Sexp.pp_hum Format.std_formatter
[%sexp (opt_mir : Middle.Program.Typed.t)] ;
if !dump_opt_mir then print_sexp [%sexp (opt_mir : Middle.Program.Typed.t)] ;
if !dump_opt_mir_pretty then Program.Typed.pp Format.std_formatter opt_mir ;
if !output_file = "" then output_file := remove_dotstan !model_file ^ ".hpp" ;
let cpp = Lower_program.lower_program ?printed_filename opt_mir in
if !dump_lir then
Sexp.pp_hum Format.std_formatter [%sexp (cpp : Cpp.program)] ;
if !dump_lir then print_sexp [%sexp (cpp : Cpp.program)] ;
let cpp_str = Fmt.(to_to_string Cpp.Printing.pp_program) cpp in
Out_channel.write_all !output_file ~data:cpp_str ;
if !print_model_cpp then print_endline cpp_str )
Expand Down
11,583 changes: 4,267 additions & 7,316 deletions test/integration/good/code-gen/lir.expected

Large diffs are not rendered by default.

6,793 changes: 2,218 additions & 4,575 deletions test/integration/good/code-gen/mir.expected

Large diffs are not rendered by default.

454 changes: 152 additions & 302 deletions test/integration/good/code-gen/profiling/transformed_mir.expected

Large diffs are not rendered by default.

10,911 changes: 3,540 additions & 7,371 deletions test/integration/good/code-gen/transformed_mir.expected

Large diffs are not rendered by default.

Loading

0 comments on commit 2fea142

Please sign in to comment.