Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate builtin_assume and unaligned SIMD intrinsics #1132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])?),
Comment on lines +617 to +618
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you do implement this, note that Rust (as of 1.81.0) now has core::hint::assert_unchecked, which should be the equivalent of __builtin_assume.

// 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
Loading