forked from ocaml/ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
CSE of involutions #410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chambart
wants to merge
11
commits into
ocaml-flambda:flambda2.0-stable
Choose a base branch
from
chambart:cse_boolean_not
base: flambda2.0-stable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
CSE of involutions #410
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e8901de
Add is_an_involution
chambart f1c5b35
Involutions are recorded in the CSE env
chambart 8036218
Involution examples
chambart 46ee9af
Start documenting missed optimisation
chambart d9655ec
Small fix
chambart abefc4a
Apply CSE on updated primitive
chambart f72f2ac
Test involution detection
chambart 176d3a0
Update involution.ml
mshinwell 04ee480
Update involution.ml
mshinwell 7f5c8d8
Turn an expected dead branch into a fatal_error
chambart 0fc2c2a
Register the canonical version of a primitive in the CSE env
chambart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # List of missed optimisations to look at later | ||
|
|
||
| This should be compiled to the identity. But the CSE environment seems to lose | ||
| the relationship in the join after the first switch. | ||
|
|
||
| let f x = | ||
| let not_x = if x then false else true in | ||
| if not_x then false else true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
|
|
||
| let f x = | ||
| not (not (not x)) | ||
|
|
||
| let g x = | ||
| ~- (~- (~- x)) | ||
|
|
||
| let h x = | ||
| Int64.(neg (neg (neg x))) | ||
|
|
||
| let i x = | ||
| Int32.(neg (neg (neg x))) | ||
|
|
||
| let j x = | ||
| Nativeint.(neg (neg (neg x))) | ||
|
|
||
| let k x = | ||
| ~-. (~-. (~-. x)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,13 +29,14 @@ let apply_cse dacc ~original_prim = | |
| match DE.find_cse (DA.denv dacc) with_fixed_value with | ||
| | None -> None | ||
| | Some simple -> | ||
| let canonical = | ||
| match | ||
| TE.get_canonical_simple_exn (DA.typing_env dacc) simple | ||
| ~min_name_mode:NM.normal | ||
| ~name_mode_of_existing_simple:NM.normal | ||
| in | ||
| match canonical with | ||
| | exception Not_found -> None | ||
| with | ||
| | exception Not_found -> | ||
| (* CR pchambart: is this really reachable ? Should this be a fatal_error ? *) | ||
| None | ||
| | simple -> Some simple | ||
|
|
||
| let try_cse dacc ~original_prim ~simplified_args_with_tys ~min_name_mode | ||
|
|
@@ -47,12 +48,12 @@ let try_cse dacc ~original_prim ~simplified_args_with_tys ~min_name_mode | |
| if not (Name_mode.equal min_name_mode Name_mode.normal) then Not_applied dacc | ||
| else | ||
| let result_var = VB.var result_var in | ||
| match apply_cse dacc ~original_prim with | ||
| let args = List.map fst simplified_args_with_tys in | ||
| match apply_cse dacc ~original_prim:(P.update_args original_prim args) with | ||
|
||
| | Some replace_with -> | ||
| let named = Named.create_simple replace_with in | ||
| let ty = T.alias_type_of (P.result_kind' original_prim) replace_with in | ||
| let env_extension = TEE.one_equation (Name.var result_var) ty in | ||
| let args = List.map fst simplified_args_with_tys in | ||
| let simplified_named = | ||
| let cost_metrics = | ||
| Cost_metrics.notify_removed | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| (* TEST | ||
| * flambda | ||
| ** native | ||
| *) | ||
|
|
||
| (* This test that involutions are effectively detected. | ||
| It checks that for an involution f, f (f x) == x | ||
| This is is verified by testing that the result of the physical equality | ||
| is a known constant, through an allocation test. *) | ||
|
|
||
| external minor_words : unit -> (float [@unboxed]) | ||
| = "caml_gc_minor_words" "caml_gc_minor_words_unboxed" | ||
|
|
||
| let[@inline never] check_no_alloc test_name f x = | ||
| let before = minor_words () in | ||
| let _ = Sys.opaque_identity ((f[@inlined never]) x) in | ||
| let after = minor_words () in | ||
| let diff = after -. before in | ||
| if diff = 0. then | ||
| Format.printf "No allocs for test '%s'@." test_name | ||
| else | ||
| Format.printf "Some allocs for test '%s'@." test_name | ||
|
|
||
| let () = | ||
| let[@inline] test_known f = | ||
| let tester x = | ||
| let x = Sys.opaque_identity x in | ||
| let n = if (f[@inlined hint]) ((f[@inlined hint]) x) == x then 1 else 0 in | ||
| Int64.of_int n | ||
| in | ||
| let () = Sys.opaque_identity () in | ||
| tester | ||
| in | ||
|
|
||
| (* Test the test: this should allocate *) | ||
| check_no_alloc "not an involution" (test_known (succ)) 1; | ||
| (* Actual tests *) | ||
| check_no_alloc "not" (test_known (not)) true; | ||
| check_no_alloc "~-." (test_known (~-.)) 42.; | ||
| check_no_alloc "~-" (test_known (~-)) 42; | ||
| check_no_alloc "Int32.neg" (test_known Int32.neg) 42l; | ||
| check_no_alloc "Int64.neg" (test_known Int64.neg) 42L; | ||
| check_no_alloc "Nativeint.neg" (test_known Nativeint.neg) 42n; | ||
| () | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably should be a fatal error, let's try.