https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129052
Bug ID: 129052
Summary: [16/17 Regression] x86_cse places __tls_get_addr
between a register-asm assignment and its asm use
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: inline-asm, wrong-code
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: lzy20000419 at outlook dot com
Target Milestone: ---
Host: x86_64-pc-linux-gnu
Target: x86_64-pc-linux-gnu
Fails with trunk, 16.1.0 and 16.2.0; 9.5.0 through 15.3.0 work.
Target: x86_64-pc-linux-gnu with -fPIC.
The source assigns a local register variable and then uses it only as an
Extended asm operand, which is the use documented in "Local Register
Variables". There is no call, and no TLS access, between that assignment and
the asm. The TLS references occur only later, in the two arms after both asm
statements. With -fPIC, x86_cse merges the global-dynamic __tls_get_addr calls
and inserts one of them in that window. r8 is call-clobbered, so the asm reads
a different value.
This is not the situation described by the warning in that manual section. That
warning is about subsequent user code and about library calls that arise while
evaluating expressions between register assignments, such as the initialization
of another register variable. It is also not PR126290, where a __thread address
is itself an operand of the same asm, so a TLS libcall is required to set up
that operand. Here the asms do not mention TLS. The compiler is hoisting a
later, unrelated TLS resolution into the assignment-to-asm sequence.
/* t.c - build as a PIC shared library */
__thread int t = 10;
__attribute__((noipa)) void g (int *p) { *p += 100; }
__attribute__((noipa)) void h (long v) { t += (int) v; }
long f (long a, long b, int c)
{
register long r9 asm ("r9") = a;
register long r8 asm ("r8");
long x, y;
__asm__ volatile ("" : "+r" (r9));
if (b == 7) __asm__ volatile ("nop" : "+r" (r9));
r8 = b;
__asm__ volatile ("" : "+r" (r8));
__asm__ volatile ("mov %1, %0" : "=r" (x) : "r" (r9));
__asm__ volatile ("mov %1, %0" : "=r" (y) : "r" (r8));
if (c > 3)
g (&t);
else
{
t *= 3;
h (y);
}
return x * 1000 + y + t;
}
/* main.c */
#include <stdio.h>
long f (long, long, int);
int main (void) { printf ("%ld\n", f (4, 17, 1)); return 0; }
/* clob.c - ABI-conforming interposer. glibc's resolver often preserves r8,
so without this the miscompile still prints the right number. */
#define _GNU_SOURCE
#include <dlfcn.h>
static void *(*real) (void *);
void *__tls_get_addr (void *p)
{
if (!real) real = (void *(*) (void *)) dlsym (RTLD_NEXT,
"__tls_get_addr");
void *r = real (p);
__asm__ volatile ("mov $0xdead, %%r8; mov $0xbeef, %%rsi; mov $0x77,
%%rcx; mov $0x55, %%rdx" ::: "r8", "rsi", "rcx", "rdx");
return r;
}
gcc -O2 -fPIC -shared t.c -o libt.so
gcc -O1 main.c clob.c -rdynamic -ldl -L. -lt -Wl,-rpath,. -o t
./t
Observed: 118040. Expected: 4064.
I confirmed this on a native x86_64-pc-linux-gnu build of GCC 17.0.0 20261126
(experimental), git commit d1dbc89caa56. With -fPIC and the interposer, -O1,
-O2 and -O3 print 118040. -O0, -Os, -O2 -fdisable-rtl-x86_cse, -O2
-mtls-dialect=gnu2, and host GCC 14.2.0 at -O2 all print 4064. Without the
interposer, glibc's __tls_get_addr happens to preserve r8, so the miscompiled
binary still prints 4064.
Trunk -O2 -fPIC code for f, where the read of r8 comes after the call:
movq %rsi, %r8
mov %r9, %rbx
call __tls_get_addr@PLT
mov %r8, %rbp
With -fdisable-rtl-x86_cse, or with 15.3.0, both movs come before any later
__tls_get_addr call.
Godbolt (trunk): https://godbolt.org/z/GMsnzb3cz
Cause: ix86_emit_tls_call (config/i386/i386-features.cc) walks the join block
from DF_LIVE_IN and removes caller-saved registers from its set at REG_DEAD
notes. The only live-in call-clobbered GPR here is r9. The REG_DEAD note on the
asm that copies r9 empties the set, and the call is emitted right after that
insn. Registers set inside the block are only added to the set for FLAGS, so
r8, assigned two insns earlier and read by the next asm, is never seen.
PR126382 is RESOLVED FIXED and is a different hole in the same pass (DRAP
missing from DF_LIVE_IN at entry).
A possible direction is, for GD/LD, also to add real sets of call-clobbered
hard registers in the block to the live set, and if one is still live at the
end of the block, to place the call in a dominator instead. I have not tested a
patch.
Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.