Skip to content

Commit ac7729d

Browse files
authored
Align symbol names with --emit-clif (#10947)
* Align symbol names with `--emit-clif` This commit fixes two issues: (1) is that `--emit-clif` generates file names that are different from symbol names and (2) is that `--emit-clif` for components today has collisions on filenames which means that output files can get corrupted. Currently (2) is causing spurious failures in CI due to the test added in #10939 being the first test with a component and functions in modules where they clash to emit the same file. * Fix typo * Fix tests * Fix tests on Windows Can't use `:` in filenames on Windows prtest:full
1 parent 239936b commit ac7729d

9 files changed

Lines changed: 47 additions & 29 deletions

File tree

crates/cranelift/src/compiler.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ impl wasmtime_environ::Compiler for Compiler {
187187
func_index: DefinedFuncIndex,
188188
input: FunctionBodyData<'_>,
189189
types: &ModuleTypesBuilder,
190+
symbol: &str,
190191
) -> Result<CompiledFunctionBody, CompileError> {
191192
let isa = &*self.isa;
192193
let module = &translation.module;
@@ -275,10 +276,7 @@ impl wasmtime_environ::Compiler for Compiler {
275276
&mut func_env,
276277
)?;
277278

278-
let func = compiler.finish_with_info(
279-
Some((&body, &self.tunables)),
280-
&format!("wasm_func_{}", func_index.as_u32()),
281-
)?;
279+
let func = compiler.finish_with_info(Some((&body, &self.tunables)), symbol)?;
282280

283281
let timing = cranelift_codegen::timing::take_current();
284282
log::debug!("{:?} translated in {:?}", func_index, timing.total());
@@ -295,6 +293,7 @@ impl wasmtime_environ::Compiler for Compiler {
295293
translation: &ModuleTranslation<'_>,
296294
types: &ModuleTypesBuilder,
297295
def_func_index: DefinedFuncIndex,
296+
symbol: &str,
298297
) -> Result<CompiledFunctionBody, CompileError> {
299298
let func_index = translation.module.func_index(def_func_index);
300299
let sig = translation.module.functions[func_index]
@@ -363,14 +362,15 @@ impl wasmtime_environ::Compiler for Compiler {
363362
builder.finalize();
364363

365364
Ok(CompiledFunctionBody {
366-
code: Box::new(compiler.finish(&format!("array_to_wasm_{}", func_index.as_u32(),))?),
365+
code: Box::new(compiler.finish(symbol)?),
367366
needs_gc_heap: false,
368367
})
369368
}
370369

371370
fn compile_wasm_to_array_trampoline(
372371
&self,
373372
wasm_func_ty: &WasmFuncType,
373+
symbol: &str,
374374
) -> Result<CompiledFunctionBody, CompileError> {
375375
let isa = &*self.isa;
376376
let pointer_type = isa.pointer_type();
@@ -436,7 +436,7 @@ impl wasmtime_environ::Compiler for Compiler {
436436
builder.finalize();
437437

438438
Ok(CompiledFunctionBody {
439-
code: Box::new(compiler.finish(&format!("wasm_to_array_trampoline_{wasm_func_ty}"))?),
439+
code: Box::new(compiler.finish(&symbol)?),
440440
needs_gc_heap: false,
441441
})
442442
}
@@ -586,6 +586,7 @@ impl wasmtime_environ::Compiler for Compiler {
586586
fn compile_wasm_to_builtin(
587587
&self,
588588
index: BuiltinFunctionIndex,
589+
symbol: &str,
589590
) -> Result<CompiledFunctionBody, CompileError> {
590591
let isa = &*self.isa;
591592
let ptr_size = isa.pointer_bytes();
@@ -656,7 +657,7 @@ impl wasmtime_environ::Compiler for Compiler {
656657
builder.finalize();
657658

658659
Ok(CompiledFunctionBody {
659-
code: Box::new(compiler.finish(&format!("wasm_to_builtin_{}", index.name()))?),
660+
code: Box::new(compiler.finish(&symbol)?),
660661
needs_gc_heap: false,
661662
})
662663
}
@@ -974,14 +975,14 @@ impl FunctionCompiler<'_> {
974975
(builder, block0)
975976
}
976977

977-
fn finish(self, clif_filename: &str) -> Result<CompiledFunction, CompileError> {
978-
self.finish_with_info(None, clif_filename)
978+
fn finish(self, symbol: &str) -> Result<CompiledFunction, CompileError> {
979+
self.finish_with_info(None, symbol)
979980
}
980981

981982
fn finish_with_info(
982983
mut self,
983984
body_and_tunables: Option<(&FunctionBody<'_>, &Tunables)>,
984-
clif_filename: &str,
985+
symbol: &str,
985986
) -> Result<CompiledFunction, CompileError> {
986987
let context = &mut self.cx.codegen_context;
987988
let isa = &*self.compiler.isa;
@@ -997,7 +998,7 @@ impl FunctionCompiler<'_> {
997998
if let Some(path) = &self.compiler.clif_dir {
998999
use std::io::Write;
9991000

1000-
let mut path = path.join(clif_filename);
1001+
let mut path = path.join(symbol.replace(":", "-"));
10011002
path.set_extension("clif");
10021003

10031004
let mut output = std::fs::File::create(path).unwrap();

crates/cranelift/src/compiler/component.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,7 @@ impl ComponentCompiler for Compiler {
14541454
types: &ComponentTypesBuilder,
14551455
index: TrampolineIndex,
14561456
tunables: &Tunables,
1457+
symbol: &str,
14571458
) -> Result<AllCallFunc<CompiledFunctionBody>> {
14581459
let compile = |abi: Abi| -> Result<_> {
14591460
let mut compiler = self.function_compiler();
@@ -1497,11 +1498,12 @@ impl ComponentCompiler for Compiler {
14971498

14981499
c.translate(&component.trampolines[index]);
14991500
c.builder.finalize();
1500-
1501+
let symbol = match abi {
1502+
Abi::Wasm => format!("{symbol}_wasm_call"),
1503+
Abi::Array => format!("{symbol}_array_call"),
1504+
};
15011505
Ok(CompiledFunctionBody {
1502-
code: Box::new(
1503-
compiler.finish(&format!("component_trampoline_{}_{abi:?}", index.as_u32()))?,
1504-
),
1506+
code: Box::new(compiler.finish(&symbol)?),
15051507
needs_gc_heap: false,
15061508
})
15071509
};

crates/environ/src/compile/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ pub trait Compiler: Send + Sync {
200200
index: DefinedFuncIndex,
201201
data: FunctionBodyData<'_>,
202202
types: &ModuleTypesBuilder,
203+
symbol: &str,
203204
) -> Result<CompiledFunctionBody, CompileError>;
204205

205206
/// Compile a trampoline for an array-call host function caller calling the
@@ -212,6 +213,7 @@ pub trait Compiler: Send + Sync {
212213
translation: &ModuleTranslation<'_>,
213214
types: &ModuleTypesBuilder,
214215
index: DefinedFuncIndex,
216+
symbol: &str,
215217
) -> Result<CompiledFunctionBody, CompileError>;
216218

217219
/// Compile a trampoline for a Wasm caller calling a array callee with the
@@ -222,6 +224,7 @@ pub trait Compiler: Send + Sync {
222224
fn compile_wasm_to_array_trampoline(
223225
&self,
224226
wasm_func_ty: &WasmFuncType,
227+
symbol: &str,
225228
) -> Result<CompiledFunctionBody, CompileError>;
226229

227230
/// Creates a trampoline that can be used to call Wasmtime's implementation
@@ -236,6 +239,7 @@ pub trait Compiler: Send + Sync {
236239
fn compile_wasm_to_builtin(
237240
&self,
238241
index: BuiltinFunctionIndex,
242+
symbol: &str,
239243
) -> Result<CompiledFunctionBody, CompileError>;
240244

241245
/// Returns the list of relocations required for a function from one of the

crates/environ/src/component/compiler.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ pub trait ComponentCompiler: Send + Sync {
1616
types: &ComponentTypesBuilder,
1717
trampoline: TrampolineIndex,
1818
tunables: &Tunables,
19+
symbol: &str,
1920
) -> Result<AllCallFunc<CompiledFunctionBody>>;
2021
}

crates/wasmtime/src/compile.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,15 @@ impl<'a> CompileInputs<'a> {
381381

382382
for (idx, trampoline) in component.trampolines.iter() {
383383
ret.push_input(move |compiler| {
384+
let symbol = trampoline.symbol_name();
384385
Ok(CompileOutput {
385386
key: CompileKey::trampoline(idx),
386-
symbol: trampoline.symbol_name(),
387387
function: compiler
388388
.component_compiler()
389-
.compile_trampoline(component, types, idx, tunables)
390-
.with_context(|| format!("failed to compile {}", trampoline.symbol_name()))?
389+
.compile_trampoline(component, types, idx, tunables, &symbol)
390+
.with_context(|| format!("failed to compile {symbol}"))?
391391
.into(),
392+
symbol,
392393
start_srcloc: FilePos::default(),
393394
})
394395
});
@@ -405,7 +406,7 @@ impl<'a> CompileInputs<'a> {
405406
ret.push_input(move |compiler| {
406407
let symbol = "resource_drop_trampoline".to_string();
407408
let function = compiler
408-
.compile_wasm_to_array_trampoline(types[sig].unwrap_func())
409+
.compile_wasm_to_array_trampoline(types[sig].unwrap_func(), &symbol)
409410
.with_context(|| format!("failed to compile `{symbol}`"))?;
410411
Ok(CompileOutput {
411412
key: CompileKey::resource_drop_wasm_to_array_trampoline(),
@@ -487,7 +488,7 @@ impl<'a> CompileInputs<'a> {
487488
let offset = data.original_position();
488489
let start_srcloc = FilePos::new(u32::try_from(offset).unwrap());
489490
let function = compiler
490-
.compile_function(translation, def_func_index, func_body, types)
491+
.compile_function(translation, def_func_index, func_body, types, &symbol)
491492
.with_context(|| format!("failed to compile: {symbol}"))?;
492493

493494
Ok(CompileOutput {
@@ -508,7 +509,12 @@ impl<'a> CompileInputs<'a> {
508509
func_index.as_u32()
509510
);
510511
let trampoline = compiler
511-
.compile_array_to_wasm_trampoline(translation, types, def_func_index)
512+
.compile_array_to_wasm_trampoline(
513+
translation,
514+
types,
515+
def_func_index,
516+
&symbol,
517+
)
512518
.with_context(|| format!("failed to compile: {symbol}"))?;
513519
Ok(CompileOutput {
514520
key: CompileKey::array_to_wasm_trampoline(module, def_func_index),
@@ -534,7 +540,7 @@ impl<'a> CompileInputs<'a> {
534540
trampoline_type_index.as_u32()
535541
);
536542
let trampoline = compiler
537-
.compile_wasm_to_array_trampoline(trampoline_func_ty)
543+
.compile_wasm_to_array_trampoline(trampoline_func_ty, &symbol)
538544
.with_context(|| format!("failed to compile: {symbol}"))?;
539545
Ok(CompileOutput {
540546
key: CompileKey::wasm_to_array_trampoline(trampoline_type_index),
@@ -579,7 +585,7 @@ fn compile_required_builtins(engine: &Engine, raw_outputs: &mut Vec<CompileOutpu
579585
Box::new(move |compiler: &dyn Compiler| {
580586
let symbol = format!("wasmtime_builtin_{}", builtin.name());
581587
let trampoline = compiler
582-
.compile_wasm_to_builtin(builtin)
588+
.compile_wasm_to_builtin(builtin, &symbol)
583589
.with_context(|| format!("failed to compile `{symbol}`"))?;
584590
Ok(CompileOutput {
585591
key: CompileKey::wasm_to_builtin_trampoline(builtin),

crates/winch/src/compiler.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl wasmtime_environ::Compiler for Compiler {
9595
index: DefinedFuncIndex,
9696
data: FunctionBodyData<'_>,
9797
types: &ModuleTypesBuilder,
98+
_symbol: &str,
9899
) -> Result<CompiledFunctionBody, CompileError> {
99100
let index = translation.module.func_index(index);
100101
let sig = translation.module.functions[index]
@@ -144,17 +145,19 @@ impl wasmtime_environ::Compiler for Compiler {
144145
translation: &ModuleTranslation<'_>,
145146
types: &ModuleTypesBuilder,
146147
index: DefinedFuncIndex,
148+
symbol: &str,
147149
) -> Result<CompiledFunctionBody, CompileError> {
148150
self.trampolines
149-
.compile_array_to_wasm_trampoline(translation, types, index)
151+
.compile_array_to_wasm_trampoline(translation, types, index, symbol)
150152
}
151153

152154
fn compile_wasm_to_array_trampoline(
153155
&self,
154156
wasm_func_ty: &wasmtime_environ::WasmFuncType,
157+
symbol: &str,
155158
) -> Result<CompiledFunctionBody, CompileError> {
156159
self.trampolines
157-
.compile_wasm_to_array_trampoline(wasm_func_ty)
160+
.compile_wasm_to_array_trampoline(wasm_func_ty, symbol)
158161
}
159162

160163
fn append_code(
@@ -234,8 +237,9 @@ impl wasmtime_environ::Compiler for Compiler {
234237
fn compile_wasm_to_builtin(
235238
&self,
236239
index: BuiltinFunctionIndex,
240+
symbol: &str,
237241
) -> Result<CompiledFunctionBody, CompileError> {
238-
self.trampolines.compile_wasm_to_builtin(index)
242+
self.trampolines.compile_wasm_to_builtin(index, symbol)
239243
}
240244

241245
fn compiled_function_relocation_targets<'a>(

tests/disas.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl Test {
221221
let entry = entry.context("failed to iterate over tempdir")?;
222222
let path = entry.path();
223223
if let Some(name) = path.file_name().and_then(|s| s.to_str()) {
224-
let filter = self.config.filter.as_deref().unwrap_or("wasm_func_");
224+
let filter = self.config.filter.as_deref().unwrap_or("wasm[0]--function");
225225
if !name.contains(filter) {
226226
continue;
227227
}

tests/disas/component-model/enum.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
;;! target = "riscv64"
22
;;! test = 'optimize'
3-
;;! filter = 'wasm_func_1'
3+
;;! filter = 'wasm[2]--function[1]'
44

55
(component
66
(type $a (enum "a" "b" "c"))

tests/disas/riscv64-component-builtins.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
;;! target = "riscv64"
22
;;! test = 'optimize'
3-
;;! filter = 'component_trampoline_0_Wasm'
3+
;;! filter = 'component-resource-drop[0]_wasm_call'
44

55
(component
66
(type $a (resource (rep i32)))

0 commit comments

Comments
 (0)