Skip to content

Conversation

@bongjunj
Copy link
Contributor

@bongjunj bongjunj commented Jan 9, 2026

Overview

The current implementation of ISLE code generation emits a single Rust function for each ISLE term, and rustc compiles the Rust code to integrate ISLE with other modules of Cranelift.

This can cause a compilation bottleneck when a ISLE term contains numerous rules ending up with producing a very very large Rust function, since rustc cannot efficiently compile such functions. Notably, the term simplify which implements the mid-end peephole optimizations has about 500 rules, and the symptom is already visible. (And the ruleset is growing!) You can check, compiling Cranelift, cranelift-codegen takes most of the time in the following report: cargo-timing.html

Plus: a timing report for compiling wasmtime:

With this PR, the ISLE codegen helps rustc by wrapping a large match statement generated by islec in a closure. By introducing closures, a portion of a huge Rust function (for a ISLE term) can be split into several smaller compilation units for rustc, reducing the compilation time.

Evaluation

On the main branch, the build times for cranelift-codegen before/after this optimization are measured. This PR saves ~2 seconds on my machine (x86-64, 64 core, 512GB memory)

  • Before: 23.81 sec
~/wasmtime (optimize-isle-compilation)> cargo clean
     Removed 10031 files, 12.6GiB total
~/wasmtime (optimize-isle-compilation)> time cargo build -q -p cranelift-codegen

________________________________________________________
Executed in   23.81 secs    fish           external
   usr time   43.59 secs    0.00 micros   43.59 secs
   sys time    5.04 secs  592.00 micros    5.04 secs
  • After: 26.04 sec
~/wasmtime ((dev))> cargo clean
t     Removed 1356 files, 1.0GiB total
~/wasmtime ((dev))> time cargo build -q -p cranelift-codegen

________________________________________________________
Executed in   26.04 secs    fish           external
   usr time   46.58 secs    0.00 micros   46.58 secs
   sys time    5.15 secs  583.00 micros    5.15 secs

Extra

I am experimenting with over 6,000 ISLE simplify rules.
The compilation time reduced from 841.37 seconds to 69.34 seconds with this PR. (+11x speedup)

~/w/cranelift ((many-rules-no-opt))> cargo clean && time cargo build -p cranelift-codegen
________________________________________________________
Executed in  841.37 secs    fish           external
   usr time  826.92 secs  325.00 micros  826.92 secs
   sys time   49.98 secs  135.00 micros   49.98 secs
   
 ~/w/cranelift (many-rules-opt)> cargo clean && time cargo build -q -p cranelift-codegen
________________________________________________________
Executed in   69.34 secs    fish           external
   usr time   98.32 secs  362.00 micros   98.32 secs
   sys time    6.42 secs  149.00 micros    6.42 secs

@bongjunj bongjunj requested a review from a team as a code owner January 9, 2026 08:48
@bongjunj bongjunj requested review from fitzgen and removed request for a team January 9, 2026 08:48
@cfallin
Copy link
Member

cfallin commented Jan 9, 2026

@bongjunj thanks for this experimentation!

Before moving further, would you mind benchmarking compilation time as well? One of the reasons for putting all of the codegen for an ISLE term in one function is so that the compiler can optimize the code together; splitting that code between functions and (especially) introducing ABI boundaries may produce slowdowns when Cranelift runs. IMHO, it's worth it to spend a few extra seconds when compiling Cranelift if it makes Cranelift itself run faster. If no effect, of course, then no problem and I'll be happy to review this. Thanks!

@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language labels Jan 9, 2026
@github-actions
Copy link

github-actions bot commented Jan 9, 2026

Subscribe to Label Action

cc @cfallin, @fitzgen

Details This issue or pull request has been labeled: "cranelift", "isle"

Thus the following users have been cc'd because of the following labels:

  • cfallin: isle
  • fitzgen: isle

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@alexcrichton
Copy link
Member

An 11x reduction in compile time is quite massive, so even if this is a ~NN% regression in compile-time-of-wasm-code I think this would be a great thing to land at least for debug builds and maybe optionally for release builds with some sort of tunable too.

@cfallin
Copy link
Member

cfallin commented Jan 9, 2026

Note, that's an 11x reduction with Bongjun's huge new ruleset; ~10% on main. That's neat but I wouldn't regress compile time for it IMHO.

@fitzgen
Copy link
Member

fitzgen commented Jan 9, 2026

If it does result in Wasm compilation time regressions, it may still make sense to have this as an option of the ISLE compiler that we can enable during development or something, if we can make the maintenance burden minimal.

Comment on lines +787 to +793
// Compile-time optimization: huge function bodies (often from very large match arms
// of constructor bodies)cause rustc to spend a lot of time in analysis passes.
// Wrap such bodies in a local closure to move the bulk of the work into a separate body
// without needing to know the types of captured locals.
const MATCH_ARM_BODY_CLOSURE_THRESHOLD: usize = 256;
if ret_kind == ReturnKind::Iterator
&& Codegen::block_weight(&arm.body) > MATCH_ARM_BODY_CLOSURE_THRESHOLD
Copy link
Member

Choose a reason for hiding this comment

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

Regarding my previous comment: it could potentially make sense to make MATCH_ARM_BODY_CLOSURE_THRESHOLD a dynamic ISLE compilation option, rather than a constant, and then tweak that value in Cranelift's invocation of the ISLE compiler depending on if a cargo feature is enabled or whether this is a release vs debug build of Cranelift or something.

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

Labels

cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants