Skip to content

Commit 72ba66c

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

File tree

2 files changed

+43
-79
lines changed

2 files changed

+43
-79
lines changed

c2rust-transpile/src/translator/atomics.rs

Lines changed: 35 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -68,35 +68,31 @@ impl<'c> Translation<'c> {
6868
.transpose()?;
6969
let weak = weak_id.and_then(|x| self.convert_constant_bool(x));
7070

71-
fn static_order<T>(order: Option<T>) -> T {
71+
fn static_order(order: Option<Ordering>) -> Ordering {
7272
order.unwrap_or_else(|| {
7373
// We have to select which intrinsic to use at runtime
7474
unimplemented!("Dynamic memory consistency arguments are not yet supported");
7575
})
7676
}
7777

78+
fn order_name(order: Ordering) -> &'static str {
79+
match order {
80+
SeqCst => "seqcst",
81+
AcqRel => "acqrel",
82+
Acquire => "acquire",
83+
Release => "release",
84+
Relaxed => "relaxed",
85+
_ => unreachable!("Did we not handle a case above??"),
86+
}
87+
}
88+
7889
match name {
7990
"__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-
})?;
91+
let intrinsic_name = "atomic_load_".to_owned() + order_name(static_order(order));
9692

9793
self.use_feature("core_intrinsics");
9894

99-
let atomic_load = mk().abs_path_expr(vec!["core", "intrinsics", intrinsic_name]);
95+
let atomic_load = mk().abs_path_expr(vec!["core", "intrinsics", &intrinsic_name]);
10096
let call = mk().call_expr(atomic_load, vec![ptr]);
10197
if name == "__atomic_load" {
10298
let ret = val1.expect("__atomic_load should have a ret argument");
@@ -124,27 +120,13 @@ impl<'c> Translation<'c> {
124120
let val = val1.expect("__atomic_store must have a val argument");
125121
ptr.and_then(|ptr| {
126122
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-
})?;
123+
let intrinsic_name =
124+
"atomic_store_".to_owned() + order_name(static_order(order));
143125

144126
self.use_feature("core_intrinsics");
145127

146128
let atomic_store =
147-
mk().abs_path_expr(vec!["core", "intrinsics", intrinsic_name]);
129+
mk().abs_path_expr(vec!["core", "intrinsics", &intrinsic_name]);
148130
let val = if name == "__atomic_store" {
149131
mk().unary_expr(UnOp::Deref(Default::default()), val)
150132
} else {
@@ -164,27 +146,13 @@ impl<'c> Translation<'c> {
164146
let val = val1.expect("__atomic_store must have a val argument");
165147
ptr.and_then(|ptr| {
166148
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-
})?;
149+
let intrinsic_name =
150+
"atomic_xchg_".to_owned() + order_name(static_order(order));
183151

184152
self.use_feature("core_intrinsics");
185153

186154
let fn_path =
187-
mk().abs_path_expr(vec!["core", "intrinsics", intrinsic_name]);
155+
mk().abs_path_expr(vec!["core", "intrinsics", &intrinsic_name]);
188156
let val = if name == "__atomic_exchange" {
189157
mk().unary_expr(UnOp::Deref(Default::default()), val)
190158
} else {
@@ -226,22 +194,25 @@ impl<'c> Translation<'c> {
226194
ptr.and_then(|ptr| {
227195
expected.and_then(|expected| {
228196
desired.and_then(|desired| {
229-
let weak = static_order(weak);
197+
// If this expect fails, we have to select which intrinsic to use at runtime
198+
let weak = weak.expect(
199+
"Dynamic memory consistency arguments are not yet supported",
200+
);
230201
let order = static_order(order);
231202
let order_fail = static_order(order_fail);
232203
use Ordering::*;
233204
let intrinsic_name = (|| {
234205
Some(match (order, order_fail) {
235206
(_, 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",
207+
(SeqCst, SeqCst) => "_seqcst_seqcst",
208+
(SeqCst, Acquire) => "_seqcst_acquire",
209+
(SeqCst, Relaxed) => "_seqcst_relaxed",
210+
(AcqRel, Acquire) => "_acqrel_acquire",
211+
(AcqRel, Relaxed) => "_acqrel_relaxed",
212+
(Release, Relaxed) => "_release_relaxed",
213+
(Acquire, Acquire) => "_acquire_acquire",
214+
(Acquire, Relaxed) => "_acquire_relaxed",
215+
(Relaxed, Relaxed) => "_relaxed_relaxed",
245216
(SeqCst | AcqRel | Release | Acquire | Relaxed, _) => {
246217
return None
247218
}
@@ -320,16 +291,8 @@ impl<'c> Translation<'c> {
320291
"atomic_and"
321292
};
322293

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}");
294+
let intrinsic_suffix = order_name(static_order(order));
295+
let intrinsic_name = format!("{intrinsic_name}_{intrinsic_suffix}");
333296

334297
let fetch_first = name.starts_with("__atomic_fetch");
335298
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)