public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Uros Bizjak <ubizjak@gmail.com>
To: GCC Development <gcc@gcc.gnu.org>
Cc: X86 ML <x86@kernel.org>, Jakub Jelinek <jakub@redhat.com>,
	 Andy Lutomirski <luto@amacapital.net>
Subject: typeof and operands in named address spaces
Date: Wed, 4 Nov 2020 19:31:42 +0100	[thread overview]
Message-ID: <CAFULd4aOca3k-AZ+ocMBqDRpH_kNisBQwfK0F4Jf7znsPxsrBw@mail.gmail.com> (raw)

Hello!

I was looking at the recent linux patch series [1] where segment
qualifiers (named address spaces) were introduced to handle percpu
variables. In the patch [2], the author mentions that:

--q--
Unfortunately, gcc does not provide a way to remove segment
qualifiers, which is needed to use typeof() to create local instances
of the per-cpu variable. For this reason, do not use the segment
qualifier for per-cpu variables, and do casting using the segment
qualifier instead.
--/q--

The core of the problem can be seen with the following testcase:

--cut here--
#define foo(_var)                    \
  ({                            \
    typeof(_var) tmp__;                    \
    asm ("mov %1, %0" : "=r"(tmp__) : "m"(_var));    \
    tmp__;                        \
  })

__seg_fs int x;

int test (void)
{
  int y;

  y = foo (x);
  return y;
}
--cut here--

when compiled with -O2 for x86 target, the compiler reports:

pcpu.c: In function ‘test’:
pcpu.c:14:3: error: ‘__seg_fs’ specified for auto variable ‘tmp__’

It looks to me that the compiler should remove address space
information when typeof is used, otherwise, there is no way to use
typeof as intended in the above example.

A related problem is exposed when we want to cast address from the
named address space to a generic address space (e.g. to use it with
LEA):

--cut here--
typedef __UINTPTR_TYPE__ uintptr_t;

__seg_fs int x;

uintptr_t test (void)
{
  uintptr_t *p = (uintptr_t *) &y;
  uintptr_t addr;

  asm volatile ("lea %1, %0" : "=r"(addr) : "m"(*p));

  return addr;
}
--cut here--

The gcc documentation advises explicit casts:

--q--
This means that explicit casts are required to convert pointers
between these address spaces and the generic address space.  In
practice the application should cast to 'uintptr_t' and apply the
segment base offset that it installed previously.
--/q--

However, a warning is emitted when compiling the above example:

pcpu1.c: In function ‘test’:
pcpu1.c:7:18: warning: cast to generic address space pointer from
disjoint __seg_fs address space pointer

but the desired result is obtained nevertheless.

       lea x(%rip), %rax

As shown in the referred patchset, named address spaces have quite
some optimization potential, please see [1] for the list.

[1] https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg2053461.html
[2] https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg2053462.html

Uros.

             reply	other threads:[~2020-11-04 18:31 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-04 18:31 Uros Bizjak [this message]
2020-11-05  7:26 ` Richard Biener
2020-11-05  8:56   ` Uros Bizjak
2020-11-05  9:36     ` Alexander Monakov
2020-11-05 10:33       ` Uros Bizjak
2020-11-05 11:38         ` Alexander Monakov
2020-11-05 12:00           ` Uros Bizjak
2020-11-05 12:14             ` Alexander Monakov
2020-11-05 12:24               ` Richard Biener
2020-11-05 12:32                 ` Uros Bizjak
2020-11-05 12:35                   ` Uros Bizjak
2020-11-05 13:22                     ` Alexander Monakov
2020-11-05 13:39                       ` Alexander Monakov
2020-11-05 13:46                         ` Uros Bizjak
2020-11-05 12:26               ` Uros Bizjak
2020-11-05 15:27                 ` Andy Lutomirski
2020-11-05 11:03       ` Uros Bizjak
2020-11-05  9:45     ` Richard Biener
2020-11-05  9:51       ` Jakub Jelinek
2020-11-09 12:47 ` Peter Zijlstra
2020-11-09 19:38   ` Segher Boessenkool
2020-11-09 19:50     ` Nick Desaulniers
2020-11-10  7:57       ` Peter Zijlstra
2020-11-10 18:42         ` Nick Desaulniers
2020-11-10 20:11           ` Peter Zijlstra
2020-11-12  0:40             ` Segher Boessenkool
2020-11-12  0:47         ` Segher Boessenkool
2020-11-10  7:52     ` Peter Zijlstra

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=CAFULd4aOca3k-AZ+ocMBqDRpH_kNisBQwfK0F4Jf7znsPxsrBw@mail.gmail.com \
    --to=ubizjak@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=luto@amacapital.net \
    --cc=x86@kernel.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).