public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "hpa at zytor dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/113686] New: [RISC-V] TLS (Local Exec) relaxation on structures (LE)
Date: Wed, 31 Jan 2024 18:47:56 +0000	[thread overview]
Message-ID: <bug-113686-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 113686
           Summary: [RISC-V] TLS (Local Exec) relaxation on structures
                    (LE)
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hpa at zytor dot com
  Target Milestone: ---

When the Local Exec TLS model is in use, gcc generates inefficient code for
accessing the member of a structure:

struct foobar {
   int alpha;
   int beta;
};

_Thread_local struct foobar foo;

void func(int bar)
{
    foo.beta = bar;
}

    # Version 1
    lui    a1,%tprel_hi(foo)
    add    a1,a1,tp,%tprel_add(foo)
    addi   a1,a1,%tprel_lo(foo)
    sw     a0,4(a1)

However, in this case it could be generated as:

    # Version 2
    lui    a1,%tprel_hi(sym+4)
    addi   a1,a1,tp,%tprel_add(sym+4)
    sw     a0,%tprel_lo(sym+4)(a1)

... which, if %tprel_hi(sym+4) == 0, as it often is for small embedded
software, the linker can relax to a simple (tp) reference:

    # Version 2a (post-relaxation with small .tbss)
    sw a0,%tprel_lo(sym+4)(tp)

The linker will *not* relax version 1 all the way; leaving an unnecessary mv:

    # Version 1a (post-relaxation with small .tbss)
    mv a1,tp
    sw a0,%tprel_lo(sym+4)(tp)

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.)

However, if the structure *is* aligned, gcc will still not optimize 1 into 2.

There are at least a few options I see:

1. gcc option: gcc can generate version 2 code for a single field reference, or
if the alignment is such that all fields are guaranteed to fall inside the same
4K window.

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.

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.)

The linker could be further optimized by allowing the TLS to offset; presumably
equivalently to the __global_pointer$ symbol.

3. binutils option: teach ld to relax these kinds of chained pointer
references.



[*] Rant: in my opinion, the lui/auipc instructions are fundamentally
misdesigned by not having an overlap bit to guarantee a sizable window.

             reply	other threads:[~2024-01-31 18:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-31 18:47 hpa at zytor dot com [this message]
2024-01-31 19:05 ` [Bug target/113686] " palmer at gcc dot gnu.org
2024-02-01 20:24 ` hpa at zytor dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-113686-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).