public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/109267] New: -Og generates empty functions with .cfi_startproc/.cfi_endproc that conflict with other functions
@ 2023-03-23 22:38 i at maskray dot me
  2023-03-23 22:47 ` [Bug c++/109267] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: i at maskray dot me @ 2023-03-23 22:38 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109267
           Summary: -Og generates empty functions with
                    .cfi_startproc/.cfi_endproc that conflict with other
                    functions
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: i at maskray dot me
  Target Milestone: ---

cat > lib.cpp <<'eof'
#include "lib.h"
#include <stdexcept>

void test(const T_value& entry_value) {
    boost::apply_visitor([](const auto& value) { test(value); },
                         entry_value);
}
void test(const Obj_1& ) {
    throw std::runtime_error("Obj_1");
}
void test(const Obj_2& ) {
    throw std::runtime_error("Obj_2");
}
eof
cat > lib.h <<'eof'
#pragma once

#include <boost/variant.hpp>

struct Obj_1 {
  int i;
};
struct Obj_2 {
  int i;
};

using T_value = boost::make_recursive_variant<
        Obj_1,
        Obj_2>::type;
void test(const T_value&);
void test(const Obj_2&);
void test(const Obj_1&);
eof
cat > main.cpp <<'eof'
#include "lib.h"
#include <stdexcept>

int main() {
    try {
        test(Obj_2{2});

    } catch (const std::runtime_error& e) {
    }
    try {
        test(Obj_1{1});

    } catch (const std::runtime_error& e) {
    }
} 
eof

g++ -isystem /usr/include/boost -Og -g -DNDEBUG -S lib.cpp main.cpp
g++ -isystem /usr/include/boost -Og -g -DNDEBUG -fuse-ld=lld lib.cpp main.cpp
return

lib.s has unusual empty functions with .cfi_startproc/.cfi_endproc . I think
this behavior is incorrect since CFI FDE associated to the empty functions may
conflict with FDE of another function.
When linking with GNU ld, GNU ld drops FDE associated to the empty functions,
and therefore the output executable will be correct.
However, I think linkers are not obliged to behave like GNU ld. ld.lld doesn't
discard FDE associated to the empty functions, and the output executable will
call std::terminate.

```
% ./a.out
terminate called after throwing an instance of 'std::runtime_error'
  what():  Obj_1
[1]    2826366 IOT instruction  ./a.out
```

-O1/-O2/-O3 discard such empty functions to avoid the problem. In -O0 codegen,
the function becomes non-empty, avoiding the problem as well.

```
% g++ -isystem /usr/include/boost -Og -g -DNDEBUG -S lib.cpp -o - | grep
11result_typeEiRSD_T0_PNS1_22apply_visitor_unrolledET1_l
        .type  
_ZN5boost6detail7variant22visitation_impl_invokeINS1_14invoke_visitorINS1_15result_wrapper1IZ4testRKNS_7variantINS1_14recursive_flagI5Obj_1EEJ5Obj_2EEEEUlRKT_E_SC_EELb0EEEPKvNSA_18has_fallback_type_EEENSD_11result_typeEiRSD_T0_PNS1_22apply_visitor_unrolledET1_l,
@function
_ZN5boost6detail7variant22visitation_impl_invokeINS1_14invoke_visitorINS1_15result_wrapper1IZ4testRKNS_7variantINS1_14recursive_flagI5Obj_1EEJ5Obj_2EEEEUlRKT_E_SC_EELb0EEEPKvNSA_18has_fallback_type_EEENSD_11result_typeEiRSD_T0_PNS1_22apply_visitor_unrolledET1_l:
        .size  
_ZN5boost6detail7variant22visitation_impl_invokeINS1_14invoke_visitorINS1_15result_wrapper1IZ4testRKNS_7variantINS1_14recursive_flagI5Obj_1EEJ5Obj_2EEEEUlRKT_E_SC_EELb0EEEPKvNSA_18has_fallback_type_EEENSD_11result_typeEiRSD_T0_PNS1_22apply_visitor_unrolledET1_l,
.-_ZN5boost6detail7variant22visitation_impl_invokeINS1_14invoke_visitorINS1_15result_wrapper1IZ4testRKNS_7variantINS1_14recursive_flagI5Obj_1EEJ5Obj_2EEEEUlRKT_E_SC_EELb0EEEPKvNSA_18has_fallback_type_EEENSD_11result_typeEiRSD_T0_PNS1_22apply_visitor_unrolledET1_l
% g++ -isystem /usr/include/boost -O1 -g -DNDEBUG -S lib.cpp -o - | grep
11result_typeEiRSD_T0_PNS1_22apply_visitor_unrolledET1_l
% g++ -isystem /usr/include/boost -O2 -g -DNDEBUG -S lib.cpp -o - | grep
11result_typeEiRSD_T0_PNS1_22apply_visitor_unrolledET1_l
% g++ -isystem /usr/include/boost -O3 -g -DNDEBUG -S lib.cpp -o - | grep
11result_typeEiRSD_T0_PNS1_22apply_visitor_unrolledET1_l
```

Link: https://github.com/llvm/llvm-project/issues/61434

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/109267] -Og generates empty functions with .cfi_startproc/.cfi_endproc that conflict with other functions
  2023-03-23 22:38 [Bug c++/109267] New: -Og generates empty functions with .cfi_startproc/.cfi_endproc that conflict with other functions i at maskray dot me
@ 2023-03-23 22:47 ` pinskia at gcc dot gnu.org
  2023-03-23 22:51 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-23 22:47 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://github.com/llvm/llv
                   |                            |m-project/issues/61434

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The functions are not empty as far as I can tell:
        .type  
_ZN5boost6detail7variant22visitation_impl_invokeINS1_14invoke_visitorINS1_15result_wrapper1IZ4testRKNS_7variantINS1_14recursive_flagI5Obj_1EEJ5Obj_2EEEEUlRKT_E_SC_EELb0EEEPKvNSA_18has_fallback_type_EEENSD_11result_typeEiRSD_T0_PNS1_22apply_visitor_unrolledET1_l,
@function
_ZN5boost6detail7variant22visitation_impl_invokeINS1_14invoke_visitorINS1_15result_wrapper1IZ4testRKNS_7variantINS1_14recursive_flagI5Obj_1EEJ5Obj_2EEEEUlRKT_E_SC_EELb0EEEPKvNSA_18has_fallback_type_EEENSD_11result_typeEiRSD_T0_PNS1_22apply_visitor_unrolledET1_l:
.LFB3869:
        .cfi_startproc
        subq    $8, %rsp
        .cfi_def_cfa_offset 16
        call    _ZN5boost6detail7variant13forced_returnIvEET_v
        .cfi_endproc
.LFE3869:
        .size  
_ZN5boost6detail7variant22visitation_impl_invokeINS1_14invoke_visitorINS1_15result_wrapper1IZ4testRKNS_7variantINS1_14recursive_flagI5Obj_1EEJ5Obj_2EEEEUlRKT_E_SC_EELb0EEEPKvNSA_18has_fallback_type_EEENSD_11result_typeEiRSD_T0_PNS1_22apply_visitor_unrolledET1_l,
.-_ZN5boost6detail7variant22visitation_impl_invokeINS1_14invoke_visitorINS1_15result_wrapper1IZ4testRKNS_7variantINS1_14recursive_flagI5Obj_1EEJ5Obj_2EEEEUlRKT_E_SC_EELb0EEEPKvNSA_18has_fallback_type_EEENSD_11result_typeEiRSD_T0_PNS1_22apply_visitor_unrolledET1_l



What version of boost is being used?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/109267] -Og generates empty functions with .cfi_startproc/.cfi_endproc that conflict with other functions
  2023-03-23 22:38 [Bug c++/109267] New: -Og generates empty functions with .cfi_startproc/.cfi_endproc that conflict with other functions i at maskray dot me
  2023-03-23 22:47 ` [Bug c++/109267] " pinskia at gcc dot gnu.org
@ 2023-03-23 22:51 ` pinskia at gcc dot gnu.org
  2023-03-23 22:52 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-23 22:51 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-03-23
             Status|UNCONFIRMED                 |WAITING

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The only time you get an empty function is for:
void f(){__builtin_unreachable();}

Which does produce:
_Z1fv:
.LFB0:
        .file 1 "/app/example.cpp"
        .loc 1 1 9 view -0
        .cfi_startproc
        .loc 1 1 10 view .LVU1
        .cfi_endproc

clang even produces a similar thing for that too:
_Z1fv:                                  # @_Z1fv
.Lfunc_begin0:
        .cfi_startproc
# %bb.0:
.Lfunc_end0:
        .size   _Z1fv, .Lfunc_end0-_Z1fv
        .cfi_endproc

Can you attach the preprocessed source for the lib.cpp?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/109267] -Og generates empty functions with .cfi_startproc/.cfi_endproc that conflict with other functions
  2023-03-23 22:38 [Bug c++/109267] New: -Og generates empty functions with .cfi_startproc/.cfi_endproc that conflict with other functions i at maskray dot me
  2023-03-23 22:47 ` [Bug c++/109267] " pinskia at gcc dot gnu.org
  2023-03-23 22:51 ` pinskia at gcc dot gnu.org
@ 2023-03-23 22:52 ` pinskia at gcc dot gnu.org
  2023-03-24  7:51 ` [Bug middle-end/109267] " rguenth at gcc dot gnu.org
  2023-12-09  9:36 ` iains at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-23 22:52 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |UNCONFIRMED
     Ever confirmed|1                           |0

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Actually I missed -DNDEBUG. But again clang produces the same bad assembly too.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug middle-end/109267] generates empty functions with .cfi_startproc/.cfi_endproc that conflict with other functions
  2023-03-23 22:38 [Bug c++/109267] New: -Og generates empty functions with .cfi_startproc/.cfi_endproc that conflict with other functions i at maskray dot me
                   ` (2 preceding siblings ...)
  2023-03-23 22:52 ` pinskia at gcc dot gnu.org
@ 2023-03-24  7:51 ` rguenth at gcc dot gnu.org
  2023-12-09  9:36 ` iains at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-03-24  7:51 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|-Og generates empty         |generates empty functions
                   |functions with              |with
                   |.cfi_startproc/.cfi_endproc |.cfi_startproc/.cfi_endproc
                   |that conflict with other    |that conflict with other
                   |functions                   |functions
          Component|c++                         |middle-end
            Version|unknown                     |13.0

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
I'm quite sure we dont "remove" any such empty functions with -O2, we might
more aggressively inline and thus if they are static they will be elided.

But the "problem" if there is any, should be generic.

void foo () { __builtin_unreachable (); }
int __attribute__((cold)) bar () { return 0; }

compiled with -O2 gets you the following where foo and bar have the same
address.

        .section        .text.unlikely,"ax",@progbits
        .globl  foo
        .type   foo, @function
foo:
.LFB0:
        .file 1 "t.i"
        .loc 1 1 13 view -0
        .cfi_startproc
        .loc 1 1 15 view .LVU1
        .cfi_endproc
.LFE0:
        .size   foo, .-foo
        .globl  bar
        .type   bar, @function
bar:
.LFB1:
        .loc 1 2 34 view -0
        .cfi_startproc
        .loc 1 2 36 view .LVU3
        .loc 1 2 46 is_stmt 0 view .LVU4
        xorl    %eax, %eax
        ret
        .cfi_endproc
.LFE1:
        .size   bar, .-bar

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug middle-end/109267] generates empty functions with .cfi_startproc/.cfi_endproc that conflict with other functions
  2023-03-23 22:38 [Bug c++/109267] New: -Og generates empty functions with .cfi_startproc/.cfi_endproc that conflict with other functions i at maskray dot me
                   ` (3 preceding siblings ...)
  2023-03-24  7:51 ` [Bug middle-end/109267] " rguenth at gcc dot gnu.org
@ 2023-12-09  9:36 ` iains at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: iains at gcc dot gnu.org @ 2023-12-09  9:36 UTC (permalink / raw)
  To: gcc-bugs

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

Iain Sandoe <iains at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
                 CC|                            |iains at gcc dot gnu.org
     Ever confirmed|0                           |1
   Last reconfirmed|2023-03-23 00:00:00         |2023-12-09

--- Comment #5 from Iain Sandoe <iains at gcc dot gnu.org> ---
I am seeing this on aarch64 darwin (bootstrap fail with the new Xcode 15 linker
in libgomp).  It can also apply equally to targets without .cfi_xxxx support
since the generation of empty functions messes up EH there too.

Note that the fix in 57438 was done in the backend, and there was a work-around
suggested in that PR which does appear still usable:

-D__builtin_unreachable=__builtin_trap

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-12-09  9:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-23 22:38 [Bug c++/109267] New: -Og generates empty functions with .cfi_startproc/.cfi_endproc that conflict with other functions i at maskray dot me
2023-03-23 22:47 ` [Bug c++/109267] " pinskia at gcc dot gnu.org
2023-03-23 22:51 ` pinskia at gcc dot gnu.org
2023-03-23 22:52 ` pinskia at gcc dot gnu.org
2023-03-24  7:51 ` [Bug middle-end/109267] " rguenth at gcc dot gnu.org
2023-12-09  9:36 ` iains at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).