[clang] Add clang_elementwise_builtin_alias (PR #86175)
https://github.com/bob80905 created https://github.com/llvm/llvm-project/pull/86175 RFC: https://discourse.llvm.org/t/rfc-elementwise-attribute-in-clang-front-end/76342/9 The above RFC gives the motivation for this PR. In summary, this PR adds an attribute that will simplify / obliviate the need to create separate elementwise builtins for pre-existing builtins. It will also allow users to create their own user-defined functions, and create elementwise variants of their functions. This should help accelerate the process of using tan in an elementwise way. >From 5e10b1e42a20a39c9a3d5ff332591713511832c8 Mon Sep 17 00:00:00 2001 From: Joshua Batista Date: Wed, 20 Mar 2024 13:24:07 -0700 Subject: [PATCH 1/4] make elemnetwise alias an alias of builtin alias --- clang/include/clang/Basic/Attr.td | 9 clang/include/clang/Basic/AttrDocs.td | 22 +++ .../clang/Basic/DiagnosticSemaKinds.td| 3 ++- clang/lib/AST/Decl.cpp| 2 ++ clang/lib/Headers/hlsl/hlsl_intrinsics.h | 2 ++ 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fd7970d0451acd..160e8ef730ef92 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -759,6 +759,15 @@ def BuiltinAlias : Attr { let Documentation = [BuiltinAliasDocs]; } +def ElementwiseBuiltinAlias : Attr { + let Spellings = [CXX11<"clang", "elementwise_builtin_alias">, + C23<"clang", "elementwise_builtin_alias">, + GNU<"clang_elementwise_builtin_alias">]; + let Args = [IdentifierArgument<"BuiltinName">]; + let Subjects = SubjectList<[Function], ErrorDiag>; + let Documentation = [ElementwiseBuiltinAliasDocs]; +} + 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 index 2c07cd09b0d5b7..7ce83bc881f064 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -5462,6 +5462,28 @@ for clang builtin functions. }]; } +def ElementwiseBuiltinAliasDocs : Documentation { + let Category = DocCatFunction; + let Heading = "clang::elementwise_builtin_alias, clang_elementwise_builtin_alias"; + 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. It also declares that the given C intrinsic can accept +vector arguments. For example, ``hlsl_intrinsics.h`` declares the function ``abs`` +with ``__attribute__((clang_elementwise_builtin_alias(__builtin_abs)))``. +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. It also enables +the ``abs`` function to take vector arguments. + +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 = [{ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index c54105507753eb..1252d3804131a6 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -4412,7 +4412,8 @@ def err_attribute_preferred_name_arg_invalid : Error< "a specialization of %1">; def err_attribute_builtin_alias : Error< "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; - +def err_attribute_elementwise_builtin_alias : Error< + "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; // called-once attribute diagnostics. def err_called_once_attribute_wrong_type : Error< "'called_once' attribute only applies to function-like parameters">; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 8626f04012f7d4..cb2c5cf6ceaf49 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3598,6 +3598,8 @@ unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); } else if (const auto *BAA = getAttr()) { BuiltinID = BAA->getBuiltinName()->getBuiltinID(); + } else if (const auto *EBAA = getAttr()) { +BuiltinID = EBAA->getBuiltinName()->getBuiltinID(); } else if (const auto *A = getAttr()) { BuiltinID = A->getID(); } diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/c
[clang] Add clang_elementwise_builtin_alias (PR #86175)
llvmbot wrote: @llvm/pr-subscribers-clang Author: Joshua Batista (bob80905) Changes RFC: https://discourse.llvm.org/t/rfc-elementwise-attribute-in-clang-front-end/76342/9 The above RFC gives the motivation for this PR. In summary, this PR adds an attribute that will simplify / obliviate the need to create separate elementwise builtins for pre-existing builtins. It will also allow users to create their own user-defined functions, and create elementwise variants of their functions. This should help accelerate the process of using tan in an elementwise way. --- Patch is 33.72 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/86175.diff 7 Files Affected: - (modified) clang/include/clang/Basic/Attr.td (+9) - (modified) clang/include/clang/Basic/AttrDocs.td (+22) - (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2-1) - (modified) clang/lib/AST/Decl.cpp (+2) - (modified) clang/lib/Headers/hlsl/hlsl_intrinsics.h (+3-1) - (modified) clang/lib/Sema/SemaChecking.cpp (+334-298) - (modified) clang/lib/Sema/SemaDeclAttr.cpp (+32) ``diff diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fd7970d0451acd..160e8ef730ef92 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -759,6 +759,15 @@ def BuiltinAlias : Attr { let Documentation = [BuiltinAliasDocs]; } +def ElementwiseBuiltinAlias : Attr { + let Spellings = [CXX11<"clang", "elementwise_builtin_alias">, + C23<"clang", "elementwise_builtin_alias">, + GNU<"clang_elementwise_builtin_alias">]; + let Args = [IdentifierArgument<"BuiltinName">]; + let Subjects = SubjectList<[Function], ErrorDiag>; + let Documentation = [ElementwiseBuiltinAliasDocs]; +} + 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 index 2c07cd09b0d5b7..7ce83bc881f064 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -5462,6 +5462,28 @@ for clang builtin functions. }]; } +def ElementwiseBuiltinAliasDocs : Documentation { + let Category = DocCatFunction; + let Heading = "clang::elementwise_builtin_alias, clang_elementwise_builtin_alias"; + 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. It also declares that the given C intrinsic can accept +vector arguments. For example, ``hlsl_intrinsics.h`` declares the function ``abs`` +with ``__attribute__((clang_elementwise_builtin_alias(__builtin_abs)))``. +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. It also enables +the ``abs`` function to take vector arguments. + +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 = [{ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index c54105507753eb..1252d3804131a6 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -4412,7 +4412,8 @@ def err_attribute_preferred_name_arg_invalid : Error< "a specialization of %1">; def err_attribute_builtin_alias : Error< "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; - +def err_attribute_elementwise_builtin_alias : Error< + "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; // called-once attribute diagnostics. def err_called_once_attribute_wrong_type : Error< "'called_once' attribute only applies to function-like parameters">; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 8626f04012f7d4..cb2c5cf6ceaf49 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3598,6 +3598,8 @@ unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); } else if (const auto *BAA = getAttr()) { BuiltinID = BAA->getBuiltinName()->getBuiltinID(); + } else if (const auto *EBAA = getAttr()) { +BuiltinID = EBAA->getBuiltinName()->getBuiltinID(); } else if (const auto *A = getAttr()) { BuiltinID = A->getID(); } diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
[clang] Add clang_elementwise_builtin_alias (PR #86175)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff 94c988bcfdea596e5c9078be8ec28688eb0d96a3 866007083a306bd4785579b691bbf99143bc0548 -- clang/lib/AST/Decl.cpp clang/lib/Headers/hlsl/hlsl_intrinsics.h clang/lib/Sema/SemaChecking.cpp clang/lib/Sema/SemaDeclAttr.cpp `` View the diff from clang-format here. ``diff diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h index 1c8e407302..9ab34cc57e 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h @@ -18,7 +18,7 @@ namespace hlsl { #define _HLSL_BUILTIN_ALIAS(builtin) \ __attribute__((clang_builtin_alias(builtin))) -#define _HLSL_ELEMENTWISE_BUILTIN_ALIAS(builtin) \ +#define _HLSL_ELEMENTWISE_BUILTIN_ALIAS(builtin) \ __attribute__((clang_elementwise_builtin_alias(builtin))) #define _HLSL_AVAILABILITY(environment, version) \ __attribute__((availability(environment, introduced = version))) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index c2621bdd2f..5c4bb98614 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -2273,320 +2273,320 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, } } } -FPOptions FPO; -switch (BuiltinID) { -case Builtin::BI__builtin_cpu_supports: -case Builtin::BI__builtin_cpu_is: - if (SemaBuiltinCpu(*this, Context.getTargetInfo(), TheCall, - Context.getAuxTargetInfo(), BuiltinID)) -return ExprError(); - break; -case Builtin::BI__builtin_cpu_init: - if (!Context.getTargetInfo().supportsCpuInit()) { -Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) -<< SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); -return ExprError(); - } - break; -case Builtin::BI__builtin___CFStringMakeConstantString: - // CFStringMakeConstantString is currently not implemented for GOFF (i.e., - // on z/OS) and for XCOFF (i.e., on AIX). Emit unsupported - if (CheckBuiltinTargetNotInUnsupported( - *this, BuiltinID, TheCall, - {llvm::Triple::GOFF, llvm::Triple::XCOFF})) -return ExprError(); - assert(TheCall->getNumArgs() == 1 && - "Wrong # arguments to builtin CFStringMakeConstantString"); - if (CheckObjCString(TheCall->getArg(0))) + FPOptions FPO; + switch (BuiltinID) { + case Builtin::BI__builtin_cpu_supports: + case Builtin::BI__builtin_cpu_is: +if (SemaBuiltinCpu(*this, Context.getTargetInfo(), TheCall, + Context.getAuxTargetInfo(), BuiltinID)) + return ExprError(); +break; + case Builtin::BI__builtin_cpu_init: +if (!Context.getTargetInfo().supportsCpuInit()) { + Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported) + << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc()); + return ExprError(); +} +break; + case Builtin::BI__builtin___CFStringMakeConstantString: +// CFStringMakeConstantString is currently not implemented for GOFF (i.e., +// on z/OS) and for XCOFF (i.e., on AIX). Emit unsupported +if (CheckBuiltinTargetNotInUnsupported( +*this, BuiltinID, TheCall, +{llvm::Triple::GOFF, llvm::Triple::XCOFF})) + return ExprError(); +assert(TheCall->getNumArgs() == 1 && + "Wrong # arguments to builtin CFStringMakeConstantString"); +if (CheckObjCString(TheCall->getArg(0))) + return ExprError(); +break; + case Builtin::BI__builtin_ms_va_start: + case Builtin::BI__builtin_stdarg_start: + case Builtin::BI__builtin_va_start: +if (SemaBuiltinVAStart(BuiltinID, TheCall)) + return ExprError(); +break; + case Builtin::BI__va_start: { +switch (Context.getTargetInfo().getTriple().getArch()) { +case llvm::Triple::aarch64: +case llvm::Triple::arm: +case llvm::Triple::thumb: + if (SemaBuiltinVAStartARMMicrosoft(TheCall)) return ExprError(); break; -case Builtin::BI__builtin_ms_va_start: -case Builtin::BI__builtin_stdarg_start: -case Builtin::BI__builtin_va_start: +default: if (SemaBuiltinVAStart(BuiltinID, TheCall)) return ExprError(); break; -case Builtin::BI__va_start: { - switch (Context.getTargetInfo().getTriple().getArch()) { - case llvm::Triple::aarch64: - case llvm::Triple::arm: - case llvm::Triple::thumb: -if (SemaBuiltinVAStartARMMicrosoft(TheCall)) - return ExprError(); -break; - default: -if (SemaBuiltinVAStart(BuiltinID, TheC
[clang] Add clang_elementwise_builtin_alias (PR #86175)
https://github.com/bob80905 updated https://github.com/llvm/llvm-project/pull/86175 >From 5e10b1e42a20a39c9a3d5ff332591713511832c8 Mon Sep 17 00:00:00 2001 From: Joshua Batista Date: Wed, 20 Mar 2024 13:24:07 -0700 Subject: [PATCH 1/4] make elemnetwise alias an alias of builtin alias --- clang/include/clang/Basic/Attr.td | 9 clang/include/clang/Basic/AttrDocs.td | 22 +++ .../clang/Basic/DiagnosticSemaKinds.td| 3 ++- clang/lib/AST/Decl.cpp| 2 ++ clang/lib/Headers/hlsl/hlsl_intrinsics.h | 2 ++ 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fd7970d0451acd..160e8ef730ef92 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -759,6 +759,15 @@ def BuiltinAlias : Attr { let Documentation = [BuiltinAliasDocs]; } +def ElementwiseBuiltinAlias : Attr { + let Spellings = [CXX11<"clang", "elementwise_builtin_alias">, + C23<"clang", "elementwise_builtin_alias">, + GNU<"clang_elementwise_builtin_alias">]; + let Args = [IdentifierArgument<"BuiltinName">]; + let Subjects = SubjectList<[Function], ErrorDiag>; + let Documentation = [ElementwiseBuiltinAliasDocs]; +} + 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 index 2c07cd09b0d5b7..7ce83bc881f064 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -5462,6 +5462,28 @@ for clang builtin functions. }]; } +def ElementwiseBuiltinAliasDocs : Documentation { + let Category = DocCatFunction; + let Heading = "clang::elementwise_builtin_alias, clang_elementwise_builtin_alias"; + 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. It also declares that the given C intrinsic can accept +vector arguments. For example, ``hlsl_intrinsics.h`` declares the function ``abs`` +with ``__attribute__((clang_elementwise_builtin_alias(__builtin_abs)))``. +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. It also enables +the ``abs`` function to take vector arguments. + +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 = [{ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index c54105507753eb..1252d3804131a6 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -4412,7 +4412,8 @@ def err_attribute_preferred_name_arg_invalid : Error< "a specialization of %1">; def err_attribute_builtin_alias : Error< "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; - +def err_attribute_elementwise_builtin_alias : Error< + "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; // called-once attribute diagnostics. def err_called_once_attribute_wrong_type : Error< "'called_once' attribute only applies to function-like parameters">; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 8626f04012f7d4..cb2c5cf6ceaf49 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3598,6 +3598,8 @@ unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); } else if (const auto *BAA = getAttr()) { BuiltinID = BAA->getBuiltinName()->getBuiltinID(); + } else if (const auto *EBAA = getAttr()) { +BuiltinID = EBAA->getBuiltinName()->getBuiltinID(); } else if (const auto *A = getAttr()) { BuiltinID = A->getID(); } diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h index 45f8544392584e..b37c4d13b26e62 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h @@ -18,6 +18,8 @@ namespace hlsl { #define _HLSL_BUILTIN_ALIAS(builtin) \ __attribute__((clang_builtin_alias(builtin))) +#define _HLSL_ELEMENTWISE_BUILTIN_ALIAS(builtin) \ + __attribute__((clang_elementwise_builti
[clang] Add clang_elementwise_builtin_alias (PR #86175)
https://github.com/bob80905 updated https://github.com/llvm/llvm-project/pull/86175 >From 5e10b1e42a20a39c9a3d5ff332591713511832c8 Mon Sep 17 00:00:00 2001 From: Joshua Batista Date: Wed, 20 Mar 2024 13:24:07 -0700 Subject: [PATCH 1/5] make elemnetwise alias an alias of builtin alias --- clang/include/clang/Basic/Attr.td | 9 clang/include/clang/Basic/AttrDocs.td | 22 +++ .../clang/Basic/DiagnosticSemaKinds.td| 3 ++- clang/lib/AST/Decl.cpp| 2 ++ clang/lib/Headers/hlsl/hlsl_intrinsics.h | 2 ++ 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fd7970d0451acd..160e8ef730ef92 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -759,6 +759,15 @@ def BuiltinAlias : Attr { let Documentation = [BuiltinAliasDocs]; } +def ElementwiseBuiltinAlias : Attr { + let Spellings = [CXX11<"clang", "elementwise_builtin_alias">, + C23<"clang", "elementwise_builtin_alias">, + GNU<"clang_elementwise_builtin_alias">]; + let Args = [IdentifierArgument<"BuiltinName">]; + let Subjects = SubjectList<[Function], ErrorDiag>; + let Documentation = [ElementwiseBuiltinAliasDocs]; +} + 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 index 2c07cd09b0d5b7..7ce83bc881f064 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -5462,6 +5462,28 @@ for clang builtin functions. }]; } +def ElementwiseBuiltinAliasDocs : Documentation { + let Category = DocCatFunction; + let Heading = "clang::elementwise_builtin_alias, clang_elementwise_builtin_alias"; + 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. It also declares that the given C intrinsic can accept +vector arguments. For example, ``hlsl_intrinsics.h`` declares the function ``abs`` +with ``__attribute__((clang_elementwise_builtin_alias(__builtin_abs)))``. +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. It also enables +the ``abs`` function to take vector arguments. + +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 = [{ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index c54105507753eb..1252d3804131a6 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -4412,7 +4412,8 @@ def err_attribute_preferred_name_arg_invalid : Error< "a specialization of %1">; def err_attribute_builtin_alias : Error< "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; - +def err_attribute_elementwise_builtin_alias : Error< + "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; // called-once attribute diagnostics. def err_called_once_attribute_wrong_type : Error< "'called_once' attribute only applies to function-like parameters">; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 8626f04012f7d4..cb2c5cf6ceaf49 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3598,6 +3598,8 @@ unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); } else if (const auto *BAA = getAttr()) { BuiltinID = BAA->getBuiltinName()->getBuiltinID(); + } else if (const auto *EBAA = getAttr()) { +BuiltinID = EBAA->getBuiltinName()->getBuiltinID(); } else if (const auto *A = getAttr()) { BuiltinID = A->getID(); } diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h index 45f8544392584e..b37c4d13b26e62 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h @@ -18,6 +18,8 @@ namespace hlsl { #define _HLSL_BUILTIN_ALIAS(builtin) \ __attribute__((clang_builtin_alias(builtin))) +#define _HLSL_ELEMENTWISE_BUILTIN_ALIAS(builtin) \ + __attribute__((clang_elementwise_builti
[clang] Add clang_elementwise_builtin_alias (PR #86175)
https://github.com/bob80905 updated https://github.com/llvm/llvm-project/pull/86175 >From 5e10b1e42a20a39c9a3d5ff332591713511832c8 Mon Sep 17 00:00:00 2001 From: Joshua Batista Date: Wed, 20 Mar 2024 13:24:07 -0700 Subject: [PATCH 1/6] make elemnetwise alias an alias of builtin alias --- clang/include/clang/Basic/Attr.td | 9 clang/include/clang/Basic/AttrDocs.td | 22 +++ .../clang/Basic/DiagnosticSemaKinds.td| 3 ++- clang/lib/AST/Decl.cpp| 2 ++ clang/lib/Headers/hlsl/hlsl_intrinsics.h | 2 ++ 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fd7970d0451acd..160e8ef730ef92 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -759,6 +759,15 @@ def BuiltinAlias : Attr { let Documentation = [BuiltinAliasDocs]; } +def ElementwiseBuiltinAlias : Attr { + let Spellings = [CXX11<"clang", "elementwise_builtin_alias">, + C23<"clang", "elementwise_builtin_alias">, + GNU<"clang_elementwise_builtin_alias">]; + let Args = [IdentifierArgument<"BuiltinName">]; + let Subjects = SubjectList<[Function], ErrorDiag>; + let Documentation = [ElementwiseBuiltinAliasDocs]; +} + 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 index 2c07cd09b0d5b7..7ce83bc881f064 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -5462,6 +5462,28 @@ for clang builtin functions. }]; } +def ElementwiseBuiltinAliasDocs : Documentation { + let Category = DocCatFunction; + let Heading = "clang::elementwise_builtin_alias, clang_elementwise_builtin_alias"; + 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. It also declares that the given C intrinsic can accept +vector arguments. For example, ``hlsl_intrinsics.h`` declares the function ``abs`` +with ``__attribute__((clang_elementwise_builtin_alias(__builtin_abs)))``. +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. It also enables +the ``abs`` function to take vector arguments. + +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 = [{ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index c54105507753eb..1252d3804131a6 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -4412,7 +4412,8 @@ def err_attribute_preferred_name_arg_invalid : Error< "a specialization of %1">; def err_attribute_builtin_alias : Error< "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; - +def err_attribute_elementwise_builtin_alias : Error< + "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; // called-once attribute diagnostics. def err_called_once_attribute_wrong_type : Error< "'called_once' attribute only applies to function-like parameters">; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 8626f04012f7d4..cb2c5cf6ceaf49 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3598,6 +3598,8 @@ unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); } else if (const auto *BAA = getAttr()) { BuiltinID = BAA->getBuiltinName()->getBuiltinID(); + } else if (const auto *EBAA = getAttr()) { +BuiltinID = EBAA->getBuiltinName()->getBuiltinID(); } else if (const auto *A = getAttr()) { BuiltinID = A->getID(); } diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h index 45f8544392584e..b37c4d13b26e62 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h @@ -18,6 +18,8 @@ namespace hlsl { #define _HLSL_BUILTIN_ALIAS(builtin) \ __attribute__((clang_builtin_alias(builtin))) +#define _HLSL_ELEMENTWISE_BUILTIN_ALIAS(builtin) \ + __attribute__((clang_elementwise_builti
[clang] Add clang_elementwise_builtin_alias (PR #86175)
@@ -2239,6 +2239,39 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, return true; ICEArguments &= ~(1 << ArgNo); } + // if the call has the elementwise attribute, then + // make sure that an elementwise expr is emitted. + if (FDecl->hasAttr()) { +switch (FDecl->getNumParams()) { +case 1: { + if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) +return ExprError(); + + QualType ArgTy = TheCall->getArg(0)->getType(); + if (checkFPMathBuiltinElementType( + *this, TheCall->getArg(0)->getBeginLoc(), ArgTy, 1)) +return ExprError(); + break; +} +case 2: { + if (SemaBuiltinElementwiseMath(TheCall)) +return ExprError(); + + QualType ArgTy = TheCall->getArg(0)->getType(); + if (checkFPMathBuiltinElementType( + *this, TheCall->getArg(0)->getBeginLoc(), ArgTy, 1) || + checkFPMathBuiltinElementType( + *this, TheCall->getArg(1)->getBeginLoc(), ArgTy, 2)) +return ExprError(); + break; +} +case 3: { + if (SemaBuiltinElementwiseTernaryMath(TheCall)) farzonl wrote: there are ternaryMath calls that are not floating point only so you really want to do this instead: ```c++ if (SemaBuiltinElementwiseTernaryMath( TheCall, /*CheckForFloatArgs*/ TheCall->getArg(0)->getType()->hasFloatingRepresentation())) ``` https://github.com/llvm/llvm-project/pull/86175 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Add clang_elementwise_builtin_alias (PR #86175)
@@ -2239,6 +2239,39 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, return true; ICEArguments &= ~(1 << ArgNo); } + // if the call has the elementwise attribute, then + // make sure that an elementwise expr is emitted. + if (FDecl->hasAttr()) { +switch (FDecl->getNumParams()) { +case 1: { + if (PrepareBuiltinElementwiseMathOneArgCall(TheCall)) +return ExprError(); + + QualType ArgTy = TheCall->getArg(0)->getType(); + if (checkFPMathBuiltinElementType( farzonl wrote: not all one arg math builtin are floating point only. https://github.com/llvm/llvm-project/pull/86175 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Add clang_elementwise_builtin_alias (PR #86175)
https://github.com/bob80905 updated https://github.com/llvm/llvm-project/pull/86175 >From 5e10b1e42a20a39c9a3d5ff332591713511832c8 Mon Sep 17 00:00:00 2001 From: Joshua Batista Date: Wed, 20 Mar 2024 13:24:07 -0700 Subject: [PATCH 1/7] make elemnetwise alias an alias of builtin alias --- clang/include/clang/Basic/Attr.td | 9 clang/include/clang/Basic/AttrDocs.td | 22 +++ .../clang/Basic/DiagnosticSemaKinds.td| 3 ++- clang/lib/AST/Decl.cpp| 2 ++ clang/lib/Headers/hlsl/hlsl_intrinsics.h | 2 ++ 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fd7970d0451acd..160e8ef730ef92 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -759,6 +759,15 @@ def BuiltinAlias : Attr { let Documentation = [BuiltinAliasDocs]; } +def ElementwiseBuiltinAlias : Attr { + let Spellings = [CXX11<"clang", "elementwise_builtin_alias">, + C23<"clang", "elementwise_builtin_alias">, + GNU<"clang_elementwise_builtin_alias">]; + let Args = [IdentifierArgument<"BuiltinName">]; + let Subjects = SubjectList<[Function], ErrorDiag>; + let Documentation = [ElementwiseBuiltinAliasDocs]; +} + 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 index 2c07cd09b0d5b7..7ce83bc881f064 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -5462,6 +5462,28 @@ for clang builtin functions. }]; } +def ElementwiseBuiltinAliasDocs : Documentation { + let Category = DocCatFunction; + let Heading = "clang::elementwise_builtin_alias, clang_elementwise_builtin_alias"; + 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. It also declares that the given C intrinsic can accept +vector arguments. For example, ``hlsl_intrinsics.h`` declares the function ``abs`` +with ``__attribute__((clang_elementwise_builtin_alias(__builtin_abs)))``. +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. It also enables +the ``abs`` function to take vector arguments. + +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 = [{ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index c54105507753eb..1252d3804131a6 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -4412,7 +4412,8 @@ def err_attribute_preferred_name_arg_invalid : Error< "a specialization of %1">; def err_attribute_builtin_alias : Error< "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; - +def err_attribute_elementwise_builtin_alias : Error< + "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; // called-once attribute diagnostics. def err_called_once_attribute_wrong_type : Error< "'called_once' attribute only applies to function-like parameters">; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 8626f04012f7d4..cb2c5cf6ceaf49 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3598,6 +3598,8 @@ unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); } else if (const auto *BAA = getAttr()) { BuiltinID = BAA->getBuiltinName()->getBuiltinID(); + } else if (const auto *EBAA = getAttr()) { +BuiltinID = EBAA->getBuiltinName()->getBuiltinID(); } else if (const auto *A = getAttr()) { BuiltinID = A->getID(); } diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h index 45f8544392584e..b37c4d13b26e62 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h @@ -18,6 +18,8 @@ namespace hlsl { #define _HLSL_BUILTIN_ALIAS(builtin) \ __attribute__((clang_builtin_alias(builtin))) +#define _HLSL_ELEMENTWISE_BUILTIN_ALIAS(builtin) \ + __attribute__((clang_elementwise_builti
[clang] Add clang_elementwise_builtin_alias (PR #86175)
bogner wrote: Thanks for working on this, but I think that what this shows is that the complexity of adding this elementwise alias builtin isn't quite justified by the utility. It's certainly convenient to be able to specify the builtins this way, but validation is a lot more complicated and I think this will be hard to test. Maybe we should consider this a successful experiment but drop the idea for now unless we run into a situation where we really need it in the future. https://github.com/llvm/llvm-project/pull/86175 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Add clang_elementwise_builtin_alias (PR #86175)
https://github.com/bob80905 updated https://github.com/llvm/llvm-project/pull/86175 >From 5e10b1e42a20a39c9a3d5ff332591713511832c8 Mon Sep 17 00:00:00 2001 From: Joshua Batista Date: Wed, 20 Mar 2024 13:24:07 -0700 Subject: [PATCH 1/8] make elemnetwise alias an alias of builtin alias --- clang/include/clang/Basic/Attr.td | 9 clang/include/clang/Basic/AttrDocs.td | 22 +++ .../clang/Basic/DiagnosticSemaKinds.td| 3 ++- clang/lib/AST/Decl.cpp| 2 ++ clang/lib/Headers/hlsl/hlsl_intrinsics.h | 2 ++ 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index fd7970d0451acd..160e8ef730ef92 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -759,6 +759,15 @@ def BuiltinAlias : Attr { let Documentation = [BuiltinAliasDocs]; } +def ElementwiseBuiltinAlias : Attr { + let Spellings = [CXX11<"clang", "elementwise_builtin_alias">, + C23<"clang", "elementwise_builtin_alias">, + GNU<"clang_elementwise_builtin_alias">]; + let Args = [IdentifierArgument<"BuiltinName">]; + let Subjects = SubjectList<[Function], ErrorDiag>; + let Documentation = [ElementwiseBuiltinAliasDocs]; +} + 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 index 2c07cd09b0d5b7..7ce83bc881f064 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -5462,6 +5462,28 @@ for clang builtin functions. }]; } +def ElementwiseBuiltinAliasDocs : Documentation { + let Category = DocCatFunction; + let Heading = "clang::elementwise_builtin_alias, clang_elementwise_builtin_alias"; + 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. It also declares that the given C intrinsic can accept +vector arguments. For example, ``hlsl_intrinsics.h`` declares the function ``abs`` +with ``__attribute__((clang_elementwise_builtin_alias(__builtin_abs)))``. +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. It also enables +the ``abs`` function to take vector arguments. + +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 = [{ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index c54105507753eb..1252d3804131a6 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -4412,7 +4412,8 @@ def err_attribute_preferred_name_arg_invalid : Error< "a specialization of %1">; def err_attribute_builtin_alias : Error< "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; - +def err_attribute_elementwise_builtin_alias : Error< + "%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">; // called-once attribute diagnostics. def err_called_once_attribute_wrong_type : Error< "'called_once' attribute only applies to function-like parameters">; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 8626f04012f7d4..cb2c5cf6ceaf49 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3598,6 +3598,8 @@ unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); } else if (const auto *BAA = getAttr()) { BuiltinID = BAA->getBuiltinName()->getBuiltinID(); + } else if (const auto *EBAA = getAttr()) { +BuiltinID = EBAA->getBuiltinName()->getBuiltinID(); } else if (const auto *A = getAttr()) { BuiltinID = A->getID(); } diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h index 45f8544392584e..b37c4d13b26e62 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h @@ -18,6 +18,8 @@ namespace hlsl { #define _HLSL_BUILTIN_ALIAS(builtin) \ __attribute__((clang_builtin_alias(builtin))) +#define _HLSL_ELEMENTWISE_BUILTIN_ALIAS(builtin) \ + __attribute__((clang_elementwise_builti
[clang] Add clang_elementwise_builtin_alias (PR #86175)
https://github.com/bob80905 converted_to_draft https://github.com/llvm/llvm-project/pull/86175 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Add clang_elementwise_builtin_alias (PR #86175)
https://github.com/farzonl closed https://github.com/llvm/llvm-project/pull/86175 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits