[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
@@ -660,15 +660,23 @@ def Clz : Builtin, BitShort_Int_Long_LongLongTemplate { let Prototype = "int(unsigned T)"; } -// FIXME: Add int clzimax(uintmax_t) +def Clzg : Builtin { + let Spellings = ["__builtin_clzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; nickdesaulniers wrote: Filed https://github.com/llvm/llvm-project/issues/86549 https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
@@ -660,15 +660,23 @@ def Clz : Builtin, BitShort_Int_Long_LongLongTemplate { let Prototype = "int(unsigned T)"; } -// FIXME: Add int clzimax(uintmax_t) +def Clzg : Builtin { + let Spellings = ["__builtin_clzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; overmighty wrote: I will implement `constexpr` support. It requires adding a few lines in clang/lib/AST/ExprConstant.cpp and clang/lib/AST/Interp/InterpBuiltin.cpp. https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
@@ -660,15 +660,23 @@ def Clz : Builtin, BitShort_Int_Long_LongLongTemplate { let Prototype = "int(unsigned T)"; } -// FIXME: Add int clzimax(uintmax_t) +def Clzg : Builtin { + let Spellings = ["__builtin_clzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; nickdesaulniers wrote: Should `Constexpr` be in this list of attributes? https://godbolt.org/z/Efd3EW5xE https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
overmighty wrote: I do need one of you to commit this for me. You're welcome. :) https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
https://github.com/nickdesaulniers closed https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
nickdesaulniers wrote: @overmighty do you need one of us to commit this for you? Thanks for working on it! https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
https://github.com/overmighty updated https://github.com/llvm/llvm-project/pull/83431 >From 5e37b3b2f57c7683686b8ac64aa1566855826a9f Mon Sep 17 00:00:00 2001 From: OverMighty Date: Thu, 29 Feb 2024 14:23:40 + Subject: [PATCH 1/3] [clang] Implement __builtin_{clzg,ctzg} Fixes #83075, fixes #83076. --- clang/docs/LanguageExtensions.rst | 41 clang/include/clang/Basic/Builtins.td | 12 ++ .../clang/Basic/DiagnosticSemaKinds.td| 15 +- clang/lib/CodeGen/CGBuiltin.cpp | 40 +++- clang/lib/Sema/SemaChecking.cpp | 53 + clang/test/CodeGen/builtins.c | 204 ++ clang/test/CodeGen/ubsan-builtin-checks.c | 6 + clang/test/Sema/builtin-popcountg.c | 23 -- clang/test/Sema/count-builtins.c | 87 9 files changed, 445 insertions(+), 36 deletions(-) delete mode 100644 clang/test/Sema/builtin-popcountg.c create mode 100644 clang/test/Sema/count-builtins.c diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index bcd69198eafdbe..3d73d772f698ba 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -3504,6 +3504,47 @@ argument can be of any unsigned integer type. ``__builtin_popcount{,l,ll}`` builtins, with support for other integer types, such as ``unsigned __int128`` and C23 ``unsigned _BitInt(N)``. +``__builtin_clzg`` and ``__builtin_ctzg`` +- + +``__builtin_clzg`` (respectively ``__builtin_ctzg``) returns the number of +leading (respectively trailing) 0 bits in the first argument. The first argument +can be of any unsigned integer type. + +If the first argument is 0 and an optional second argument of ``int`` type is +provided, then the second argument is returned. If the first argument is 0, but +only one argument is provided, then the returned value is undefined. + +**Syntax**: + +.. code-block:: c++ + + int __builtin_clzg(type x[, int fallback]) + int __builtin_ctzg(type x[, int fallback]) + +**Examples**: + +.. code-block:: c++ + + unsigned int x = 1; + int x_lz = __builtin_clzg(x); + int x_tz = __builtin_ctzg(x); + + unsigned long y = 2; + int y_lz = __builtin_clzg(y); + int y_tz = __builtin_ctzg(y); + + unsigned _BitInt(128) z = 4; + int z_lz = __builtin_clzg(z); + int z_tz = __builtin_ctzg(z); + +**Description**: + +``__builtin_clzg`` (respectively ``__builtin_ctzg``) is meant to be a +type-generic alternative to the ``__builtin_clz{,l,ll}`` (respectively +``__builtin_ctz{,l,ll}``) builtins, with support for other integer types, such +as ``unsigned __int128`` and C23 ``unsigned _BitInt(N)``. + Multiprecision Arithmetic Builtins -- diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index 2c83dca248fb7d..206ccaf1bdaa13 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -662,6 +662,12 @@ def Clz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int clzimax(uintmax_t) +def Clzg : Builtin { + let Spellings = ["__builtin_clzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; + let Prototype = "int(...)"; +} + def Ctz : Builtin, BitShort_Int_Long_LongLongTemplate { let Spellings = ["__builtin_ctz"]; let Attributes = [NoThrow, Const, Constexpr]; @@ -670,6 +676,12 @@ def Ctz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int ctzimax(uintmax_t) +def Ctzg : Builtin { + let Spellings = ["__builtin_ctzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; + let Prototype = "int(...)"; +} + def FFS : Builtin, BitInt_Long_LongLongTemplate { let Spellings = ["__builtin_ffs"]; let Attributes = [FunctionWithBuiltinPrefix, NoThrow, Const, Constexpr]; diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 91105d4231f06a..a65052e2b54f29 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11978,13 +11978,14 @@ def err_builtin_launder_invalid_arg : Error< "'__builtin_launder' is not allowed">; def err_builtin_invalid_arg_type: Error < - "%ordinal0 argument must be a " - "%select{vector, integer or floating point type|matrix|" - "pointer to a valid matrix element type|" - "signed integer or floating point type|vector type|" - "floating point type|" - "vector of integers|" - "type of unsigned integer}1 (was %2)">; + "%ordinal0 argument must be " + "%select{a vector, integer or floating point type|a matrix|" + "a pointer to a valid matrix element type|" + "a signed integer or floating point type|a vector type|" + "a floating point type|" + "a vector of integers|" + "an unsigned integer|" + "an 'int'}1 (was %2)">; def err_builtin_matrix_disabled: Error< "matrix types extension is disabled. Pass -fenable-matrix to
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
https://github.com/efriedma-quic approved this pull request. LGTM with one minor comment https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
https://github.com/efriedma-quic edited https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
@@ -3504,6 +3504,47 @@ argument can be of any unsigned integer type. ``__builtin_popcount{,l,ll}`` builtins, with support for other integer types, such as ``unsigned __int128`` and C23 ``unsigned _BitInt(N)``. +``__builtin_clzg`` and ``__builtin_ctzg`` +- + +``__builtin_clzg`` (respectively ``__builtin_ctzg``) returns the number of +leading (respectively trailing) 0 bits in the first argument. The first argument +can be of any unsigned integer type. + +If the first argument is 0 and an optional second argument of ``int`` type is +provided, then the second argument is returned. If the first argument is 0, but +only one argument is provided, then the returned value is undefined. efriedma-quic wrote: Probably should say "the behavior is undefined", to match the C standard wording for this sort of thing. (The IR intrinsic returns poison, but "poison" isn't a C language concept.) https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
nickdesaulniers wrote: @efriedma-quic parting thoughts here? https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
overmighty wrote: cc @nickdesaulniers https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
@@ -3157,7 +3177,15 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, if (Result->getType() != ResultType) overmighty wrote: Right. Is it worth making tests for this with a target where `isCLZForZeroUndef()` is false? https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
@@ -2212,6 +2212,54 @@ static bool SemaBuiltinPopcountg(Sema , CallExpr *TheCall) { return false; } +/// Checks that __builtin_{clzg,ctzg} was called with a first argument, which is +/// an unsigned integer, and an optional second argument, which is promoted to +/// an 'int'. +static bool SemaBuiltinCountZeroBitsGeneric(Sema , CallExpr *TheCall) { + if (checkArgCountRange(S, TheCall, 1, 2)) +return true; + + ExprResult Arg0Res = S.DefaultLvalueConversion(TheCall->getArg(0)); + if (Arg0Res.isInvalid()) +return true; + + Expr *Arg0 = Arg0Res.get(); + TheCall->setArg(0, Arg0); + + QualType Arg0Ty = Arg0->getType(); + + if (!Arg0Ty->isUnsignedIntegerType()) { +S.Diag(Arg0->getBeginLoc(), diag::err_builtin_invalid_arg_type) +<< 1 << /*unsigned integer ty*/ 7 << Arg0Ty; +return true; + } + + if (TheCall->getNumArgs() > 1) { +ExprResult Arg1Res = S.DefaultLvalueConversion(TheCall->getArg(1)); +if (Arg1Res.isInvalid()) + return true; + +Expr *Arg1 = Arg1Res.get(); +TheCall->setArg(1, Arg1); + +QualType Arg1Ty = Arg1->getType(); + +if (S.Context.isPromotableIntegerType(Arg1Ty)) { overmighty wrote: I had forgotten about `Sema::UsualUnaryConversions`, I think that's what I want. https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
https://github.com/overmighty updated https://github.com/llvm/llvm-project/pull/83431 >From 5e37b3b2f57c7683686b8ac64aa1566855826a9f Mon Sep 17 00:00:00 2001 From: OverMighty Date: Thu, 29 Feb 2024 14:23:40 + Subject: [PATCH 1/2] [clang] Implement __builtin_{clzg,ctzg} Fixes #83075, fixes #83076. --- clang/docs/LanguageExtensions.rst | 41 clang/include/clang/Basic/Builtins.td | 12 ++ .../clang/Basic/DiagnosticSemaKinds.td| 15 +- clang/lib/CodeGen/CGBuiltin.cpp | 40 +++- clang/lib/Sema/SemaChecking.cpp | 53 + clang/test/CodeGen/builtins.c | 204 ++ clang/test/CodeGen/ubsan-builtin-checks.c | 6 + clang/test/Sema/builtin-popcountg.c | 23 -- clang/test/Sema/count-builtins.c | 87 9 files changed, 445 insertions(+), 36 deletions(-) delete mode 100644 clang/test/Sema/builtin-popcountg.c create mode 100644 clang/test/Sema/count-builtins.c diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index bcd69198eafdbe..3d73d772f698ba 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -3504,6 +3504,47 @@ argument can be of any unsigned integer type. ``__builtin_popcount{,l,ll}`` builtins, with support for other integer types, such as ``unsigned __int128`` and C23 ``unsigned _BitInt(N)``. +``__builtin_clzg`` and ``__builtin_ctzg`` +- + +``__builtin_clzg`` (respectively ``__builtin_ctzg``) returns the number of +leading (respectively trailing) 0 bits in the first argument. The first argument +can be of any unsigned integer type. + +If the first argument is 0 and an optional second argument of ``int`` type is +provided, then the second argument is returned. If the first argument is 0, but +only one argument is provided, then the returned value is undefined. + +**Syntax**: + +.. code-block:: c++ + + int __builtin_clzg(type x[, int fallback]) + int __builtin_ctzg(type x[, int fallback]) + +**Examples**: + +.. code-block:: c++ + + unsigned int x = 1; + int x_lz = __builtin_clzg(x); + int x_tz = __builtin_ctzg(x); + + unsigned long y = 2; + int y_lz = __builtin_clzg(y); + int y_tz = __builtin_ctzg(y); + + unsigned _BitInt(128) z = 4; + int z_lz = __builtin_clzg(z); + int z_tz = __builtin_ctzg(z); + +**Description**: + +``__builtin_clzg`` (respectively ``__builtin_ctzg``) is meant to be a +type-generic alternative to the ``__builtin_clz{,l,ll}`` (respectively +``__builtin_ctz{,l,ll}``) builtins, with support for other integer types, such +as ``unsigned __int128`` and C23 ``unsigned _BitInt(N)``. + Multiprecision Arithmetic Builtins -- diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index 2c83dca248fb7d..206ccaf1bdaa13 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -662,6 +662,12 @@ def Clz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int clzimax(uintmax_t) +def Clzg : Builtin { + let Spellings = ["__builtin_clzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; + let Prototype = "int(...)"; +} + def Ctz : Builtin, BitShort_Int_Long_LongLongTemplate { let Spellings = ["__builtin_ctz"]; let Attributes = [NoThrow, Const, Constexpr]; @@ -670,6 +676,12 @@ def Ctz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int ctzimax(uintmax_t) +def Ctzg : Builtin { + let Spellings = ["__builtin_ctzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; + let Prototype = "int(...)"; +} + def FFS : Builtin, BitInt_Long_LongLongTemplate { let Spellings = ["__builtin_ffs"]; let Attributes = [FunctionWithBuiltinPrefix, NoThrow, Const, Constexpr]; diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 91105d4231f06a..a65052e2b54f29 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11978,13 +11978,14 @@ def err_builtin_launder_invalid_arg : Error< "'__builtin_launder' is not allowed">; def err_builtin_invalid_arg_type: Error < - "%ordinal0 argument must be a " - "%select{vector, integer or floating point type|matrix|" - "pointer to a valid matrix element type|" - "signed integer or floating point type|vector type|" - "floating point type|" - "vector of integers|" - "type of unsigned integer}1 (was %2)">; + "%ordinal0 argument must be " + "%select{a vector, integer or floating point type|a matrix|" + "a pointer to a valid matrix element type|" + "a signed integer or floating point type|a vector type|" + "a floating point type|" + "a vector of integers|" + "an unsigned integer|" + "an 'int'}1 (was %2)">; def err_builtin_matrix_disabled: Error< "matrix types extension is disabled. Pass -fenable-matrix to
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
@@ -3157,7 +3177,15 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, if (Result->getType() != ResultType) efriedma-quic wrote: I was more thinking of the opposite: we don't need to make the result of the clz defined if we're not using it. https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
@@ -3157,7 +3177,15 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, if (Result->getType() != ResultType) overmighty wrote: Since in the additions below I check if the argument itself is zero instead of checking the result of `ctlz`, I don't see why the `ctlz` result for zero being undefined would be a problem. https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
@@ -3157,7 +3177,15 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, if (Result->getType() != ResultType) efriedma-quic wrote: Not sure if the way this is handling isCLZForZeroUndef() is what you want? https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
@@ -2212,6 +2212,54 @@ static bool SemaBuiltinPopcountg(Sema , CallExpr *TheCall) { return false; } +/// Checks that __builtin_{clzg,ctzg} was called with a first argument, which is +/// an unsigned integer, and an optional second argument, which is promoted to +/// an 'int'. +static bool SemaBuiltinCountZeroBitsGeneric(Sema , CallExpr *TheCall) { + if (checkArgCountRange(S, TheCall, 1, 2)) +return true; + + ExprResult Arg0Res = S.DefaultLvalueConversion(TheCall->getArg(0)); + if (Arg0Res.isInvalid()) +return true; + + Expr *Arg0 = Arg0Res.get(); + TheCall->setArg(0, Arg0); + + QualType Arg0Ty = Arg0->getType(); + + if (!Arg0Ty->isUnsignedIntegerType()) { +S.Diag(Arg0->getBeginLoc(), diag::err_builtin_invalid_arg_type) +<< 1 << /*unsigned integer ty*/ 7 << Arg0Ty; +return true; + } + + if (TheCall->getNumArgs() > 1) { +ExprResult Arg1Res = S.DefaultLvalueConversion(TheCall->getArg(1)); +if (Arg1Res.isInvalid()) + return true; + +Expr *Arg1 = Arg1Res.get(); +TheCall->setArg(1, Arg1); + +QualType Arg1Ty = Arg1->getType(); + +if (S.Context.isPromotableIntegerType(Arg1Ty)) { efriedma-quic wrote: This isn't quite the same as the way promotion normally works for arguments; it forbids some conversions we would normally do. Maybe that's fine? Maybe worth stating a bit more explicitly in the documentation. https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
@@ -662,6 +662,12 @@ def Clz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int clzimax(uintmax_t) efriedma-quic wrote: Probably; it's unlikely we're going to add them at this point. https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
@@ -662,6 +662,12 @@ def Clz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int clzimax(uintmax_t) overmighty wrote: Should this FIXME and the `ctzimax(uintmax_t)` one below be removed? https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
https://github.com/overmighty edited https://github.com/llvm/llvm-project/pull/83431 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
https://github.com/overmighty updated https://github.com/llvm/llvm-project/pull/83431 >From 580ee1d6f9807e02762a4d5e25fc9306aab9ddde Mon Sep 17 00:00:00 2001 From: OverMighty Date: Thu, 29 Feb 2024 14:23:40 + Subject: [PATCH] [clang] Implement __builtin_{clzg,ctzg} Fixes #83075, fixes #83076. --- clang/docs/LanguageExtensions.rst | 41 clang/include/clang/Basic/Builtins.td | 12 ++ .../clang/Basic/DiagnosticSemaKinds.td| 15 +- clang/lib/CodeGen/CGBuiltin.cpp | 40 +++- clang/lib/Sema/SemaChecking.cpp | 53 + clang/test/CodeGen/builtins.c | 204 ++ clang/test/CodeGen/ubsan-builtin-checks.c | 6 + clang/test/Sema/builtin-popcountg.c | 23 -- clang/test/Sema/count-builtins.c | 87 9 files changed, 445 insertions(+), 36 deletions(-) delete mode 100644 clang/test/Sema/builtin-popcountg.c create mode 100644 clang/test/Sema/count-builtins.c diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index bcd69198eafdbe..3d73d772f698ba 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -3504,6 +3504,47 @@ argument can be of any unsigned integer type. ``__builtin_popcount{,l,ll}`` builtins, with support for other integer types, such as ``unsigned __int128`` and C23 ``unsigned _BitInt(N)``. +``__builtin_clzg`` and ``__builtin_ctzg`` +- + +``__builtin_clzg`` (respectively ``__builtin_ctzg``) returns the number of +leading (respectively trailing) 0 bits in the first argument. The first argument +can be of any unsigned integer type. + +If the first argument is 0 and an optional second argument of ``int`` type is +provided, then the second argument is returned. If the first argument is 0, but +only one argument is provided, then the returned value is undefined. + +**Syntax**: + +.. code-block:: c++ + + int __builtin_clzg(type x[, int fallback]) + int __builtin_ctzg(type x[, int fallback]) + +**Examples**: + +.. code-block:: c++ + + unsigned int x = 1; + int x_lz = __builtin_clzg(x); + int x_tz = __builtin_ctzg(x); + + unsigned long y = 2; + int y_lz = __builtin_clzg(y); + int y_tz = __builtin_ctzg(y); + + unsigned _BitInt(128) z = 4; + int z_lz = __builtin_clzg(z); + int z_tz = __builtin_ctzg(z); + +**Description**: + +``__builtin_clzg`` (respectively ``__builtin_ctzg``) is meant to be a +type-generic alternative to the ``__builtin_clz{,l,ll}`` (respectively +``__builtin_ctz{,l,ll}``) builtins, with support for other integer types, such +as ``unsigned __int128`` and C23 ``unsigned _BitInt(N)``. + Multiprecision Arithmetic Builtins -- diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index 2fbc56d49a59a1..ec5a8819ed4057 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -662,6 +662,12 @@ def Clz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int clzimax(uintmax_t) +def Clzg : Builtin { + let Spellings = ["__builtin_clzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; + let Prototype = "int(...)"; +} + def Ctz : Builtin, BitShort_Int_Long_LongLongTemplate { let Spellings = ["__builtin_ctz"]; let Attributes = [NoThrow, Const, Constexpr]; @@ -670,6 +676,12 @@ def Ctz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int ctzimax(uintmax_t) +def Ctzg : Builtin { + let Spellings = ["__builtin_ctzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; + let Prototype = "int(...)"; +} + def FFS : Builtin, BitInt_Long_LongLongTemplate { let Spellings = ["__builtin_ffs"]; let Attributes = [FunctionWithBuiltinPrefix, NoThrow, Const, Constexpr]; diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index f726805dc02bd9..d2a18b7bb9cfdd 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11978,13 +11978,14 @@ def err_builtin_launder_invalid_arg : Error< "'__builtin_launder' is not allowed">; def err_builtin_invalid_arg_type: Error < - "%ordinal0 argument must be a " - "%select{vector, integer or floating point type|matrix|" - "pointer to a valid matrix element type|" - "signed integer or floating point type|vector type|" - "floating point type|" - "vector of integers|" - "type of unsigned integer}1 (was %2)">; + "%ordinal0 argument must be " + "%select{a vector, integer or floating point type|a matrix|" + "a pointer to a valid matrix element type|" + "a signed integer or floating point type|a vector type|" + "a floating point type|" + "a vector of integers|" + "an unsigned integer|" + "an 'int'}1 (was %2)">; def err_builtin_matrix_disabled: Error< "matrix types extension is disabled. Pass -fenable-matrix to enable
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
llvmbot wrote: @llvm/pr-subscribers-clang Author: OverMighty (overmighty) Changes Fixes #83075, #83076. --- Patch is 26.95 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/83431.diff 9 Files Affected: - (modified) clang/docs/LanguageExtensions.rst (+41) - (modified) clang/include/clang/Basic/Builtins.td (+12) - (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+8-7) - (modified) clang/lib/CodeGen/CGBuiltin.cpp (+34-6) - (modified) clang/lib/Sema/SemaChecking.cpp (+53) - (modified) clang/test/CodeGen/builtins.c (+204) - (modified) clang/test/CodeGen/ubsan-builtin-checks.c (+6) - (removed) clang/test/Sema/builtin-popcountg.c (-23) - (added) clang/test/Sema/count-builtins.c (+87) ``diff diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index bcd69198eafdbe..3d73d772f698ba 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -3504,6 +3504,47 @@ argument can be of any unsigned integer type. ``__builtin_popcount{,l,ll}`` builtins, with support for other integer types, such as ``unsigned __int128`` and C23 ``unsigned _BitInt(N)``. +``__builtin_clzg`` and ``__builtin_ctzg`` +- + +``__builtin_clzg`` (respectively ``__builtin_ctzg``) returns the number of +leading (respectively trailing) 0 bits in the first argument. The first argument +can be of any unsigned integer type. + +If the first argument is 0 and an optional second argument of ``int`` type is +provided, then the second argument is returned. If the first argument is 0, but +only one argument is provided, then the returned value is undefined. + +**Syntax**: + +.. code-block:: c++ + + int __builtin_clzg(type x[, int fallback]) + int __builtin_ctzg(type x[, int fallback]) + +**Examples**: + +.. code-block:: c++ + + unsigned int x = 1; + int x_lz = __builtin_clzg(x); + int x_tz = __builtin_ctzg(x); + + unsigned long y = 2; + int y_lz = __builtin_clzg(y); + int y_tz = __builtin_ctzg(y); + + unsigned _BitInt(128) z = 4; + int z_lz = __builtin_clzg(z); + int z_tz = __builtin_ctzg(z); + +**Description**: + +``__builtin_clzg`` (respectively ``__builtin_ctzg``) is meant to be a +type-generic alternative to the ``__builtin_clz{,l,ll}`` (respectively +``__builtin_ctz{,l,ll}``) builtins, with support for other integer types, such +as ``unsigned __int128`` and C23 ``unsigned _BitInt(N)``. + Multiprecision Arithmetic Builtins -- diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index 2fbc56d49a59a1..ec5a8819ed4057 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -662,6 +662,12 @@ def Clz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int clzimax(uintmax_t) +def Clzg : Builtin { + let Spellings = ["__builtin_clzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; + let Prototype = "int(...)"; +} + def Ctz : Builtin, BitShort_Int_Long_LongLongTemplate { let Spellings = ["__builtin_ctz"]; let Attributes = [NoThrow, Const, Constexpr]; @@ -670,6 +676,12 @@ def Ctz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int ctzimax(uintmax_t) +def Ctzg : Builtin { + let Spellings = ["__builtin_ctzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; + let Prototype = "int(...)"; +} + def FFS : Builtin, BitInt_Long_LongLongTemplate { let Spellings = ["__builtin_ffs"]; let Attributes = [FunctionWithBuiltinPrefix, NoThrow, Const, Constexpr]; diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index f726805dc02bd9..d2a18b7bb9cfdd 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11978,13 +11978,14 @@ def err_builtin_launder_invalid_arg : Error< "'__builtin_launder' is not allowed">; def err_builtin_invalid_arg_type: Error < - "%ordinal0 argument must be a " - "%select{vector, integer or floating point type|matrix|" - "pointer to a valid matrix element type|" - "signed integer or floating point type|vector type|" - "floating point type|" - "vector of integers|" - "type of unsigned integer}1 (was %2)">; + "%ordinal0 argument must be " + "%select{a vector, integer or floating point type|a matrix|" + "a pointer to a valid matrix element type|" + "a signed integer or floating point type|a vector type|" + "a floating point type|" + "a vector of integers|" + "an unsigned integer|" + "an 'int'}1 (was %2)">; def err_builtin_matrix_disabled: Error< "matrix types extension is disabled. Pass -fenable-matrix to enable it">; diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 2d16e7cdc06053..bd7cf9ae79f690 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -3128,8
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
llvmbot wrote: @llvm/pr-subscribers-clang-codegen Author: OverMighty (overmighty) Changes Fixes #83075, #83076. --- Patch is 26.95 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/83431.diff 9 Files Affected: - (modified) clang/docs/LanguageExtensions.rst (+41) - (modified) clang/include/clang/Basic/Builtins.td (+12) - (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+8-7) - (modified) clang/lib/CodeGen/CGBuiltin.cpp (+34-6) - (modified) clang/lib/Sema/SemaChecking.cpp (+53) - (modified) clang/test/CodeGen/builtins.c (+204) - (modified) clang/test/CodeGen/ubsan-builtin-checks.c (+6) - (removed) clang/test/Sema/builtin-popcountg.c (-23) - (added) clang/test/Sema/count-builtins.c (+87) ``diff diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index bcd69198eafdbe..3d73d772f698ba 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -3504,6 +3504,47 @@ argument can be of any unsigned integer type. ``__builtin_popcount{,l,ll}`` builtins, with support for other integer types, such as ``unsigned __int128`` and C23 ``unsigned _BitInt(N)``. +``__builtin_clzg`` and ``__builtin_ctzg`` +- + +``__builtin_clzg`` (respectively ``__builtin_ctzg``) returns the number of +leading (respectively trailing) 0 bits in the first argument. The first argument +can be of any unsigned integer type. + +If the first argument is 0 and an optional second argument of ``int`` type is +provided, then the second argument is returned. If the first argument is 0, but +only one argument is provided, then the returned value is undefined. + +**Syntax**: + +.. code-block:: c++ + + int __builtin_clzg(type x[, int fallback]) + int __builtin_ctzg(type x[, int fallback]) + +**Examples**: + +.. code-block:: c++ + + unsigned int x = 1; + int x_lz = __builtin_clzg(x); + int x_tz = __builtin_ctzg(x); + + unsigned long y = 2; + int y_lz = __builtin_clzg(y); + int y_tz = __builtin_ctzg(y); + + unsigned _BitInt(128) z = 4; + int z_lz = __builtin_clzg(z); + int z_tz = __builtin_ctzg(z); + +**Description**: + +``__builtin_clzg`` (respectively ``__builtin_ctzg``) is meant to be a +type-generic alternative to the ``__builtin_clz{,l,ll}`` (respectively +``__builtin_ctz{,l,ll}``) builtins, with support for other integer types, such +as ``unsigned __int128`` and C23 ``unsigned _BitInt(N)``. + Multiprecision Arithmetic Builtins -- diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index 2fbc56d49a59a1..ec5a8819ed4057 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -662,6 +662,12 @@ def Clz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int clzimax(uintmax_t) +def Clzg : Builtin { + let Spellings = ["__builtin_clzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; + let Prototype = "int(...)"; +} + def Ctz : Builtin, BitShort_Int_Long_LongLongTemplate { let Spellings = ["__builtin_ctz"]; let Attributes = [NoThrow, Const, Constexpr]; @@ -670,6 +676,12 @@ def Ctz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int ctzimax(uintmax_t) +def Ctzg : Builtin { + let Spellings = ["__builtin_ctzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; + let Prototype = "int(...)"; +} + def FFS : Builtin, BitInt_Long_LongLongTemplate { let Spellings = ["__builtin_ffs"]; let Attributes = [FunctionWithBuiltinPrefix, NoThrow, Const, Constexpr]; diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index f726805dc02bd9..d2a18b7bb9cfdd 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11978,13 +11978,14 @@ def err_builtin_launder_invalid_arg : Error< "'__builtin_launder' is not allowed">; def err_builtin_invalid_arg_type: Error < - "%ordinal0 argument must be a " - "%select{vector, integer or floating point type|matrix|" - "pointer to a valid matrix element type|" - "signed integer or floating point type|vector type|" - "floating point type|" - "vector of integers|" - "type of unsigned integer}1 (was %2)">; + "%ordinal0 argument must be " + "%select{a vector, integer or floating point type|a matrix|" + "a pointer to a valid matrix element type|" + "a signed integer or floating point type|a vector type|" + "a floating point type|" + "a vector of integers|" + "an unsigned integer|" + "an 'int'}1 (was %2)">; def err_builtin_matrix_disabled: Error< "matrix types extension is disabled. Pass -fenable-matrix to enable it">; diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 2d16e7cdc06053..bd7cf9ae79f690 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@
[clang] [clang] Implement __builtin_{clzg,ctzg} (PR #83431)
https://github.com/overmighty created https://github.com/llvm/llvm-project/pull/83431 Fixes #83075, #83076. >From 3dbfd1fb21ef0ddccef27e1ceffce16dc298e1cf Mon Sep 17 00:00:00 2001 From: OverMighty Date: Thu, 29 Feb 2024 14:23:40 + Subject: [PATCH] [clang] Implement __builtin_{clzg,ctzg} Fixes #83075, #83076. --- clang/docs/LanguageExtensions.rst | 41 clang/include/clang/Basic/Builtins.td | 12 ++ .../clang/Basic/DiagnosticSemaKinds.td| 15 +- clang/lib/CodeGen/CGBuiltin.cpp | 40 +++- clang/lib/Sema/SemaChecking.cpp | 53 + clang/test/CodeGen/builtins.c | 204 ++ clang/test/CodeGen/ubsan-builtin-checks.c | 6 + clang/test/Sema/builtin-popcountg.c | 23 -- clang/test/Sema/count-builtins.c | 87 9 files changed, 445 insertions(+), 36 deletions(-) delete mode 100644 clang/test/Sema/builtin-popcountg.c create mode 100644 clang/test/Sema/count-builtins.c diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index bcd69198eafdbe..3d73d772f698ba 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -3504,6 +3504,47 @@ argument can be of any unsigned integer type. ``__builtin_popcount{,l,ll}`` builtins, with support for other integer types, such as ``unsigned __int128`` and C23 ``unsigned _BitInt(N)``. +``__builtin_clzg`` and ``__builtin_ctzg`` +- + +``__builtin_clzg`` (respectively ``__builtin_ctzg``) returns the number of +leading (respectively trailing) 0 bits in the first argument. The first argument +can be of any unsigned integer type. + +If the first argument is 0 and an optional second argument of ``int`` type is +provided, then the second argument is returned. If the first argument is 0, but +only one argument is provided, then the returned value is undefined. + +**Syntax**: + +.. code-block:: c++ + + int __builtin_clzg(type x[, int fallback]) + int __builtin_ctzg(type x[, int fallback]) + +**Examples**: + +.. code-block:: c++ + + unsigned int x = 1; + int x_lz = __builtin_clzg(x); + int x_tz = __builtin_ctzg(x); + + unsigned long y = 2; + int y_lz = __builtin_clzg(y); + int y_tz = __builtin_ctzg(y); + + unsigned _BitInt(128) z = 4; + int z_lz = __builtin_clzg(z); + int z_tz = __builtin_ctzg(z); + +**Description**: + +``__builtin_clzg`` (respectively ``__builtin_ctzg``) is meant to be a +type-generic alternative to the ``__builtin_clz{,l,ll}`` (respectively +``__builtin_ctz{,l,ll}``) builtins, with support for other integer types, such +as ``unsigned __int128`` and C23 ``unsigned _BitInt(N)``. + Multiprecision Arithmetic Builtins -- diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index 2fbc56d49a59a1..ec5a8819ed4057 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -662,6 +662,12 @@ def Clz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int clzimax(uintmax_t) +def Clzg : Builtin { + let Spellings = ["__builtin_clzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; + let Prototype = "int(...)"; +} + def Ctz : Builtin, BitShort_Int_Long_LongLongTemplate { let Spellings = ["__builtin_ctz"]; let Attributes = [NoThrow, Const, Constexpr]; @@ -670,6 +676,12 @@ def Ctz : Builtin, BitShort_Int_Long_LongLongTemplate { // FIXME: Add int ctzimax(uintmax_t) +def Ctzg : Builtin { + let Spellings = ["__builtin_ctzg"]; + let Attributes = [NoThrow, Const, CustomTypeChecking]; + let Prototype = "int(...)"; +} + def FFS : Builtin, BitInt_Long_LongLongTemplate { let Spellings = ["__builtin_ffs"]; let Attributes = [FunctionWithBuiltinPrefix, NoThrow, Const, Constexpr]; diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index f726805dc02bd9..d2a18b7bb9cfdd 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11978,13 +11978,14 @@ def err_builtin_launder_invalid_arg : Error< "'__builtin_launder' is not allowed">; def err_builtin_invalid_arg_type: Error < - "%ordinal0 argument must be a " - "%select{vector, integer or floating point type|matrix|" - "pointer to a valid matrix element type|" - "signed integer or floating point type|vector type|" - "floating point type|" - "vector of integers|" - "type of unsigned integer}1 (was %2)">; + "%ordinal0 argument must be " + "%select{a vector, integer or floating point type|a matrix|" + "a pointer to a valid matrix element type|" + "a signed integer or floating point type|a vector type|" + "a floating point type|" + "a vector of integers|" + "an unsigned integer|" + "an 'int'}1 (was %2)">; def err_builtin_matrix_disabled: Error< "matrix types extension is disabled. Pass