Skip to content

Cranelift: magic division and remainder optimization can slow down execution time #13554

@bongjunj

Description

@bongjunj

Hi, I was a bit fond of the following rules and trying to understand how they work.

;; General cases for `udiv` with constant divisors.
(rule (simplify_skeleton (udiv x (iconst_u $I32 (u64_extract_non_zero (u32_from_u64 d)))))
(if-let false (u32_is_power_of_two d))
(apply_div_const_magic_u32 (Opcode.Udiv) x d))
(rule (simplify_skeleton (udiv x (iconst_u $I64 (u64_extract_non_zero d))))
(if-let false (u64_is_power_of_two d))
(apply_div_const_magic_u64 (Opcode.Udiv) x d))
;; General cases for `sdiv` with constant divisors.
(rule (simplify_skeleton (sdiv x (iconst_s $I32 (i64_extract_non_zero (i32_from_i64 d)))))
(if-let false (i64_is_any_sign_power_of_two d))
(apply_div_const_magic_s32 (Opcode.Sdiv) x d))
(rule (simplify_skeleton (sdiv x (iconst_s $I64 (i64_extract_non_zero d))))
(if-let false (i64_is_any_sign_power_of_two d))
(apply_div_const_magic_s64 (Opcode.Sdiv) x d))

;; General cases for `urem` with constant divisors.
(rule (simplify_skeleton (urem x (iconst_u $I32 (u64_extract_non_zero (u32_from_u64 d)))))
(if-let false (u32_is_power_of_two d))
(apply_div_const_magic_u32 (Opcode.Urem) x d))
(rule (simplify_skeleton (urem x (iconst_u $I64 (u64_extract_non_zero d))))
(if-let false (u64_is_power_of_two d))
(apply_div_const_magic_u64 (Opcode.Urem) x d))
;; General cases for `srem` with constant divisors.
(rule (simplify_skeleton (srem x (iconst_s $I32 (i64_extract_non_zero (i32_from_i64 d)))))
(if-let false (i64_is_any_sign_power_of_two d))
(apply_div_const_magic_s32 (Opcode.Srem) x d))
(rule (simplify_skeleton (srem x (iconst_s $I64 (i64_extract_non_zero d))))
(if-let false (i64_is_any_sign_power_of_two d))
(apply_div_const_magic_s64 (Opcode.Srem) x d))

While I was reading the code, I ran some performance benchmark to see the performance impact of them.
When I was expecting it should speed up execution time, the result turned out that it can be less than desired.
For a spidermoney-regex, this slows the execution by 5.73%

The table below shows the average execution time measured with sightglass-cli using the default config (processes=10, iterations=10).
Slowdown = 1 - Upstream / NoMagic

Program Upstream NoMagic Slowdown
blake3-scalar/benchmark.wasm 416809.8 415652.7 0.28%
blake3-simd/benchmark.wasm 408021.7 406928.1 0.27%
bz2/benchmark.wasm 114914714.4 114829391.1 0.07%
pulldown-cmark/benchmark.wasm 7975220.8 7916613.0 0.73%
regex/benchmark.wasm 272510604.8 272406933.4 0.04%
spidermonkey/spidermonkey-json.wasm 512516258.4 536087944.1 -4.60%
spidermonkey/spidermonkey-markdown.wasm 296328581.8 292117657.4 1.42%
spidermonkey/spidermonkey-regex.wasm 122409216.8 115393885.2 5.73%
sqlite3/sqlite3.wasm 3611039582.9 3610499560.4 0.01%

Steps to Reproduce

  • Upstream commit: 1f0294d
  • NoMagic: remove the rules from upstream

Expected Results

It should speed up.

Actual Results

It slowed down.

Versions and Environment

Cranelift version or commit: stated above

Operating system: Linux 6.8.0-110-generic #110-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 19 15:09:20 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux

Architecture:

lscpu
Architecture:                            x86_64
CPU op-mode(s):                          32-bit, 64-bit
Address sizes:                           46 bits physical, 57 bits virtual
Byte Order:                              Little Endian
CPU(s):                                  64
On-line CPU(s) list:                     0-63
Vendor ID:                               GenuineIntel
Model name:                              Intel(R) Xeon(R) Gold 6326 CPU @ 2.90GHz
CPU family:                              6
Model:                                   106
Thread(s) per core:                      2
Core(s) per socket:                      16
Socket(s):                               2
Stepping:                                6
CPU(s) scaling MHz:                      37%
CPU max MHz:                             3500.0000
CPU min MHz:                             800.0000
BogoMIPS:                                5800.00
Flags:                                   fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm ida arat pln pts vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid fsrm md_clear pconfig flush_l1d arch_capabilities
Virtualization:                          VT-x
L1d cache:                               1.5 MiB (32 instances)
L1i cache:                               1 MiB (32 instances)
L2 cache:                                40 MiB (32 instances)
L3 cache:                                48 MiB (2 instances)
NUMA node(s):                            2
NUMA node0 CPU(s):                       0-15,32-47
NUMA node1 CPU(s):                       16-31,48-63
Vulnerability Gather data sampling:      Mitigation; Microcode
Vulnerability Indirect target selection: Mitigation; Aligned branch/return thunks
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS SW sequence; BHI SW loop, KVM SW loop
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIncorrect behavior in the current implementation that needs fixingcraneliftIssues related to the Cranelift code generator

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions