Skip to content

Commit b2d06ea

Browse files
committed
c2rust-transpile: add partial bfloat16 support
like Half, this is basically just a stub for now supporting these types properly could use the `half` crate, but this is just to get basic AArch64 translation working this type was also added in Clang/LLVM 11, so ifdef'd accordingly we need to handle these now because ARMv8.6-a adds the BFloat16/__bf16 type as a vector element of one of the SVE vector types just added
1 parent 4313729 commit b2d06ea

File tree

7 files changed

+20
-3
lines changed

7 files changed

+20
-3
lines changed

c2rust-ast-exporter/src/AstExporter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,9 @@ class TypeEncoder final : public TypeVisitor<TypeEncoder> {
364364
case BuiltinType::ULong: tag = TagULong; break;
365365
case BuiltinType::ULongLong: tag = TagULongLong; break;
366366
case BuiltinType::Half: tag = TagHalf; break;
367+
#if CLANG_VERSION_MAJOR >= 11
368+
case BuiltinType::BFloat16: tag = TagBFloat16; break;
369+
#endif
367370
case BuiltinType::Float: tag = TagFloat; break;
368371
case BuiltinType::Double: tag = TagDouble; break;
369372
case BuiltinType::LongDouble: tag = TagLongDouble; break;

c2rust-ast-exporter/src/ast_tags.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ enum TypeTag {
141141
TagBlockPointer,
142142
TagComplexType,
143143
TagHalf,
144+
TagBFloat16,
144145
};
145146

146147
enum StringTypeTag {

c2rust-transpile/src/c_ast/conversion.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,11 @@ impl ConversionContext {
578578
self.processed_nodes.insert(new_id, OTHER_TYPE);
579579
}
580580

581+
TypeTag::TagBFloat16 if expected_ty & OTHER_TYPE != 0 => {
582+
self.add_type(new_id, not_located(CTypeKind::BFloat16));
583+
self.processed_nodes.insert(new_id, OTHER_TYPE);
584+
}
585+
581586
TypeTag::TagInt128 if expected_ty & OTHER_TYPE != 0 => {
582587
self.add_type(new_id, not_located(CTypeKind::Int128));
583588
self.processed_nodes.insert(new_id, OTHER_TYPE);
@@ -783,6 +788,12 @@ impl ConversionContext {
783788
self.processed_nodes.insert(new_id, OTHER_TYPE);
784789
}
785790

791+
TypeTag::TagBFloat16 => {
792+
let ty = CTypeKind::BuiltinFn;
793+
self.add_type(new_id, not_located(ty));
794+
self.processed_nodes.insert(new_id, OTHER_TYPE);
795+
}
796+
786797
TypeTag::TagVectorType => {
787798
let elt = from_value(ty_node.extras[0].clone()).expect("Vector child not found");
788799
let elt_new = self.visit_qualified_type(elt);

c2rust-transpile/src/c_ast/iterators.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ fn immediate_type_children(kind: &CTypeKind) -> Vec<SomeId> {
267267
Elaborated(_) => vec![], // These are references to previous definitions
268268
TypeOfExpr(e) => intos![e],
269269
Void | Bool | Short | Int | Long | LongLong | UShort | UInt | ULong | ULongLong | SChar
270-
| UChar | Char | Double | LongDouble | Float | Int128 | UInt128 | BuiltinFn | Half => {
270+
| UChar | Char | Double | LongDouble | Float | Int128 | UInt128 | BuiltinFn | Half
271+
| BFloat16 => {
271272
vec![]
272273
}
273274

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,6 +1577,7 @@ pub enum CTypeKind {
15771577
Vector(CQualTypeId, usize),
15781578

15791579
Half,
1580+
BFloat16,
15801581
}
15811582

15821583
#[derive(Copy, Clone, Debug)]

c2rust-transpile/src/convert_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ impl TypeConverter {
332332
CTypeKind::LongDouble => Ok(mk().path_ty(mk().path(vec!["f128", "f128"]))),
333333
CTypeKind::Float => Ok(mk().path_ty(mk().path(vec!["libc", "c_float"]))),
334334
CTypeKind::Int128 => Ok(mk().path_ty(mk().path(vec!["i128"]))),
335-
CTypeKind::UInt128 => Ok(mk().path_ty(mk().path(vec!["u128"]))),
335+
CTypeKind::UInt128 => Ok(mk().path_ty(mk().path(vec!["bf16"]))),
336336

337337
CTypeKind::Pointer(qtype) => self.convert_pointer(ctxt, qtype),
338338

c2rust-transpile/src/translator/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4675,7 +4675,7 @@ impl<'c> Translation<'c> {
46754675
match type_kind {
46764676
// libc can be accessed from anywhere as of Rust 2019 by full path
46774677
Void | Char | SChar | UChar | Short | UShort | Int | UInt | Long | ULong | LongLong
4678-
| ULongLong | Int128 | UInt128 | Half | Float | Double | LongDouble => {}
4678+
| ULongLong | Int128 | UInt128 | Half | BFloat16 | Float | Double | LongDouble => {}
46794679
// Bool uses the bool type, so no dependency on libc
46804680
Bool => {}
46814681
Paren(ctype)

0 commit comments

Comments
 (0)