Skip to content

Comments

cranelift/x64: implement cls for all integer types#12644

Open
shreyasravi320 wants to merge 2 commits intobytecodealliance:mainfrom
shreyasravi320:cls-impl-x86_64
Open

cranelift/x64: implement cls for all integer types#12644
shreyasravi320 wants to merge 2 commits intobytecodealliance:mainfrom
shreyasravi320:cls-impl-x86_64

Conversation

@shreyasravi320
Copy link

@shreyasravi320 shreyasravi320 commented Feb 24, 2026

Fixes #5107
cls implemented with the identity cls(x) = clz(x ^ (x >> 1)) - 1
All integer types (i8, i16, i32, i64, and i128) handled
Added tests to filetests

@shreyasravi320 shreyasravi320 requested a review from a team as a code owner February 24, 2026 03:35
@shreyasravi320 shreyasravi320 requested review from alexcrichton and removed request for a team February 24, 2026 03:35
@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:area:x64 Issues related to x64 codegen labels Feb 24, 2026
Copy link
Member

Choose a reason for hiding this comment

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

For runnable tests we typically use the runtest directory instead of isa/x64. I see as well there's already a cls.clif file in that directory, so can that file be enabled for x64, and perhaps these tests merged there too?

Copy link
Author

Choose a reason for hiding this comment

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

Got it. Added x86 support in the cls test there, and added the i128 tests. The existing tests were pretty comprehensive so I just added the i128 tests in the same format.

Comment on lines 2364 to 2374
(rule 1 (do_cls $I64 src)
(let ((shifted Gpr (x64_sarq_mi src 1))
(xored Gpr (x64_xor $I64 src (RegMemImm.Reg shifted)))
(clz Gpr (do_clz $I64 $I64 xored)))
(x64_sub $I64 clz (RegMemImm.Imm 1))))

(rule 0 (do_cls $I32 src)
(let ((shifted Gpr (x64_sarl_mi src 1))
(xored Gpr (x64_xor $I32 src (RegMemImm.Reg shifted)))
(clz Gpr (do_clz $I32 $I32 xored)))
(x64_sub $I32 clz (RegMemImm.Imm 1))))
Copy link
Member

Choose a reason for hiding this comment

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

These two rules can be merged together through the x64_sar helper perhaps?

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good. I decided to keep x64_sarq_mi in the cls rule for i128 since it would be more effort to switch that over to x64_sar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:area:x64 Issues related to x64 codegen cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cranelift: cls not implemented for any type on x86_64

2 participants