From 2ba60e4d4ef28658279cc494ff6d2d51b9dd50ac Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 14 Jan 2025 13:41:58 -0800 Subject: [PATCH] Emit CLIF even if compilation fails Just after #10011 I ran into a case where I wanted to peek at CLIF after compilation failed but the structure of the code failed to enable this, so I've refactored `--emit-clif` to emit the CLIF even if the lowering step fails. --- crates/cranelift/src/compiler.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/cranelift/src/compiler.rs b/crates/cranelift/src/compiler.rs index 26cdf62e5d4f..f8a80582b687 100644 --- a/crates/cranelift/src/compiler.rs +++ b/crates/cranelift/src/compiler.rs @@ -971,8 +971,14 @@ impl FunctionCompiler<'_> { ) -> Result<(WasmFunctionInfo, CompiledFunction), CompileError> { let context = &mut self.cx.codegen_context; let isa = &*self.compiler.isa; - let mut compiled_code = - compile_maybe_cached(context, isa, self.cx.incremental_cache_ctx.as_mut())?; + + // Run compilation, but don't propagate the error just yet. This'll + // mutate `context` and the IR contained within (optionally) but it may + // fail if the backend has a bug in it. Use `context` after this + // finishes to optionally emit CLIF and then after that's done actually + // propagate the error if one happened. + let compilation_result = + compile_maybe_cached(context, isa, self.cx.incremental_cache_ctx.as_mut()); if let Some(path) = &self.compiler.clif_dir { use std::io::Write; @@ -984,6 +990,8 @@ impl FunctionCompiler<'_> { write!(output, "{}", context.func.display()).unwrap(); } + let mut compiled_code = compilation_result?; + // Give wasm functions, user defined code, a "preferred" alignment // instead of the minimum alignment as this can help perf in niche // situations.