From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 50F183858416; Wed, 31 Jan 2024 19:05:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 50F183858416 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1706727955; bh=O8jkiTFiEXqcc1c/KxTOviz9af6NxWlSmZWj6niDgwk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=RAKvUkOuKhTASmwseputWbiSQEmISa605cs1lO1FyKRCUvkUrN0Ag1KkoJR6B+MYU Hc0mdegzg9QZHaDrrposCKYU/93wGQeRHwpIy5A6WCos1ccNksNeKYdfD0aSaKJkKP xFs+YuMO2v9bYDoOwSQNWb8TexdJdZ24Pysj6QGg= From: "palmer at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/113686] [RISC-V] TLS (Local Exec) relaxation on structures (LE) Date: Wed, 31 Jan 2024 19:05:54 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 13.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: palmer at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc bug_status everconfirmed cf_reconfirmed_on Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D113686 palmer at gcc dot gnu.org changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |nelsonc1225 at sourceware = dot org, | |palmer at gcc dot gnu.org Status|UNCONFIRMED |NEW Ever confirmed|0 |1 Last reconfirmed| |2024-01-31 --- Comment #1 from palmer at gcc dot gnu.org --- (In reply to H. Peter Anvin from comment #0) > When the Local Exec TLS model is in use, gcc generates inefficient code f= or > accessing the member of a structure: >=20 > struct foobar { > int alpha; > int beta; > }; >=20 > _Thread_local struct foobar foo; >=20 > void func(int bar) > { > foo.beta =3D bar; > } >=20 > # Version 1 > lui a1,%tprel_hi(foo) > add a1,a1,tp,%tprel_add(foo) > addi a1,a1,%tprel_lo(foo) > sw a0,4(a1) >=20 > However, in this case it could be generated as: >=20 > # Version 2 > lui a1,%tprel_hi(sym+4) > addi a1,a1,tp,%tprel_add(sym+4) > sw a0,%tprel_lo(sym+4)(a1) >=20 > ... which, if %tprel_hi(sym+4) =3D=3D 0, as it often is for small embedded > software, the linker can relax to a simple (tp) reference: >=20 > # Version 2a (post-relaxation with small .tbss) > sw a0,%tprel_lo(sym+4)(tp) >=20 > The linker will *not* relax version 1 all the way; leaving an unnecessary= mv: >=20 > # Version 1a (post-relaxation with small .tbss) > mv a1,tp > sw a0,%tprel_lo(sym+4)(tp) >=20 > It is of course trickier for the case of multiple subsequent references to > the structure if the structure is not aligned, as gcc can't know a priori > where the 4K breaks are[*]. The version 1 code is more efficient in that > case (3 instructions + 1 instruction/field as opposed to 3 > instructions/field.) >=20 > However, if the structure *is* aligned, gcc will still not optimize 1 int= o 2. >=20 > There are at least a few options I see: >=20 > 1. gcc option: gcc can generate version 2 code for a single field referen= ce, > or if the alignment is such that all fields are guaranteed to fall inside > the same 4K window. IIUC we could do this without adding anything to the linker or psABI, it's = just better code from GCC (we already have TPREL_LO12_S for the stores). That's just better code so it seems uncontroversial to me. > 2. gcc and optional ABI option: introduce a "TLS TE-tiny" model for deep > embedded use, where the combined size of the TSS area is limited to 4K > equivalent to the way direct gp references [or zero, if the global pointer > is 0] work. Thus, direct (tp) references can be used. Unless I'm missing something, we never emit direct GP references from GCC r= ight now. We rely on the linker to relax them. > NOTE: With the current binutils, this will error unless .option norelax is > in effect. It might be desirable to instead have a new relocation type, > which would require binutils support. Alternatively, ld should recognize > that the TLS offset is within +/- 2K and suppress the warning in that case > (since at that point the address is available the the linker.) >=20 > The linker could be further optimized by allowing the TLS to offset; > presumably equivalently to the __global_pointer$ symbol. >=20 > 3. binutils option: teach ld to relax these kinds of chained pointer > references. I'd favor adding support better for relaxing TP-relative sequences to the linker where we can, it avoids the need for a new code model and we've alre= ady got most of the linker complexity as it's required for GP. So I think we c= an essentially just call these LD missed optimizations. Nelson might be out f= or a bit, but I added him to the CC list. > [*] Rant: in my opinion, the lui/auipc instructions are fundamentally > misdesigned by not having an overlap bit to guarantee a sizable window. I agree we've got auipc issues, it bites us all over the place (we essentia= lly can't share a hi* between multiple lo*s, as we don't know when overflow is going to happen). There'd been some vague proposals to add a third relocat= ion in the chain to align things, but I think they fizzled out because it'd req= uire talking to the psABI folks. I think we're broadly safe for lui, though, so not sure if I'm missing something there? The low bits are always 0 so the intermediate alignment is known.=