Skip to content

Commit

Permalink
Auto merge of rust-lang#125926 - jieyouxu:rollup-46ewsu9, r=jieyouxu
Browse files Browse the repository at this point in the history
Rollup of 7 pull requests

Successful merges:

 - rust-lang#122597 (Show files produced by `--emit foo` in json artifact notifications)
 - rust-lang#125886 (Migrate run make issue 15460)
 - rust-lang#125893 (Handle all GVN binops in a single place.)
 - rust-lang#125903 (rustc_span: Inline some hot functions)
 - rust-lang#125909 (rustdoc: add a regression test for a former blanket impl synthesis ICE)
 - rust-lang#125917 (Create `run-make` `env_var` and `env_var_os` helpers)
 - rust-lang#125919 (Remove stray "this")

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Jun 3, 2024
2 parents 8768db9 + b03b3ec commit 1fe3579
Show file tree
Hide file tree
Showing 28 changed files with 317 additions and 83 deletions.
23 changes: 23 additions & 0 deletions compiler/rustc_codegen_cranelift/src/driver/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,29 @@ fn produce_final_output_artifacts(
}
}

if sess.opts.json_artifact_notifications {
if codegen_results.modules.len() == 1 {
codegen_results.modules[0].for_each_output(|_path, ty| {
if sess.opts.output_types.contains_key(&ty) {
let descr = ty.shorthand();
// for single cgu file is renamed to drop cgu specific suffix
// so we regenerate it the same way
let path = crate_output.path(ty);
sess.dcx().emit_artifact_notification(path.as_path(), descr);
}
});
} else {
for module in &codegen_results.modules {
module.for_each_output(|path, ty| {
if sess.opts.output_types.contains_key(&ty) {
let descr = ty.shorthand();
sess.dcx().emit_artifact_notification(&path, descr);
}
});
}
}
}

// We leave the following files around by default:
// - #crate#.o
// - #crate#.crate.metadata.o
Expand Down
23 changes: 23 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,29 @@ fn produce_final_output_artifacts(
}
}

if sess.opts.json_artifact_notifications {
if compiled_modules.modules.len() == 1 {
compiled_modules.modules[0].for_each_output(|_path, ty| {
if sess.opts.output_types.contains_key(&ty) {
let descr = ty.shorthand();
// for single cgu file is renamed to drop cgu specific suffix
// so we regenerate it the same way
let path = crate_output.path(ty);
sess.dcx().emit_artifact_notification(path.as_path(), descr);
}
});
} else {
for module in &compiled_modules.modules {
module.for_each_output(|path, ty| {
if sess.opts.output_types.contains_key(&ty) {
let descr = ty.shorthand();
sess.dcx().emit_artifact_notification(&path, descr);
}
});
}
}
}

// We leave the following files around by default:
// - #crate#.o
// - #crate#.crate.metadata.o
Expand Down
18 changes: 18 additions & 0 deletions compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ pub struct CompiledModule {
pub llvm_ir: Option<PathBuf>, // --emit=llvm-ir, llvm-bc is in bytecode
}

impl CompiledModule {
/// Call `emit` function with every artifact type currently compiled
pub fn for_each_output(&self, mut emit: impl FnMut(&Path, OutputType)) {
if let Some(path) = self.object.as_deref() {
emit(path, OutputType::Object);
}
if let Some(path) = self.bytecode.as_deref() {
emit(path, OutputType::Bitcode);
}
if let Some(path) = self.llvm_ir.as_deref() {
emit(path, OutputType::LlvmAssembly);
}
if let Some(path) = self.assembly.as_deref() {
emit(path, OutputType::Assembly);
}
}
}

pub struct CachedModuleCodegen {
pub name: String,
pub source: WorkProduct,
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_mir_transform/src/dump_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub fn emit_mir(tcx: TyCtxt<'_>) -> io::Result<()> {
OutFileName::Real(path) => {
let mut f = io::BufWriter::new(File::create(&path)?);
write_mir_pretty(tcx, None, &mut f)?;
if tcx.sess.opts.json_artifact_notifications {
tcx.dcx().emit_artifact_notification(&path, "mir");
}
}
}
Ok(())
Expand Down
70 changes: 40 additions & 30 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ enum Value<'tcx> {
NullaryOp(NullOp<'tcx>, Ty<'tcx>),
UnaryOp(UnOp, VnIndex),
BinaryOp(BinOp, VnIndex, VnIndex),
CheckedBinaryOp(BinOp, VnIndex, VnIndex), // FIXME get rid of this, work like MIR instead
Cast {
kind: CastKind,
value: VnIndex,
Expand Down Expand Up @@ -508,17 +507,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
let val = self.ecx.binary_op(bin_op, &lhs, &rhs).ok()?;
val.into()
}
CheckedBinaryOp(bin_op, lhs, rhs) => {
let lhs = self.evaluated[lhs].as_ref()?;
let lhs = self.ecx.read_immediate(lhs).ok()?;
let rhs = self.evaluated[rhs].as_ref()?;
let rhs = self.ecx.read_immediate(rhs).ok()?;
let val = self
.ecx
.binary_op(bin_op.wrapping_to_overflowing().unwrap(), &lhs, &rhs)
.ok()?;
val.into()
}
Cast { kind, value, from: _, to } => match kind {
CastKind::IntToInt | CastKind::IntToFloat => {
let value = self.evaluated[value].as_ref()?;
Expand Down Expand Up @@ -829,17 +817,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
let lhs = lhs?;
let rhs = rhs?;

if let Some(op) = op.overflowing_to_wrapping() {
if let Some(value) = self.simplify_binary(op, true, ty, lhs, rhs) {
return Some(value);
}
Value::CheckedBinaryOp(op, lhs, rhs)
} else {
if let Some(value) = self.simplify_binary(op, false, ty, lhs, rhs) {
return Some(value);
}
Value::BinaryOp(op, lhs, rhs)
if let Some(value) = self.simplify_binary(op, ty, lhs, rhs) {
return Some(value);
}
Value::BinaryOp(op, lhs, rhs)
}
Rvalue::UnaryOp(op, ref mut arg) => {
let arg = self.simplify_operand(arg, location)?;
Expand Down Expand Up @@ -970,7 +951,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
fn simplify_binary(
&mut self,
op: BinOp,
checked: bool,
lhs_ty: Ty<'tcx>,
lhs: VnIndex,
rhs: VnIndex,
Expand Down Expand Up @@ -999,22 +979,39 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
use Either::{Left, Right};
let a = as_bits(lhs).map_or(Right(lhs), Left);
let b = as_bits(rhs).map_or(Right(rhs), Left);

let result = match (op, a, b) {
// Neutral elements.
(BinOp::Add | BinOp::BitOr | BinOp::BitXor, Left(0), Right(p))
(
BinOp::Add
| BinOp::AddWithOverflow
| BinOp::AddUnchecked
| BinOp::BitOr
| BinOp::BitXor,
Left(0),
Right(p),
)
| (
BinOp::Add
| BinOp::AddWithOverflow
| BinOp::AddUnchecked
| BinOp::BitOr
| BinOp::BitXor
| BinOp::Sub
| BinOp::SubWithOverflow
| BinOp::SubUnchecked
| BinOp::Offset
| BinOp::Shl
| BinOp::Shr,
Right(p),
Left(0),
)
| (BinOp::Mul, Left(1), Right(p))
| (BinOp::Mul | BinOp::Div, Right(p), Left(1)) => p,
| (BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked, Left(1), Right(p))
| (
BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked | BinOp::Div,
Right(p),
Left(1),
) => p,
// Attempt to simplify `x & ALL_ONES` to `x`, with `ALL_ONES` depending on type size.
(BinOp::BitAnd, Right(p), Left(ones)) | (BinOp::BitAnd, Left(ones), Right(p))
if ones == layout.size.truncate(u128::MAX)
Expand All @@ -1023,10 +1020,21 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
p
}
// Absorbing elements.
(BinOp::Mul | BinOp::BitAnd, _, Left(0))
(
BinOp::Mul | BinOp::MulWithOverflow | BinOp::MulUnchecked | BinOp::BitAnd,
_,
Left(0),
)
| (BinOp::Rem, _, Left(1))
| (
BinOp::Mul | BinOp::Div | BinOp::Rem | BinOp::BitAnd | BinOp::Shl | BinOp::Shr,
BinOp::Mul
| BinOp::MulWithOverflow
| BinOp::MulUnchecked
| BinOp::Div
| BinOp::Rem
| BinOp::BitAnd
| BinOp::Shl
| BinOp::Shr,
Left(0),
_,
) => self.insert_scalar(Scalar::from_uint(0u128, layout.size), lhs_ty),
Expand All @@ -1038,7 +1046,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
self.insert_scalar(Scalar::from_uint(ones, layout.size), lhs_ty)
}
// Sub/Xor with itself.
(BinOp::Sub | BinOp::BitXor, a, b) if a == b => {
(BinOp::Sub | BinOp::SubWithOverflow | BinOp::SubUnchecked | BinOp::BitXor, a, b)
if a == b =>
{
self.insert_scalar(Scalar::from_uint(0u128, layout.size), lhs_ty)
}
// Comparison:
Expand All @@ -1052,7 +1062,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
_ => return None,
};

if checked {
if op.is_overflowing() {
let false_val = self.insert_bool(false);
Some(self.insert_tuple(vec![result, false_val]))
} else {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/span_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ impl Span {

// Returns either syntactic context, if it can be retrieved without taking the interner lock,
// or an index into the interner if it cannot.
#[inline]
fn inline_ctxt(self) -> Result<SyntaxContext, usize> {
Ok(if self.len_with_tag_or_marker != BASE_LEN_INTERNED_MARKER {
if self.len_with_tag_or_marker & PARENT_TAG == 0 {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,7 @@ impl fmt::Display for IdentPrinter {
pub struct MacroRulesNormalizedIdent(Ident);

impl MacroRulesNormalizedIdent {
#[inline]
pub fn new(ident: Ident) -> Self {
Self(ident.normalize_to_macro_rules())
}
Expand Down
3 changes: 1 addition & 2 deletions library/std/src/sys/pal/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1910,8 +1910,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
// The code below ensures that `FreeOnDrop` is never a null pointer
unsafe {
// `copyfile_state_free` returns -1 if the `to` or `from` files
// cannot be closed. However, this is not considered this an
// error.
// cannot be closed. However, this is not considered an error.
libc::copyfile_state_free(self.0);
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/doc/rustc/src/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ Diagnostics have the following format:
Artifact notifications are emitted when the [`--json=artifacts`
flag][option-json] is used. They indicate that a file artifact has been saved
to disk. More information about emit kinds may be found in the [`--emit`
flag][option-emit] documentation.
flag][option-emit] documentation. Notifications can contain more than one file
for each type, for example when using multiple codegen units.

```javascript
{
Expand All @@ -229,6 +230,11 @@ flag][option-emit] documentation.
- "link": The generated crate as specified by the crate-type.
- "dep-info": The `.d` file with dependency information in a Makefile-like syntax.
- "metadata": The Rust `.rmeta` file containing metadata about the crate.
- "asm": The `.s` file with generated assembly
- "llvm-ir": The `.ll` file with generated textual LLVM IR
- "llvm-bc": The `.bc` file with generated LLVM bitcode
- "mir": The `.mir` file with rustc's mid-level intermediate representation.
- "obj": The `.o` file with generated native object code
*/
"emit": "link"
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3655,6 +3655,7 @@ impl<'test> TestCx<'test> {
cmd.env("IS_MSVC", "1")
.env("IS_WINDOWS", "1")
.env("MSVC_LIB", format!("'{}' -nologo", lib.display()))
.env("MSVC_LIB_PATH", format!("{}", lib.display()))
// Note: we diverge from legacy run_make and don't lump `CC` the compiler and
// default flags together.
.env("CC_DEFAULT_FLAGS", &cflags)
Expand Down
9 changes: 5 additions & 4 deletions src/tools/run-make-support/src/cc.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::env;
use std::path::Path;
use std::process::Command;

use crate::{bin_name, cygpath_windows, handle_failed_output, is_msvc, is_windows, tmp_dir, uname};
use crate::{
bin_name, cygpath_windows, env_var, handle_failed_output, is_msvc, is_windows, tmp_dir, uname,
};

/// Construct a new platform-specific C compiler invocation.
///
Expand All @@ -27,11 +28,11 @@ impl Cc {
/// WARNING: This means that what flags are accepted by the underlying C compile is
/// platform- AND compiler-specific. Consult the relevant docs for `gcc`, `clang` and `mvsc`.
pub fn new() -> Self {
let compiler = env::var("CC").unwrap();
let compiler = env_var("CC");

let mut cmd = Command::new(compiler);

let default_cflags = env::var("CC_DEFAULT_FLAGS").unwrap();
let default_cflags = env_var("CC_DEFAULT_FLAGS");
for flag in default_cflags.split(char::is_whitespace) {
cmd.arg(flag);
}
Expand Down
6 changes: 2 additions & 4 deletions src/tools/run-make-support/src/clang.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::env;
use std::path::Path;
use std::process::Command;

use crate::{bin_name, handle_failed_output, tmp_dir};
use crate::{bin_name, env_var, handle_failed_output, tmp_dir};

/// Construct a new `clang` invocation. `clang` is not always available for all targets.
pub fn clang() -> Clang {
Expand All @@ -20,8 +19,7 @@ crate::impl_common_helpers!(Clang);
impl Clang {
/// Construct a new `clang` invocation. `clang` is not always available for all targets.
pub fn new() -> Self {
let clang =
env::var("CLANG").expect("`CLANG` not specified, but this is required to find `clang`");
let clang = env_var("CLANG");
let cmd = Command::new(clang);
Self { cmd }
}
Expand Down
Loading

0 comments on commit 1fe3579

Please sign in to comment.