Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Assembly output is incorrect for a compiled binary affecting gcc versions 11.2 - 12.2 for x84_64 #4848

Open
vsolontsov-ll opened this issue Mar 11, 2023 · 1 comment · May be fixed by compiler-explorer/asm-parser#32

Comments

@vsolontsov-ll
Copy link

vsolontsov-ll commented Mar 11, 2023

Describe the bug

_rdtsc() intrinsic somehow get dropped at linkage. Sample:

#include <x86intrin.h>
#include <utility>

long tsc;

[[gnu::noinline]] 
void call_cold(auto&& f) {
    std::forward<decltype(f)>(f)();
}

int main() {
    call_cold([&](){
        tsc = _rdtsc();
    });
    return tsc;
}

Without 'link to binary' output flag the code is correct -- the call_cold<>() instance contains RDTSC instruction (with all expected calculations). However with linking to binary the instruction is dropped:

call_cold<main::{lambda()#1}>(main::{lambda()#1}&&) [clone .constprop.0]::
 mov    %rax,0x2f00(%rip)        # 404030 <tsc>
 ret    

I checked on my local compiler 12.2.1 -- objdump -dC a.out gives expected code (with RDTSC) so it doesn't seem like a compiler issue.

Actually, it might be just cutting off the beginning of the function as the function address is expected to be aligned while the first instruction in the function which I can see is 401129.

Steps to reproduce

  1. Copy-paste the snippet to the godbolt editor
  2. Select g++ 12.2 for x86_64
  3. Make sure the "link-to-binary" flag is chosen
  4. Inspect the assembler output for function call_cold<main::{lambda()#1}>(main::{lambda()#1}&&) [clone .constprop.0] -- for me is shows only the above 2 instructions (mov + ret)

Expected behavior

Taken from objdump locally:
0000000000001020 <void call_cold<main::{lambda()#1}>(main::{lambda()#1}&&) [clone .constprop.0]>:
    1020:       0f 31                   rdtsc
    1022:       48 c1 e2 20             shl    $0x20,%rdx
    1026:       48 09 d0                or     %rdx,%rax
    1029:       48 89 05 e8 2f 00 00    mov    %rax,0x2fe8(%rip)        # 4018 <tsc>
    1030:       c3                      ret

Reproduction link

https://godbolt.org/z/xPe6s597e

Screenshots

Not applicable

Operating System

Manjaro Linux

Browser version

Firefox 110.0 (64-bit)

@partouf
Copy link
Member

partouf commented Mar 11, 2023

There also seems to be a problem if you disable Demangling

I'm extremely confused by this bug... will have to debug.. the objdump looks fine to me, but our parser disagrees I guess

@partouf partouf linked a pull request Mar 11, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants