Skip to content
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

optimize cmm shifts and tags #3669

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Conversation

jvanburen
Copy link
Contributor

add some optimizations for shifts and tags in preparation for cmm-scalar-type, which needs these to keep decent performance

@jvanburen jvanburen requested a review from Gbury March 7, 2025 17:58
@jvanburen
Copy link
Contributor Author

Array.length gets compiled worse now, i need to fix that

-      (+ (<< (or (>>u (<< (load_mut int (+a odata/3502 -8)) 8) 17) 1) 1) -1))
+      (+ (or (<< (>>u (<< (load_mut int (+a odata/3502 -8)) 8) 17) 1) 2) -1))

the latter doesn't turn into a lea on x86

@jvanburen jvanburen force-pushed the jvb/optimize-cmm-shifts-and-tags branch from 3468a12 to 529a6d8 Compare March 10, 2025 14:14
This will be used by a future pr to simplify casting without impacting
performance as much
@jvanburen jvanburen force-pushed the jvb/optimize-cmm-shifts-and-tags branch from 2d05f61 to e1c12d7 Compare March 10, 2025 15:30
@jvanburen jvanburen force-pushed the jvb/optimize-cmm-shifts-and-tags branch from cd768b1 to 93513c0 Compare March 10, 2025 21:26
@jvanburen
Copy link
Contributor Author

array length looks strictly better now, the only problems are in cmm-scalar-type, which i'll fix in yet another PR

@jvanburen jvanburen marked this pull request as ready for review March 10, 2025 21:37
Copy link
Contributor

@Gbury Gbury left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR should be good to merge once comment have been adressed/answered.

Copy link
Contributor

@Gbury Gbury left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few comments/questions, but as far as code semantics goes, we should be good !

| Cop (Csubi, [c; Cconst_int (x, _)], _) when Misc.no_overflow_sub n x ->
add_const c (n - x) dbg
| c -> Cop (Caddi, [c; Cconst_int (n, dbg)], dbg)
map_tail1 c ~f:(fun c ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you encounter cases where the map_tail1 was important ? if so it would be nice to document them (at least some of the general shape/conditions when it can happen)

| Cop (Caddi, [c1; Cconst_int (n1, _)], _), c2 ->
add_const (sub_int c1 c2 dbg) n1 dbg
| c1, c2 -> Cop (Csubi, [c1; c2], dbg)
map_tail2 c1 c2 ~f:(fun c1 c2 ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question as above: if only for curiosity/better understanding, what kind of situations lead to map_tail2 being needed/useful ?

add_const (add_int c1 c2 dbg) n2 dbg
| c1, Cop (Cor, [c2; Cconst_int (n2, _)], _)
when can_interchange_add_with_or c2 n2 ->
add_const (add_int c1 c2 dbg) n2 dbg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be simpler in this case to call prefer_add on c1 and c2 before the match ?

| Cop (Cxor, [x; ((Cconst_int _ | Cconst_natint _) as y)], _) ->
xor_int (lsl_int x c2 dbg) (lsl_int y c2 dbg) dbg
| c1 -> Cop (Clsl, [c1; c2], dbg)))
| Cop (Clsl, [x; (Cconst_int (n', _) as y)], z), c2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| Cop (Clsl, [x; (Cconst_int (n', _) as y)], z), c2
| Cop (Clsl, [x; (Cconst_int (n', _) as y)], dbg'), c2

this code is already tricky as is without having to remember that z is a debuginfo, contrary to x and y, ^^

Cop
(Cor, [asr_int c (Cconst_int (n - 1, dbg)) dbg; Cconst_int (1, dbg)], dbg)
| c -> incr_int (lsl_int c (Cconst_int (1, dbg)) dbg) dbg
or_const (asr_const c (n - 1) dbg) 1n dbg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check whether is_defined_shift n here ? This code is correct regardless , so it might deserve a comment to explain/explicit that it doesn't matter here.

or_const (asr_const c (n - 1) dbg) 1n dbg
| Cop (Clsr, [c; Cconst_int (n, _)], _) when n > 0 ->
or_const (lsr_const c (n - 1) dbg) 1n dbg
| c -> incr_int (lsl_const c 1 dbg) dbg

let untag_int i dbg =
match i with
| Cconst_int (n, _) -> Cconst_int (n asr 1, dbg)
| Cop (Cor, [Cop (Casr, [c; Cconst_int (n, _)], _); Cconst_int (1, _)], _)
when n > 0 && n < (size_int * 8) - 1 ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
when n > 0 && n < (size_int * 8) - 1 ->
when n > 0 && is_defined_shift (n + 1) ->

might be simpler and more consistent ?

or_int (sign_extend ~bits x ~dbg) (sign_extend ~bits y ~dbg) dbg
| Cop (Cxor, [x; y], _) when is_constant y ->
xor_int (sign_extend ~bits x ~dbg) (sign_extend ~bits y ~dbg) dbg
| Cop (((Casr | Clsr) as op), [inner; Cconst_int (n, _)], _) as e
when 0 <= n && n < arch_bits ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
when 0 <= n && n < arch_bits ->
when (is_defined_shift n) ->

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants