Skip to content

Commit fb3add3

Browse files
committed
Translate builtin_assume and unaligned SIMD intrinsics
Unaligned vector types such as `__m128_u` or `__256_u` not not have any equivalents in Rust so we translate them to their aligned equivalents. This is unsound so we emit warning that the program may be mistranspiled.
1 parent 9ad9d35 commit fb3add3

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

c2rust-transpile/src/translator/builtins.rs

+3
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,9 @@ impl<'c> Translation<'c> {
613613
)
614614
})
615615
}
616+
617+
// TODO: provide proper implementation, this is just to get stuff building
618+
"__builtin_assume" => Ok(self.convert_expr(ctx.used(), args[0])?),
616619
// There's currently no way to replicate this functionality in Rust, so we just
617620
// pass the ptr input param in its place.
618621
"__builtin_assume_aligned" => Ok(self.convert_expr(ctx.used(), args[0])?),

c2rust-transpile/src/translator/simd.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ impl<'c> Translation<'c> {
9191
pub fn import_simd_typedef(&self, name: &str) -> TranslationResult<bool> {
9292
Ok(match name {
9393
// Public API SIMD typedefs:
94-
"__m128i" | "__m128" | "__m128d" | "__m64" | "__m256" | "__m256d" | "__m256i" => {
94+
"__m64" | "__m128i" | "__m128i_u" | "__m128" | "__m128d" | "__m128_u" | "__m256"
95+
| "__m256d" | "__m256_u" | "__m256i" | "__m256i_u" | "__m512 " | "__m512d"
96+
| "__m512i" | "__m512i_u" | "__m512_u" => {
9597
// __m64 and MMX support were removed from upstream Rust.
9698
// See https://github.com/immunant/c2rust/issues/369
9799
if name == "__m64" {
@@ -100,6 +102,21 @@ impl<'c> Translation<'c> {
100102
).into());
101103
}
102104

105+
// Drop the `_u` suffix for unaligned SIMD types as they have
106+
// no equivalents in Rust. Warn the user about possible probems.
107+
let name = if name.ends_with("_u") {
108+
warn!(
109+
"Unaligned SIMD type {} has no equivalent in Rust. \
110+
Emitting {} instead. This likely means the potential \
111+
for miscompilation has been introduced.",
112+
name,
113+
&name[..name.len() - 2]
114+
);
115+
&name[..name.len() - 2]
116+
} else {
117+
name
118+
};
119+
103120
self.with_cur_file_item_store(|item_store| {
104121
add_arch_use(item_store, "x86", name);
105122
add_arch_use(item_store, "x86_64", name);

c2rust-transpile/src/translator/structs.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,33 @@ impl<'a> Translation<'a> {
377377

378378
field_entries.push(field);
379379
}
380-
FieldType::Regular { field, .. } => field_entries.push(*field),
380+
FieldType::Regular {
381+
mut field, name, ..
382+
} => {
383+
if let crate::translator::Type::Path(segments, ..) = &field.ty {
384+
for seg in &segments.path.segments {
385+
let tynam = seg.ident.to_string();
386+
match tynam.as_str() {
387+
"__m128i_u" | "__m128_u" | "__m256i_u" | "__m256_u"
388+
| "__m512_u" | "__m512i_u" => {
389+
// TODO: We might already have emitted a warning in `import_simd_typedef`,
390+
// is it always safe to skip the warning here?
391+
log::warn!("Unaligned SIMD type {} in field {} has no equivalent in Rust. \
392+
Emitting {} instead. This likely means the potential \
393+
for miscompilation has been introduced.",
394+
tynam,
395+
name,
396+
&tynam[..tynam.len() - 2],
397+
);
398+
field.ty = *mk().ident_ty(&tynam[..tynam.len() - 2]);
399+
break;
400+
}
401+
_ => {}
402+
}
403+
}
404+
}
405+
field_entries.push(*field)
406+
}
381407
}
382408
}
383409
Ok((field_entries, contains_va_list))

0 commit comments

Comments
 (0)