Skip to content

Commit d570c77

Browse files
authored
Improve SIMD constant error messages (#2466)
* fix * errors * add error tests
1 parent c64943a commit d570c77

33 files changed

+513
-8
lines changed

backend/cmm_builtins.ml

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ open Cmm
1717
open Cmm_helpers
1818
open Arch
1919

20+
type error = Bad_immediate of string
21+
22+
exception Error of error
23+
24+
let bad_immediate fmt =
25+
Format.kasprintf (fun msg -> raise (Error (Bad_immediate msg))) fmt
26+
2027
let four_args name args =
2128
match args with
2229
| [arg1; arg2; arg3; arg4] -> arg1, arg2, arg3, arg4
@@ -193,14 +200,14 @@ let rec const_float_args n args name =
193200
match n, args with
194201
| 0, [] -> []
195202
| n, Cconst_float (f, _) :: args -> f :: const_float_args (n - 1) args name
196-
| _ -> Misc.fatal_errorf "Invalid float constant arguments for %s" name
203+
| _ -> bad_immediate "Did not find constant float arguments for %s" name
197204

198205
(* Assumes untagged int or unboxed int32, always representable by int63 *)
199206
let rec const_int_args n args name =
200207
match n, args with
201208
| 0, [] -> []
202209
| n, Cconst_int (i, _) :: args -> i :: const_int_args (n - 1) args name
203-
| _ -> Misc.fatal_errorf "Invalid int constant arguments for %s" name
210+
| _ -> bad_immediate "Did not find constant int arguments for %s" name
204211

205212
(* Assumes unboxed int64: no tag, comes as Cconst_int when representable by
206213
int63, otherwise we get Cconst_natint *)
@@ -211,23 +218,23 @@ let rec const_int64_args n args name =
211218
Int64.of_int i :: const_int64_args (n - 1) args name
212219
| n, Cconst_natint (i, _) :: args ->
213220
Int64.of_nativeint i :: const_int64_args (n - 1) args name
214-
| _ -> Misc.fatal_errorf "Invalid int64 constant arguments for %s" name
221+
| _ -> bad_immediate "Did not find constant int64 arguments for %s" name
215222

216223
let int64_of_int8 i =
217224
(* CR mslater: (SIMD) replace once we have unboxed int8 *)
218225
if i < 0 || i > 0xff
219-
then Misc.fatal_errorf "Int8 constant must be in [0x0,0xff]: %016x" i;
226+
then bad_immediate "Int8 constant not in range [0x0,0xff]: 0x%016x" i;
220227
Int64.of_int i
221228

222229
let int64_of_int16 i =
223230
(* CR mslater: (SIMD) replace once we have unboxed int16 *)
224231
if i < 0 || i > 0xffff
225-
then Misc.fatal_errorf "Int16 constant must be in [0x0,0xffff]: %016x" i;
232+
then bad_immediate "Int16 constant not in range [0x0,0xffff]: 0x%016x" i;
226233
Int64.of_int i
227234

228235
let int64_of_int32 i =
229236
if i < Int32.to_int Int32.min_int || i > Int32.to_int Int32.max_int
230-
then Misc.fatal_errorf "Constant was not an int32: %016x" i;
237+
then bad_immediate "Int32 constant not in range [0x0,0xffffffff]: 0x%016x" i;
231238
Int64.of_int i |> Int64.logand 0xffffffffL
232239

233240
let int64_of_float32 f =
@@ -749,3 +756,11 @@ let extcall ~dbg ~returns ~alloc ~is_c_builtin ~effects ~coeffects ~ty_args name
749756
| Some op -> op
750757
| None -> default
751758
else default
759+
760+
let report_error ppf = function
761+
| Bad_immediate msg -> Format.pp_print_string ppf msg
762+
763+
let () =
764+
Location.register_error_of_exn (function
765+
| Error err -> Some (Location.error_of_printer_file report_error err)
766+
| _ -> None)

tests/simd/dune

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@
5050
(rule
5151
(alias runtest)
5252
(enabled_if
53-
(and (= %{context_name} "main")
54-
(= %{architecture} amd64)))
53+
(and
54+
(= %{context_name} "main")
55+
(= %{architecture} amd64)))
5556
(action
5657
(progn
5758
(diff empty.expected basic.out)

0 commit comments

Comments
 (0)