Skip to content

Commit cbe60c0

Browse files
committed
update atomic intrinsic usage for newer rustc
1 parent df32e6c commit cbe60c0

File tree

2 files changed

+39
-77
lines changed

2 files changed

+39
-77
lines changed

c2rust-transpile/src/translator/atomics.rs

Lines changed: 31 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,25 @@ impl<'c> Translation<'c> {
7575
})
7676
}
7777

78+
fn order_name(order: Ordering) -> &'static str {
79+
use Ordering::*;
80+
match order {
81+
SeqCst => "seqcst",
82+
AcqRel => "acqrel",
83+
Acquire => "acquire",
84+
Release => "release",
85+
Relaxed => "relaxed",
86+
_ => unreachable!("Did we not handle a case above??"),
87+
}
88+
}
89+
7890
match name {
7991
"__atomic_load" | "__atomic_load_n" => ptr.and_then(|ptr| {
80-
use Ordering::*;
81-
let intrinsic_name = match static_order(order) {
82-
SeqCst => Some("atomic_load"),
83-
AcqRel => None,
84-
Acquire => Some("atomic_load_acq"),
85-
Release => None,
86-
Relaxed => Some("atomic_load_relaxed"),
87-
_ => unreachable!("Did we not handle a case above??"),
88-
}
89-
.ok_or_else(|| {
90-
format_translation_err!(
91-
self.ast_context
92-
.display_loc(&self.ast_context[order_id].loc),
93-
"Invalid memory ordering for __atomic_load",
94-
)
95-
})?;
92+
let intrinsic_name = "atomic_load_".to_owned() + order_name(static_order(order));
9693

9794
self.use_feature("core_intrinsics");
9895

99-
let atomic_load = mk().abs_path_expr(vec!["core", "intrinsics", intrinsic_name]);
96+
let atomic_load = mk().abs_path_expr(vec!["core", "intrinsics", &intrinsic_name]);
10097
let call = mk().call_expr(atomic_load, vec![ptr]);
10198
if name == "__atomic_load" {
10299
let ret = val1.expect("__atomic_load should have a ret argument");
@@ -124,27 +121,13 @@ impl<'c> Translation<'c> {
124121
let val = val1.expect("__atomic_store must have a val argument");
125122
ptr.and_then(|ptr| {
126123
val.and_then(|val| {
127-
use Ordering::*;
128-
let intrinsic_name = match static_order(order) {
129-
SeqCst => Some("atomic_store"),
130-
AcqRel => None,
131-
Acquire => None,
132-
Release => Some("atomic_store_rel"),
133-
Relaxed => Some("atomic_store_relaxed"),
134-
_ => unreachable!("Did we not handle a case above??"),
135-
}
136-
.ok_or_else(|| {
137-
format_translation_err!(
138-
self.ast_context
139-
.display_loc(&self.ast_context[order_id].loc),
140-
"Invalid memory ordering for __atomic_store",
141-
)
142-
})?;
124+
let intrinsic_name =
125+
"atomic_store_".to_owned() + order_name(static_order(order));
143126

144127
self.use_feature("core_intrinsics");
145128

146129
let atomic_store =
147-
mk().abs_path_expr(vec!["core", "intrinsics", intrinsic_name]);
130+
mk().abs_path_expr(vec!["core", "intrinsics", &intrinsic_name]);
148131
let val = if name == "__atomic_store" {
149132
mk().unary_expr(UnOp::Deref(Default::default()), val)
150133
} else {
@@ -164,27 +147,13 @@ impl<'c> Translation<'c> {
164147
let val = val1.expect("__atomic_store must have a val argument");
165148
ptr.and_then(|ptr| {
166149
val.and_then(|val| {
167-
use Ordering::*;
168-
let intrinsic_name = match static_order(order) {
169-
SeqCst => Some("atomic_xchg"),
170-
AcqRel => Some("atomic_xchg_acqrel"),
171-
Acquire => Some("atomic_xchg_acq"),
172-
Release => Some("atomic_xchg_rel"),
173-
Relaxed => Some("atomic_xchg_relaxed"),
174-
_ => unreachable!("Did we not handle a case above??"),
175-
}
176-
.ok_or_else(|| {
177-
format_translation_err!(
178-
self.ast_context
179-
.display_loc(&self.ast_context[order_id].loc),
180-
"Invalid memory ordering for __atomic_exchange",
181-
)
182-
})?;
150+
let intrinsic_name =
151+
"atomic_xchg_".to_owned() + order_name(static_order(order));
183152

184153
self.use_feature("core_intrinsics");
185154

186155
let fn_path =
187-
mk().abs_path_expr(vec!["core", "intrinsics", intrinsic_name]);
156+
mk().abs_path_expr(vec!["core", "intrinsics", &intrinsic_name]);
188157
let val = if name == "__atomic_exchange" {
189158
mk().unary_expr(UnOp::Deref(Default::default()), val)
190159
} else {
@@ -233,15 +202,15 @@ impl<'c> Translation<'c> {
233202
let intrinsic_name = (|| {
234203
Some(match (order, order_fail) {
235204
(_, Release | AcqRel) => return None,
236-
(SeqCst, SeqCst) => "",
237-
(SeqCst, Acquire) => "_failacq",
238-
(SeqCst, Relaxed) => "_failrelaxed",
239-
(AcqRel, Acquire) => "_acqrel",
240-
(AcqRel, Relaxed) => "_acqrel_failrelaxed",
241-
(Release, Relaxed) => "_rel",
242-
(Acquire, Acquire) => "_acq",
243-
(Acquire, Relaxed) => "_acq_failrelaxed",
244-
(Relaxed, Relaxed) => "_relaxed",
205+
(SeqCst, SeqCst) => "_seqcst_seqcst",
206+
(SeqCst, Acquire) => "_seqcst_acquire",
207+
(SeqCst, Relaxed) => "_seqcst_relaxed",
208+
(AcqRel, Acquire) => "_acqrel_acquire",
209+
(AcqRel, Relaxed) => "_acqrel_relaxed",
210+
(Release, Relaxed) => "_release_relaxed",
211+
(Acquire, Acquire) => "_acquire_acquire",
212+
(Acquire, Relaxed) => "_acquire_relaxed",
213+
(Relaxed, Relaxed) => "_relaxed_relaxed",
245214
(SeqCst | AcqRel | Release | Acquire | Relaxed, _) => {
246215
return None
247216
}
@@ -320,16 +289,8 @@ impl<'c> Translation<'c> {
320289
"atomic_and"
321290
};
322291

323-
use Ordering::*;
324-
let intrinsic_suffix = match static_order(order) {
325-
SeqCst => "",
326-
AcqRel => "_acqrel",
327-
Acquire => "_acq",
328-
Release => "_rel",
329-
Relaxed => "_relaxed",
330-
_ => unreachable!("Unknown memory ordering"),
331-
};
332-
let intrinsic_name = format!("{intrinsic_name}{intrinsic_suffix}");
292+
let intrinsic_suffix = order_name(static_order(order));
293+
let intrinsic_name = format!("{intrinsic_name}_{intrinsic_suffix}");
333294

334295
let fetch_first = name.starts_with("__atomic_fetch");
335296
let val = val1.expect("__atomic arithmetic operations must have a val argument");

c2rust-transpile/src/translator/builtins.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -493,18 +493,18 @@ impl<'c> Translation<'c> {
493493
| "__sync_nand_and_fetch_8"
494494
| "__sync_nand_and_fetch_16" => {
495495
let func_name = if builtin_name.contains("_add_") {
496-
"atomic_xadd"
496+
"atomic_xadd_seqcst"
497497
} else if builtin_name.contains("_sub_") {
498-
"atomic_xsub"
498+
"atomic_xsub_seqcst"
499499
} else if builtin_name.contains("_or_") {
500-
"atomic_or"
500+
"atomic_or_seqcst"
501501
} else if builtin_name.contains("_xor_") {
502-
"atomic_xor"
502+
"atomic_xor_seqcst"
503503
} else if builtin_name.contains("_nand_") {
504-
"atomic_nand"
504+
"atomic_nand_seqcst"
505505
} else {
506506
// We can't explicitly check for "_and_" since they all contain it
507-
"atomic_and"
507+
"atomic_and_seqcst"
508508
};
509509

510510
let arg0 = self.convert_expr(ctx.used(), args[0])?;
@@ -520,7 +520,8 @@ impl<'c> Translation<'c> {
520520
"__sync_synchronize" => {
521521
self.use_feature("core_intrinsics");
522522

523-
let atomic_func = mk().abs_path_expr(vec!["core", "intrinsics", "atomic_fence"]);
523+
let atomic_func =
524+
mk().abs_path_expr(vec!["core", "intrinsics", "atomic_fence_seqcst"]);
524525
let call_expr = mk().call_expr(atomic_func, vec![]);
525526
self.convert_side_effects_expr(
526527
ctx,

0 commit comments

Comments
 (0)