diff mbox

[i386] Support BMI and BMI2 targets in multiversioning

Message ID CAFULd4bJVYwfhhrdnQpRkSPSwWFO2hZr8UFtXdU-eFvduSCPuQ@mail.gmail.com
State New
Headers show

Commit Message

Uros Bizjak Jan. 25, 2015, 6:37 p.m. UTC
On Sun, Jan 25, 2015 at 7:23 PM, Uros Bizjak <ubizjak@gmail.com> wrote:
> On Sat, Jan 24, 2015 at 11:49 AM, Allan Sandfeld Jensen
> <allan@carewolf.com> wrote:
>> On Saturday 24 January 2015, Uros Bizjak wrote:
>>> On Mon, Jan 12, 2015 at 6:02 PM, Uros Bizjak <ubizjak@gmail.com> wrote:
>>> > Hello!
>>> >
>>> >>> On Wed, Dec 31, 2014 at 01:28:47PM +0100, Allan Sandfeld Jensen wrote:
>>> >>> > I recently wanted to use multiversioning for BMI2 specific extensions
>>> >>> > PDEP/PEXT, and noticed it wasn't there. So I wrote this patch to add
>>> >>> > it, and also added AES, F16C and BMI1 for completeness.
>>> >>>
>>> >>> AES nor F16C doesn't make any sense IMHO for multiversioning, you need
>>> >>> special intrinsics for that anyway and when you use them, the function
>>> >>> will fail to compile without those features.
>>> >>> Multiversioning only makes sense for ISA features the compiler uses for
>>> >>> normal C/C++ code without any intrinsics.
>>> >>
>>> >> Patch reduced to just adding BMI and BMI2 multiversioning:
>>> > +2014-12-29  Allan Sandfeld Jensen  <sandfeld@kde.org>
>>> > +
>>> > + * config/i386/i386.c (get_builtin_code_for_version): Add
>>> > + support for BMI and BMI2 multiversion functions.
>>> >
>>> > +2014-12-29  Allan Sandfeld Jensen  <sandfeld@kde.org>
>>> > +
>>> > + * gcc.target/i386/funcspec-5.c: Test new multiversion targets.
>>> > + * g++.dg/ext/mv17.C: Test BMI/BMI2 multiversion dispatcher.
>>> >
>>> > +2014-12-29  Allan Sandfeld Jensen  <sandfeld@kde.org>
>>> > +
>>> > + * config/i386/cpuinfo.c (enum processor_features): Add FEATURE_BMI and
>>> > + FEATURE_BMI2.
>>> > + (get_available_features): Detect FEATURE_BMI and FEATURE_BMI2.
>>> >
>>> > OK for mainline
>>>
>>> Allan, did you commit the patch to mainline? I don't see it in SVN logs.
>>>
>>> (If you don't have SVN commit access, please mention it in the patch
>>> submission, so someone will commit the patch for you).
>>>
>> Sorry. I don't have SVN commit access.
>
> Committed with a bunch of fixes (e.g. missing fold_builtin_cpu part in
> gcc/config/i386/i386.c, and mv17.C test didn't compile at all due to
> missing parenthesis).

... and now with committed ChangeLog and patch.

gcc/ChangeLog:

    * config/i386/i386.c (get_builtin_code_for_version): Add
    support for BMI and BMI2 multiversion functions.
    (fold_builtin_cpu): Add F_BMI and F_BMI2.

libgcc/ChangeLog:

    * config/i386/cpuinfo.c (enum processor_features): Add FEATURE_BMI and
    FEATURE_BMI2.
    (get_available_features): Detect FEATURE_BMI and FEATURE_BMI2.

testsuite/ChangeLog:

    * gcc.target/i386/funcspec-5.c: Test new multiversion targets.
    * g++.dg/ext/mv17.C: Test BMI/BMI2 multiversion dispatcher.

Uros.
diff mbox

Patch

Index: gcc/config/i386/i386.c
===================================================================
--- gcc/config/i386/i386.c	(revision 220091)
+++ gcc/config/i386/i386.c	(working copy)
@@ -34289,15 +34289,18 @@  get_builtin_code_for_version (tree decl, tree *pre
     P_PROC_SSE4_A,
     P_SSE4_1,
     P_SSE4_2,
+    P_POPCNT,
     P_PROC_SSE4_2,
-    P_POPCNT,
     P_AVX,
     P_PROC_AVX,
+    P_BMI,
+    P_PROC_BMI,
     P_FMA4,
     P_XOP,
     P_PROC_XOP,
     P_FMA,    
     P_PROC_FMA,
+    P_BMI2,
     P_AVX2,
     P_PROC_AVX2,
     P_AVX512F,
@@ -34323,12 +34326,14 @@  get_builtin_code_for_version (tree decl, tree *pre
       {"sse4.2", P_SSE4_2},
       {"popcnt", P_POPCNT},
       {"avx", P_AVX},
+      {"bmi", P_BMI},
       {"fma4", P_FMA4},
       {"xop", P_XOP},
       {"fma", P_FMA},
+      {"bmi2", P_BMI2},
       {"avx2", P_AVX2},
       {"avx512f", P_AVX512F}
     };
@@ -34423,7 +34428,7 @@  get_builtin_code_for_version (tree decl, tree *pre
 	      break;
 	    case PROCESSOR_BTVER2:
 	      arg_str = "btver2";
-	      priority = P_PROC_AVX;
+	      priority = P_PROC_BMI;
 	      break;
 	    case PROCESSOR_BDVER1:
 	      arg_str = "bdver1";
@@ -35310,6 +35315,8 @@  fold_builtin_cpu (tree fndecl, tree *args)
     F_XOP,
     F_FMA,
     F_AVX512F,
+    F_BMI,
+    F_BMI2,
     F_MAX
   };
 
@@ -35403,7 +35410,9 @@  fold_builtin_cpu (tree fndecl, tree *args)
       {"xop",    F_XOP},
       {"fma",    F_FMA},
       {"avx2",   F_AVX2},
-      {"avx512f",F_AVX512F}
+      {"avx512f",F_AVX512F},
+      {"bmi",    F_BMI},
+      {"bmi2",   F_BMI2}
     };
 
   tree __processor_model_type = build_processor_model_struct ();
Index: gcc/testsuite/gcc.target/i386/funcspec-5.c
===================================================================
--- gcc/testsuite/gcc.target/i386/funcspec-5.c	(revision 220091)
+++ gcc/testsuite/gcc.target/i386/funcspec-5.c	(working copy)
@@ -25,6 +25,8 @@  extern void test_tbm (void)			__attribute__((__tar
 extern void test_avx (void)			__attribute__((__target__("avx")));
 extern void test_avx2 (void)			__attribute__((__target__("avx2")));
 extern void test_avx512f (void)			__attribute__((__target__("avx512f")));
+extern void test_bmi (void)			__attribute__((__target__("bmi")));
+extern void test_bmi2 (void)			__attribute__((__target__("bmi2")));
 
 extern void test_no_abm (void)			__attribute__((__target__("no-abm")));
 extern void test_no_aes (void)			__attribute__((__target__("no-aes")));
@@ -48,6 +50,8 @@  extern void test_no_tbm (void)			__attribute__((__
 extern void test_no_avx (void)			__attribute__((__target__("no-avx")));
 extern void test_no_avx2 (void)   		__attribute__((__target__("no-avx2")));
 extern void test_no_avx512f (void)   		__attribute__((__target__("no-avx512f")));
+extern void test_no_bmi (void)			__attribute__((__target__("no-bmi")));
+extern void test_no_bmi2 (void)			__attribute__((__target__("no-bmi2")));
 
 extern void test_arch_i386 (void)		__attribute__((__target__("arch=i386")));
 extern void test_arch_i486 (void)		__attribute__((__target__("arch=i486")));
Index: gcc/testsuite/g++.dg/ext/mv17.C
===================================================================
--- gcc/testsuite/g++.dg/ext/mv17.C	(revision 0)
+++ gcc/testsuite/g++.dg/ext/mv17.C	(revision 220095)
@@ -0,0 +1,93 @@ 
+// Test case to check if Multiversioning works for BMI and BMI2.
+
+// { dg-do run { target i?86-*-* x86_64-*-* } }
+// { dg-require-ifunc "" }
+// { dg-options "-O2" }
+
+#include <assert.h>
+
+// Check BMI feature selection works
+int foo () __attribute__((target("default")));
+int foo () __attribute__((target("bmi")));
+int foo () __attribute__((target("bmi2")));
+
+// Check specialized versions for archs with BMI is chosen over generic BMI versions.
+int bar () __attribute__((target("default")));
+int bar () __attribute__((target("bmi")));
+int bar () __attribute__((target("bmi2")));
+int bar () __attribute__((target("arch=btver2")));
+int bar () __attribute__((target("arch=haswell")));
+
+int main ()
+{
+  int val = foo ();
+
+  if (__builtin_cpu_supports ("bmi2"))
+    assert (val == 2);
+  else if (__builtin_cpu_supports ("bmi"))
+    assert (val == 1);
+  else
+    assert (val == 0);
+
+  val = bar ();
+
+  if (__builtin_cpu_is ("btver2"))
+    assert (val == 5);
+  else if (__builtin_cpu_is ("haswell"))
+    assert (val == 6);
+  else if (__builtin_cpu_supports ("bmi2"))
+    assert (val == 2);
+  else if (__builtin_cpu_supports ("bmi"))
+    assert (val == 1);
+  else
+    assert (val == 0);
+
+  return 0;
+}
+
+int __attribute__ ((target("default")))
+foo ()
+{
+  return 0;
+}
+
+int __attribute__ ((target("bmi")))
+foo ()
+{
+  return 1;
+}
+int __attribute__ ((target("bmi2")))
+foo ()
+{
+  return 2;
+}
+
+int __attribute__ ((target("default")))
+bar ()
+{
+  return 0;
+}
+
+int __attribute__ ((target("bmi")))
+bar ()
+{
+  return 1;
+}
+int __attribute__ ((target("bmi2")))
+bar ()
+{
+  return 2;
+}
+
+int __attribute__ ((target("arch=btver2")))
+bar ()
+{
+  return 5;
+}
+
+int __attribute__ ((target("arch=haswell")))
+bar ()
+{
+  return 6;
+}
+
Index: libgcc/config/i386/cpuinfo.c
===================================================================
--- libgcc/config/i386/cpuinfo.c	(revision 220091)
+++ libgcc/config/i386/cpuinfo.c	(working copy)
@@ -98,7 +98,9 @@  enum processor_features
   FEATURE_FMA4,
   FEATURE_XOP,
   FEATURE_FMA,
-  FEATURE_AVX512F
+  FEATURE_AVX512F,
+  FEATURE_BMI,
+  FEATURE_BMI2
 };
 
 struct __processor_model
@@ -289,8 +291,12 @@  get_available_features (unsigned int ecx, unsigned
     {
       unsigned int eax, ebx, ecx, edx;
       __cpuid_count (7, 0, eax, ebx, ecx, edx);
+      if (ebx & bit_BMI)
+        features |= (1 << FEATURE_BMI);
       if (ebx & bit_AVX2)
 	features |= (1 << FEATURE_AVX2);
+      if (ebx & bit_BMI2)
+        features |= (1 << FEATURE_BMI2);
       if (ebx & bit_AVX512F)
 	features |= (1 << FEATURE_AVX512F);
     }