Skip to content

Commit

Permalink
Translate builtin_assume and unaligned SIMD intrinsics
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thedataking committed Sep 21, 2024
1 parent 9ad9d35 commit fb3add3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 3 additions & 0 deletions c2rust-transpile/src/translator/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,9 @@ impl<'c> Translation<'c> {
)
})
}

// TODO: provide proper implementation, this is just to get stuff building
"__builtin_assume" => Ok(self.convert_expr(ctx.used(), args[0])?),
// There's currently no way to replicate this functionality in Rust, so we just
// pass the ptr input param in its place.
"__builtin_assume_aligned" => Ok(self.convert_expr(ctx.used(), args[0])?),
Expand Down
19 changes: 18 additions & 1 deletion c2rust-transpile/src/translator/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ impl<'c> Translation<'c> {
pub fn import_simd_typedef(&self, name: &str) -> TranslationResult<bool> {
Ok(match name {
// Public API SIMD typedefs:
"__m128i" | "__m128" | "__m128d" | "__m64" | "__m256" | "__m256d" | "__m256i" => {
"__m64" | "__m128i" | "__m128i_u" | "__m128" | "__m128d" | "__m128_u" | "__m256"
| "__m256d" | "__m256_u" | "__m256i" | "__m256i_u" | "__m512 " | "__m512d"
| "__m512i" | "__m512i_u" | "__m512_u" => {
// __m64 and MMX support were removed from upstream Rust.
// See https://github.com/immunant/c2rust/issues/369
if name == "__m64" {
Expand All @@ -100,6 +102,21 @@ impl<'c> Translation<'c> {
).into());
}

// Drop the `_u` suffix for unaligned SIMD types as they have
// no equivalents in Rust. Warn the user about possible probems.
let name = if name.ends_with("_u") {
warn!(
"Unaligned SIMD type {} has no equivalent in Rust. \
Emitting {} instead. This likely means the potential \
for miscompilation has been introduced.",
name,
&name[..name.len() - 2]
);
&name[..name.len() - 2]
} else {
name
};

self.with_cur_file_item_store(|item_store| {
add_arch_use(item_store, "x86", name);
add_arch_use(item_store, "x86_64", name);
Expand Down
28 changes: 27 additions & 1 deletion c2rust-transpile/src/translator/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,33 @@ impl<'a> Translation<'a> {

field_entries.push(field);
}
FieldType::Regular { field, .. } => field_entries.push(*field),
FieldType::Regular {
mut field, name, ..
} => {
if let crate::translator::Type::Path(segments, ..) = &field.ty {
for seg in &segments.path.segments {
let tynam = seg.ident.to_string();
match tynam.as_str() {
"__m128i_u" | "__m128_u" | "__m256i_u" | "__m256_u"
| "__m512_u" | "__m512i_u" => {
// TODO: We might already have emitted a warning in `import_simd_typedef`,
// is it always safe to skip the warning here?
log::warn!("Unaligned SIMD type {} in field {} has no equivalent in Rust. \
Emitting {} instead. This likely means the potential \
for miscompilation has been introduced.",
tynam,
name,
&tynam[..tynam.len() - 2],
);
field.ty = *mk().ident_ty(&tynam[..tynam.len() - 2]);
break;
}
_ => {}
}
}
}
field_entries.push(*field)
}
}
}
Ok((field_entries, contains_va_list))
Expand Down

0 comments on commit fb3add3

Please sign in to comment.