diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -642,6 +642,15 @@ let Documentation = [Undocumented]; } +def BuiltinAlias : Attr { + let Spellings = [CXX11<"clang", "builtin_alias">, + C2x<"clang", "builtin_alias">, + GNU<"clang_builtin_alias">]; + let Args = [IdentifierArgument<"BuiltinName">]; + let Subjects = SubjectList<[Function], ErrorDiag>; + let Documentation = [BuiltinAliasDocs]; +} + def ArmBuiltinAlias : InheritableAttr, TargetSpecificAttr { let Spellings = [Clang<"__clang_arm_builtin_alias">]; let Args = [IdentifierArgument<"BuiltinName">]; diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -4699,6 +4699,25 @@ }]; } +def BuiltinAliasDocs : Documentation { + let Category = DocCatFunction; + let Content = [{ +This attribute is used in the implementation of the C intrinsics. +It allows the C intrinsic functions to be declared using the names defined +in target builtins, and still be recognized as clang builtins equivalent to the +underlying name. For example, ``riscv_vector.h`` declares the function ``vadd`` +with ``__attribute__((clang_builtin_alias(__builtin_rvv_vadd_vv_i8m1)))``. +This ensures that both functions are recognized as that clang builtin, +and in the latter case, the choice of which builtin to identify the +function as can be deferred until after overload resolution. + +This attribute can only be used to set up the aliases for certain ARM/RISC-V +C intrinsic functions; it is intended for use only inside ``arm_*.h`` and +``riscv_*.h`` and is not a general mechanism for declaring arbitrary aliases +for clang builtin functions. + }]; +} + def PreferredNameDocs : Documentation { let Category = DocCatDecl; let Content = [{ @@ -5693,6 +5712,10 @@ intrinsic functions; it is intended for use only inside ``arm_*.h`` and is not a general mechanism for declaring arbitrary aliases for clang builtin functions. + +In order to avoid duplicating the attribute definitions for similar +purpose for other architecture, there is a general form for the +attribute `clang_builtin_alias`. }]; } 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 @@ -4026,6 +4026,8 @@ def err_attribute_preferred_name_arg_invalid : Error< "argument %0 to 'preferred_name' attribute is not a typedef for " "a specialization of %1">; +def err_attribute_builtin_alias : Error< + "%0 attribute can only be applied to a ARM or RISC-V builtin">; // called-once attribute diagnostics. def err_called_once_attribute_wrong_type : Error< diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3265,6 +3265,8 @@ if (const auto *ABAA = getAttr()) { BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); + } else if (const auto *BAA = getAttr()) { + BuiltinID = BAA->getBuiltinName()->getBuiltinID(); } else if (const auto *A = getAttr()) { BuiltinID = A->getID(); } @@ -3275,7 +3277,7 @@ // If the function is marked "overloadable", it has a different mangled name // and is not the C library function. if (!ConsiderWrapperFunctions && hasAttr() && - !hasAttr()) + (!hasAttr() && !hasAttr())) return 0; ASTContext &Context = getASTContext(); diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -5120,6 +5120,7 @@ #define GET_SVE_BUILTINS #define BUILTIN(name, types, attr) case SVE::BI##name: #include "clang/Basic/arm_sve_builtins.inc" +#undef BUILTIN return true; } } @@ -5146,6 +5147,44 @@ D->addAttr(::new (S.Context) ArmBuiltinAliasAttr(S.Context, AL, Ident)); } +static bool RISCVAliasValid(unsigned BuiltinID, StringRef AliasName) { + switch (BuiltinID) { + default: + return false; +#define BUILTIN(ID, TYPE, ATTRS) case RISCV::BI##ID: +#include "clang/Basic/BuiltinsRISCV.def" +#undef BUILTIN + return true; + } +} + +static void handleBuiltinAliasAttr(Sema &S, Decl *D, + const ParsedAttr &AL) { + if (!AL.isArgIdent(0)) { + S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) + << AL << 1 << AANT_ArgumentIdentifier; + return; + } + + IdentifierInfo *Ident = AL.getArgAsIdent(0)->Ident; + unsigned BuiltinID = Ident->getBuiltinID(); + StringRef AliasName = cast(D)->getIdentifier()->getName(); + + bool IsAArch64 = S.Context.getTargetInfo().getTriple().isAArch64(); + bool IsARM = S.Context.getTargetInfo().getTriple().isARM(); + bool IsRISCV = S.Context.getTargetInfo().getTriple().isRISCV(); + if ((IsAArch64 && !ArmSveAliasValid(BuiltinID, AliasName)) || + (IsARM && !ArmMveAliasValid(BuiltinID, AliasName) && + !ArmCdeAliasValid(BuiltinID, AliasName)) || + (IsRISCV && !RISCVAliasValid(BuiltinID, AliasName)) || + (!IsAArch64 && !IsARM && !IsRISCV)) { + S.Diag(AL.getLoc(), diag::err_attribute_builtin_alias) << AL; + return; + } + + D->addAttr(::new (S.Context) BuiltinAliasAttr(S.Context, AL, Ident)); +} + //===----------------------------------------------------------------------===// // Checker-specific attribute handlers. //===----------------------------------------------------------------------===// @@ -8243,6 +8282,10 @@ case ParsedAttr::AT_EnforceTCBLeaf: handleEnforceTCBAttr(S, D, AL); break; + + case ParsedAttr::AT_BuiltinAlias: + handleBuiltinAliasAttr(S, D, AL); + break; } } diff --git a/clang/test/CodeGen/RISCV/riscv-attr-builtin-alias-err.c b/clang/test/CodeGen/RISCV/riscv-attr-builtin-alias-err.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/RISCV/riscv-attr-builtin-alias-err.c @@ -0,0 +1,19 @@ +// REQUIRES: riscv-registered-target +// RUN: not %clang_cc1 -triple riscv64 -fsyntax-only -verify \ +// RUN: -target-feature +experimental-v %s 2>&1 \ +// RUN: | FileCheck %s + +#include + +#define __rvv_generic \ +static inline __attribute__((__always_inline__, __nodebug__)) + +__rvv_generic +__attribute__((clang_builtin_alias(__builtin_rvv_vadd_vv_i8m1))) +vint8m1_t vadd_generic (vint8m1_t op0, vint8m1_t op1, size_t op2); + +// CHECK: passing 'vint8m2_t' (aka '__rvv_int8m2_t') to parameter of incompatible type 'vint8m1_t' +vint8m2_t test(vint8m2_t op0, vint8m2_t op1, size_t vl) { + vint8m2_t ret = vadd_generic(op0, op1, vl); + return ret; +} diff --git a/clang/test/CodeGen/RISCV/riscv-attr-builtin-alias.c b/clang/test/CodeGen/RISCV/riscv-attr-builtin-alias.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/RISCV/riscv-attr-builtin-alias.c @@ -0,0 +1,36 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py +// REQUIRES: riscv-registered-target +// RUN: %clang_cc1 -triple riscv64 -emit-llvm -target-feature +experimental-v \ +// RUN: %s -o - \ +// RUN: | FileCheck %s + +#include + +#define __rvv_generic \ +static inline __attribute__((__always_inline__, __nodebug__)) + +__rvv_generic +__attribute__((clang_builtin_alias(__builtin_rvv_vadd_vv_i8m1))) +vint8m1_t vadd_generic (vint8m1_t op0, vint8m1_t op1, size_t op2); + +// CHECK-LABEL: @test( +// CHECK-NEXT: entry: +// CHECK-NEXT: [[OP0_ADDR:%.*]] = alloca , align 1 +// CHECK-NEXT: [[OP1_ADDR:%.*]] = alloca , align 1 +// CHECK-NEXT: [[VL_ADDR:%.*]] = alloca i64, align 8 +// CHECK-NEXT: [[RET:%.*]] = alloca , align 1 +// CHECK-NEXT: store [[OP0:%.*]], * [[OP0_ADDR]], align 1 +// CHECK-NEXT: store [[OP1:%.*]], * [[OP1_ADDR]], align 1 +// CHECK-NEXT: store i64 [[VL:%.*]], i64* [[VL_ADDR]], align 8 +// CHECK-NEXT: [[TMP0:%.*]] = load , * [[OP0_ADDR]], align 1 +// CHECK-NEXT: [[TMP1:%.*]] = load , * [[OP1_ADDR]], align 1 +// CHECK-NEXT: [[TMP2:%.*]] = load i64, i64* [[VL_ADDR]], align 8 +// CHECK-NEXT: [[TMP3:%.*]] = call @llvm.riscv.vadd.nxv8i8.nxv8i8.i64( [[TMP0]], [[TMP1]], i64 [[TMP2]]) +// CHECK-NEXT: store [[TMP3]], * [[RET]], align 1 +// CHECK-NEXT: [[TMP4:%.*]] = load , * [[RET]], align 1 +// CHECK-NEXT: ret [[TMP4]] +// +vint8m1_t test(vint8m1_t op0, vint8m1_t op1, size_t vl) { + vint8m1_t ret = vadd_generic(op0, op1, vl); + return ret; +} diff --git a/clang/test/Misc/pragma-attribute-supported-attributes-list.test b/clang/test/Misc/pragma-attribute-supported-attributes-list.test --- a/clang/test/Misc/pragma-attribute-supported-attributes-list.test +++ b/clang/test/Misc/pragma-attribute-supported-attributes-list.test @@ -22,6 +22,7 @@ // CHECK-NEXT: Assumption (SubjectMatchRule_function, SubjectMatchRule_objc_method) // CHECK-NEXT: Availability ((SubjectMatchRule_record, SubjectMatchRule_enum, SubjectMatchRule_enum_constant, SubjectMatchRule_field, SubjectMatchRule_function, SubjectMatchRule_namespace, SubjectMatchRule_objc_category, SubjectMatchRule_objc_implementation, SubjectMatchRule_objc_interface, SubjectMatchRule_objc_method, SubjectMatchRule_objc_property, SubjectMatchRule_objc_protocol, SubjectMatchRule_record, SubjectMatchRule_type_alias, SubjectMatchRule_variable)) // CHECK-NEXT: BPFPreserveAccessIndex (SubjectMatchRule_record) +// CHECK-NEXT: BuiltinAlias (SubjectMatchRule_function) // CHECK-NEXT: CFAuditedTransfer (SubjectMatchRule_function) // CHECK-NEXT: CFConsumed (SubjectMatchRule_variable_is_parameter) // CHECK-NEXT: CFICanonicalJumpTable (SubjectMatchRule_function)