@@ -26,15 +26,6 @@ let ctx_arrays context = context.arrays
2626
2727type buffer_ptr = ctx_array [@@ deriving sexp_of ]
2828
29- (* * Alternative approach:
30-
31- {[
32- type buffer_ptr = unit Ctypes_static.ptr
33-
34- let sexp_of_buffer_ptr ptr = Sexp.Atom (Ops.ptr_to_string ptr Ops.Void_prec)
35- let buffer_ptr ctx_array = Ndarray.get_voidptr ctx_array
36- ]} *)
37-
3829let buffer_ptr ctx_array = ctx_array
3930
4031let alloc_buffer ?old_buffer ~size_in_bytes () =
@@ -112,18 +103,38 @@ let c_compile_and_load ~f_name =
112103 while rc = 0 && (not @@ (Stdlib.Sys. file_exists libname && Stdlib.Sys. file_exists log_fname)) do
113104 Unix. sleepf 0.001
114105 done ;
115- ( if rc <> 0 then
116- let errors =
117- " Cc_backend.c_compile_and_load: compilation failed with errors:\n "
118- ^ Stdio.In_channel. read_all log_fname
119- in
120- Stdio. prerr_endline errors;
121- invalid_arg errors);
106+ if rc <> 0 then (
107+ let errors =
108+ " Cc_backend.c_compile_and_load: compilation failed with errors:\n "
109+ ^ Stdio.In_channel. read_all log_fname
110+ in
111+ Stdio. prerr_endline errors;
112+ invalid_arg errors);
122113 (* Note: RTLD_DEEPBIND not available on MacOS. *)
123114 let result = { lib = Dl. dlopen ~filename: libname ~flags: [ RTLD_NOW ]; libname } in
124115 Stdlib.Gc. finalise (fun lib -> Dl. dlclose ~handle: lib.lib) result;
125116 result
126117
118+ module C_syntax_config (Input : sig
119+ val for_lowereds : Low_level .optimized array
120+ val opt_ctx_arrays : (Tn .t , buffer_ptr , Tn .comparator_witness ) Base.Map .t option
121+ end ) =
122+ struct
123+ let for_lowereds = Input. for_lowereds
124+
125+ type nonrec ctx_array = ctx_array
126+
127+ let opt_ctx_arrays = Input. opt_ctx_arrays
128+ let hardcoded_context_ptr = Some Ndarray. c_ptr_to_string
129+ let is_in_context = is_in_context
130+ let host_ptrs_for_readonly = true
131+ let logs_to_stdout = false
132+ let main_kernel_prefix = " "
133+ let kernel_prep_line = " "
134+ let extra_include_lines = []
135+ let typ_of_prec = Ops. c_typ_of_prec
136+ end
137+
127138let % diagn_sexp compile ~(name : string ) ~opt_ctx_arrays bindings (lowered : Low_level.optimized ) =
128139 let opt_ctx_arrays =
129140 Option. map opt_ctx_arrays ~f: (fun ctx_arrays ->
@@ -140,20 +151,10 @@ let%diagn_sexp compile ~(name : string) ~opt_ctx_arrays bindings (lowered : Low_
140151 else ctx_arrays
141152 | Some _ -> ctx_arrays))
142153 in
143- let module Syntax = Backend_utils. C_syntax (struct
154+ let module Syntax = Backend_utils. C_syntax (C_syntax_config ( struct
144155 let for_lowereds = [| lowered |]
145-
146- type nonrec ctx_array = ctx_array
147-
148156 let opt_ctx_arrays = opt_ctx_arrays
149- let hardcoded_context_ptr = Some Ndarray. c_ptr_to_string
150- let is_in_context = is_in_context
151- let host_ptrs_for_readonly = true
152- let logs_to_stdout = false
153- let main_kernel_prefix = " "
154- let kernel_prep_line = " "
155- let extra_include_lines = []
156- end ) in
157+ end )) in
157158 (* FIXME: do we really want all of them, or only the used ones? *)
158159 let idx_params = Indexing. bound_symbols bindings in
159160 let pp_file = Utils. pp_file ~base_name: name ~extension: " .c" in
@@ -183,20 +184,10 @@ let%diagn_sexp compile_batch ~names ~opt_ctx_arrays bindings
183184 else ctx_arrays
184185 | Some _ -> ctx_arrays)))
185186 in
186- let module Syntax = Backend_utils. C_syntax (struct
187+ let module Syntax = Backend_utils. C_syntax (C_syntax_config ( struct
187188 let for_lowereds = for_lowereds
188-
189- type nonrec ctx_array = ctx_array
190-
191189 let opt_ctx_arrays = opt_ctx_arrays
192- let hardcoded_context_ptr = Some Ndarray. c_ptr_to_string
193- let is_in_context = is_in_context
194- let host_ptrs_for_readonly = true
195- let logs_to_stdout = false
196- let main_kernel_prefix = " "
197- let kernel_prep_line = " "
198- let extra_include_lines = []
199- end ) in
190+ end )) in
200191 (* FIXME: do we really want all of them, or only the used ones? *)
201192 let idx_params = Indexing. bound_symbols bindings in
202193 let global_ctx_arrays =
@@ -270,13 +261,11 @@ let%diagn_sexp link_compiled ~merge_buffer (prior_context : context) (code : pro
270261 | bs , Log_file_name :: ps ->
271262 Param_1 (ref (Some log_file_name), link bs ps Ctypes. (string @-> cs))
272263 | bs , Merge_buffer :: ps ->
273- let get_ptr (buffer , _ ) = Ndarray. get_voidptr buffer in
264+ let get_ptr (buffer , _ ) = Ndarray. get_voidptr_not_managed buffer in
274265 Param_2f (get_ptr, merge_buffer, link bs ps Ctypes. (ptr void @-> cs))
275266 | bs , Param_ptr tn :: ps ->
276267 let nd = match Map. find arrays tn with Some nd -> nd | None -> assert false in
277- (* let f ba = Ctypes.bigarray_start Ctypes_static.Genarray ba in let c_ptr =
278- Ndarray.(map { f } nd) in *)
279- let c_ptr = Ndarray. get_voidptr nd in
268+ let c_ptr = Ndarray. get_voidptr_not_managed nd in
280269 Param_2 (ref (Some c_ptr), link bs ps Ctypes. (ptr void @-> cs))
281270 in
282271 (* Reverse the input order because [Indexing.apply] will reverse it again. Important:
0 commit comments