public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug ipa/95790] New: Incorrect static target dispatch
@ 2020-06-20 19:08 yyc1992 at gmail dot com
  2020-06-20 19:22 ` [Bug ipa/95790] " hjl.tools at gmail dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: yyc1992 at gmail dot com @ 2020-06-20 19:08 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95790
           Summary: Incorrect static target dispatch
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: ipa
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yyc1992 at gmail dot com
                CC: marxin at gcc dot gnu.org
  Target Milestone: ---

The indirection elimination code currently only check for match of the target
for the specific version but doesn't check if all the targets are matching.

Modifying from
https://github.com/gcc-mirror/gcc/commit/b8ce8129a560f64f8b2855c4a3812b7c3c0ebf3f#diff-e2d535917af8555baad2e9c8749e96a5

```
__attribute__ ((target ("default")))
static unsigned foo(const char *buf, unsigned size) {
  return 1;
}

__attribute__ ((target ("avx")))
static unsigned foo(const char *buf, unsigned size) {
  return 2;
}

__attribute__ ((target ("avx512f")))
static unsigned foo(const char *buf, unsigned size) {
  return 3;
}

__attribute__ ((target ("default")))
unsigned bar() {
  char buf[4096];
  unsigned acc = 0;
  for (int i = 0; i < sizeof(buf); i++) {
    acc += foo(&buf[i], 1);
  }
  return acc;
}

__attribute__ ((target ("avx")))
unsigned bar() {
  char buf[4096];
  unsigned acc = 0;
  for (int i = 0; i < sizeof(buf); i++) {
    acc += foo(&buf[i], 1);
  }
  return acc;
}
```

With the optimization disabled, which is possible by adding a flatten attribute
to the functions and triggering PR95780 and PR95778, a resolver function is
automatically generated for foo like

```
        .text
.LHOTB0:
        .p2align 4
        .type   _ZL3fooPKcj.resolver, @function
_ZL3fooPKcj.resolver:
        subq    $8, %rsp
        call    __cpu_indicator_init@PLT
        movq    __cpu_model@GOTPCREL(%rip), %rax
        movl    12(%rax), %eax
        testb   $-128, %ah
        je      .L8
        leaq    _ZL3fooPKcj.avx512f(%rip), %rax
.L7:
        addq    $8, %rsp
        ret
        .section        .text.unlikely
        .type   _ZL3fooPKcj.resolver.cold, @function
_ZL3fooPKcj.resolver.cold:
.L8:
        testb   $2, %ah
        leaq    _ZL3fooPKcj.avx(%rip), %rdx
        leaq    _ZL3fooPKcj(%rip), %rax
        cmovne  %rdx, %rax
        jmp     .L7
        .text
        .size   _ZL3fooPKcj.resolver, .-_ZL3fooPKcj.resolver
        .section        .text.unlikely
        .size   _ZL3fooPKcj.resolver.cold, .-_ZL3fooPKcj.resolver.cold
.LCOLDE0:
        .text
.LHOTE0:
        .type   _Z11_ZL3fooPKcjPKcj, @gnu_indirect_function
        .set    _Z11_ZL3fooPKcjPKcj,_ZL3fooPKcj.resolver
```

and the calls from bar goes through the PLT. This is the correct behavior
(albeit sub-optimal since the default could call the default directly) and
allows avx512f version of foo to be called on the correct processor from the
avx version of bar.

With the optimization enabled, however, the call of foo's are inlined to bar
and the avx512f version is never used.

This is somewhat a regression caused by
b8ce8129a560f64f8b2855c4a3812b7c3c0ebf3f.

It'll also affect my fix for PR95780 and PR95778.
https://gcc.gnu.org/pipermail/gcc-patches/2020-June/548631.html

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

end of thread, other threads:[~2020-06-22  8:12 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-20 19:08 [Bug ipa/95790] New: Incorrect static target dispatch yyc1992 at gmail dot com
2020-06-20 19:22 ` [Bug ipa/95790] " hjl.tools at gmail dot com
2020-06-20 19:25 ` yyc1992 at gmail dot com
2020-06-20 19:26 ` yyc1992 at gmail dot com
2020-06-20 20:33 ` hjl.tools at gmail dot com
2020-06-20 20:59 ` yyc1992 at gmail dot com
2020-06-20 22:36 ` hjl.tools at gmail dot com
2020-06-21  0:02 ` yyc1992 at gmail dot com
2020-06-21  0:12 ` yyc1992 at gmail dot com
2020-06-22  8:12 ` rguenth 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).