compiler: rustc_abi::Abi
=> BackendRepr
The initial naming of "Abi" was an awful mistake, conveying wrong ideas about how psABIs worked and even more about what the enum meant. It was only meant to represent the way the value would be described to a codegen backend as it was lowered to that intermediate representation. It was never meant to mean anything about the actual psABI handling! The conflation is because LLVM typically will associate a certain form with a certain ABI, but even that does not hold when the special cases that actually exist arise, plus the IR annotations that modify the ABI. Reframe `rustc_abi::Abi` as the `BackendRepr` of the type, and rename `BackendRepr::Aggregate` as `BackendRepr::Memory`. Unfortunately, due to the persistent misunderstandings, this too is now incorrect: - Scattered ABI-relevant code is entangled with BackendRepr - We do not always pre-compute a correct BackendRepr that reflects how we "actually" want this value to be handled, so we leave the backend interface to also inject various special-cases here - In some cases `BackendRepr::Memory` is a "real" aggregate, but in others it is in fact using memory, and in some cases it is a scalar! Our rustc-to-backend lowering code handles this sort of thing right now. That will eventually be addressed by lifting duplicated lowering code to either rustc_codegen_ssa or rustc_target as appropriate.
This commit is contained in:
parent
2dece5bb62
commit
7086dd83cc
51 changed files with 517 additions and 428 deletions
|
@ -2,6 +2,7 @@
|
|||
//!
|
||||
//! Currently, this pass only propagates scalar values.
|
||||
|
||||
use rustc_abi::{BackendRepr, FIRST_VARIANT, FieldIdx, Size, VariantIdx};
|
||||
use rustc_const_eval::const_eval::{DummyMachine, throw_machine_stop_str};
|
||||
use rustc_const_eval::interpret::{
|
||||
ImmTy, Immediate, InterpCx, OpTy, PlaceTy, Projectable, interp_ok,
|
||||
|
@ -20,7 +21,6 @@ use rustc_mir_dataflow::value_analysis::{
|
|||
};
|
||||
use rustc_mir_dataflow::{Analysis, Results, ResultsVisitor};
|
||||
use rustc_span::DUMMY_SP;
|
||||
use rustc_target::abi::{Abi, FIRST_VARIANT, FieldIdx, Size, VariantIdx};
|
||||
use tracing::{debug, debug_span, instrument};
|
||||
|
||||
// These constants are somewhat random guesses and have not been optimized.
|
||||
|
@ -457,7 +457,7 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
|
|||
// a pair and sometimes not. But as a hack we always return a pair
|
||||
// and just make the 2nd component `Bottom` when it does not exist.
|
||||
Some(val) => {
|
||||
if matches!(val.layout.abi, Abi::ScalarPair(..)) {
|
||||
if matches!(val.layout.backend_repr, BackendRepr::ScalarPair(..)) {
|
||||
let (val, overflow) = val.to_scalar_pair();
|
||||
(FlatSet::Elem(val), FlatSet::Elem(overflow))
|
||||
} else {
|
||||
|
@ -470,7 +470,7 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
|
|||
// Exactly one side is known, attempt some algebraic simplifications.
|
||||
(FlatSet::Elem(const_arg), _) | (_, FlatSet::Elem(const_arg)) => {
|
||||
let layout = const_arg.layout;
|
||||
if !matches!(layout.abi, rustc_target::abi::Abi::Scalar(..)) {
|
||||
if !matches!(layout.backend_repr, rustc_target::abi::BackendRepr::Scalar(..)) {
|
||||
return (FlatSet::Top, FlatSet::Top);
|
||||
}
|
||||
|
||||
|
@ -589,13 +589,13 @@ impl<'a, 'tcx> Collector<'a, 'tcx> {
|
|||
}
|
||||
|
||||
let place = map.find(place.as_ref())?;
|
||||
if layout.abi.is_scalar()
|
||||
if layout.backend_repr.is_scalar()
|
||||
&& let Some(value) = propagatable_scalar(place, state, map)
|
||||
{
|
||||
return Some(Const::Val(ConstValue::Scalar(value), ty));
|
||||
}
|
||||
|
||||
if matches!(layout.abi, Abi::Scalar(..) | Abi::ScalarPair(..)) {
|
||||
if matches!(layout.backend_repr, BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..)) {
|
||||
let alloc_id = ecx
|
||||
.intern_with_temp_alloc(layout, |ecx, dest| {
|
||||
try_write_constant(ecx, dest, place, ty, state, map)
|
||||
|
@ -641,7 +641,7 @@ fn try_write_constant<'tcx>(
|
|||
}
|
||||
|
||||
// Fast path for scalars.
|
||||
if layout.abi.is_scalar()
|
||||
if layout.backend_repr.is_scalar()
|
||||
&& let Some(value) = propagatable_scalar(place, state, map)
|
||||
{
|
||||
return ecx.write_immediate(Immediate::Scalar(value), dest);
|
||||
|
|
|
@ -85,6 +85,7 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use either::Either;
|
||||
use rustc_abi::{self as abi, BackendRepr, FIRST_VARIANT, FieldIdx, Primitive, Size, VariantIdx};
|
||||
use rustc_const_eval::const_eval::DummyMachine;
|
||||
use rustc_const_eval::interpret::{
|
||||
ImmTy, Immediate, InterpCx, MemPlaceMeta, MemoryKind, OpTy, Projectable, Scalar,
|
||||
|
@ -103,7 +104,6 @@ use rustc_middle::ty::layout::{HasParamEnv, LayoutOf};
|
|||
use rustc_middle::ty::{self, Ty, TyCtxt};
|
||||
use rustc_span::DUMMY_SP;
|
||||
use rustc_span::def_id::DefId;
|
||||
use rustc_target::abi::{self, Abi, FIRST_VARIANT, FieldIdx, Primitive, Size, VariantIdx};
|
||||
use smallvec::SmallVec;
|
||||
use tracing::{debug, instrument, trace};
|
||||
|
||||
|
@ -427,7 +427,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
|
|||
};
|
||||
let ptr_imm = Immediate::new_pointer_with_meta(data, meta, &self.ecx);
|
||||
ImmTy::from_immediate(ptr_imm, ty).into()
|
||||
} else if matches!(ty.abi, Abi::Scalar(..) | Abi::ScalarPair(..)) {
|
||||
} else if matches!(
|
||||
ty.backend_repr,
|
||||
BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..)
|
||||
) {
|
||||
let dest = self.ecx.allocate(ty, MemoryKind::Stack).discard_err()?;
|
||||
let variant_dest = if let Some(variant) = variant {
|
||||
self.ecx.project_downcast(&dest, variant).discard_err()?
|
||||
|
@ -573,12 +576,12 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
|
|||
// limited transmutes: it only works between types with the same layout, and
|
||||
// cannot transmute pointers to integers.
|
||||
if value.as_mplace_or_imm().is_right() {
|
||||
let can_transmute = match (value.layout.abi, to.abi) {
|
||||
(Abi::Scalar(s1), Abi::Scalar(s2)) => {
|
||||
let can_transmute = match (value.layout.backend_repr, to.backend_repr) {
|
||||
(BackendRepr::Scalar(s1), BackendRepr::Scalar(s2)) => {
|
||||
s1.size(&self.ecx) == s2.size(&self.ecx)
|
||||
&& !matches!(s1.primitive(), Primitive::Pointer(..))
|
||||
}
|
||||
(Abi::ScalarPair(a1, b1), Abi::ScalarPair(a2, b2)) => {
|
||||
(BackendRepr::ScalarPair(a1, b1), BackendRepr::ScalarPair(a2, b2)) => {
|
||||
a1.size(&self.ecx) == a2.size(&self.ecx) &&
|
||||
b1.size(&self.ecx) == b2.size(&self.ecx) &&
|
||||
// The alignment of the second component determines its offset, so that also needs to match.
|
||||
|
@ -1241,7 +1244,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
|
|||
|
||||
let as_bits = |value| {
|
||||
let constant = self.evaluated[value].as_ref()?;
|
||||
if layout.abi.is_scalar() {
|
||||
if layout.backend_repr.is_scalar() {
|
||||
let scalar = self.ecx.read_scalar(constant).discard_err()?;
|
||||
scalar.to_bits(constant.layout.size).discard_err()
|
||||
} else {
|
||||
|
@ -1497,12 +1500,12 @@ fn op_to_prop_const<'tcx>(
|
|||
|
||||
// Do not synthetize too large constants. Codegen will just memcpy them, which we'd like to
|
||||
// avoid.
|
||||
if !matches!(op.layout.abi, Abi::Scalar(..) | Abi::ScalarPair(..)) {
|
||||
if !matches!(op.layout.backend_repr, BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..)) {
|
||||
return None;
|
||||
}
|
||||
|
||||
// If this constant has scalar ABI, return it as a `ConstValue::Scalar`.
|
||||
if let Abi::Scalar(abi::Scalar::Initialized { .. }) = op.layout.abi
|
||||
if let BackendRepr::Scalar(abi::Scalar::Initialized { .. }) = op.layout.backend_repr
|
||||
&& let Some(scalar) = ecx.read_scalar(op).discard_err()
|
||||
{
|
||||
if !scalar.try_to_scalar_int().is_ok() {
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
use std::fmt::Debug;
|
||||
|
||||
use rustc_abi::{BackendRepr, FieldIdx, HasDataLayout, Size, TargetDataLayout, VariantIdx};
|
||||
use rustc_const_eval::const_eval::DummyMachine;
|
||||
use rustc_const_eval::interpret::{
|
||||
ImmTy, InterpCx, InterpResult, Projectable, Scalar, format_interp_error, interp_ok,
|
||||
|
@ -19,7 +20,6 @@ use rustc_middle::mir::*;
|
|||
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
|
||||
use rustc_middle::ty::{self, ConstInt, ParamEnv, ScalarInt, Ty, TyCtxt, TypeVisitableExt};
|
||||
use rustc_span::Span;
|
||||
use rustc_target::abi::{Abi, FieldIdx, HasDataLayout, Size, TargetDataLayout, VariantIdx};
|
||||
use tracing::{debug, instrument, trace};
|
||||
|
||||
use crate::errors::{AssertLint, AssertLintKind};
|
||||
|
@ -557,7 +557,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
|||
let right = self.use_ecx(|this| this.ecx.read_immediate(&right))?;
|
||||
|
||||
let val = self.use_ecx(|this| this.ecx.binary_op(bin_op, &left, &right))?;
|
||||
if matches!(val.layout.abi, Abi::ScalarPair(..)) {
|
||||
if matches!(val.layout.backend_repr, BackendRepr::ScalarPair(..)) {
|
||||
// FIXME `Value` should properly support pairs in `Immediate`... but currently
|
||||
// it does not.
|
||||
let (val, overflow) = val.to_pair(&self.ecx);
|
||||
|
@ -651,9 +651,9 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
|||
let to = self.ecx.layout_of(to).ok()?;
|
||||
// `offset` for immediates only supports scalar/scalar-pair ABIs,
|
||||
// so bail out if the target is not one.
|
||||
match (value.layout.abi, to.abi) {
|
||||
(Abi::Scalar(..), Abi::Scalar(..)) => {}
|
||||
(Abi::ScalarPair(..), Abi::ScalarPair(..)) => {}
|
||||
match (value.layout.backend_repr, to.backend_repr) {
|
||||
(BackendRepr::Scalar(..), BackendRepr::Scalar(..)) => {}
|
||||
(BackendRepr::ScalarPair(..), BackendRepr::ScalarPair(..)) => {}
|
||||
_ => return None,
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue