[pypy-commit] pypy default: Slim down the mess in detect_cpu.

arigo noreply at buildbot.pypy.org
Mon May 6 14:32:00 CEST 2013


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r63876:ca87dbabe3df
Date: 2013-05-06 14:31 +0200
http://bitbucket.org/pypy/pypy/changeset/ca87dbabe3df/

Log:	Slim down the mess in detect_cpu.

diff --git a/pypy/module/__pypy__/__init__.py b/pypy/module/__pypy__/__init__.py
--- a/pypy/module/__pypy__/__init__.py
+++ b/pypy/module/__pypy__/__init__.py
@@ -82,9 +82,16 @@
         PYC_MAGIC = get_pyc_magic(self.space)
         self.extra_interpdef('PYC_MAGIC', 'space.wrap(%d)' % PYC_MAGIC)
         #
-        from rpython.jit.backend import detect_cpu
-        model = detect_cpu.autodetect_main_model_and_size()
-        self.extra_interpdef('cpumodel', 'space.wrap(%r)' % model)
+        try:
+            from rpython.jit.backend import detect_cpu
+            model = detect_cpu.autodetect()
+            self.extra_interpdef('cpumodel', 'space.wrap(%r)' % model)
+        except Exception:
+            if self.space.config.translation.jit:
+                raise
+            else:
+                pass   # ok fine to ignore in this case
+        #
         if self.space.config.translation.jit:
             features = detect_cpu.getcpufeatures(model)
             self.extra_interpdef('jit_backend_features',
diff --git a/pypy/tool/jitlogparser/test/test_parser.py b/pypy/tool/jitlogparser/test/test_parser.py
--- a/pypy/tool/jitlogparser/test/test_parser.py
+++ b/pypy/tool/jitlogparser/test/test_parser.py
@@ -4,7 +4,7 @@
                                            parse_log_counts)
 from pypy.tool.jitlogparser.storage import LoopStorage
 import py, sys
-from rpython.jit.backend.detect_cpu import autodetect_main_model
+from rpython.jit.backend.detect_cpu import autodetect
 
 def parse(input, **kwds):
     return SimpleParser.parse_from_input(input, **kwds)
@@ -189,7 +189,7 @@
     assert chunk.bytecode_name.startswith('StrLiteralSearch')
 
 def test_parsing_assembler():
-    if not autodetect_main_model() == 'x86':
+    if not autodetect().startswith('x86'):
         py.test.skip('x86 only test')
     backend_dump = "554889E5534154415541564157488DA500000000488B042590C5540148C7042590C554010000000048898570FFFFFF488B042598C5540148C7042598C554010000000048898568FFFFFF488B0425A0C5540148C70425A0C554010000000048898560FFFFFF488B0425A8C5540148C70425A8C554010000000048898558FFFFFF4C8B3C2550525B0149BB30E06C96FC7F00004D8B334983C60149BB30E06C96FC7F00004D89334981FF102700000F8D000000004983C7014C8B342580F76A024983EE014C89342580F76A024983FE000F8C00000000E9AEFFFFFF488B042588F76A024829E0483B042580EC3C01760D49BB05F30894FC7F000041FFD3554889E5534154415541564157488DA550FFFFFF4889BD70FFFFFF4889B568FFFFFF48899560FFFFFF48898D58FFFFFF4D89C7E954FFFFFF49BB00F00894FC7F000041FFD34440484C3D030300000049BB00F00894FC7F000041FFD34440484C3D070304000000"
     dump_start = 0x7f3b0b2e63d5
@@ -218,7 +218,7 @@
     assert 'jmp' in loop.operations[-1].asm
 
 def test_parsing_arm_assembler():
-    if not autodetect_main_model() == 'arm':
+    if not autodetect().startswith('arm'):
         py.test.skip('ARM only test')
     backend_dump = "F04F2DE9108B2DED2CD04DE20DB0A0E17CC302E3DFC040E300409CE5085084E2086000E3006084E504B084E500508CE508D04BE20000A0E10000A0E1B0A10DE30EA044E300A09AE501A08AE2B0910DE30E9044E300A089E5C0910DE30E9044E3009099E5019089E2C0A10DE30EA044E300908AE5010050E1700020E124A092E500C08AE00C90DCE5288000E3090058E10180A0030080A013297000E3090057E10170A0030070A013077088E1200059E30180A0030080A013099049E2050059E30190A0330090A023099088E1000059E30190A0130090A003099087E1000059E3700020E1010080E204200BE5D0210DE30E2044E3002092E5012082E2D0910DE30E9044E3002089E5010050E1700020E100C08AE00C90DCE5282000E3090052E10120A0030020A013297000E3090057E10170A0030070A013077082E1200059E30120A0030020A013099049E2050059E30190A0330090A023099082E1000059E30190A0130090A003099087E1000059E3700020E1010080E20D005BE10FF0A0A1700020E1D8FFFFEA68C100E301C04BE33CFF2FE105010803560000000000000068C100E301C04BE33CFF2FE105010803570000000000000068C100E301C04BE33CFF2FE105014003580000000000000068C100E301C04BE33CFF2FE1050140035900000000000000"
     dump_start = int(-0x4ffee930)
@@ -272,7 +272,7 @@
 
 
 def test_import_log():
-    if not autodetect_main_model() == 'x86':
+    if not autodetect().startswith('x86'):
         py.test.skip('x86 only test')
     _, loops = import_log(str(py.path.local(__file__).join('..',
                                                            'logtest.log')))
@@ -281,7 +281,7 @@
     assert 'jge' in loops[0].operations[3].asm
 
 def test_import_log_2():
-    if not autodetect_main_model() == 'x86':
+    if not autodetect().startswith('x86'):
         py.test.skip('x86 only test')
     _, loops = import_log(str(py.path.local(__file__).join('..',
                                                            'logtest2.log')))
diff --git a/rpython/jit/backend/detect_cpu.py b/rpython/jit/backend/detect_cpu.py
--- a/rpython/jit/backend/detect_cpu.py
+++ b/rpython/jit/backend/detect_cpu.py
@@ -10,31 +10,31 @@
     pass
 
 
-def detect_main_model_and_size_from_platform():
+MODEL_X86         = 'x86'
+MODEL_X86_NO_SSE2 = 'x86-without-sse2'
+MODEL_X86_64      = 'x86-64'
+MODEL_ARM         = 'arm'
+MODEL_PPC_64      = 'ppc-64'
+# don't use '_' in the model strings; they are replaced by '-'
+
+
+def detect_model_from_c_compiler():
     # based on http://sourceforge.net/p/predef/wiki/Architectures/
     mapping = {
-            ('x86', '64'): [
-                '__amd64__', '__amd64', '__x86_64__', '__x86_64',  # AMD64
-                ],
-            ('arm', '32'): ['__arm__', '__thumb__'],
-            ('x86', '32'): ['i386', '__i386', '__i386__', '__i686__',],
-            ('ppc', '64'): ['__powerpc64__'],
+        MODEL_X86_64: ['__amd64__', '__amd64', '__x86_64__', '__x86_64'],
+        MODEL_ARM:    ['__arm__', '__thumb__'],
+        MODEL_X86:    ['i386', '__i386', '__i386__', '__i686__'],
+        MODEL_PPC_64: ['__powerpc64__'],
     }
     for k, v in mapping.iteritems():
         for macro in v:
             if not getdefined(macro, ''):
                 continue
-            return '_'.join(k)
+            return k
     raise ProcessorAutodetectError, "Cannot detect processor using compiler macros"
 
 
-def detect_main_model_from_platform():
-    return detect_main_model_and_size_from_platform()[0]
-
-
-def autodetect_main_model():
-    if not is_host_build():
-        return detect_main_model_from_platform()
+def detect_model_from_host_platform():
     mach = None
     try:
         import platform
@@ -44,67 +44,64 @@
     if not mach:
         platform = sys.platform.lower()
         if platform.startswith('win'):   # assume an Intel Windows
-            return 'x86'
+            return MODEL_X86
         # assume we have 'uname'
         mach = os.popen('uname -m', 'r').read().strip()
         if not mach:
             raise ProcessorAutodetectError, "cannot run 'uname -m'"
-    try:
-        return {'i386': 'x86',
-                'i486': 'x86',
-                'i586': 'x86',
-                'i686': 'x86',
-                'i86pc': 'x86',    # Solaris/Intel
-                'x86':   'x86',    # Apple
-                'Power Macintosh': 'ppc',
-                'x86_64': 'x86',
-                'amd64': 'x86',    # freebsd
-                'AMD64': 'x86',    # win64
-                'armv7l': 'arm',
-                'armv6l': 'arm',
-                }[mach]
-    except KeyError:
-        return mach
+    #
+    result ={'i386': MODEL_X86,
+            'i486': MODEL_X86,
+            'i586': MODEL_X86,
+            'i686': MODEL_X86,
+            'i86pc': MODEL_X86,    # Solaris/Intel
+            'x86': MODEL_X86,      # Apple
+            'Power Macintosh': MODEL_PPC_64,
+            'x86_64': MODEL_X86,
+            'amd64': MODEL_X86,    # freebsd
+            'AMD64': MODEL_X86,    # win64
+            'armv7l': MODEL_ARM,
+            'armv6l': MODEL_ARM,
+            }[mach]
+    #
+    if result.startswith('x86'):
+        if sys.maxint == 2**63-1:
+            result = MODEL_X86_64
+        else:
+            assert sys.maxint == 2**31-1
+            from rpython.jit.backend.x86.detect_sse2 import detect_sse2
+            if detect_sse2():
+                result = MODEL_X86
+            else:
+                result = MODEL_X86_NO_SSE2
+    #
+    if result.startswith('arm'):
+        from rpython.jit.backend.arm.detect import detect_float
+        assert detect_float(), 'the JIT-compiler requires a vfp unit'
+    #
+    return result
 
-def autodetect_main_model_and_size():
-    if not is_host_build():
-        return detect_main_model_and_size_from_platform()
-    model = autodetect_main_model()
-    if sys.maxint == 2**31-1:
-        model += '_32'
-    elif sys.maxint == 2**63-1:
-        model += '_64'
-    else:
-        raise AssertionError, "bad value for sys.maxint"
-    return model
 
 def autodetect():
-    model = autodetect_main_model()
-    if sys.maxint == 2**63-1:
-        model += '_64'
+    if not is_host_build():
+        return detect_model_from_c_compiler()
     else:
-        assert sys.maxint == 2**31-1
-        if model == 'x86':
-            from rpython.jit.backend.x86.detect_sse2 import detect_sse2
-            if not detect_sse2():
-                model = 'x86-without-sse2'
-    if model.startswith('arm'):
-        from rpython.jit.backend.arm.detect import detect_hardfloat, detect_float
-        assert detect_float(), 'the JIT-compiler requires a vfp unit'
-    return model
+        return detect_model_from_host_platform()
+
 
 def getcpuclassname(backend_name="auto"):
     if backend_name == "auto":
         backend_name = autodetect()
-    if backend_name == 'x86' or backend_name == 'x86_32':
+    backend_name = backend_name.replace('_', '-')
+    if backend_name == MODEL_X86:
         return "rpython.jit.backend.x86.runner", "CPU"
-    elif backend_name == 'x86-without-sse2':
+    elif backend_name == MODEL_X86_NO_SSE2:
         return "rpython.jit.backend.x86.runner", "CPU386_NO_SSE2"
-    elif backend_name == 'x86_64':
+    elif backend_name == MODEL_X86_64:
         return "rpython.jit.backend.x86.runner", "CPU_X86_64"
-    elif backend_name == 'cli':
-        return "rpython.jit.backend.cli.runner", "CliCPU"
-    elif backend_name.startswith('arm'):
+    #elif backend_name == 'cli':
+    #    return "rpython.jit.backend.cli.runner", "CliCPU"
+    elif backend_name == MODEL_ARM:
         return "rpython.jit.backend.arm.runner", "CPU_ARM"
     else:
         raise ProcessorAutodetectError, (
@@ -124,5 +121,13 @@
                                 and getattr(cpucls, attr)]
 
 if __name__ == '__main__':
-    print autodetect()
-    print getcpuclassname()
+    if len(sys.argv) > 1:
+        name = sys.argv[1]
+        x = name
+    else:
+        name = 'auto'
+        x = autodetect()
+    x = (x, getcpuclassname(name), getcpufeatures(name))
+    print 'autodetect:     ', x[0]
+    print 'getcpuclassname:', x[1]
+    print 'getcpufeatures: ', x[2]
diff --git a/rpython/jit/backend/test/runner_test.py b/rpython/jit/backend/test/runner_test.py
--- a/rpython/jit/backend/test/runner_test.py
+++ b/rpython/jit/backend/test/runner_test.py
@@ -18,7 +18,7 @@
 from rpython.jit.codewriter import heaptracker, longlong
 from rpython.rlib import longlong2float
 from rpython.rlib.rarithmetic import intmask, is_valid_int
-from rpython.jit.backend.detect_cpu import autodetect_main_model_and_size
+from rpython.jit.backend.detect_cpu import autodetect
 from rpython.jit.backend.llsupport import jitframe
 
 
@@ -3539,7 +3539,7 @@
                                               looptoken)
         self.cpu.assembler.set_debug(True) # always on untranslated
         assert info.asmlen != 0
-        cpuname = autodetect_main_model_and_size()
+        cpuname = autodetect()
         # XXX we have to check the precise assembler, otherwise
         # we don't quite know if borders are correct
 
diff --git a/rpython/jit/backend/test/test_detect_cpu.py b/rpython/jit/backend/test/test_detect_cpu.py
--- a/rpython/jit/backend/test/test_detect_cpu.py
+++ b/rpython/jit/backend/test/test_detect_cpu.py
@@ -28,9 +28,10 @@
         assert issubclass(cpu, AbstractCPU)
 
 
-def test_detect_main_model_and_size_from_platform():
-    info = autodetect_main_model_and_size()
-    assert detect_main_model_and_size_from_platform() == info
+def test_detect_model_from_c_compiler():
+    info1 = detect_model_from_host_platform()
+    info2 = detect_model_from_c_compiler()
+    assert info1 == info2
 
 def test_getcpufeatures():
     features = getcpufeatures()
diff --git a/rpython/jit/backend/tool/viewcode.py b/rpython/jit/backend/tool/viewcode.py
--- a/rpython/jit/backend/tool/viewcode.py
+++ b/rpython/jit/backend/tool/viewcode.py
@@ -50,9 +50,12 @@
 def machine_code_dump(data, originaddr, backend_name, label_list=None):
     objdump_backend_option = {
         'x86': 'i386',
+        'x86-without-sse2': 'i386',
         'x86_32': 'i386',
         'x86_64': 'x86-64',
+        'x86-64': 'x86-64',
         'i386': 'i386',
+        'arm': 'arm',
         'arm_32': 'arm',
     }
     cmd = find_objdump()
diff --git a/rpython/jit/backend/x86/test/conftest.py b/rpython/jit/backend/x86/test/conftest.py
--- a/rpython/jit/backend/x86/test/conftest.py
+++ b/rpython/jit/backend/x86/test/conftest.py
@@ -3,7 +3,7 @@
 
 cpu = detect_cpu.autodetect()
 def pytest_runtest_setup(item):
-    if cpu not in ('x86', 'x86_64'):
+    if not cpu.startswith('x86'):
         py.test.skip("x86/x86_64 tests skipped: cpu is %r" % (cpu,))
     if cpu == 'x86_64':
         if os.name == "nt":
diff --git a/rpython/translator/c/gcc/test/conftest.py b/rpython/translator/c/gcc/test/conftest.py
--- a/rpython/translator/c/gcc/test/conftest.py
+++ b/rpython/translator/c/gcc/test/conftest.py
@@ -2,5 +2,5 @@
 from rpython.jit.backend import detect_cpu
 cpu = detect_cpu.autodetect()
 def pytest_runtest_setup(item):
-    if cpu not in ('x86', 'x86_64'):
+    if not cpu.startswith('x86'):
         py.test.skip("x86 directory skipped: cpu is %r" % (cpu,))


More information about the pypy-commit mailing list