Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 4 pull requests #110612

Merged
merged 9 commits into from
Apr 20, 2023
13 changes: 0 additions & 13 deletions compiler/rustc_middle/src/ty/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use std::marker::PhantomData;
use std::mem;
use std::num::NonZeroUsize;
use std::ops::{ControlFlow, Deref};
use std::slice;

/// An entity in the Rust type system, which can be one of
/// several kinds (types, lifetimes, and consts).
Expand Down Expand Up @@ -55,18 +54,6 @@ pub enum GenericArgKind<'tcx> {
Const(ty::Const<'tcx>),
}

/// This function goes from `&'a [Ty<'tcx>]` to `&'a [GenericArg<'tcx>]`
///
/// This is sound as, for types, `GenericArg` is just
/// `NonZeroUsize::new_unchecked(ty as *const _ as usize)` as
/// long as we use `0` for the `TYPE_TAG`.
pub fn ty_slice_as_generic_args<'a, 'tcx>(ts: &'a [Ty<'tcx>]) -> &'a [GenericArg<'tcx>] {
assert_eq!(TYPE_TAG, 0);
// SAFETY: the whole slice is valid and immutable.
// `Ty` and `GenericArg` is explained above.
unsafe { slice::from_raw_parts(ts.as_ptr().cast(), ts.len()) }
}

impl<'tcx> GenericArgKind<'tcx> {
#[inline]
fn pack(self) -> GenericArg<'tcx> {
Expand Down
22 changes: 21 additions & 1 deletion compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ fn rustc_place_to_place(place: &rustc_middle::mir::Place<'_>) -> stable_mir::mir
stable_mir::mir::Place { local: place.local.as_usize() }
}

fn rustc_unwind_to_unwind(
unwind: &rustc_middle::mir::UnwindAction,
) -> stable_mir::mir::UnwindAction {
use rustc_middle::mir::UnwindAction;
match unwind {
UnwindAction::Continue => stable_mir::mir::UnwindAction::Continue,
UnwindAction::Unreachable => stable_mir::mir::UnwindAction::Unreachable,
UnwindAction::Terminate => stable_mir::mir::UnwindAction::Terminate,
UnwindAction::Cleanup(bb) => stable_mir::mir::UnwindAction::Cleanup(bb.as_usize()),
}
}

fn rustc_terminator_to_terminator(
terminator: &rustc_middle::mir::Terminator<'_>,
) -> stable_mir::mir::Terminator {
Expand All @@ -151,7 +163,15 @@ fn rustc_terminator_to_terminator(
Return => Terminator::Return,
Unreachable => Terminator::Unreachable,
Drop { .. } => todo!(),
Call { .. } => todo!(),
Call { func, args, destination, target, unwind, from_hir_call: _, fn_span: _ } => {
Terminator::Call {
func: rustc_op_to_op(func),
args: args.iter().map(|arg| rustc_op_to_op(arg)).collect(),
destination: rustc_place_to_place(destination),
target: target.map(|t| t.as_usize()),
unwind: rustc_unwind_to_unwind(unwind),
}
}
Assert { .. } => todo!(),
Yield { .. } => todo!(),
GeneratorDrop => todo!(),
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_smir/src/stable_mir/mir/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum Terminator {
args: Vec<Operand>,
destination: Place,
target: Option<usize>,
cleanup: Option<usize>,
unwind: UnwindAction,
},
Assert {
cond: Operand,
Expand All @@ -44,6 +44,14 @@ pub enum Terminator {
},
}

#[derive(Clone, Debug)]
pub enum UnwindAction {
Continue,
Unreachable,
Terminate,
Cleanup(usize),
}

#[derive(Clone, Debug)]
pub enum Statement {
Assign(Place, Operand),
Expand Down
21 changes: 16 additions & 5 deletions compiler/rustc_target/src/spec/apple_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum Arch {
I386,
I686,
X86_64,
X86_64h,
X86_64_sim,
X86_64_macabi,
Arm64_macabi,
Expand All @@ -36,6 +37,7 @@ impl Arch {
I386 => "i386",
I686 => "i686",
X86_64 | X86_64_sim | X86_64_macabi => "x86_64",
X86_64h => "x86_64h",
}
}

Expand All @@ -44,13 +46,13 @@ impl Arch {
Armv7 | Armv7k | Armv7s => "arm",
Arm64 | Arm64_32 | Arm64_macabi | Arm64_sim => "aarch64",
I386 | I686 => "x86",
X86_64 | X86_64_sim | X86_64_macabi => "x86_64",
X86_64 | X86_64_sim | X86_64_macabi | X86_64h => "x86_64",
})
}

fn target_abi(self) -> &'static str {
match self {
Armv7 | Armv7k | Armv7s | Arm64 | Arm64_32 | I386 | I686 | X86_64 => "",
Armv7 | Armv7k | Armv7s | Arm64 | Arm64_32 | I386 | I686 | X86_64 | X86_64h => "",
X86_64_macabi | Arm64_macabi => "macabi",
// x86_64-apple-ios is a simulator target, even though it isn't
// declared that way in the target like the other ones...
Expand All @@ -67,6 +69,10 @@ impl Arch {
Arm64_32 => "apple-s4",
I386 | I686 => "yonah",
X86_64 | X86_64_sim => "core2",
// Note: `core-avx2` is slightly more advanced than `x86_64h`, see
// comments (and disabled features) in `x86_64h_apple_darwin` for
// details.
X86_64h => "core-avx2",
X86_64_macabi => "core2",
Arm64_macabi => "apple-a12",
Arm64_sim => "apple-a12",
Expand Down Expand Up @@ -182,8 +188,13 @@ fn deployment_target(var_name: &str) -> Option<(u32, u32)> {
}

fn macos_default_deployment_target(arch: Arch) -> (u32, u32) {
// Note: Arm64_sim is not included since macOS has no simulator.
if matches!(arch, Arm64 | Arm64_macabi) { (11, 0) } else { (10, 7) }
match arch {
// Note: Arm64_sim is not included since macOS has no simulator.
Arm64 | Arm64_macabi => (11, 0),
// x86_64h-apple-darwin only supports macOS 10.8 and later
X86_64h => (10, 8),
_ => (10, 7),
}
}

fn macos_deployment_target(arch: Arch) -> (u32, u32) {
Expand Down Expand Up @@ -227,7 +238,7 @@ fn link_env_remove(arch: Arch, os: &'static str) -> StaticCow<[StaticCow<str>]>
// of the linking environment that's wrong and reversed.
match arch {
Armv7 | Armv7k | Armv7s | Arm64 | Arm64_32 | I386 | I686 | X86_64 | X86_64_sim
| Arm64_sim => {
| X86_64h | Arm64_sim => {
cvs!["MACOSX_DEPLOYMENT_TARGET"]
}
X86_64_macabi | Arm64_macabi => cvs!["IPHONEOS_DEPLOYMENT_TARGET"],
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ supported_targets! {

("aarch64-apple-darwin", aarch64_apple_darwin),
("x86_64-apple-darwin", x86_64_apple_darwin),
("x86_64h-apple-darwin", x86_64h_apple_darwin),
("i686-apple-darwin", i686_apple_darwin),

// FIXME(#106649): Remove aarch64-fuchsia in favor of aarch64-unknown-fuchsia
Expand Down
44 changes: 44 additions & 0 deletions compiler/rustc_target/src/spec/x86_64h_apple_darwin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use super::apple_base::{macos_llvm_target, opts, Arch};
use crate::spec::{Cc, FramePointer, LinkerFlavor, Lld, SanitizerSet};
use crate::spec::{StackProbeType, Target, TargetOptions};

pub fn target() -> Target {
let arch = Arch::X86_64h;
let mut base = opts("macos", arch);
base.max_atomic_width = Some(128);
base.frame_pointer = FramePointer::Always;
base.add_pre_link_args(LinkerFlavor::Darwin(Cc::Yes, Lld::No), &["-m64"]);
base.stack_probes = StackProbeType::X86;
base.supported_sanitizers =
SanitizerSet::ADDRESS | SanitizerSet::CFI | SanitizerSet::LEAK | SanitizerSet::THREAD;

// x86_64h is core2-avx without a few of the features which would otherwise
// be guaranteed, so we need to disable those. This imitates clang's logic:
// - https://github.com/llvm/llvm-project/blob/bd1f7c417/clang/lib/Driver/ToolChains/Arch/X86.cpp#L77-L78
// - https://github.com/llvm/llvm-project/blob/bd1f7c417/clang/lib/Driver/ToolChains/Arch/X86.cpp#L133-L141
//
// FIXME: Sadly, turning these off here disables them in such a way that they
// aren't re-enabled by `-Ctarget-cpu=native` (on a machine that has them).
// It would be nice if this were not the case, but fixing it seems tricky
// (and given that the main use-case for this target is for use in universal
// binaries, probably not that important).
base.features = "-rdrnd,-aes,-pclmul,-rtm,-fsgsbase".into();
// Double-check that the `cpu` is what we expect (if it's not the list above
// may need updating).
assert_eq!(
base.cpu, "core-avx2",
"you need to adjust the feature list in x86_64h-apple-darwin if you change this",
);

Target {
// Clang automatically chooses a more specific target based on
// MACOSX_DEPLOYMENT_TARGET. To enable cross-language LTO to work
// correctly, we do too.
llvm_target: macos_llvm_target(arch).into(),
pointer_width: 64,
data_layout: "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
.into(),
arch: arch.target_arch(),
options: TargetOptions { mcount: "\u{1}mcount".into(), ..base },
}
}
1 change: 1 addition & 0 deletions src/doc/rustc/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- [wasm64-unknown-unknown](platform-support/wasm64-unknown-unknown.md)
- [x86_64-fortanix-unknown-sgx](platform-support/x86_64-fortanix-unknown-sgx.md)
- [x86_64-unknown-none](platform-support/x86_64-unknown-none.md)
- [x86_64h-apple-darwin](platform-support/x86_64h-apple-darwin.md)
- [Targets](targets/index.md)
- [Built-in Targets](targets/built-in.md)
- [Custom Targets](targets/custom.md)
Expand Down
1 change: 1 addition & 0 deletions src/doc/rustc/src/platform-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,6 @@ target | std | host | notes
`x86_64-uwp-windows-gnu` | ✓ | |
`x86_64-uwp-windows-msvc` | ✓ | |
`x86_64-wrs-vxworks` | ? | |
`x86_64h-apple-darwin` | ✓ | ✓ | macOS with late-gen Intel (at least Haswell)

[runs on NVIDIA GPUs]: https://github.com/japaric-archived/nvptx#targets
57 changes: 57 additions & 0 deletions src/doc/rustc/src/platform-support/x86_64h-apple-darwin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# `x86_64h-apple-darwin`

**Tier: 3**

Target for macOS on late-generation `x86_64` Apple chips, usable as the
`x86_64h` entry in universal binaries, and equivalent to LLVM's
`x86_64h-apple-macosx*` targets.

## Target maintainers

- Thom Chiovoloni `[email protected]` <https://github.com/thomcc>

## Requirements

This target is an `x86_64` target that only supports Apple's late-gen
(Haswell-compatible) Intel chips. It enables a set of target features available
on these chips (AVX2 and similar), and MachO binaries built with this target may
be used as the `x86_64h` entry in universal binaries ("fat" MachO binaries), and
will fail to load on machines that do not support this.

It should support the full standard library (`std` and `alloc` either with
default or user-defined allocators). This target is probably most useful when
targetted via cross-compilation (including from `x86_64-apple-darwin`), but if
built manually, the host tools work.

It is similar to `x86_64-apple-darwin` in nearly all respects, although the
minimum supported OS version is slightly higher (it requires 10.8 rather than
`x86_64-apple-darwin`'s 10.7).

## Building the target

Users on Apple targets can build this by adding it to the `target` list in
`config.toml`, or with `-Zbuild-std`.

## Building Rust programs

Rust does not yet ship pre-compiled artifacts for this target. To compile for
this target, you will either need to build Rust with the target enabled (see
"Building the target" above), or build your own copy of `core` by using
`build-std` or similar.

## Testing

Code built with this target can be run on the set of Intel macOS machines that
support running `x86_64h` binaries (relatively recent Intel macs). The Rust test
suite seems to work.

## Cross-compilation toolchains and C code

Cross-compilation to this target from Apple hosts should generally work without
much configuration, so long as XCode and the CommandLineTools are installed.
Targetting it from non-Apple hosts is difficult, but no moreso than targetting
`x86_64-apple-darwin`.

When compiling C code for this target, either the "`x86_64h-apple-macosx*`" LLVM
targets should be used, or an argument like `-arch x86_64h` should be passed to
the C compiler.
26 changes: 18 additions & 8 deletions src/tools/linkchecker/linkcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
#
# --all Check all books. This can help make sure you don't break links
# from other books into your book.
#
# --path <book-path>
# Path to the root directory for the book. Default to the current
# working directory if omitted.

set -e

if [ ! -f book.toml ] && [ ! -f src/SUMMARY.md ]
then
echo "Run command in root directory of the book."
exit 1
fi

html_dir="$(rustc +nightly --print sysroot)/share/doc/rust/html"

if [ ! -d "$html_dir" ]
Expand All @@ -38,6 +36,8 @@ fi
export MDBOOK_OUTPUT__HTML__INPUT_404=""

book_name=""
# Default to the current directory
book_path="."
# Iterative will avoid cleaning up, so you can quickly run it repeatedly.
iterative=0
# If "1", test all books, else only this book.
Expand All @@ -52,6 +52,10 @@ do
--all)
all_books=1
;;
--path)
book_path="${2:-.}"
shift
;;
*)
if [ -n "$book_name" ]
then
Expand All @@ -70,6 +74,12 @@ then
exit 1
fi

if [ ! -f "$book_path/book.toml" ] && [ ! -f "$book_path/src/SUMMARY.md" ]
then
echo "Run command in root directory of the book or provide a path to the book"
exit 1
fi

if [ ! -d "$html_dir/$book_name" ]
then
echo "book name \"$book_name\" not found in sysroot \"$html_dir\""
Expand All @@ -93,11 +103,11 @@ then
fi

echo "Building book \"$book_name\"..."
mdbook build
mdbook build "$book_path"

cp -R "$html_dir" linkcheck
rm -rf "linkcheck/$book_name"
cp -R book "linkcheck/$book_name"
cp -R "$book_path/book" "linkcheck/$book_name"

if [ "$all_books" = "1" ]
then
Expand Down
10 changes: 9 additions & 1 deletion tests/ui-fulldeps/stable-mir/crate-info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ fn test_stable_mir(tcx: TyCtxt<'_>) {

// Find items in the local crate.
let items = stable_mir::all_local_items();
assert!(get_item(tcx, &items, (DefKind::Fn, "foo_bar")).is_some());
assert!(get_item(tcx, &items, (DefKind::Fn, "foo::bar")).is_some());

// Find the `std` crate.
Expand All @@ -52,6 +51,15 @@ fn test_stable_mir(tcx: TyCtxt<'_>) {
stable_mir::mir::Terminator::Return => {}
other => panic!("{other:?}"),
}

let foo_bar = get_item(tcx, &items, (DefKind::Fn, "foo_bar")).unwrap();
let body = foo_bar.body();
assert_eq!(body.blocks.len(), 4);
let block = &body.blocks[0];
match &block.terminator {
stable_mir::mir::Terminator::Call { .. } => {}
other => panic!("{other:?}"),
}
}

// Use internal API to find a function in a crate.
Expand Down