Skip to content

Commit 3aa50a9

Browse files
authored
Merge pull request #1498 from stan-dev/pre-generate-signatures
Generate signature map at build time
2 parents 2033733 + 9c82399 commit 3aa50a9

File tree

31 files changed

+2538
-2484
lines changed

31 files changed

+2538
-2484
lines changed

Jenkinsfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ pipeline {
260260

261261
sh '''
262262
eval $(opam env)
263-
bisect-ppx-report summary --expect src/ --do-not-expect src/stancjs/
264-
bisect-ppx-report coveralls coverage.json --service-name jenkins --service-job-id $BUILD_ID --expect src/ --do-not-expect src/stancjs/
263+
bisect-ppx-report summary --expect src/ --do-not-expect src/stancjs/ --do-not-expect src/stan_math_signatures/Generate.ml
264+
bisect-ppx-report coveralls coverage.json --service-name jenkins --service-job-id $BUILD_ID --expect src/ --do-not-expect src/stancjs/ --do-not-expect src/stan_math_signatures/Generate.ml
265265
'''
266266

267267
withCredentials([usernamePassword(credentialsId: 'stan-stanc3-codecov-token', usernameVariable: 'DUMMY_USERNAME', passwordVariable: 'CODECOV_TOKEN')]) {

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ testcoverage:
1010
@find . -name '*.coverage' | xargs rm -f
1111
dune clean
1212
BISECT_FILE=`pwd`/bisect dune runtest --instrument-with bisect_ppx --force
13-
bisect-ppx-report html --expect src/ --do-not-expect src/stancjs/
14-
bisect-ppx-report summary --expect src/ --do-not-expect src/stancjs/
13+
bisect-ppx-report html --expect src/ --do-not-expect src/stancjs/ --do-not-expect src/stan_math_signatures/Generate.ml
14+
bisect-ppx-report summary --expect src/ --do-not-expect src/stancjs/ --do-not-expect src/stan_math_signatures/Generate.ml
1515
@rm *.coverage
1616

1717
format:

docs/exposing_new_functions.mld

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
For a function to be built into Stan, it has to be included in the Stan Math library and its signature has to be
88
exposed to the compiler.
99

10-
To do the latter, we have to add a corresponding line in [src/middle/Stan_math_signatures.ml].
11-
The compiler uses the signatures defined there to do type checking.
10+
To do the latter, we have to add a corresponding line in [src/stan_math_signatures/Generate.ml].
11+
The compiler uses the signatures defined there to do type checking. (Aside: this file generates a
12+
binary representation of the signatures, which is then stored in the executable. This is faster
13+
than re-generating the list of signatures every time the compiler is run.)
1214

1315

1416
{1 Adding a distribution function }

docs/index.mld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Frontend
5454
Middle
5555
Common
5656
Analysis_and_optimization
57+
Stan_math_signatures
5758
Stan_math_backend
5859
}
5960

src/analysis_and_optimization/Memory_patterns.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,12 @@ let query_stan_math_mem_pattern_support (name : string)
9797
let namematches = lookup_stan_math_function name in
9898
let filteredmatches =
9999
List.filter
100-
~f:(fun x ->
101-
Frontend.SignatureMismatch.check_compatible_arguments_mod_conv
102-
(snd3 x) args
100+
~f:(fun (x, _, _, _) ->
101+
Frontend.SignatureMismatch.check_compatible_arguments_mod_conv x
102+
args
103103
|> Result.is_ok)
104104
namematches in
105-
let is_soa ((_ : UnsizedType.returntype), _, mem) =
106-
mem = Mem_pattern.SoA in
105+
let is_soa = function _, _, _, Mem_pattern.SoA -> true | _ -> false in
107106
List.exists ~f:is_soa filteredmatches
108107

109108
(*Validate whether a function can support SoA matrices*)

src/analysis_and_optimization/dune

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
(library
22
(name analysis_and_optimization)
33
(public_name stanc.analysis)
4-
(libraries core middle yojson frontend)
4+
(libraries core middle yojson frontend stan_math_signatures)
55
(instrumentation
66
(backend bisect_ppx))
77
(inline_tests)
88
;; TODO: Not sure what's going on but it's throwing an error that this module has no implementation
99
(modules_without_implementation monotone_framework_sigs)
1010
(preprocess
11-
(pps ppx_jane ppx_deriving.map ppx_deriving.fold)))
11+
(pps ppx_jane)))

src/common/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
(backend bisect_ppx))
77
(inline_tests)
88
(preprocess
9-
(pps ppx_jane ppx_deriving.map ppx_deriving.fold ppx_deriving.create)))
9+
(pps ppx_jane ppx_deriving.map ppx_deriving.fold)))

src/frontend/Environment.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ let stan_math_environment =
2929
Stan_math_signatures.get_stan_math_signatures_alist ()
3030
|> List.map ~f:(fun (key, values) ->
3131
( key
32-
, List.map values ~f:(fun (rt, args, mem) ->
33-
let type_ =
34-
UnsizedType.UFun
35-
(args, rt, Fun_kind.suffix_from_name key, mem) in
36-
{type_; kind= `StanMath}) ))
32+
, List.map values ~f:(fun s ->
33+
{type_= UnsizedType.UFun s; kind= `StanMath}) ))
3734
|> String.Map.of_alist_exn in
3835
functions
3936

src/frontend/SignatureMismatch.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ let pp_math_lib_assignmentoperator_sigs ppf (lt, op) =
424424
Stan_math_signatures.make_assignmentoperator_stan_math_signatures op in
425425
let errors =
426426
List.filter
427-
~f:(fun (_, args, _) ->
427+
~f:(fun (args, _, _, _) ->
428428
Result.is_ok (check_same_type 0 lt (snd (List.hd_exn args))))
429429
errors in
430430
match List.split_n errors max_n_errors with
@@ -433,7 +433,7 @@ let pp_math_lib_assignmentoperator_sigs ppf (lt, op) =
433433
| errors, _ -> Some (errors, true) in
434434
let pp_sigs ppf (signatures, omitted) =
435435
Fmt.pf ppf "@[<v>%a%a@]"
436-
(Fmt.list ~sep:Fmt.cut Stan_math_signatures.pp_math_sig)
436+
(Fmt.list ~sep:Fmt.cut UnsizedType.pp_math_sig)
437437
signatures
438438
(if omitted then Fmt.pf else Fmt.nop)
439439
"@ (Additional signatures omitted)" in

src/frontend/Typechecker.ml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,9 @@ let check_normal_fn ~is_cond_dist loc tenv id es =
501501
| [] ->
502502
(match Utils.split_distribution_suffix id.name with
503503
| Some (prefix, suffix) -> (
504-
let known_families =
505-
List.map
506-
~f:(fun (_, y, _, _) -> y)
507-
Stan_math_signatures.distributions in
508504
let is_known_family s =
509-
List.mem known_families s ~equal:String.equal in
505+
List.Assoc.mem Stan_math_signatures.distributions s
506+
~equal:String.equal in
510507
match suffix with
511508
| ("lpmf" | "lupmf") when Env.mem tenv (prefix ^ "_lpdf") ->
512509
Semantic_error.returning_fn_expected_wrong_dist_suffix_found loc
@@ -663,8 +660,8 @@ and check_reduce_sum ~is_cond_dist loc cf tenv id tes =
663660
|> error
664661

665662
and check_variadic ~is_cond_dist loc cf tenv id tes =
666-
let Stan_math_signatures.
667-
{control_args; required_fn_args; required_fn_rt; return_type} =
663+
let UnsizedType.{control_args; required_fn_args; required_fn_rt; return_type}
664+
=
668665
Stan_math_signatures.lookup_stan_math_variadic_function id.name
669666
|> Option.value_exn in
670667
let matching remaining_es Env.{type_= ftype; _} =

0 commit comments

Comments
 (0)