Skip to content

Commit

Permalink
[Clang][RISCV] Guard vector float16 type correctly with semantic anal…
Browse files Browse the repository at this point in the history
…ysis

Before this commit, vector float 16 types (e.g. `vfloat16m1_t`) of RVV
is only defined when extension `zvfh` is defined. However this
generate inaccurate diagnostics like:

```
error: unknown type name 'vfloat16m1_t'
```

This commit improves the compiler by guarding type check correctly
under semantic analysis.

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D143657
  • Loading branch information
eopXD committed Feb 14, 2023
1 parent 4b815d8 commit 235e90c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
13 changes: 13 additions & 0 deletions clang/include/clang/AST/Type.h
Expand Up @@ -2275,6 +2275,8 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {

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;

Expand Down Expand Up @@ -7153,6 +7155,17 @@ inline bool Type::isRVVType() const {
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<TemplateTypeParmType>(CanonicalType);
}
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Expand Up @@ -11738,6 +11738,9 @@ def err_riscv_builtin_requires_extension : Error<
"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">;
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Sema/Sema.cpp
Expand Up @@ -2039,6 +2039,12 @@ void Sema::checkTypeSupport(QualType Ty, SourceLocation Loc, ValueDecl *D) {
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<bool> CallerFeatureMap;
Expand Down
4 changes: 0 additions & 4 deletions clang/lib/Sema/SemaRISCVVectorLookup.cpp
Expand Up @@ -171,7 +171,6 @@ void RISCVIntrinsicManagerImpl::InitIntrinsicList() {
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");

Expand Down Expand Up @@ -223,9 +222,6 @@ void RISCVIntrinsicManagerImpl::InitIntrinsicList() {
continue;

// Check requirement.
if (BaseType == BasicType::Float16 && !HasZvfh)
continue;

if (BaseType == BasicType::Float32 && !HasVectorFloat32)
continue;

Expand Down
8 changes: 8 additions & 0 deletions 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 <riscv_vector.h>

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}}*/
3 changes: 1 addition & 2 deletions clang/utils/TableGen/RISCVVEmitter.cpp
Expand Up @@ -368,14 +368,13 @@ void RVVEmitter::createHeader(raw_ostream &OS) {
}
}
}
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) {
Expand Down

0 comments on commit 235e90c

Please sign in to comment.