public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: "H.J. Lu via Libc-alpha" <libc-alpha@sourceware.org>
Subject: Re: V2 [PATCH] <sys/platform/x86.h>: Remove the C preprocessor magic
Date: Wed, 20 Jan 2021 16:58:30 +0100	[thread overview]
Message-ID: <87turb4n2x.fsf@oldenburg.str.redhat.com> (raw)
In-Reply-To: <20210120144339.GA1866418@gmail.com> (H. J. Lu via Libc-alpha's message of "Wed, 20 Jan 2021 06:43:39 -0800")

* H. J. Lu via Libc-alpha:

> On Wed, Jan 20, 2021 at 10:53:07AM +0100, Florian Weimer wrote:
>> * H. J. Lu via Libc-alpha:
>> 
>> > +enum
>> > +{
>> > +  COMMON_CPUID_INDEX_1 = 0,
>> > +  COMMON_CPUID_INDEX_7,
>> > +  COMMON_CPUID_INDEX_80000001,
>> > +  COMMON_CPUID_INDEX_D_ECX_1,
>> > +  COMMON_CPUID_INDEX_80000007,
>> > +  COMMON_CPUID_INDEX_80000008,
>> > +  COMMON_CPUID_INDEX_7_ECX_1,
>> > +  COMMON_CPUID_INDEX_19,
>> > +  /* Keep the following line at the end.  */
>> > +  COMMON_CPUID_INDEX_MAX
>> > +};
>> 
>> COMMON_CPUID_INDEX_MAX should not be in the public header because it
>> subject to changes.
>
> Fixed.
>
>> 
>> > +struct cpuid_registers
>> > +{
>> > +  unsigned int eax;
>> > +  unsigned int ebx;
>> > +  unsigned int ecx;
>> > +  unsigned int edx;
>> > +};
>> 
>> Should the non-public interfaces use __?
>
> Moved to sysdeps/x86/include/cpu-features.h.
>
>> 
>> > +enum cpu_features_kind
>> > +{
>> > +  arch_kind_unknown = 0,
>> > +  arch_kind_intel,
>> > +  arch_kind_amd,
>> > +  arch_kind_zhaoxin,
>> > +  arch_kind_other
>> > +};
>> > +
>> > +struct cpu_features_basic
>> > +{
>> > +  enum cpu_features_kind kind;
>> > +  int max_cpuid;
>> > +  unsigned int family;
>> > +  unsigned int model;
>> > +  unsigned int stepping;
>> > +};
>> 
>> Should we really expose all this?  It's not documented.  enum
>> cpu_features_kind doesn't seem to be compatible with how people use
>> virtualization.
>
> Moved to sysdeps/x86/include/cpu-features.h.
>
>> 
>> > diff --git a/sysdeps/x86/dl-get-cpu-features.c b/sysdeps/x86/dl-get-cpu-features.c
>> > index 349472d99f..4636d9f4a7 100644
>> > --- a/sysdeps/x86/dl-get-cpu-features.c
>> > +++ b/sysdeps/x86/dl-get-cpu-features.c
>> > @@ -46,9 +46,9 @@ __ifunc (__x86_cpu_features, __x86_cpu_features, NULL, void,
>> >  #undef __x86_get_cpu_features
>> >  
>> >  const struct cpu_features *
>> > -__x86_get_cpu_features (unsigned int max)
>> > +__x86_get_cpu_features (unsigned int index)
>> >  {
>> > -  if (max > COMMON_CPUID_INDEX_MAX)
>> > +  if (index > COMMON_CPUID_INDEX_MAX * 8 * sizeof (unsigned int) * 4)
>> >      return NULL;
>> >    return &GLRO(dl_x86_cpu_features);
>> >  }
>> 
>> This should work in principle (but I haven't verified the boundary
>> condition).
>> 
>> Could you pass the COMMON_CPUID_INDEX_* value (without the register and
>> bit selectors)?  Then more calls can be subject to common subexpression
>> elimination in the caller, leading to more compact code.
>
> My current public API uses a single enum index for each CPU feature
> detection.  Use 2 indices for each CPU feature makes it less user
> friendly.  Internally, we use the C preprocessor magic so that more
> calls can be subject to common subexpression elimination in the caller,
> leading to more compact code for glibc.

I had this in mind for the implementation:

struct cpuid_feature
{
  unsigned int cpuid_array[4];
  unsigned int usable_array[4];
};

const struct cpuid_feature *
__x86_get_cpu_features (unsigned int leaf)
{
  const struct cpu_feature future = { };
  if (leaf < CPUID_INDEX_MAX)
    return &GLRO(dl_x86_cpu_features).features[leaf];
  else
    return &future;
}

And in the header:

  unsigned int leaf = index / (8 * sizeof (unsigned int) * 4);

const struct cpuid_feature * __x86_get_cpu_features (unsigned int leaf)
  __attribute__ ((pure));

static inline const struct cpuid_feature *
__x86_get_cpu_leaf (unsigned int index)
{
  return __x86_get_cpu_features (index / (8 * sizeof (unsigned int) * 4));
}
#define HAS_CPU_FEATURE(name)					\
   (x86_cpu_has_feature (__x86_get_cpu_leaf (x86_cpu_##name),   \
                         x86_cpu_##name))
#define HAS_CPU_FEATURE(name)					\
   (x86_cpu_is_usable (__x86_get_cpu_leaf (x86_cpu_##name),     \
                       x86_cpu_##name))


I expect that the index arguments to __x86_get_cpu_features are
constant-folded by GCC, and many of the __x86_get_cpu_features calls can
be CSE'ed as a result.

In the public interface, the max_cpuid field appears redundant, so this
is why I think this will work.

Thanks,
Florian
-- 
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill


  reply	other threads:[~2021-01-20 15:58 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-25 16:24 H.J. Lu
2021-01-20  9:53 ` Florian Weimer
2021-01-20 11:23   ` Florian Weimer
2021-01-20 14:43   ` V2 " H.J. Lu
2021-01-20 15:58     ` Florian Weimer [this message]
2021-01-21  0:19       ` V3 " H.J. Lu
2021-01-21 11:19         ` Florian Weimer
2021-01-21 13:29           ` V4 " H.J. Lu
2021-01-21 13:57             ` Florian Weimer
2021-01-21 14:12               ` Adhemerval Zanella

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=87turb4n2x.fsf@oldenburg.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.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).