Skip to content

Commit 4db95ce

Browse files
committed
Merge commit 'def04540a4e2541b995195c752c751295606a388' into sync_cg_clif-2023-11-16
2 parents 820f06b + def0454 commit 4db95ce

File tree

15 files changed

+473
-192
lines changed

15 files changed

+473
-192
lines changed

compiler/rustc_codegen_cranelift/.github/workflows/abi-cafe.yml

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ jobs:
3535
steps:
3636
- uses: actions/checkout@v3
3737

38+
- name: CPU features
39+
if: matrix.os == 'ubuntu-latest'
40+
run: cat /proc/cpuinfo
41+
3842
- name: Cache cargo target dir
3943
uses: actions/cache@v3
4044
with:

compiler/rustc_codegen_cranelift/.github/workflows/main.yml

+10
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ jobs:
6666
steps:
6767
- uses: actions/checkout@v3
6868

69+
- name: CPU features
70+
if: matrix.os == 'ubuntu-latest'
71+
run: cat /proc/cpuinfo
72+
6973
- name: Cache cargo target dir
7074
uses: actions/cache@v3
7175
with:
@@ -136,6 +140,9 @@ jobs:
136140
steps:
137141
- uses: actions/checkout@v3
138142

143+
- name: CPU features
144+
run: cat /proc/cpuinfo
145+
139146
- name: Prepare dependencies
140147
run: ./y.sh prepare
141148

@@ -159,6 +166,9 @@ jobs:
159166
steps:
160167
- uses: actions/checkout@v3
161168

169+
- name: CPU features
170+
run: cat /proc/cpuinfo
171+
162172
- name: Cache cargo target dir
163173
uses: actions/cache@v3
164174
with:

compiler/rustc_codegen_cranelift/.github/workflows/rustc.yml

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ jobs:
1111
steps:
1212
- uses: actions/checkout@v3
1313

14+
- name: CPU features
15+
run: cat /proc/cpuinfo
16+
1417
- name: Cache cargo target dir
1518
uses: actions/cache@v3
1619
with:
@@ -31,6 +34,9 @@ jobs:
3134
steps:
3235
- uses: actions/checkout@v3
3336

37+
- name: CPU features
38+
run: cat /proc/cpuinfo
39+
3440
- name: Cache cargo target dir
3541
uses: actions/cache@v3
3642
with:

compiler/rustc_codegen_cranelift/Readme.md

+40-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,48 @@ This has the potential to improve compilation times in debug mode.
55
If your project doesn't use any of the things listed under "Not yet supported", it should work fine.
66
If not please open an issue.
77

8+
## Download using Rustup
9+
10+
The Cranelift codegen backend is distributed in nightly builds on Linux and x86_64 macOS. If you want to
11+
install it using Rustup, you can do that by running:
12+
13+
```bash
14+
$ rustup component add rustc-codegen-cranelift-preview --toolchain nightly
15+
```
16+
17+
Once it is installed, you can enable it with one of the following approaches:
18+
- `CARGO_PROFILE_DEV_CODEGEN_BACKEND=cranelift cargo +nightly build -Zcodegen-backend`
19+
- `RUSTFLAGS="-Zcodegen-backend=cranelift" cargo +nightly build`
20+
- Add the following to `.cargo/config.toml`:
21+
```toml
22+
[unstable]
23+
codegen-backend = true
24+
25+
[profile.dev]
26+
codegen-backend = "cranelift"
27+
```
28+
- Add the following to `Cargo.toml`:
29+
```toml
30+
# This line needs to come before anything else in Cargo.toml
31+
cargo-features = ["codegen-backend"]
32+
33+
[profile.dev]
34+
codegen-backend = "cranelift"
35+
```
36+
37+
## Precompiled builds
38+
39+
You can also download a pre-built version from the [releases] page.
40+
Extract the `dist` directory in the archive anywhere you want.
41+
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`.
42+
(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)).
43+
44+
[releases]: https://github.com/rust-lang/rustc_codegen_cranelift/releases/tag/dev
45+
846
## Building and testing
947

48+
If you want to build the backend manually, you can download it from GitHub and build it yourself:
49+
1050
```bash
1151
$ git clone https://github.com/rust-lang/rustc_codegen_cranelift
1252
$ cd rustc_codegen_cranelift
@@ -22,15 +62,6 @@ $ ./test.sh
2262

2363
For more docs on how to build and test see [build_system/usage.txt](build_system/usage.txt) or the help message of `./y.sh`.
2464

25-
## Precompiled builds
26-
27-
Alternatively you can download a pre built version from the [releases] page.
28-
Extract the `dist` directory in the archive anywhere you want.
29-
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`.
30-
(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)).
31-
32-
[releases]: https://github.com/rust-lang/rustc_codegen_cranelift/releases/tag/dev
33-
3465
## Usage
3566

3667
rustc_codegen_cranelift can be used as a near-drop-in replacement for `cargo build` or `cargo run` for existing projects.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2023-11-10"
2+
channel = "nightly-2023-11-16"
33
components = ["rust-src", "rustc-dev", "llvm-tools"]

compiler/rustc_codegen_cranelift/scripts/rustup.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ case $1 in
4646
git pull origin master
4747
branch=sync_cg_clif-$(date +%Y-%m-%d)
4848
git checkout -b "$branch"
49-
"$cg_clif/git-fixed-subtree.sh" pull --prefix=compiler/rustc_codegen_cranelift/ https://github.com/bjorn3/rustc_codegen_cranelift.git master
49+
"$cg_clif/git-fixed-subtree.sh" pull --prefix=compiler/rustc_codegen_cranelift/ https://github.com/rust-lang/rustc_codegen_cranelift.git master
5050
git push -u my "$branch"
5151

5252
# immediately merge the merge commit into cg_clif to prevent merge conflicts when syncing

compiler/rustc_codegen_cranelift/scripts/setup_rust_fork.sh

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#!/usr/bin/env bash
22
set -e
33

4+
# CG_CLIF_FORCE_GNU_AS will force usage of as instead of the LLVM backend of rustc as we
5+
# the LLVM backend isn't compiled in here.
6+
export CG_CLIF_FORCE_GNU_AS=1
7+
48
# Compiletest expects all standard library paths to start with /rustc/FAKE_PREFIX.
59
# CG_CLIF_STDLIB_REMAP_PATH_PREFIX will cause cg_clif's build system to pass
610
# --remap-path-prefix to handle this.
7-
# CG_CLIF_FORCE_GNU_AS will force usage of as instead of the LLVM backend of rustc as we
8-
# the LLVM backend isn't compiled in here.
9-
CG_CLIF_FORCE_GNU_AS=1 CG_CLIF_STDLIB_REMAP_PATH_PREFIX=/rustc/FAKE_PREFIX ./y.sh build
11+
CG_CLIF_STDLIB_REMAP_PATH_PREFIX=/rustc/FAKE_PREFIX ./y.sh build
1012

1113
echo "[SETUP] Rust fork"
12-
git clone https://github.com/rust-lang/rust.git || true
14+
git clone https://github.com/rust-lang/rust.git --filter=tree:0 || true
1315
pushd rust
1416
git fetch
1517
git checkout -- .

compiler/rustc_codegen_cranelift/scripts/test_bootstrap.sh

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,5 @@ rm -r compiler/rustc_codegen_cranelift/{Cargo.*,src}
1111
cp ../Cargo.* compiler/rustc_codegen_cranelift/
1212
cp -r ../src compiler/rustc_codegen_cranelift/src
1313

14-
# CG_CLIF_FORCE_GNU_AS will force usage of as instead of the LLVM backend of rustc as we
15-
# the LLVM backend isn't compiled in here.
16-
CG_CLIF_FORCE_GNU_AS=1 ./x.py build --stage 1 library/std
14+
./x.py build --stage 1 library/std
1715
popd

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
383383
args,
384384
ret_place,
385385
target,
386+
source_info.span,
386387
);
387388
return;
388389
}

compiler/rustc_codegen_cranelift/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
456456
);
457457
}
458458

459-
crate::inline_asm::codegen_inline_asm(
459+
crate::inline_asm::codegen_inline_asm_terminator(
460460
fx,
461461
source_info.span,
462462
template,

compiler/rustc_codegen_cranelift/src/constant.rs

+31-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
//! Handling of `static`s, `const`s and promoted allocations
22
3+
use std::cmp::Ordering;
4+
35
use cranelift_module::*;
46
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
57
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
68
use rustc_middle::mir::interpret::{read_target_uint, AllocId, GlobalAlloc, Scalar};
79
use rustc_middle::mir::ConstValue;
10+
use rustc_middle::ty::ScalarInt;
811

912
use crate::prelude::*;
1013

@@ -430,17 +433,17 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
430433
pub(crate) fn mir_operand_get_const_val<'tcx>(
431434
fx: &FunctionCx<'_, '_, 'tcx>,
432435
operand: &Operand<'tcx>,
433-
) -> Option<ConstValue<'tcx>> {
436+
) -> Option<ScalarInt> {
434437
match operand {
435-
Operand::Constant(const_) => Some(eval_mir_constant(fx, const_).0),
438+
Operand::Constant(const_) => eval_mir_constant(fx, const_).0.try_to_scalar_int(),
436439
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
437440
// inside a temporary before being passed to the intrinsic requiring the const argument.
438441
// This code tries to find a single constant defining definition of the referenced local.
439442
Operand::Copy(place) | Operand::Move(place) => {
440443
if !place.projection.is_empty() {
441444
return None;
442445
}
443-
let mut computed_const_val = None;
446+
let mut computed_scalar_int = None;
444447
for bb_data in fx.mir.basic_blocks.iter() {
445448
for stmt in &bb_data.statements {
446449
match &stmt.kind {
@@ -456,22 +459,38 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
456459
operand,
457460
ty,
458461
) => {
459-
if computed_const_val.is_some() {
462+
if computed_scalar_int.is_some() {
460463
return None; // local assigned twice
461464
}
462465
if !matches!(ty.kind(), ty::Uint(_) | ty::Int(_)) {
463466
return None;
464467
}
465-
let const_val = mir_operand_get_const_val(fx, operand)?;
466-
if fx.layout_of(*ty).size
467-
!= const_val.try_to_scalar_int()?.size()
468+
let scalar_int = mir_operand_get_const_val(fx, operand)?;
469+
let scalar_int = match fx
470+
.layout_of(*ty)
471+
.size
472+
.cmp(&scalar_int.size())
468473
{
469-
return None;
470-
}
471-
computed_const_val = Some(const_val);
474+
Ordering::Equal => scalar_int,
475+
Ordering::Less => match ty.kind() {
476+
ty::Uint(_) => ScalarInt::try_from_uint(
477+
scalar_int.try_to_uint(scalar_int.size()).unwrap(),
478+
fx.layout_of(*ty).size,
479+
)
480+
.unwrap(),
481+
ty::Int(_) => ScalarInt::try_from_int(
482+
scalar_int.try_to_int(scalar_int.size()).unwrap(),
483+
fx.layout_of(*ty).size,
484+
)
485+
.unwrap(),
486+
_ => unreachable!(),
487+
},
488+
Ordering::Greater => return None,
489+
};
490+
computed_scalar_int = Some(scalar_int);
472491
}
473492
Rvalue::Use(operand) => {
474-
computed_const_val = mir_operand_get_const_val(fx, operand)
493+
computed_scalar_int = mir_operand_get_const_val(fx, operand)
475494
}
476495
_ => return None,
477496
}
@@ -522,7 +541,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
522541
TerminatorKind::Call { .. } => {}
523542
}
524543
}
525-
computed_const_val
544+
computed_scalar_int
526545
}
527546
}
528547
}

0 commit comments

Comments
 (0)