Skip to content

Commit fd99c07

Browse files
committed
cc_backend: remove obsolete builtins obj; simpler approach to compile errors, help from Claude
Signed-off-by: Lukasz Stafiniak <lukstafi@gmail.com>
1 parent 5053274 commit fd99c07

File tree

2 files changed

+38
-66
lines changed

2 files changed

+38
-66
lines changed

arrayjit/lib/cc_backend.ml

Lines changed: 37 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ let%track7_sexp c_compile_and_load ~f_path =
6161
(* There can be only one library with a given name, the object gets cached. Moreover, [Dl.dlclose]
6262
is not required to unload the library, although ideally it should. *)
6363
let run_id = Int.to_string @@ get_global_run_id () in
64-
let log_fname = base_name ^ "_run_id_" ^ run_id ^ ".log" in
6564
let libname =
6665
let file_stem = Stdlib.Filename.chop_extension @@ Stdlib.Filename.basename f_path in
6766
if Utils.get_global_flag ~default:false ~arg_name:"output_dlls_in_build_directory" then
@@ -71,7 +70,6 @@ let%track7_sexp c_compile_and_load ~f_path =
7170
(* Use temp_file without the run_id component *)
7271
Stdlib.Filename.temp_file file_stem (if Sys.win32 then ".dll" else ".so")
7372
in
74-
(try Stdlib.Sys.remove log_fname with _ -> ());
7573
(try Stdlib.Sys.remove libname with _ -> ());
7674
let kernel_link_flags =
7775
match Sys.os_type with
@@ -82,84 +80,58 @@ let%track7_sexp c_compile_and_load ~f_path =
8280
| "Win32" | "Cygwin" -> "-shared"
8381
| _ -> "-shared -fPIC"
8482
in
85-
(* On Windows, we need to link with the builtins library *)
86-
let builtins_lib =
87-
if String.equal Sys.os_type "Win32" || String.equal Sys.os_type "Cygwin" then
88-
(* Try to find the builtins object file in the build directory *)
89-
let paths =
90-
[
91-
"_build/default/arrayjit/lib/builtins.o";
92-
"_build/default/arrayjit/lib/libir_stubs.a";
93-
"arrayjit/lib/builtins.o";
94-
]
95-
in
96-
match List.find ~f:Stdlib.Sys.file_exists paths with Some path -> " " ^ path | None -> ""
97-
else ""
98-
in
83+
let temp_log = Stdlib.Filename.temp_file "ocannl_cc_" ".log" in
9984
let cmdline : string =
100-
Printf.sprintf "%s %s%s -O%d -o %s %s >> %s 2>&1" (compiler_command ()) f_path builtins_lib
101-
(optimization_level ()) libname kernel_link_flags log_fname
102-
in
103-
(* Debug: write the command to the log file *)
104-
let () =
105-
let oc = Stdio.Out_channel.create ~append:false log_fname in
106-
Stdio.Out_channel.fprintf oc "Command: %s\n" cmdline;
107-
Stdio.Out_channel.fprintf oc "Builtins lib: '%s'\n" builtins_lib;
108-
Stdio.Out_channel.close oc
85+
Printf.sprintf "%s %s -O%d -o %s %s > %s 2>&1" (compiler_command ()) f_path
86+
(optimization_level ()) libname kernel_link_flags temp_log
10987
in
88+
(* Debug: log the command if debugging is enabled *)
89+
[%log3 "command", cmdline];
11090
let rc : int = Stdlib.Sys.command cmdline in
111-
(* Note: it seems waiting for the file to exist is necessary here and below regardless of needing
112-
the logs. *)
91+
(if rc <> 0 then (
92+
let compiler_output =
93+
try Stdio.In_channel.read_all temp_log with _ -> "(unable to read compiler output)"
94+
in
95+
(try Stdlib.Sys.remove temp_log with _ -> ());
96+
invalid_arg
97+
@@ Printf.sprintf
98+
"OCANNL cc backend: generated code failed to compile (exit code %d).\n\
99+
This is a bug in OCANNL. Please file an issue with the generated .c file at %s\n\
100+
Compilation command: %s\n\
101+
Compiler output:\n\
102+
%s"
103+
rc f_path cmdline compiler_output)
104+
else try Stdlib.Sys.remove temp_log with _ -> ());
105+
(* Wait a moment for the file to be fully written on success *)
113106
let start_time = Unix.gettimeofday () in
114-
let wait_counter = ref 1 in
115107
let timeout =
116108
Float.of_string
117-
@@ Utils.get_global_arg ~default:"3600.0" ~arg_name:"cc_backend_post_compile_timeout"
109+
@@ Utils.get_global_arg ~default:"10.0" ~arg_name:"cc_backend_post_compile_timeout"
118110
in
119-
while rc = 0 && (not @@ (Stdlib.Sys.file_exists libname && Stdlib.Sys.file_exists log_fname)) do
111+
while not (Stdlib.Sys.file_exists libname) do
120112
let elapsed = Unix.gettimeofday () -. start_time in
121113
if Float.(elapsed > timeout) then
122-
failwith "Cc_backend.c_compile_and_load: timeout waiting for compilation files to appear";
123-
wait_counter := !wait_counter + 1;
124-
if !wait_counter = 3000 then
125-
Stdio.printf "Cc_backend.c_compile_and_load: waiting for compilation files to appear%!";
126-
if !wait_counter > 3000 && !wait_counter % 1000 = 0 then Stdio.printf ".%!";
127-
if !wait_counter % 30000 = 0 then Stdio.printf "\n%!";
114+
failwith
115+
@@ Printf.sprintf
116+
"Cc_backend.c_compile_and_load: compiled library %s not found after successful \
117+
compilation"
118+
libname;
128119
Unix.sleepf 0.001
129120
done;
130-
if !wait_counter >= 3000 then Stdio.printf "\n%!";
131-
if rc <> 0 then (
132-
let errors =
133-
"Cc_backend.c_compile_and_load: compilation failed with errors:\n"
134-
^ Stdio.In_channel.read_all log_fname
135-
in
136-
Stdio.prerr_endline errors;
137-
invalid_arg errors);
138121
(* Expected to succeed on MacOS only. *)
139-
let sign_log_fname = base_name ^ "_run_id_" ^ run_id ^ "-sign.log" in
140-
let rc =
141-
Stdlib.Sys.command @@ Printf.sprintf "codesign -s - %s >> %s 2>&1" libname sign_log_fname
142-
in
143-
let start_time_sign = Unix.gettimeofday () in
144-
let timeout_sign = 1.0 in
145-
while
146-
rc = 0 && (not @@ (Stdlib.Sys.file_exists libname && Stdlib.Sys.file_exists sign_log_fname))
147-
do
148-
let elapsed_sign = Unix.gettimeofday () -. start_time_sign in
149-
if Float.(elapsed_sign > timeout_sign) then
150-
failwith "Cc_backend.c_compile_and_load: timeout waiting for codesign files to appear";
151-
Unix.sleepf 0.001
152-
done;
153122
let verify_codesign =
154123
Utils.get_global_flag ~default:false ~arg_name:"cc_backend_verify_codesign"
155124
in
156-
if verify_codesign && rc <> 0 then (
157-
let errors =
158-
"Cc_backend.c_compile_and_load: codesign failed with errors:\n"
159-
^ Stdio.In_channel.read_all sign_log_fname
160-
in
161-
Stdio.prerr_endline errors;
162-
invalid_arg errors);
125+
(if verify_codesign then
126+
let null_device = if Sys.win32 then "nul" else "/dev/null" in
127+
let rc =
128+
Stdlib.Sys.command @@ Printf.sprintf "codesign -s - %s > %s 2>&1" libname null_device
129+
in
130+
if rc <> 0 then
131+
invalid_arg
132+
@@ Printf.sprintf
133+
"Cc_backend.c_compile_and_load: codesign failed with exit code %d for library %s" rc
134+
libname);
163135
(* Note: RTLD_DEEPBIND not available on MacOS. *)
164136
let result = { lib = Dl.dlopen ~filename:libname ~flags:[ RTLD_NOW ]; libname } in
165137
let%track7_sexp finalize (lib : library) : unit = Dl.dlclose ~handle:lib.lib in

ocannl_config.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ limit_constant_fill_size=16
206206

207207
# The timeout for the CC backend to wait for the compilation files to appear, in seconds.
208208
# This is the wait after the compiler command exits (doesn't include the compilation time).
209-
cc_backend_post_compile_timeout=3600.0
209+
cc_backend_post_compile_timeout=10.0
210210

211211
# If true, the CC backend will fail if the codesign of the compiled dynamic library fails.
212212
cc_backend_verify_codesign=false

0 commit comments

Comments
 (0)