Skip to content

Commit 4a714a9

Browse files
authored
More precise prove_variant function (#408)
1 parent 300a818 commit 4a714a9

File tree

3 files changed

+48
-51
lines changed

3 files changed

+48
-51
lines changed

middle_end/flambda/types/flambda_type.mli

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,15 +514,15 @@ val prove_naked_int64s : Typing_env.t -> t -> Numbers.Int64.Set.t proof
514514

515515
val prove_naked_nativeints : Typing_env.t -> t -> Targetint.Set.t proof
516516

517-
type variant_proof = private {
518-
const_ctors : Target_imm.Set.t;
517+
type variant_like_proof = private {
518+
const_ctors : Target_imm.Set.t Or_unknown.t;
519519
non_const_ctors_with_sizes : Targetint.OCaml.t Tag.Scannable.Map.t;
520520
}
521521

522-
val prove_variant
522+
val prove_variant_like
523523
: Typing_env.t
524524
-> t
525-
-> variant_proof proof_allowing_kind_mismatch
525+
-> variant_like_proof proof_allowing_kind_mismatch
526526

527527
(** If [ty] is known to represent a boxed number or a tagged integer,
528528
[prove_is_a_boxed_number env ty] is [Proved kind]. [kind] is the kind of

middle_end/flambda/types/template/flambda_type.templ.ml

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -504,60 +504,57 @@ let prove_unique_tag_and_size env t
504504
| None -> Unknown
505505
| Some (tag, size) -> Proved (tag, size)
506506

507-
type variant_proof = {
508-
const_ctors : Target_imm.Set.t;
507+
type variant_like_proof = {
508+
const_ctors : Target_imm.Set.t Or_unknown.t;
509509
non_const_ctors_with_sizes : Targetint.OCaml.t Tag.Scannable.Map.t;
510510
}
511511

512-
let prove_variant env t : variant_proof proof_allowing_kind_mismatch =
512+
let prove_variant_like env t : variant_like_proof proof_allowing_kind_mismatch =
513513
(* Format.eprintf "prove_variant:@ %a\n%!" print t; *)
514514
match expand_head t env with
515-
| Const (Tagged_immediate _) -> Unknown
515+
| Const (Tagged_immediate imm) ->
516+
Proved {
517+
const_ctors = Known (Target_imm.Set.singleton imm);
518+
non_const_ctors_with_sizes = Tag.Scannable.Map.empty;
519+
}
516520
| Const _ -> Wrong_kind
517521
| Value (Ok (Variant blocks_imms)) ->
518-
begin match blocks_imms.immediates with
522+
begin match blocks_imms.blocks with
519523
| Unknown -> Unknown
520-
| Known imms ->
521-
let const_ctors : _ Or_unknown.t =
522-
match prove_naked_immediates env imms with
523-
| Unknown -> Unknown
524-
| Invalid -> Known Target_imm.Set.empty
525-
| Proved const_ctors -> Known const_ctors
526-
in
527-
match const_ctors with
524+
| Known blocks ->
525+
match Row_like.For_blocks.all_tags_and_sizes blocks with
528526
| Unknown -> Unknown
529-
| Known const_ctors ->
530-
let valid =
531-
Target_imm.Set.for_all Target_imm.is_non_negative const_ctors
527+
| Known non_const_ctors_with_sizes ->
528+
let non_const_ctors_with_sizes =
529+
Tag.Map.fold
530+
(fun tag size (result : _ Or_unknown.t) : _ Or_unknown.t ->
531+
match result with
532+
| Unknown -> Unknown
533+
| Known result ->
534+
match Tag.Scannable.of_tag tag with
535+
| None -> Unknown
536+
| Some tag ->
537+
Known (Tag.Scannable.Map.add tag size result))
538+
non_const_ctors_with_sizes
539+
(Or_unknown.Known Tag.Scannable.Map.empty)
532540
in
533-
if not valid then Invalid
534-
else
535-
match blocks_imms.blocks with
536-
| Unknown -> Unknown
537-
| Known blocks ->
538-
match Row_like.For_blocks.all_tags_and_sizes blocks with
541+
match non_const_ctors_with_sizes with
542+
| Unknown -> Unknown
543+
| Known non_const_ctors_with_sizes ->
544+
let const_ctors : _ Or_unknown.t =
545+
match blocks_imms.immediates with
539546
| Unknown -> Unknown
540-
| Known non_const_ctors_with_sizes ->
541-
let non_const_ctors_with_sizes =
542-
Tag.Map.fold
543-
(fun tag size (result : _ Or_bottom.t) : _ Or_bottom.t ->
544-
match result with
545-
| Bottom -> Bottom
546-
| Ok result ->
547-
match Tag.Scannable.of_tag tag with
548-
| None -> Bottom
549-
| Some tag ->
550-
Ok (Tag.Scannable.Map.add tag size result))
551-
non_const_ctors_with_sizes
552-
(Or_bottom.Ok Tag.Scannable.Map.empty)
553-
in
554-
match non_const_ctors_with_sizes with
555-
| Bottom -> Invalid
556-
| Ok non_const_ctors_with_sizes ->
557-
Proved {
558-
const_ctors;
559-
non_const_ctors_with_sizes;
560-
}
547+
| Known imms ->
548+
begin match prove_naked_immediates env imms with
549+
| Unknown -> Unknown
550+
| Invalid -> Known Target_imm.Set.empty
551+
| Proved const_ctors -> Known const_ctors
552+
end
553+
in
554+
Proved {
555+
const_ctors;
556+
non_const_ctors_with_sizes;
557+
}
561558
end
562559
| Value (Ok _) -> Invalid
563560
| Value Unknown -> Unknown

middle_end/flambda/unboxing/unbox_continuation_params.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,15 @@ end
460460
module Variant : sig
461461
type t
462462

463-
val create : T.variant_proof -> t
463+
val create : T.variant_like_proof -> t
464464

465465
val max_size : t -> Targetint.OCaml.t
466466

467-
val const_ctors : t -> Target_imm.Set.t
467+
val const_ctors : t -> Target_imm.Set.t Or_unknown.t
468468

469469
val non_const_ctors_with_sizes : t -> Targetint.OCaml.t Tag.Scannable.Map.t
470470
end = struct
471-
type t = T.variant_proof
471+
type t = T.variant_like_proof
472472

473473
let create variant = variant
474474

@@ -1059,7 +1059,7 @@ let rec make_unboxing_decision denv ~depth ~arg_types_by_use_id
10591059
DE.print denv
10601060
end
10611061
| Wrong_kind | Invalid | Unknown ->
1062-
match T.prove_variant (DE.typing_env denv) param_type with
1062+
match T.prove_variant_like (DE.typing_env denv) param_type with
10631063
| Proved variant ->
10641064
(*
10651065
Format.eprintf "Starting variant unboxing\n%!";

0 commit comments

Comments
 (0)