Skip to content

Commit e61dcc7

Browse files
committed
Auto merge of rust-lang#122220 - saethlin:ppc-can-into-atomicptr, r=oli-obk
Only generate a ptrtoint in AtomicPtr codegen when absolutely necessary This special case was added in this PR: rust-lang#77611 in response to this error message: ``` Intrinsic has incorrect argument type! void ({}*)* `@llvm.ppc.cfence.p0sl_s` in function rust_oom LLVM ERROR: Broken function found, compilation aborted! [RUSTC-TIMING] std test:false 20.161 error: could not compile `std` ``` But when I tried searching for more information about that intrinsic I found this: llvm/llvm-project#55983 which is a report of someone hitting this same error and a fix was landed in LLVM, 2 years after the above Rust PR.
2 parents a165f1f + aa6cfb2 commit e61dcc7

File tree

3 files changed

+18
-42
lines changed

3 files changed

+18
-42
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1132,9 +1132,15 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11321132
&mut self,
11331133
op: rustc_codegen_ssa::common::AtomicRmwBinOp,
11341134
dst: &'ll Value,
1135-
src: &'ll Value,
1135+
mut src: &'ll Value,
11361136
order: rustc_codegen_ssa::common::AtomicOrdering,
11371137
) -> &'ll Value {
1138+
// The only RMW operation that LLVM supports on pointers is compare-exchange.
1139+
if self.val_ty(src) == self.type_ptr()
1140+
&& op != rustc_codegen_ssa::common::AtomicRmwBinOp::AtomicXchg
1141+
{
1142+
src = self.ptrtoint(src, self.type_isize());
1143+
}
11381144
unsafe {
11391145
llvm::LLVMBuildAtomicRMW(
11401146
self.llbuilder,

compiler/rustc_codegen_ssa/src/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub enum RealPredicate {
4242
RealPredicateTrue,
4343
}
4444

45-
#[derive(Copy, Clone)]
45+
#[derive(Copy, Clone, PartialEq)]
4646
pub enum AtomicRmwBinOp {
4747
AtomicXchg,
4848
AtomicAdd,

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+10-40
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
350350
if int_type_width_signed(ty, bx.tcx()).is_some() || ty.is_unsafe_ptr() {
351351
let weak = instruction == "cxchgweak";
352352
let dst = args[0].immediate();
353-
let mut cmp = args[1].immediate();
354-
let mut src = args[2].immediate();
355-
if ty.is_unsafe_ptr() {
356-
// Some platforms do not support atomic operations on pointers,
357-
// so we cast to integer first.
358-
cmp = bx.ptrtoint(cmp, bx.type_isize());
359-
src = bx.ptrtoint(src, bx.type_isize());
360-
}
353+
let cmp = args[1].immediate();
354+
let src = args[2].immediate();
361355
let (val, success) = bx.atomic_cmpxchg(
362356
dst,
363357
cmp,
@@ -385,26 +379,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
385379
let layout = bx.layout_of(ty);
386380
let size = layout.size;
387381
let source = args[0].immediate();
388-
if ty.is_unsafe_ptr() {
389-
// Some platforms do not support atomic operations on pointers,
390-
// so we cast to integer first...
391-
let llty = bx.type_isize();
392-
let result = bx.atomic_load(
393-
llty,
394-
source,
395-
parse_ordering(bx, ordering),
396-
size,
397-
);
398-
// ... and then cast the result back to a pointer
399-
bx.inttoptr(result, bx.backend_type(layout))
400-
} else {
401-
bx.atomic_load(
402-
bx.backend_type(layout),
403-
source,
404-
parse_ordering(bx, ordering),
405-
size,
406-
)
407-
}
382+
bx.atomic_load(
383+
bx.backend_type(layout),
384+
source,
385+
parse_ordering(bx, ordering),
386+
size,
387+
)
408388
} else {
409389
invalid_monomorphization(ty);
410390
return Ok(());
@@ -415,13 +395,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
415395
let ty = fn_args.type_at(0);
416396
if int_type_width_signed(ty, bx.tcx()).is_some() || ty.is_unsafe_ptr() {
417397
let size = bx.layout_of(ty).size;
418-
let mut val = args[1].immediate();
398+
let val = args[1].immediate();
419399
let ptr = args[0].immediate();
420-
if ty.is_unsafe_ptr() {
421-
// Some platforms do not support atomic operations on pointers,
422-
// so we cast to integer first.
423-
val = bx.ptrtoint(val, bx.type_isize());
424-
}
425400
bx.atomic_store(val, ptr, parse_ordering(bx, ordering), size);
426401
} else {
427402
invalid_monomorphization(ty);
@@ -465,12 +440,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
465440
let ty = fn_args.type_at(0);
466441
if int_type_width_signed(ty, bx.tcx()).is_some() || ty.is_unsafe_ptr() {
467442
let ptr = args[0].immediate();
468-
let mut val = args[1].immediate();
469-
if ty.is_unsafe_ptr() {
470-
// Some platforms do not support atomic operations on pointers,
471-
// so we cast to integer first.
472-
val = bx.ptrtoint(val, bx.type_isize());
473-
}
443+
let val = args[1].immediate();
474444
bx.atomic_rmw(atom_op, ptr, val, parse_ordering(bx, ordering))
475445
} else {
476446
invalid_monomorphization(ty);

0 commit comments

Comments
 (0)