From 0feb8f3b091711a2897c1261eeaee73e1007ac6d Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 28 Sep 2022 09:36:21 +0200 Subject: [PATCH 1/2] ast-exporter: Refactor recognition of vector types Co-authored-by: Khyber Sen --- c2rust-ast-exporter/src/AstExporter.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/c2rust-ast-exporter/src/AstExporter.cpp b/c2rust-ast-exporter/src/AstExporter.cpp index 80f1d67bdc..9906ed250e 100644 --- a/c2rust-ast-exporter/src/AstExporter.cpp +++ b/c2rust-ast-exporter/src/AstExporter.cpp @@ -322,13 +322,21 @@ class TypeEncoder final : public TypeVisitor { auto kind = T->getKind(); #if CLANG_VERSION_MAJOR >= 10 - // Handle built-in vector types as if they're normal vector types - if (kind >= BuiltinType::SveInt8 && kind <= BuiltinType::SveBool + // Recognize ARM vector types + const bool is_sve = kind >= BuiltinType::SveInt8 && kind <= BuiltinType::SveBool; +#else + const bool is_sve = false; +#endif // CLANG_VERSION_MAJOR >= 10 + #if CLANG_VERSION_MAJOR >= 13 - /* RISC-V vector types */ - || kind >= BuiltinType::RvvInt8mf8 && kind <= BuiltinType::RvvBool64 + // Recognize RISC-V vector types + const bool is_rvv = kind >= BuiltinType::RvvInt8mf8 && kind <= BuiltinType::RvvBool64; +#else + const bool is_rvv = false; #endif // CLANG_VERSION_MAJOR >= 13 - ) { + + // Handle built-in vector types as if they're normal vector types + if (is_sve || is_rvv) { // Declare ElemType and ElemCount as needed by various Clang versions #if CLANG_VERSION_MAJOR >= 11 auto Info = Context->getBuiltinVectorTypeInfo(T); @@ -348,6 +356,7 @@ class TypeEncoder final : public TypeVisitor { auto ElemType = [&] { switch (kind) { default: llvm_unreachable("Unknown builtin SVE type!"); +#if CLANG_VERSION_MAJOR >= 10 case BuiltinType::SveInt8: return Ctx.SignedCharTy; case BuiltinType::SveUint8: return Ctx.UnsignedCharTy; case BuiltinType::SveBool: return Ctx.UnsignedCharTy; @@ -360,6 +369,7 @@ class TypeEncoder final : public TypeVisitor { case BuiltinType::SveFloat16: return Ctx.Float16Ty; case BuiltinType::SveFloat32: return Ctx.FloatTy; case BuiltinType::SveFloat64: return Ctx.DoubleTy; +#endif // CLANG_VERSION_MAJOR >= 10 } }(); // All the SVE types present in Clang 10 are 128-bit vectors @@ -377,7 +387,6 @@ class TypeEncoder final : public TypeVisitor { VisitQualType(ElemType); return; } -#endif // CLANG_VERSION_MAJOR >= 10 const TypeTag tag = [&] { switch (kind) { From dfb2b10549b3d42dec32f661fd88e8dd18809833 Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 28 Sep 2022 09:59:07 +0200 Subject: [PATCH 2/2] ast-exporter: Work around failing type inference in old LLVM versions --- c2rust-ast-exporter/src/AstExporter.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/c2rust-ast-exporter/src/AstExporter.cpp b/c2rust-ast-exporter/src/AstExporter.cpp index 9906ed250e..e6ec22c2f7 100644 --- a/c2rust-ast-exporter/src/AstExporter.cpp +++ b/c2rust-ast-exporter/src/AstExporter.cpp @@ -353,10 +353,10 @@ class TypeEncoder final : public TypeVisitor { // Copy-pasted from Type::getSveEltType introduced after Clang 10: // (Not extended for RISCV types // as they are not available in that version anyway). +#if CLANG_VERSION_MAJOR >= 10 auto ElemType = [&] { switch (kind) { default: llvm_unreachable("Unknown builtin SVE type!"); -#if CLANG_VERSION_MAJOR >= 10 case BuiltinType::SveInt8: return Ctx.SignedCharTy; case BuiltinType::SveUint8: return Ctx.UnsignedCharTy; case BuiltinType::SveBool: return Ctx.UnsignedCharTy; @@ -369,13 +369,15 @@ class TypeEncoder final : public TypeVisitor { case BuiltinType::SveFloat16: return Ctx.Float16Ty; case BuiltinType::SveFloat32: return Ctx.FloatTy; case BuiltinType::SveFloat64: return Ctx.DoubleTy; -#endif // CLANG_VERSION_MAJOR >= 10 } }(); // All the SVE types present in Clang 10 are 128-bit vectors // (see `AArch64SVEACLETypes.def`), so we can divide 128 // by their element size to get element count. auto ElemCount = 128 / Context->getTypeSize(ElemType); +#else + llvm_unreachable("LLVM version does not have any vector types"); +#endif // CLANG_VERSION_MAJOR >= 10 #endif // CLANG_VERSION_MAJOR >= 11 auto ElemTypeTag = encodeQualType(ElemType); encodeType(T, TagVectorType,