Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions compiler/rustc_codegen_gcc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::convert::TryFrom;
use std::ops::Deref;

use gccjit::{
BinaryOp, Block, CType, ComparisonOp, Context, Function, LValue, Location, RValue, ToRValue,
Type, UnaryOp,
BinaryOp, Block, ComparisonOp, Context, Function, LValue, Location, RValue, ToRValue, Type,
UnaryOp,
};
use rustc_abi as abi;
use rustc_abi::{Align, HasDataLayout, Size, TargetDataLayout, WrappingRange};
Expand Down Expand Up @@ -35,9 +35,9 @@ use rustc_target::spec::{HasTargetSpec, HasX86AbiOpt, Target, X86Abi};
use crate::abi::FnAbiGccExt;
use crate::builder;
use crate::common::{SignType, TypeReflection, type_is_pointer};
use crate::context::CodegenCx;
#[cfg(feature = "master")]
use crate::context::PendingCleanup;
use crate::context::{CodegenCx, PhiBuilder};
use crate::intrinsic::llvm;
use crate::type_of::LayoutGccExt;

Expand Down Expand Up @@ -510,6 +510,7 @@ impl<'gcc, 'tcx> BackendTypes for Builder<'_, 'gcc, 'tcx> {
type Function = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Function;
type BasicBlock = <CodegenCx<'gcc, 'tcx> as BackendTypes>::BasicBlock;
type Funclet = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Funclet;
type PhiBuilder = <CodegenCx<'gcc, 'tcx> as BackendTypes>::PhiBuilder;

type Value = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Value;
type Type = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Type;
Expand Down Expand Up @@ -592,6 +593,33 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
self.llbb().end_with_jump(self.location, dest)
}

fn br_with_phi<const N: usize>(
&mut self,
dest: Block<'gcc>,
phi_builders: [(&mut PhiBuilder<'gcc>, RValue<'gcc>); N],
) {
for (builder, value) in phi_builders {
match builder.var {
None => {
let typ = value.get_type();
let var = self.new_temp(self.llbb().get_function(), self.location, typ);
self.assign(var, value);
builder.var = Some((var, typ));
}
Some((var, typ)) => {
// Multiple GCC types can correspond to the same SSA type (e.g. pointers).
let value = if value.get_type() != typ {
self.context.new_bitcast(self.location, value, typ)
} else {
value
};
self.assign(var, value);
}
}
}
self.br(dest);
}

fn cond_br(&mut self, cond: RValue<'gcc>, then_block: Block<'gcc>, else_block: Block<'gcc>) {
self.llbb().end_with_conditional(self.location, cond, then_block, else_block)
}
Expand Down Expand Up @@ -1262,6 +1290,15 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
);
}

fn ptr_mask(&mut self, ptr: RValue<'gcc>, mask: RValue<'gcc>) -> RValue<'gcc> {
let usize_type = self.context.new_type::<usize>();
let void_ptr_type = self.context.new_type::<*const ()>();

let addr = self.bitcast(ptr, usize_type);
let masked = self.and(addr, mask);
self.bitcast(masked, void_ptr_type)
}

fn gep(&mut self, stride: Size, ptr: RValue<'gcc>, index: RValue<'gcc>) -> RValue<'gcc> {
let ptr_type = ptr.get_type();
// NOTE: we cannot use array indexing here like in inbounds_gep because array indexing is
Expand Down Expand Up @@ -1542,12 +1579,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
variable.to_rvalue()
}

fn va_arg(&mut self, list: RValue<'gcc>, ty: Type<'gcc>) -> RValue<'gcc> {
let va_list_type = self.context.new_c_type(CType::VaList);
let list = self.context.new_cast(self.location, list, va_list_type.make_pointer());
self.context.new_va_arg(self.location, list, ty)
}

#[cfg(feature = "master")]
fn extract_element(&mut self, vec: RValue<'gcc>, idx: RValue<'gcc>) -> RValue<'gcc> {
self.context.new_vector_access(self.location, vec, idx).to_rvalue()
Expand Down Expand Up @@ -1918,6 +1949,14 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
fn fptosi_sat(&mut self, val: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
self.fptoint_sat(true, val, dest_ty)
}

fn new_phi(&mut self) -> PhiBuilder<'gcc> {
PhiBuilder { var: None }
}

fn phi_output(&mut self, phi: PhiBuilder<'gcc>) -> RValue<'gcc> {
self.assign_to_var(phi.var.expect("empty phi builder").0.to_rvalue())
}
}

impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_codegen_gcc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,15 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
}
}

pub struct PhiBuilder<'gcc> {
pub(crate) var: Option<(LValue<'gcc>, Type<'gcc>)>,
}

impl<'gcc, 'tcx> BackendTypes for CodegenCx<'gcc, 'tcx> {
type Function = Function<'gcc>;
type BasicBlock = Block<'gcc>;
type Funclet = (); // FIXME(antoyo)
type PhiBuilder = PhiBuilder<'gcc>;

type Value = RValue<'gcc>;
type Type = Type<'gcc>;
Expand Down
19 changes: 1 addition & 18 deletions compiler/rustc_codegen_gcc/src/intrinsic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_codegen_ssa::mir::place::{PlaceRef, PlaceValue};
use rustc_codegen_ssa::traits::MiscCodegenMethods;
Comment thread
beetrees marked this conversation as resolved.
Resolved
use rustc_codegen_ssa::traits::{
ArgAbiBuilderMethods, BaseTypeCodegenMethods, BuilderMethods, ConstCodegenMethods,
IntrinsicCallBuilderMethods, LayoutTypeCodegenMethods, ReturnSlot,
IntrinsicCallBuilderMethods, ReturnSlot,
};
use rustc_codegen_ssa::{MemFlags, RetagInfo};
use rustc_data_structures::fx::FxHashSet;
Expand Down Expand Up @@ -162,11 +162,6 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
sym::breakpoint | sym::resumable_breakpoint => {
unimplemented!();
}
sym::va_arg => {
let va_list = args[0].immediate();
let gcc_type = self.immediate_backend_type(result.layout);
self.va_arg(va_list, gcc_type)
}

sym::volatile_load | sym::unaligned_volatile_load => {
let ptr = args[0].immediate();
Expand Down Expand Up @@ -465,18 +460,6 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
return IntrinsicResult::WroteIntoPlace;
}

sym::ptr_mask => {
let usize_type = self.context.new_type::<usize>();
let void_ptr_type = self.context.new_type::<*const ()>();

let ptr = args[0].immediate();
let mask = args[1].immediate();

let addr = self.bitcast(ptr, usize_type);
let masked = self.and(addr, mask);
self.bitcast(masked, void_ptr_type)
}

_ if name_str.starts_with("simd_") => {
match generic_simd_intrinsic(
self,
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_codegen_llvm/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,7 @@ impl<'ll, 'tcx> ArgAbiBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
if let Some(dbg_loc) = dbg_loc {
self.set_dbg_loc(dbg_loc);
}
self.phi(
self.type_x87_f80(),
&[is_nan_res, is_not_nan_res],
&[is_nan_block, is_not_nan_block],
)
self.phi(&[is_nan_res, is_not_nan_res], &[is_nan_block, is_not_nan_block])
}

fn x87_lossless_fp_stack_to_float(
Expand Down Expand Up @@ -527,7 +523,7 @@ impl<'ll, 'tcx> ArgAbiBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
if let Some(dbg_loc) = dbg_loc {
self.set_dbg_loc(dbg_loc);
}
self.phi(float_type, &[is_nan_res, is_not_nan_res], &[is_nan_block, is_not_nan_block])
self.phi(&[is_nan_res, is_not_nan_res], &[is_nan_block, is_not_nan_block])
}
}

Expand Down
48 changes: 33 additions & 15 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ use rustc_session::config::OptLevel;
use rustc_span::{Span, bug};
use rustc_target::callconv::{FnAbi, PassMode};
use rustc_target::spec::{Arch, HasTargetSpec, SanitizerSet, Target};
use smallvec::SmallVec;
use smallvec::{SmallVec, smallvec};
use tracing::{debug, instrument};

use crate::abi::FnAbiLlvmExt;
use crate::attributes;
use crate::common::Funclet;
use crate::common::{Funclet, PhiBuilder};
use crate::context::{CodegenCx, FullCx, GenericCx, SCx};
use crate::llvm::{
self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, FromGeneric, GEPNoWrapFlags, Metadata, TRUE,
ToLlvmBool, Type, TypeKind, Value,
};
use crate::type_of::LayoutLlvmExt;
use crate::{attributes, common};

#[must_use]
pub(crate) struct GenericBuilder<'a, 'll, CX: Borrow<SCx<'ll>>> {
Expand Down Expand Up @@ -214,6 +214,7 @@ impl<'ll, CX: Borrow<SCx<'ll>>> BackendTypes for GenericBuilder<'_, 'll, CX> {
type Function = <GenericCx<'ll, CX> as BackendTypes>::Function;
type BasicBlock = <GenericCx<'ll, CX> as BackendTypes>::BasicBlock;
type Funclet = <GenericCx<'ll, CX> as BackendTypes>::Funclet;
type PhiBuilder = <GenericCx<'ll, CX> as BackendTypes>::PhiBuilder;

type Value = <GenericCx<'ll, CX> as BackendTypes>::Value;
type Type = <GenericCx<'ll, CX> as BackendTypes>::Type;
Expand Down Expand Up @@ -396,6 +397,18 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
}
}

fn br_with_phi<const N: usize>(
&mut self,
dest: &'ll BasicBlock,
phi_builders: [(&mut PhiBuilder<'ll>, Value<'ll>); N],
) {
for (builder, value) in phi_builders {
builder.values.push(value);
builder.blocks.push(self.llbb());
}
self.br(dest);
}

fn cond_br(
&mut self,
cond: Value<'ll>,
Expand Down Expand Up @@ -959,6 +972,10 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
}
}

fn ptr_mask(&mut self, ptr: Self::Value, mask: Self::Value) -> Self::Value {
self.call_intrinsic("llvm.ptrmask", &[self.val_ty(ptr), self.type_isize()], &[ptr, mask])
}

fn gep(&mut self, stride: Size, ptr: Value<'ll>, index: Value<'ll>) -> Value<'ll> {
self.build_gep(stride, ptr, index, GEPNoWrapFlags::default())
}
Expand Down Expand Up @@ -1224,10 +1241,6 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
unsafe { llvm::LLVMBuildSelect(self.llbuilder(), cond, then_val, else_val, UNNAMED) }
}

fn va_arg(&mut self, list: Value<'ll>, ty: Type<'ll>) -> Value<'ll> {
unsafe { llvm::LLVMBuildVAArg(self.llbuilder(), list, ty, UNNAMED) }
}

fn extract_element(&mut self, vec: Value<'ll>, idx: Value<'ll>) -> Value<'ll> {
unsafe { llvm::LLVMBuildExtractElement(self.llbuilder(), vec, idx, UNNAMED) }
}
Expand Down Expand Up @@ -1532,6 +1545,15 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
let cold_inline = llvm::AttributeKind::Cold.create_attr(self.llcx);
attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[cold_inline]);
}

fn new_phi(&mut self) -> PhiBuilder<'ll> {
PhiBuilder { values: smallvec![], blocks: smallvec![] }
}

fn phi_output(&mut self, phi: PhiBuilder<'ll>) -> Value<'ll> {
assert!(!phi.values.is_empty());
self.phi(&phi.values, &phi.blocks)
}
}

impl<'ll> Builder<'_, 'll, '_> {
Expand Down Expand Up @@ -1643,7 +1665,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
self.br(header_bb);

let mut header_bx = Self::build(self.cx, header_bb);
let i = header_bx.phi(self.val_ty(zero), &[zero], &[self.llbb()]);
let i = header_bx.phi(&[zero], &[self.llbb()]);

let keep_going = header_bx.icmp(IntPredicate::IntULT, i, count);
header_bx.cond_br(keep_going, body_bb, next_bb);
Expand Down Expand Up @@ -1677,7 +1699,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
self.br(header_bb);

let mut header_bx = Self::build(self.cx, header_bb);
let current = header_bx.phi(self.val_ty(start), &[start], &[self.llbb()]);
let current = header_bx.phi(&[start], &[self.llbb()]);

let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end);
header_bx.cond_br(keep_going, body_bb, next_bb);
Expand Down Expand Up @@ -1873,13 +1895,9 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
}
}
impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
pub(crate) fn phi(
&mut self,
ty: Type<'ll>,
vals: &[Value<'ll>],
bbs: &[&'ll BasicBlock],
) -> Value<'ll> {
pub(crate) fn phi(&mut self, vals: &[Value<'ll>], bbs: &[&'ll BasicBlock]) -> Value<'ll> {
assert_eq!(vals.len(), bbs.len());
let ty = common::val_ty(vals[0]);
let phi = unsafe { llvm::LLVMBuildPhi(self.llbuilder(), ty, UNNAMED) };
unsafe {
llvm::LLVMAddIncoming(phi, vals.as_ptr(), bbs.as_ptr(), vals.len() as c_uint);
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_codegen_llvm/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use rustc_middle::mir::interpret::{GlobalAlloc, PointerArithmetic, Scalar};
use rustc_middle::ty::{Instance, TyCtxt};
use rustc_session::{PointerAuthAddressDiscriminator, PointerAuthSchema};
use rustc_span::bug;
use smallvec::SmallVec;
use tracing::debug;

use crate::consts::{IsInitOrFini, IsStatic, const_alloc_to_llvm};
Expand Down Expand Up @@ -124,11 +125,17 @@ impl<'ll> Funclet<'ll> {
}
}

pub(crate) struct PhiBuilder<'ll> {
pub(crate) values: SmallVec<[Value<'ll>; 2]>,
pub(crate) blocks: SmallVec<[&'ll BasicBlock; 2]>,
}

impl<'ll, CX: Borrow<SCx<'ll>>> BackendTypes for GenericCx<'ll, CX> {
// FIXME(eddyb) replace this with a `Function` "subclass" of `Value`.
type Function = Value<'ll>;
type BasicBlock = &'ll BasicBlock;
type Funclet = Funclet<'ll>;
type PhiBuilder = PhiBuilder<'ll>;

type Value = Value<'ll>;
type Type = Type<'ll>;
Expand Down
Loading
Loading