Skip to content

Commit 642bfb2

Browse files
committed
Auto merge of rust-lang#117124 - bjorn3:sync_cg_clif-2023-10-24, r=bjorn3
Sync rustc_codegen_cranelift This contains fixes for the last two remaining known miscompilations. One is the lack of stack alignment support in cranelift which has been worked around by dynamically realigning at runtime. This fixed rayon and by extension Wasmtime. And the other is lack of zero/sign extending of small arguments when the ABI requires this. This is completely fine when only using cg_clif compiled code, but LLVM depends on this resulting in weird behavior of mixed LLVM, Cranelift binaries. The update to Cranelift 0.101.1 fixes this. In addition I have implemented all x86_64 SIMD intrinsics required by the image and rav1e crates. r? `@ghost` `@rustbot` label +A-codegen +A-cranelift +T-compiler
2 parents cee6db1 + 484bc7f commit 642bfb2

14 files changed

+260
-138
lines changed

Diff for: compiler/rustc_codegen_cranelift/.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
]
3434
},
3535
{
36-
"sysroot_src": "./download/sysroot/sysroot_src/library",
36+
"sysroot_src": "./build/stdlib/library",
3737
"crates": [
3838
{
3939
"root_module": "./example/std_example.rs",

Diff for: compiler/rustc_codegen_cranelift/Cargo.lock

+26-26
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
4545

4646
[[package]]
4747
name = "cranelift-bforest"
48-
version = "0.101.0"
48+
version = "0.101.1"
4949
source = "registry+https://github.com/rust-lang/crates.io-index"
50-
checksum = "8e5e1df0da8488dd03b34afc134ba84b754d61862cc465932a9e5d07952f661e"
50+
checksum = "c1512c3bb6b13018e7109fc3ac964bc87b329eaf3a77825d337558d0c7f6f1be"
5151
dependencies = [
5252
"cranelift-entity",
5353
]
5454

5555
[[package]]
5656
name = "cranelift-codegen"
57-
version = "0.101.0"
57+
version = "0.101.1"
5858
source = "registry+https://github.com/rust-lang/crates.io-index"
59-
checksum = "77a17ca4e699a0aaf49a0c88f6311a864f321048aa63f6b787cab20eb5f93f10"
59+
checksum = "16cb8fb9220a6ea7a226705a273ab905309ee546267bdf34948d57932d7f0396"
6060
dependencies = [
6161
"bumpalo",
6262
"cranelift-bforest",
@@ -75,39 +75,39 @@ dependencies = [
7575

7676
[[package]]
7777
name = "cranelift-codegen-meta"
78-
version = "0.101.0"
78+
version = "0.101.1"
7979
source = "registry+https://github.com/rust-lang/crates.io-index"
80-
checksum = "022f2793cdade1d37a1f755ac42938a3f832f533eac6cafc8b26b209544c3c06"
80+
checksum = "ab3a8d3b0d4745b183da5ea0792b13d79f5c23d6e69ac04761728e2532b56649"
8181
dependencies = [
8282
"cranelift-codegen-shared",
8383
]
8484

8585
[[package]]
8686
name = "cranelift-codegen-shared"
87-
version = "0.101.0"
87+
version = "0.101.1"
8888
source = "registry+https://github.com/rust-lang/crates.io-index"
89-
checksum = "a4d72dbb83c2ad788dec4ad0843070973cb48c35a3ca19b1e7437ac40834fd9c"
89+
checksum = "524141c8e68f2abc2043de4c2b31f6d9dd42432738c246431d0572a1422a4a84"
9090

9191
[[package]]
9292
name = "cranelift-control"
93-
version = "0.101.0"
93+
version = "0.101.1"
9494
source = "registry+https://github.com/rust-lang/crates.io-index"
95-
checksum = "ae07cf26dcc90d546826d747ac63b6c40c916f34b03e92a6ae0422c28d771b8a"
95+
checksum = "97513b57c961c713789a03886a57b43e14ebcd204cbaa8ae50ca6c70a8e716b3"
9696
dependencies = [
9797
"arbitrary",
9898
]
9999

100100
[[package]]
101101
name = "cranelift-entity"
102-
version = "0.101.0"
102+
version = "0.101.1"
103103
source = "registry+https://github.com/rust-lang/crates.io-index"
104-
checksum = "c2fe6b7e49820893691aea497f36257e9d6f52061d8c4758d61d802d5f101a3d"
104+
checksum = "e3f23d3cf3afa7e45f239702612c76d87964f652a55e28d13ed6d7e20f3479dd"
105105

106106
[[package]]
107107
name = "cranelift-frontend"
108-
version = "0.101.0"
108+
version = "0.101.1"
109109
source = "registry+https://github.com/rust-lang/crates.io-index"
110-
checksum = "44f497576ca3674581581601b6a55ccc1b43447217648c880e5bce70db3cf659"
110+
checksum = "554cd4947ec9209b58bf9ae5bf83581b5ddf9128bd967208e334b504a57db54e"
111111
dependencies = [
112112
"cranelift-codegen",
113113
"log",
@@ -117,15 +117,15 @@ dependencies = [
117117

118118
[[package]]
119119
name = "cranelift-isle"
120-
version = "0.101.0"
120+
version = "0.101.1"
121121
source = "registry+https://github.com/rust-lang/crates.io-index"
122-
checksum = "b96aa02eac00fffee13b0cd37d17874ccdb3d5458983041accd825ef78ce6454"
122+
checksum = "6c1892a439696b6413cb54083806f5fd9fc431768b8de74864b3d9e8b93b124f"
123123

124124
[[package]]
125125
name = "cranelift-jit"
126-
version = "0.101.0"
126+
version = "0.101.1"
127127
source = "registry+https://github.com/rust-lang/crates.io-index"
128-
checksum = "b1d6e0e308c873eefc185745a6b21daec2a10f7554c9fb67e334c2d7d756d979"
128+
checksum = "32209252fb38acaf1662ccd0397907bbe0e92bdb13b6ddbfd2f74e437f83e685"
129129
dependencies = [
130130
"anyhow",
131131
"cranelift-codegen",
@@ -143,9 +143,9 @@ dependencies = [
143143

144144
[[package]]
145145
name = "cranelift-module"
146-
version = "0.101.0"
146+
version = "0.101.1"
147147
source = "registry+https://github.com/rust-lang/crates.io-index"
148-
checksum = "c1aa8ebb06eced4e478c3f94f1d65d4e7c93493f4640057912b27a3e34b84841"
148+
checksum = "bf42656f5f6df7bfafc4dd7b63a1888b0627c07b43b2cb9aa54e13843fed39eb"
149149
dependencies = [
150150
"anyhow",
151151
"cranelift-codegen",
@@ -154,9 +154,9 @@ dependencies = [
154154

155155
[[package]]
156156
name = "cranelift-native"
157-
version = "0.101.0"
157+
version = "0.101.1"
158158
source = "registry+https://github.com/rust-lang/crates.io-index"
159-
checksum = "2870170ca44054b202c737626607b87be6e35655084bd94a6ff807a5812ba7df"
159+
checksum = "e0c2d3badd4b9690865f5bb68a71fa94de592fa2df3f3d11a5a062c60c0a107a"
160160
dependencies = [
161161
"cranelift-codegen",
162162
"libc",
@@ -165,9 +165,9 @@ dependencies = [
165165

166166
[[package]]
167167
name = "cranelift-object"
168-
version = "0.101.0"
168+
version = "0.101.1"
169169
source = "registry+https://github.com/rust-lang/crates.io-index"
170-
checksum = "20647761742d17dabac8205da958910ede78599550e06418a16711a3ee2fc897"
170+
checksum = "88eca54bbecea3170035168357306e9c779d4a63d8bf036c9e16bd21fdaa69b5"
171171
dependencies = [
172172
"anyhow",
173173
"cranelift-codegen",
@@ -374,9 +374,9 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
374374

375375
[[package]]
376376
name = "wasmtime-jit-icache-coherence"
377-
version = "14.0.0"
377+
version = "14.0.1"
378378
source = "registry+https://github.com/rust-lang/crates.io-index"
379-
checksum = "a3a5dda53ad6993f9b0a2d65fb49e0348a7232a27a8794064122870d6ee19eb2"
379+
checksum = "9aaf2fa8fd2d6b65abae9b92edfe69254cc5d6b166e342364036c3e347de8da9"
380380
dependencies = [
381381
"cfg-if",
382382
"libc",

Diff for: compiler/rustc_codegen_cranelift/Cargo.toml

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ crate-type = ["dylib"]
88

99
[dependencies]
1010
# These have to be in sync with each other
11-
cranelift-codegen = { version = "0.101", features = ["unwind", "all-arch"] }
12-
cranelift-frontend = { version = "0.101" }
13-
cranelift-module = { version = "0.101" }
14-
cranelift-native = { version = "0.101" }
15-
cranelift-jit = { version = "0.101", optional = true }
16-
cranelift-object = { version = "0.101" }
11+
cranelift-codegen = { version = "0.101.1", features = ["unwind", "all-arch"] }
12+
cranelift-frontend = { version = "0.101.1" }
13+
cranelift-module = { version = "0.101.1" }
14+
cranelift-native = { version = "0.101.1" }
15+
cranelift-jit = { version = "0.101.1", optional = true }
16+
cranelift-object = { version = "0.101.1" }
1717
target-lexicon = "0.12.0"
1818
gimli = { version = "0.28", default-features = false, features = ["write"]}
1919
object = { version = "0.32", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe"] }

Diff for: compiler/rustc_codegen_cranelift/Readme.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ If not please open an issue.
88
## Building and testing
99

1010
```bash
11-
$ git clone https://github.com/bjorn3/rustc_codegen_cranelift
11+
$ git clone https://github.com/rust-lang/rustc_codegen_cranelift
1212
$ cd rustc_codegen_cranelift
1313
$ ./y.sh prepare
1414
$ ./y.sh build
@@ -29,7 +29,7 @@ Extract the `dist` directory in the archive anywhere you want.
2929
If you want to use `cargo clif build` instead of having to specify the full path to the `cargo-clif` executable, you can add the `bin` subdirectory of the extracted `dist` directory to your `PATH`.
3030
(tutorial [for Windows](https://stackoverflow.com/a/44272417), and [for Linux/MacOS](https://unix.stackexchange.com/questions/26047/how-to-correctly-add-a-path-to-path/26059#26059)).
3131

32-
[releases]: https://github.com/bjorn3/rustc_codegen_cranelift/releases/tag/dev
32+
[releases]: https://github.com/rust-lang/rustc_codegen_cranelift/releases/tag/dev
3333

3434
## Usage
3535

@@ -78,7 +78,7 @@ configuration options.
7878

7979
* Inline assembly ([no cranelift support](https://github.com/bytecodealliance/wasmtime/issues/1041))
8080
* On UNIX there is support for invoking an external assembler for `global_asm!` and `asm!`.
81-
* SIMD ([tracked here](https://github.com/bjorn3/rustc_codegen_cranelift/issues/171), `std::simd` fully works, `std::arch` is partially supported)
81+
* SIMD ([tracked here](https://github.com/rust-lang/rustc_codegen_cranelift/issues/171), `std::simd` fully works, `std::arch` is partially supported)
8282
* Unwinding on panics ([no cranelift support](https://github.com/bytecodealliance/wasmtime/issues/1677), `-Cpanic=abort` is enabled by default)
8383

8484
## License

Diff for: compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs

+11
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,17 @@ fn main() {
353353

354354
let f = V([0.0, 1.0]);
355355
let _a = f.0[0];
356+
357+
stack_val_align();
358+
}
359+
360+
#[inline(never)]
361+
fn stack_val_align() {
362+
#[repr(align(8192))]
363+
struct Foo(u8);
364+
365+
let a = Foo(0);
366+
assert_eq!(&a as *const Foo as usize % 8192, 0);
356367
}
357368

358369
#[cfg(all(

Diff for: compiler/rustc_codegen_cranelift/patches/0001-regex-Ignore-test-which-gets-miscompiled-with-llvm-sysroot.patch

-25
This file was deleted.

Diff for: compiler/rustc_codegen_cranelift/src/abi/mod.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -120,32 +120,25 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
120120
args: &[Value],
121121
) -> Cow<'_, [Value]> {
122122
if self.tcx.sess.target.is_like_windows {
123-
let (mut params, mut args): (Vec<_>, Vec<_>) =
124-
params
125-
.into_iter()
126-
.zip(args)
127-
.map(|(param, &arg)| {
128-
if param.value_type == types::I128 {
129-
let arg_ptr = Pointer::stack_slot(self.bcx.create_sized_stack_slot(
130-
StackSlotData { kind: StackSlotKind::ExplicitSlot, size: 16 },
131-
));
132-
arg_ptr.store(self, arg, MemFlags::trusted());
133-
(AbiParam::new(self.pointer_type), arg_ptr.get_addr(self))
134-
} else {
135-
(param, arg)
136-
}
137-
})
138-
.unzip();
123+
let (mut params, mut args): (Vec<_>, Vec<_>) = params
124+
.into_iter()
125+
.zip(args)
126+
.map(|(param, &arg)| {
127+
if param.value_type == types::I128 {
128+
let arg_ptr = self.create_stack_slot(16, 16);
129+
arg_ptr.store(self, arg, MemFlags::trusted());
130+
(AbiParam::new(self.pointer_type), arg_ptr.get_addr(self))
131+
} else {
132+
(param, arg)
133+
}
134+
})
135+
.unzip();
139136

140137
let indirect_ret_val = returns.len() == 1 && returns[0].value_type == types::I128;
141138

142139
if indirect_ret_val {
143140
params.insert(0, AbiParam::new(self.pointer_type));
144-
let ret_ptr =
145-
Pointer::stack_slot(self.bcx.create_sized_stack_slot(StackSlotData {
146-
kind: StackSlotKind::ExplicitSlot,
147-
size: 16,
148-
}));
141+
let ret_ptr = self.create_stack_slot(16, 16);
149142
args.insert(0, ret_ptr.get_addr(self));
150143
self.lib_call_unadjusted(name, params, vec![], &args);
151144
return Cow::Owned(vec![ret_ptr.load(self, types::I128, MemFlags::trusted())]);

Diff for: compiler/rustc_codegen_cranelift/src/abi/pass_mode.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,13 @@ pub(super) fn from_casted_value<'tcx>(
189189
let abi_params = cast_target_to_abi_params(cast);
190190
let abi_param_size: u32 = abi_params.iter().map(|param| param.value_type.bytes()).sum();
191191
let layout_size = u32::try_from(layout.size.bytes()).unwrap();
192-
let stack_slot = fx.bcx.create_sized_stack_slot(StackSlotData {
193-
kind: StackSlotKind::ExplicitSlot,
194-
// FIXME Don't force the size to a multiple of 16 bytes once Cranelift gets a way to
195-
// specify stack slot alignment.
192+
let ptr = fx.create_stack_slot(
196193
// Stack slot size may be bigger for example `[u8; 3]` which is packed into an `i32`.
197194
// It may also be smaller for example when the type is a wrapper around an integer with a
198195
// larger alignment than the integer.
199-
size: (std::cmp::max(abi_param_size, layout_size) + 15) / 16 * 16,
200-
});
201-
let ptr = Pointer::stack_slot(stack_slot);
196+
std::cmp::max(abi_param_size, layout_size),
197+
u32::try_from(layout.align.pref.bytes()).unwrap(),
198+
);
202199
let mut offset = 0;
203200
let mut block_params_iter = block_params.iter().copied();
204201
for param in abi_params {

Diff for: compiler/rustc_codegen_cranelift/src/cast.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,7 @@ pub(crate) fn clif_int_or_float_cast(
104104
&[from],
105105
)[0];
106106
// FIXME(bytecodealliance/wasmtime#6104) use bitcast instead of store to get from i64x2 to i128
107-
let stack_slot = fx.bcx.create_sized_stack_slot(StackSlotData {
108-
kind: StackSlotKind::ExplicitSlot,
109-
size: 16,
110-
});
111-
let ret_ptr = Pointer::stack_slot(stack_slot);
107+
let ret_ptr = fx.create_stack_slot(16, 16);
112108
ret_ptr.store(fx, ret, MemFlags::trusted());
113109
ret_ptr.load(fx, types::I128, MemFlags::trusted())
114110
} else {

Diff for: compiler/rustc_codegen_cranelift/src/common.rs

+19
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,25 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
383383
})
384384
}
385385

386+
pub(crate) fn create_stack_slot(&mut self, size: u32, align: u32) -> Pointer {
387+
if align <= 16 {
388+
let stack_slot = self.bcx.create_sized_stack_slot(StackSlotData {
389+
kind: StackSlotKind::ExplicitSlot,
390+
// FIXME Don't force the size to a multiple of 16 bytes once Cranelift gets a way to
391+
// specify stack slot alignment.
392+
size: (size + 15) / 16 * 16,
393+
});
394+
Pointer::stack_slot(stack_slot)
395+
} else {
396+
// Alignment is too big to handle using the above hack. Dynamically realign a stack slot
397+
// instead. This wastes some space for the realignment.
398+
let base_ptr = self.create_stack_slot(size + align, 16).get_addr(self);
399+
let misalign_offset = self.bcx.ins().urem_imm(base_ptr, i64::from(align));
400+
let realign_offset = self.bcx.ins().irsub_imm(misalign_offset, i64::from(align));
401+
Pointer::new(self.bcx.ins().iadd(base_ptr, realign_offset))
402+
}
403+
}
404+
386405
pub(crate) fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
387406
if let Some(debug_context) = &mut self.cx.debug_context {
388407
let (file, line, column) =

Diff for: compiler/rustc_codegen_cranelift/src/driver/aot.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,26 @@ pub(crate) fn run_aot(
361361
metadata: EncodedMetadata,
362362
need_metadata_module: bool,
363363
) -> Box<OngoingCodegen> {
364+
// FIXME handle `-Ctarget-cpu=native`
365+
let target_cpu = match tcx.sess.opts.cg.target_cpu {
366+
Some(ref name) => name,
367+
None => tcx.sess.target.cpu.as_ref(),
368+
}
369+
.to_owned();
370+
364371
let cgus = if tcx.sess.opts.output_types.should_codegen() {
365372
tcx.collect_and_partition_mono_items(()).1
366373
} else {
367374
// If only `--emit metadata` is used, we shouldn't perform any codegen.
368375
// Also `tcx.collect_and_partition_mono_items` may panic in that case.
369-
&[]
376+
return Box::new(OngoingCodegen {
377+
modules: vec![],
378+
allocator_module: None,
379+
metadata_module: None,
380+
metadata,
381+
crate_info: CrateInfo::new(tcx, target_cpu),
382+
concurrency_limiter: ConcurrencyLimiter::new(tcx.sess, 0),
383+
});
370384
};
371385

372386
if tcx.dep_graph.is_fully_enabled() {
@@ -481,13 +495,6 @@ pub(crate) fn run_aot(
481495
None
482496
};
483497

484-
// FIXME handle `-Ctarget-cpu=native`
485-
let target_cpu = match tcx.sess.opts.cg.target_cpu {
486-
Some(ref name) => name,
487-
None => tcx.sess.target.cpu.as_ref(),
488-
}
489-
.to_owned();
490-
491498
Box::new(OngoingCodegen {
492499
modules,
493500
allocator_module,

0 commit comments

Comments
 (0)