Skip to content

Commit 8cc6c91

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 8cc6c91

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
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

+15-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ 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" => {
9596
// __m64 and MMX support were removed from upstream Rust.
9697
// See https://github.com/immunant/c2rust/issues/369
9798
if name == "__m64" {
@@ -100,6 +101,19 @@ impl<'c> Translation<'c> {
100101
).into());
101102
}
102103

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

c2rust-transpile/src/translator/structs.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use crate::with_stmts::WithStmts;
1414
use c2rust_ast_builder::mk;
1515
use c2rust_ast_printer::pprust;
1616
use syn::{
17-
self, AttrStyle, BinOp as RBinOp, Expr, ExprAssign, ExprAssignOp, ExprBinary, ExprBlock,
18-
ExprCast, ExprMethodCall, ExprUnary, Field, Meta, NestedMeta, Stmt, Type,
17+
self, AttrStyle, BinOp as RBinOp, Expr, ExprAssign, ExprAssignOp, ExprBinary, ExprBlock, ExprCast, ExprMethodCall, ExprUnary, Field, Meta, NestedMeta, Stmt, Type
1918
};
2019

2120
use itertools::EitherOrBoth::{Both, Right};
@@ -377,7 +376,30 @@ impl<'a> Translation<'a> {
377376

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

0 commit comments

Comments
 (0)