On Fri, Oct 30, 2026 at 07:45:45PM +0100, Jakub Jelinek wrote:
> Hi!
>
> On Wed, Oct 28, 2026 at 01:48:35PM +0100, Richard Biener wrote:
> > The documentation might benefit from one of your examples above
> > being added.
>
> I've added example how to use the builtin into the docs now.
>
> On Thu, Oct 29, 2026 at 06:12:06PM -0400, Jason Merrill wrote:
> > On 10/28/26 6:48 AM, Jakub Jelinek wrote:
> > > --- a/gcc/cp/constexpr.cc 2026-10-23 15:12:24.052873710 +0200
> > > +++ b/gcc/cp/constexpr.cc 2026-10-27 08:41:24.586219933 +0100
> > > + location_t loc = cp_expr_loc_or_input_loc (t);
> > > + if (TREE_CODE (args[0]) != INTEGER_CST
> > > + || TREE_CODE (args[1]) != INTEGER_CST
> > > + || !INTEGRAL_TYPE_P (TREE_TYPE (args[0]))
> > > + || !TYPE_UNSIGNED (TREE_TYPE (args[0]))
> > > + || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (args[0])),
> > > + TYPE_MAIN_VARIANT (TREE_TYPE (args[1])))
> > > + || (args[2] && !POINTER_TYPE_P (TREE_TYPE (args[2]))))
> >
> > Aren't the types checked when building the expression in the first place?
>
> It is checked in build_x_clmulg when none of the operands are
> type-dependent, otherwise it creates the internal fn call with
> build_min_nt_call_vec. I was afraid fold_non_dependent_expr might
> have this called even in that case, but thinking about it again,
> I really hope it shouldn't, such call is type dependent...
>
> > > + tree mod = build2_loc (loc, MODIFY_EXPR, void_type_node,
> > > + build_fold_addr_expr_loc (loc, args[2]),
> >
> > Shouldn't this be build_fold_indirect_ref_loc?
>
> You're right, fixed.
>
> > It looks like there are no constexpr tests?
>
> Yeah, the 2 argument calls are actually tested already in various constant
> expressions and asserts, but indeed the 3 argument one wasn't and the
> above spot is about the 3 argument case.
>
> I've added a test for this now.
>
> Additionally, I've added another test for x86, basically compiling and
> running the main new test with -O2 -mpclmul rather than -O2 (+ making sure
> it doesn't fail if the ISA isn't supported).
> That revealed another bug, one of the conditions guarding the use of
> the non-widening optab was bogus.
> Earlier
> if (prec0 > this_prec || prec1 > this_prec)
> continue;
> already guarantees that both the operands can be cast to cmode without
> losing any non-zero bits, and in
> if (optab_handler (clmul_optab, cmode) != CODE_FOR_nothing
> && (prec0 + prec1 - 1 <= this_prec || prec0 + prec1 - 1 <= prec))
> the prec0 + prec1 - 1 <= this_prec check is correct, we can use the
> non-widening hw insn with this_prec x this_prec -> this_prec bits
> if the result clearly won't need bits beyond this_prec, but
> prec0 + prec1 - 1 <= prec is bogus, that is true or false regardless of
> cmode/this_prec we pick and just says that the whole expand_clmul is
> computing all non-zero bits rather than just subset of those.
> We want prec <= this_prec instead (i.e. whether cmode gives us everything
> we need). And can use it later on for the widening case where it just uses
> the low half too.
>
> So far lightly tested.
>
> 2026-10-30 Jakub Jelinek <[email protected]>
>
> PR middle-end/128111
> * doc/extend.texi (__builtin_clmulg): Document.
> * builtins.h (fold_build_builtin_clmulg): Declare.
> * fold-const-call.cc (fold_const_call_sss): Handle CFN_CLMUL.
> (fold_const_call): Handle CFN_CLMUL_WIDEN.
> * internal-fn.h (expand_CLMUL, expand_CLMUL_WIDEN): Declare.
> * fold-const.cc (tree_call_nonnegative_p): Handle CFN_CLMUL.
> * wide-int.cc (wi::bit_repeat, wi::clmul): New functions.
> * wide-int.h (wi::bit_repeat, wi::clmul): Declare.
> * builtins.cc (fold_build_builtin_clmulg): New function.
> * internal-fn.def (CLMUL, CLMUL_WIDEN): New internal functions.
> * internal-fn.cc (commutative_binary_fn_p): Handle IFN_CLMUL
> and IFN_CLMUL_WIDEN.
> (expand_clmul, expand_CLMUL, expand_CLMUL_WIDEN): New functions.
> * optabs.def (clmul_optab, clmul_widen_optab): New optabs.
> * gimple-lower-bitint.cc (struct bitint_large_huge): Declare
> lower_clmul method.
> (bitint_large_huge::lower_clmul): New method.
> (bitint_large_huge::lower_call): Handle IFN_CLMUL.
> (bitint_large_huge::lower_stmt): Likewise.
> (stmt_needs_operand_addr): Likewise.
> (build_bitint_stmt_ssa_conflicts): Likewise.
> (gimple_lower_bitint): Lower IFN_CLMUL_WIDEN to 2 IFN_CLMUL
> calls, 3 IFN_BITREVERSE calls, one shift and one COMPLEX_EXPR.
> Handle IFN_CLMUL.
> * match.pd (.CLMUL (X, Y) for [0, 1] Y -> X * Y,
> (.CLMUL_WIDEN (X, Y) -> { .CLMUL (X, 0), 0 } if upper bits are
> known to be zeros): New simplifications.
> * genmatch.cc (genmatch_diag_selftests):
> (commutative_op): Handle CFN_CLMUL and CFN_CLMUL_WIDEN.
> * config/i386/sse.md (clmuldi3, clmul_widendi3): New expanders.
> gcc/c-family/
> * c-common.h (enum rid): Add RID_BUILTIN_CLMULG enumerator.
> * c-common.cc (c_common_reswords): Add __builtin_clmulg entry.
> gcc/c/
> * c-parser.cc (c_parser_postfix_expression): Handle
> RID_BUILTIN_CLMULG.
> * c-decl.cc (names_builtin_p): Handle RID_BUILTIN_CLMULG.
> gcc/cp/
> * parser.cc (cp_parser_postfix_expression): Handle RID_BUILTIN_CLMULG.
> * typeck.cc (build_x_clmulg): New function.
> * cp-tree.h (build_x_clmulg): Declare.
> * pt.cc (tsubst_expr): Handle IFN_CLMUL.
> * mangle.cc (write_expression): Likewise.
> * cp-objcp-common.cc (names_builtin_p): Handle RID_BUILTIN_CLMULG.
> * cp-gimplify.cc (cp_gimplify_expr): Handle IFN_CLMUL.
> * constexpr.cc (cxx_eval_internal_function): Likewise.
> (potential_constant_expression_1): Likewise.
> gcc/testsuite/
> * c-c++-common/builtin-clmulg-1.c: New test.
> * c-c++-common/builtin-clmulg-2.c: New test.
> * g++.dg/ext/builtin-clmulg-1.C: New test.
> * gcc.target/i386/pclmul-builtin-clmulg-1.c: New test.
>
> --- a/gcc/c-family/c-common.h 2026-10-27 14:32:12.567304216 +0100
> +++ b/gcc/c-family/c-common.h 2026-10-30 17:06:34.328025285 +0100
> @@ -112,7 +112,7 @@ enum rid
> RID_BUILTIN_SHUFFLEVECTOR, RID_BUILTIN_CONVERTVECTOR,
> RID_BUILTIN_TGMATH,
> RID_BUILTIN_HAS_ATTRIBUTE, RID_BUILTIN_ASSOC_BARRIER, RID_BUILTIN_STDC,
> RID_BUILTIN_COUNTED_BY_REF, RID_BUILTIN_BSWAPG,
> - RID_BUILTIN_BITREVERSEG,
> + RID_BUILTIN_BITREVERSEG, RID_BUILTIN_CLMULG,
> RID_DFLOAT32, RID_DFLOAT64, RID_DFLOAT128, RID_DFLOAT64X,
>
> /* TS 18661-3 keywords, in the same sequence as the TI_* values. */
> --- a/gcc/c-family/c-common.cc 2026-10-30 17:05:21.661552875 +0100
> +++ b/gcc/c-family/c-common.cc 2026-10-30 17:06:34.328833881 +0100
> @@ -439,6 +439,7 @@ const struct c_common_resword c_common_r
> { "__builtin_call_with_static_chain",
> RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
> { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
> + { "__builtin_clmulg", RID_BUILTIN_CLMULG, 0 },
> { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
> { "__builtin_convertvector", RID_BUILTIN_CONVERTVECTOR, 0 },
> { "__builtin_counted_by_ref", RID_BUILTIN_COUNTED_BY_REF, D_CONLY },
> --- a/gcc/c/c-parser.cc 2026-10-27 14:32:12.572304150 +0100
> +++ b/gcc/c/c-parser.cc 2026-10-30 17:06:34.330615332 +0100
> @@ -13556,15 +13556,29 @@ c_parser_postfix_expression (c_parser *p
> }
> case RID_BUILTIN_BSWAPG:
> case RID_BUILTIN_BITREVERSEG:
> + case RID_BUILTIN_CLMULG:
> {
> vec<c_expr_t, va_gc> *cexpr_list;
> location_t close_paren_loc;
> - internal_fn ifn
> - = (c_parser_peek_token (parser)->keyword == RID_BUILTIN_BSWAPG
> - ? IFN_BSWAP : IFN_BITREVERSE);
> - const char *name
> - = (ifn == IFN_BSWAP ? "__builtin_bswapg"
> - : "__builtin_bitreverseg");
> + internal_fn ifn;
> + const char *name;
> + switch (c_parser_peek_token (parser)->keyword)
> + {
> + case RID_BUILTIN_BSWAPG:
> + ifn = IFN_BSWAP;
> + name = "__builtin_bswapg";
> + break;
> + case RID_BUILTIN_BITREVERSEG:
> + ifn = IFN_BITREVERSE;
> + name = "__builtin_bitreverseg";
> + break;
> + case RID_BUILTIN_CLMULG:
> + ifn = IFN_CLMUL;
> + name = "__builtin_clmulg";
> + break;
> + default:
> + gcc_unreachable ();
> + }
This hunk is fine.
> c_parser_consume_token (parser);
> if (!c_parser_get_builtin_args (parser, name, &cexpr_list, false,
> @@ -13574,7 +13588,8 @@ c_parser_postfix_expression (c_parser *p
> break;
> }
>
> - if (vec_safe_length (cexpr_list) != 1)
> + if (vec_safe_length (cexpr_list) < (ifn == IFN_CLMUL ? 2 : 1)
> + || vec_safe_length (cexpr_list) > (ifn == IFN_CLMUL ? 3 : 1))
Looks like vec_safe_length (cexpr_list) is computed 3x, so
const unsigned int len = vec_safe_length (cexpr_list);
?
> {
> error_at (loc, "wrong number of arguments to %qs", name);
> expr.set_error ();
> @@ -13626,7 +13641,70 @@ c_parser_postfix_expression (c_parser *p
> break;
> }
> arg = c_fully_fold (arg, false, NULL);
> - expr.value = fold_build_builtin_bswapg_bitreverseg (loc, ifn, arg);
> + if (ifn == IFN_CLMUL)
> + {
> + arg_p = &(*cexpr_list)[1];
> + *arg_p = convert_lvalue_to_rvalue (loc, *arg_p, true, true);
> + tree arg2 = arg_p->value;
> + if (error_operand_p (arg2))
> + {
> + expr.set_error ();
> + break;
> + }
> + if (!comptypes_same_p (type,
> + TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
> + {
> + error_at (loc, "arguments 1 and 2 to function %qs "
I guess using loc here is fine but the existing <case RID_BUILTIN_TGMATH>
uses (*cexpr_list)[j].get_location () which maybe you can use here too
if you want (but with arg_p). This is not a requirement.
> + "have different types %qT and %qT",
> + name, type,
> + TYPE_MAIN_VARIANT (TREE_TYPE (arg2)));
> + expr.set_error ();
> + break;
> + }
> + arg2 = c_fully_fold (arg2, false, NULL);
> + tree arg3 = NULL_TREE;
> + if (vec_safe_length (cexpr_list) == 3)
s/vec_safe_length (cexpr_list)/len/
> + {
> + arg_p = &(*cexpr_list)[2];
> + *arg_p = convert_lvalue_to_rvalue (loc, *arg_p,
> + true, true);
> + arg3 = arg_p->value;
> + if (error_operand_p (arg3))
> + {
> + expr.set_error ();
> + break;
> + }
> + if (!POINTER_TYPE_P (TREE_TYPE (arg3)))
> + {
> + error_at (loc,
> + "argument 3 to function %qs does not have "
> + "pointer type", name);
> + expr.set_error ();
> + break;
> + }
> + tree htype = TREE_TYPE (TREE_TYPE (arg3));
> + if (!comptypes_same_p (type, TYPE_MAIN_VARIANT (htype)))
> + {
> + error_at (loc, "arguments 1 and 3 to function %qs "
> + "have incompatible types %qT and %qT",
> + name, type, TREE_TYPE (arg3));
> + expr.set_error ();
> + break;
> + }
> + if (TYPE_READONLY (htype))
> + {
> + error_at (loc, "arguments 3 to function %qs "
Here we are talking about only one argument so "argument 3 [...] has [...]"?
build_x_clmulg has this too.
> + "have pointer to %<const%> type %qT",
> + name, TREE_TYPE (arg3));
> + expr.set_error ();
> + break;
> + }
Note that <case BUILT_IN_MUL_OVERFLOW> also checks
else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (args[2]))))
{
if (complain)
error_at (ARG_LOCATION (2),
"argument %u in call to function %qE "
"has pointer to %qs type (%qT)",
3, fndecl, "_Atomic", TREE_TYPE (args[2]));
return false;
}
and it seems reasonable to me to check it here too.
> + }
> + expr.value = fold_build_builtin_clmulg (loc, arg, arg2, arg3);
> + }
> + else
> + expr.value = fold_build_builtin_bswapg_bitreverseg (loc, ifn,
> + arg);
> set_c_expr_source_range (&expr, loc, close_paren_loc);
> break;
> }
> --- a/gcc/c/c-decl.cc 2026-10-27 14:32:12.568304203 +0100
> +++ b/gcc/c/c-decl.cc 2026-10-30 17:06:34.332930406 +0100
> @@ -11936,6 +11936,7 @@ names_builtin_p (const char *name)
> case RID_BUILTIN_ASSOC_BARRIER:
> case RID_BUILTIN_BITREVERSEG:
> case RID_BUILTIN_BSWAPG:
> + case RID_BUILTIN_CLMULG:
> case RID_BUILTIN_CONVERTVECTOR:
> case RID_BUILTIN_HAS_ATTRIBUTE:
> case RID_BUILTIN_SHUFFLE:
This hunk is obvious.
Marek