-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cranelift: return programmatic error rather than panic when temporari…
…es run out. (#7114) * Cranelift: return programmatic error rather than panic when temporaries run out. Cranelift currently has a limit of `2^21` vregs per function body in VCode. This is a consequence of (performance-motivated) bitpacking of `Operand`s into `u32`s in regalloc2. As a result of this, it is possible to produce a function body that will fail to compile by running out of vreg temporaries during lowering. Currently, this results in a panic. Ideally, we would propagate the error upward and return it programmatically. This PR does that, with a "deferred error" mechanism. A cleaner solution would be to properly thread the `Result` types through all layers of lowering. However, that would require supporting `Result`s in ISLE, and that is a deeper language-design and `islec`-hacking question that I think we can tackle later if we continue to see motivating cases. The deferral works by returning a valid but bogus `ValueReg`s to the lowering rule, but storing the error and checking for it in the toplevel lowering loop. (Note that we have to return a bogus `v0` rather than `VReg::invalid()`, because the latter causes the `ValueRegs` to think there is no register provided.) This PR also includes a test at the Wasmtime level. Note that it takes ~22s to run on my (relatively fast) laptop, because it has to run until it runs out of VRegs in a debug build of the compiler. We could remove the test if we feel we're otherwise confident. Thanks to Venkkatesh Sekar for reporting this issue! The integration test uses one of the example inputs from the report. * Review feedback. * Handle alloc after a deferred error occurs. * Add uncommitted fix for previous.
- Loading branch information
Showing
5 changed files
with
95 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,36 @@ | ||
#![cfg(not(miri))] | ||
|
||
use anyhow::Result; | ||
use wasmtime::*; | ||
|
||
#[test] | ||
fn code_too_large_without_panic() -> Result<()> { | ||
const N: usize = 120000; | ||
|
||
// Build a module with a function whose body will allocate too many | ||
// temporaries for our current (Cranelift-based) compiler backend to | ||
// handle. This test ensures that we propagate the failure upward | ||
// and return it programmatically, rather than panic'ing. If we ever | ||
// improve our compiler backend to actually handle such a large | ||
// function body, we'll need to increase the limits here too! | ||
let mut s = String::new(); | ||
s.push_str("(module\n"); | ||
s.push_str("(table 1 1 funcref)\n"); | ||
s.push_str("(func (export \"\") (result i32)\n"); | ||
s.push_str("i32.const 0\n"); | ||
for _ in 0..N { | ||
s.push_str("table.get 0\n"); | ||
s.push_str("ref.is_null\n"); | ||
} | ||
s.push_str("))\n"); | ||
|
||
let store = Store::<()>::default(); | ||
let result = Module::new(store.engine(), &s); | ||
match result { | ||
Err(e) => assert!(e | ||
.to_string() | ||
.starts_with("Compilation error: Code for function is too large")), | ||
Ok(_) => panic!("Please adjust limits to make the module too large to compile!"), | ||
} | ||
Ok(()) | ||
} |
This file contains 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