diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h --- a/clang/include/clang/AST/Type.h +++ b/clang/include/clang/AST/Type.h @@ -2279,6 +2279,8 @@ bool isRVVType() const; + bool isRVVType(unsigned Bitwidth, bool IsFloat) const; + /// Return the implicit lifetime for this type, which must not be dependent. Qualifiers::ObjCLifetime getObjCARCImplicitLifetime() const; @@ -7160,6 +7162,17 @@ false; // end of boolean or operation. } +inline bool Type::isRVVType(unsigned Bitwidth, bool IsFloat) const { + bool Ret = false; +#define RVV_TYPE(Name, Id, SingletonId) +#define RVV_VECTOR_TYPE(Name, Id, SingletonId, NumEls, ElBits, NF, IsSigned, \ + IsFP) \ + if (ElBits == Bitwidth && IsFloat == IsFP) \ + Ret |= isSpecificBuiltinType(BuiltinType::Id); +#include "clang/Basic/RISCVVTypes.def" + return Ret; +} + inline bool Type::isTemplateTypeParmType() const { return isa(CanonicalType); } diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11738,6 +11738,9 @@ "builtin requires%select{| at least one of the following extensions to be enabled}0: %1">; def err_riscv_builtin_invalid_lmul : Error< "LMUL argument must be in the range [0,3] or [5,7]">; +def err_riscv_type_requires_extension : Error< + "RISC-V type %0 requires the '%1' extension" +>; def err_std_source_location_impl_not_found : Error< "'std::source_location::__impl' was not found; it must be defined before '__builtin_source_location' is called">; diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -2046,6 +2046,12 @@ targetDiag(D->getLocation(), diag::note_defined_here, FD) << D; } + if (Ty->isRVVType(/* Bitwidth */ 16, /* IsFloat */ true) && + !Context.getTargetInfo().hasFeature("experimental-zvfh")) { + Diag(Loc, diag::err_riscv_type_requires_extension, FD) + << Ty << "zvfh"; + } + // Don't allow SVE types in functions without a SVE target. if (Ty->isSVESizelessBuiltinType() && FD && FD->hasBody()) { llvm::StringMap CallerFeatureMap; diff --git a/clang/lib/Sema/SemaRISCVVectorLookup.cpp b/clang/lib/Sema/SemaRISCVVectorLookup.cpp --- a/clang/lib/Sema/SemaRISCVVectorLookup.cpp +++ b/clang/lib/Sema/SemaRISCVVectorLookup.cpp @@ -171,7 +171,6 @@ const TargetInfo &TI = Context.getTargetInfo(); bool HasVectorFloat32 = TI.hasFeature("zve32f"); bool HasVectorFloat64 = TI.hasFeature("zve64d"); - bool HasZvfh = TI.hasFeature("experimental-zvfh"); bool HasRV64 = TI.hasFeature("64bit"); bool HasFullMultiply = TI.hasFeature("v"); @@ -223,9 +222,6 @@ continue; // Check requirement. - if (BaseType == BasicType::Float16 && !HasZvfh) - continue; - if (BaseType == BasicType::Float32 && !HasVectorFloat32) continue; diff --git a/clang/test/Sema/riscv-vector-float16-check.c b/clang/test/Sema/riscv-vector-float16-check.c new file mode 100644 --- /dev/null +++ b/clang/test/Sema/riscv-vector-float16-check.c @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -triple riscv64 -target-feature +f -target-feature +d \ +// RUN: -target-feature +v -target-feature +zfh \ +// RUN: -disable-O0-optnone -o - -fsyntax-only %s -verify +// REQUIRES: riscv-registered-target +#include + +vfloat16m1_t foo() { /* expected-error {{RISC-V type 'vfloat16m1_t' (aka '__rvv_float16m1_t') requires the 'zvfh' extension}} */ +} /* expected-warning {{non-void function does not return a value}}*/ diff --git a/clang/utils/TableGen/RISCVVEmitter.cpp b/clang/utils/TableGen/RISCVVEmitter.cpp --- a/clang/utils/TableGen/RISCVVEmitter.cpp +++ b/clang/utils/TableGen/RISCVVEmitter.cpp @@ -368,14 +368,13 @@ } } } - OS << "#if defined(__riscv_zvfh)\n"; + for (int Log2LMUL : Log2LMULs) { auto T = TypeCache.computeType(BasicType::Float16, Log2LMUL, PrototypeDescriptor::Vector); if (T) printType(*T); } - OS << "#endif\n"; OS << "#if (__riscv_v_elen_fp >= 32)\n"; for (int Log2LMUL : Log2LMULs) {