Skip to content

Commit

Permalink
Auto merge of #118705 - WaffleLapkin:codegen-atomic-exhange-untuple, …
Browse files Browse the repository at this point in the history
…r=cjgillot

Change `rustc_codegen_ssa`'s `atomic_cmpxchg` interface to return a pair of values

Doesn't change much, but a little nicer that way.
  • Loading branch information
bors committed Dec 28, 2023
2 parents fb5ed72 + 6cf6139 commit 1a147be
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
20 changes: 6 additions & 14 deletions compiler/rustc_codegen_gcc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
}

// Atomic Operations
fn atomic_cmpxchg(&mut self, dst: RValue<'gcc>, cmp: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering, failure_order: AtomicOrdering, weak: bool) -> RValue<'gcc> {
fn atomic_cmpxchg(&mut self, dst: RValue<'gcc>, cmp: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering, failure_order: AtomicOrdering, weak: bool) -> (RValue<'gcc>, RValue<'gcc>) {
let expected = self.current_func().new_local(None, cmp.get_type(), "expected");
self.llbb().add_assignment(None, expected, cmp);
// NOTE: gcc doesn't support a failure memory model that is stronger than the success
Expand All @@ -1310,20 +1310,12 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
};
let success = self.compare_exchange(dst, expected, src, order, failure_order, weak);

let pair_type = self.cx.type_struct(&[src.get_type(), self.bool_type], false);
let result = self.current_func().new_local(None, pair_type, "atomic_cmpxchg_result");
let align = Align::from_bits(64).expect("align"); // TODO(antoyo): use good align.
// NOTE: since success contains the call to the intrinsic, it must be added to the basic block before
// expected so that we store expected after the call.
let success_var = self.current_func().new_local(None, self.bool_type, "success");
self.llbb().add_assignment(None, success_var, success);

let value_type = result.to_rvalue().get_type();
if let Some(struct_type) = value_type.is_struct() {
self.store(success, result.access_field(None, struct_type.get_field(1)).get_address(None), align);
// NOTE: since success contains the call to the intrinsic, it must be stored before
// expected so that we store expected after the call.
self.store(expected.to_rvalue(), result.access_field(None, struct_type.get_field(0)).get_address(None), align);
}
// TODO(antoyo): handle when value is not a struct.

result.to_rvalue()
(expected.to_rvalue(), success_var.to_rvalue())
}

fn atomic_rmw(&mut self, op: AtomicRmwBinOp, dst: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering) -> RValue<'gcc> {
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
order: rustc_codegen_ssa::common::AtomicOrdering,
failure_order: rustc_codegen_ssa::common::AtomicOrdering,
weak: bool,
) -> &'ll Value {
) -> (&'ll Value, &'ll Value) {
let weak = if weak { llvm::True } else { llvm::False };
unsafe {
let value = llvm::LLVMBuildAtomicCmpXchg(
Expand All @@ -1085,7 +1085,9 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
llvm::False, // SingleThreaded
);
llvm::LLVMSetWeak(value, weak);
value
let val = self.extract_value(value, 0);
let success = self.extract_value(value, 1);
(val, success)
}
}
fn atomic_rmw(
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_codegen_ssa/src/mir/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
cmp = bx.ptrtoint(cmp, bx.type_isize());
src = bx.ptrtoint(src, bx.type_isize());
}
let pair = bx.atomic_cmpxchg(
let (val, success) = bx.atomic_cmpxchg(
dst,
cmp,
src,
parse_ordering(bx, success),
parse_ordering(bx, failure),
weak,
);
let val = bx.extract_value(pair, 0);
let success = bx.extract_value(pair, 1);
let val = bx.from_immediate(val);
let success = bx.from_immediate(success);

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/traits/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub trait BuilderMethods<'a, 'tcx>:
order: AtomicOrdering,
failure_order: AtomicOrdering,
weak: bool,
) -> Self::Value;
) -> (Self::Value, Self::Value);
fn atomic_rmw(
&mut self,
op: AtomicRmwBinOp,
Expand Down

0 comments on commit 1a147be

Please sign in to comment.