Bug 111069 - Mangling of static structured bindings
Summary: Mangling of static structured bindings
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 14.0
: P3 normal
Target Milestone: ---
Assignee: Jakub Jelinek
URL:
Keywords: ABI
Depends on:
Blocks:
 
Reported: 2023-08-18 18:47 UTC by Jakub Jelinek
Modified: 2023-09-01 13:15 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-08-18 00:00:00


Attachments
gcc14-pr111069-wip.patch (1.34 KB, patch)
2023-08-19 10:12 UTC, Jakub Jelinek
Details | Diff
gcc14-pr111069.patch (3.15 KB, patch)
2023-08-21 09:20 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jakub Jelinek 2023-08-18 18:47:44 UTC
void
corge ()
{
  static int a[2];
  static auto [i, j] = a;
  static auto [k, l] = a;
  {
    static auto [i, j] = a;
    static auto [k, l] = a;
  }
  {
    static auto [i, j] = a;
    static auto [k, l] = a;
  }
}

fails to assemble with -std=c++20, we use
_ZNDC1i1jEE and _ZNDC1k1lEE as names of the local variables multiple times
(+ _ZGVZ5corgevE11_ZNDC1i1jEE and _ZGVZ5corgevE11_ZNDC1k1lEE for guards which
are emitted each just once but prevent the initialization of the second and third case).
clang++ uses _ZZ5corgevEDC1i1jE, _ZZ5corgevEDC1k1lE_0, _ZZ5corgevEDC1i1jE_1, _ZZ5corgevEDC1k1lE_2 etc. and corresponding guard vars.
Now, I guess for local vars it is up to us to name those as we want as long as there is no clash, with
inline void
freddy ()
{
  static int a[2];
  static auto [i, j] = a;
  static auto [k, l] = a;
  {
    static auto [i, j] = a;
    static auto [k, l] = a;
  }
  {
    static auto [i, j] = a;
    static auto [k, l] = a;
  }
}
void (*p)() = &freddy;
it is an ABI issue.
Comment 1 Jakub Jelinek 2023-08-18 18:50:40 UTC
Apparently my fault as I've done r10-720-gb7f0df71a9de2354fcb7 which enabled it,
but am not really sure what is the correct mangling.
Comment 2 Andrew Pinski 2023-08-18 18:51:44 UTC
I suspect this is a dup of bug 108257 .
Comment 3 Jakub Jelinek 2023-08-18 19:02:21 UTC
Yes, though this PR has more info.

grep 1[ik]1[jl] pr111069-2.s | grep :
_ZZ6freddyvEDC1i1jE:
_ZGVZ6freddyvEDC1i1jE:
_ZZ6freddyvEDC1k1lE:
_ZGVZ6freddyvEDC1k1lE:
_ZZ6freddyvEDC1i1jE_0:
_ZGVZ6freddyvEDC1i1jE_0:
_ZZ6freddyvEDC1k1lE_0:
_ZGVZ6freddyvEDC1k1lE_0:
_ZZ6freddyvEDC1i1jE_1:
_ZGVZ6freddyvEDC1i1jE_1:
_ZZ6freddyvEDC1k1lE_1:
_ZGVZ6freddyvEDC1k1lE_1:
shows that for inline functions I think clang++ mangling is right.
Comment 4 Jakub Jelinek 2023-08-19 10:12:41 UTC
Created attachment 55763 [details]
gcc14-pr111069-wip.patch

WIP patch.  Seems to get the basics right, but mangling of guard vars (_ZGV*) and life-extended vars (_ZGR*) needs work.  The latter will be hard, because
adding a full demangler to mangle.cc so that it handles arbitrary local names would be undesirable.
Comment 5 Jakub Jelinek 2023-08-21 09:20:29 UTC
Created attachment 55769 [details]
gcc14-pr111069.patch

Full untested patch.
Comment 6 GCC Commits 2023-09-01 13:15:42 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:cd37325b8d500bf4021692620686572d5ffb0868

commit r14-3624-gcd37325b8d500bf4021692620686572d5ffb0868
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Sep 1 15:07:48 2023 +0200

    c++: Fix up mangling of function/block scope static structured bindings [PR111069]
    
    As can be seen on the testcase, we weren't correctly mangling
    static/thread_local structured bindings (C++20 feature) at function/block
    scope.  The following patch fixes that by using what write_local_name
    does for those cases (note, structured binding mandling doesn't use the
    standard path because it needs to pass a list of all the identifiers in
    the structured binding to the mangling).  In addition to that it fixes
    mangling of various helpers which use write_guarded_name (_ZGV*, _ZTH*,
    _ZTW*) and kills find_decomp_unqualified_name which for the local names
    would be too hard to implement and uses write_guarded_name for structured
    binding related _ZGR* names as well.
    
    All the mangled names on the first testcase match now clang++ and my
    expectations.
    Because the old mangled names were plain wrong (they mangled the same as
    structured binding at global scope and resulted in assembly errors if there
    was more than one static structured binding with the same identifiers in
    the same (or another) function, I think we don't need to play with another
    mangling ABI level which turns on/off the old broken way.
    
    In addition to that the patch starts to emit abi-tags into the mangle_decomp
    produced names when needed and emits a -Wabi warning for that as well.
    To make that work, I had to move cp_maybe_mangle_decomp calls from before
    cp_finish_decl into a middle of cp_finish_decl after type is deduced and
    maybe_commonize_var (which also had to be changed not to ignore structured
    bindings) is called but before anything might need a mangled name for the
    decl, so a new cp_decomp structure is passed to cp_finish_decl; various
    other structured binding related functions have been changed to pass
    pointer to that around instead of passing a tree and unsigned int separately.
    
    On decomp9.C, there is a
    _ZZ3barI1TB3quxEivEDC1o1pEB3qux
    (g++) vs.
    _ZZ3barI1TB3quxEivEDC1o1pE
    (clang++) mangling difference, but that seems to be a clang++ bug and happens
    also with normal static block vars, doesn't need structured bindings.
    
    2023-09-01  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/111069
    gcc/
            * common.opt (fabi-version=): Document version 19.
            * doc/invoke.texi (-fabi-version=): Likewise.
    gcc/c-family/
            * c-opts.cc (c_common_post_options): Change latest_abi_version to 19.
    gcc/cp/
            * cp-tree.h (determine_local_discriminator): Add NAME argument with
            NULL_TREE default.
            (struct cp_decomp): New type.
            (cp_finish_decl): Add DECOMP argument defaulted to nullptr.
            (cp_maybe_mangle_decomp): Remove declaration.
            (cp_finish_decomp): Add cp_decomp * argument, remove tree and unsigned
            args.
            (cp_convert_range_for): Likewise.
            * decl.cc (determine_local_discriminator): Add NAME argument, use it
            if non-NULL, otherwise compute it the old way.
            (maybe_commonize_var): Don't return early for structured bindings.
            (cp_finish_decl): Add DECOMP argument, if non-NULL, call
            cp_maybe_mangle_decomp.
            (cp_maybe_mangle_decomp): Make it static with a forward declaration.
            Call determine_local_discriminator.  Replace FIRST and COUNT arguments
            with DECOMP argument.
            (cp_finish_decomp): Replace FIRST and COUNT arguments with DECOMP
            argument.
            * mangle.cc (find_decomp_unqualified_name): Remove.
            (write_unqualified_name): Don't call find_decomp_unqualified_name.
            (mangle_decomp): Handle mangling of static function/block scope
            structured bindings.  Don't call decl_mangling_context twice.  Call
            check_abi_tags, call write_abi_tags for abi version >= 19 and emit
            -Wabi warnings if needed.
            (write_guarded_var_name): Handle structured bindings.
            (mangle_ref_init_variable): Use write_guarded_var_name.
            * parser.cc (cp_parser_range_for): Adjust do_range_for_auto_deduction
            and cp_convert_range_for callers.
            (do_range_for_auto_deduction): Replace DECOMP_FIRST_NAME and
            DECOMP_CNT arguments with DECOMP.  Adjust cp_finish_decomp caller.
            (cp_convert_range_for): Replace DECOMP_FIRST_NAME and
            DECOMP_CNT arguments with DECOMP.  Don't call cp_maybe_mangle_decomp,
            adjust cp_finish_decl and cp_finish_decomp callers.
            (cp_parser_decomposition_declaration): Don't call
            cp_maybe_mangle_decomp, adjust cp_finish_decl and cp_finish_decomp
            callers.
            (cp_convert_omp_range_for): Adjust do_range_for_auto_deduction
            and cp_finish_decomp callers.
            (cp_finish_omp_range_for): Don't call cp_maybe_mangle_decomp,
            adjust cp_finish_decl and cp_finish_decomp callers.
            * pt.cc (tsubst_omp_for_iterator): Adjust tsubst_decomp_names
            caller.
            (tsubst_decomp_names): Replace FIRST and CNT arguments with DECOMP.
            (tsubst_expr): Don't call cp_maybe_mangle_decomp, adjust
            tsubst_decomp_names, cp_finish_decl, cp_finish_decomp and
            cp_convert_range_for callers.
    gcc/testsuite/
            * g++.dg/cpp2a/decomp8.C: New test.
            * g++.dg/cpp2a/decomp9.C: New test.
            * g++.dg/abi/macro0.C: Expect __GXX_ABI_VERSION 1019 rather than
            1018.