https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106654

--- Comment #13 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <ja...@gcc.gnu.org>:

https://gcc.gnu.org/g:08b51baddc53d64aa4c5e7a81ef3c4bf320293be

commit r13-3106-g08b51baddc53d64aa4c5e7a81ef3c4bf320293be
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Thu Oct 6 08:56:48 2022 +0200

    c++, c: Implement C++23 P1774R8 - Portable assumptions [PR106654]

    The following patch implements C++23 P1774R8 - Portable assumptions
    paper, by introducing support for [[assume (cond)]]; attribute for C++.
    In addition to that the patch adds [[gnu::assume (cond)]]; and
    __attribute__((assume (cond))); support to both C and C++.
    As described in C++23, the attribute argument is conditional-expression
    rather than the usual assignment-expression for attribute arguments,
    the condition is contextually converted to bool (for C truthvalue
conversion
    is done on it) and is never evaluated at runtime.
    For C++ constant expression evaluation, I only check the simplest
conditions
    for undefined behavior, because otherwise I'd need to undo changes to
    *ctx->global which happened during the evaluation (but I believe the spec
    allows that and we can further improve later).
    The patch uses a new internal function, .ASSUME, to hold the condition
    in the FEs.  At gimplification time, if the condition is simple/without
    side-effects, it is gimplified as if (cond) ; else __builtin_unreachable
();
    and otherwise for now dropped on the floor.  The intent is to incrementally
    outline the conditions into separate artificial functions and use
    .ASSUME further to tell the ranger and perhaps other optimization passes
    about the assumptions, as detailed in the PR.

    When implementing it, I found that assume entry hasn't been added to
    https://eel.is/c++draft/cpp.cond#6
    Jonathan said he'll file a NB comment about it, this patch assumes it
    has been added into the table as 202207L when the paper has been voted in.

    With the attributes for both C/C++, I'd say we don't need to add
    __builtin_assume with similar purpose, especially when __builtin_assume
    in LLVM is just weird.  It is strange for side-effects in function call's
    argument not to be evaluated, and LLVM in that case (annoyingly) warns
    and ignores the side-effects (but doesn't do then anything with it),
    if there are no side-effects, it will work like our
    if (!cond) __builtin_unreachable ();

    2022-10-06  Jakub Jelinek  <ja...@redhat.com>

            PR c++/106654
    gcc/
            * internal-fn.def (ASSUME): New internal function.
            * internal-fn.h (expand_ASSUME): Declare.
            * internal-fn.cc (expand_ASSUME): Define.
            * gimplify.cc (gimplify_call_expr): Gimplify IFN_ASSUME.
            * fold-const.h (simple_condition_p): Declare.
            * fold-const.cc (simple_operand_p_2): Rename to ...
            (simple_condition_p): ... this.  Remove forward declaration.
            No longer static.  Adjust function comment and fix a typo in it.
            Adjust recursive call.
            (simple_operand_p): Adjust function comment.
            (fold_truth_andor): Adjust simple_operand_p_2 callers to call
            simple_condition_p.
            * doc/extend.texi: Document assume attribute.  Move fallthrough
            attribute example to its section.
    gcc/c-family/
            * c-attribs.cc (handle_assume_attribute): New function.
            (c_common_attribute_table): Add entry for assume attribute.
            * c-lex.cc (c_common_has_attribute): Handle
            __have_cpp_attribute (assume).
    gcc/c/
            * c-parser.cc (handle_assume_attribute): New function.
            (c_parser_declaration_or_fndef): Handle assume attribute.
            (c_parser_attribute_arguments): Add assume_attr argument,
            if true, parse first argument as conditional expression.
            (c_parser_gnu_attribute, c_parser_std_attribute): Adjust
            c_parser_attribute_arguments callers.
            (c_parser_statement_after_labels) <case RID_ATTRIBUTE>: Handle
            assume attribute.
    gcc/cp/
            * cp-tree.h (process_stmt_assume_attribute): Implement C++23
            P1774R8 - Portable assumptions.  Declare.
            (diagnose_failing_condition): Declare.
            (find_failing_clause): Likewise.
            * parser.cc (assume_attr): New enumerator.
            (cp_parser_parenthesized_expression_list): Handle assume_attr.
            Remove identifier variable, for id_attr push the identifier into
            expression_list right away instead of inserting it before all the
            others at the end.
            (cp_parser_conditional_expression): New function.
            (cp_parser_constant_expression): Use it.
            (cp_parser_statement): Handle assume attribute.
            (cp_parser_expression_statement): Likewise.
            (cp_parser_gnu_attribute_list): Use assume_attr for assume
            attribute.
            (cp_parser_std_attribute): Likewise.  Handle standard assume
            attribute like gnu::assume.
            * cp-gimplify.cc (process_stmt_assume_attribute): New function.
            * constexpr.cc: Include fold-const.h.
            (find_failing_clause_r, find_failing_clause): New functions,
            moved from semantics.cc with ctx argument added and if non-NULL,
            call cxx_eval_constant_expression rather than
fold_non_dependent_expr.
            (cxx_eval_internal_function): Handle IFN_ASSUME.
            (potential_constant_expression_1): Likewise.
            * pt.cc (tsubst_copy_and_build): Likewise.
            * semantics.cc (diagnose_failing_condition): New function.
            (find_failing_clause_r, find_failing_clause): Moved to
constexpr.cc.
            (finish_static_assert): Use it.  Add auto_diagnostic_group.
    gcc/testsuite/
            * gcc.dg/attr-assume-1.c: New test.
            * gcc.dg/attr-assume-2.c: New test.
            * gcc.dg/attr-assume-3.c: New test.
            * g++.dg/cpp2a/feat-cxx2a.C: Add colon to C++20 features
            comment, add C++20 attributes comment and move C++20
            new features after the attributes before them.
            * g++.dg/cpp23/feat-cxx2b.C: Likewise.  Test
            __has_cpp_attribute(assume).
            * g++.dg/cpp23/attr-assume1.C: New test.
            * g++.dg/cpp23/attr-assume2.C: New test.
            * g++.dg/cpp23/attr-assume3.C: New test.
            * g++.dg/cpp23/attr-assume4.C: New test.

Reply via email to