| 1 |
""" |
| 2 |
Processor auto-detection |
| 3 |
""" |
| 4 |
from __future__ import print_function |
| 5 |
|
| 6 |
import sys, os |
| 7 |
from rpython.rtyper.tool.rffi_platform import getdefined, getdefinedinteger |
| 8 |
from rpython.translator.platform import is_host_build |
| 9 |
|
| 10 |
|
| 11 |
class ProcessorAutodetectError(Exception): |
| 12 |
pass |
| 13 |
|
| 14 |
|
| 15 |
MODEL_X86 = 'x86' |
| 16 |
MODEL_X86_NO_SSE2 = 'x86-without-sse2' |
| 17 |
MODEL_X86_64 = 'x86-64' |
| 18 |
MODEL_ARM = 'arm' |
| 19 |
MODEL_ARM64 = 'aarch64' |
| 20 |
MODEL_PPC_64 = 'powerpc64le' |
| 21 |
MODEL_S390_64 = 's390x' |
| 22 |
MODEL_RISCV_64 = 'riscv64' |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
def detect_model_from_c_compiler(): |
| 27 |
|
| 28 |
|
| 29 |
mapping = { |
| 30 |
MODEL_X86_64: ['__amd64__', '__amd64', '__x86_64__', '__x86_64', '_M_X64', '_M_AMD64'], |
| 31 |
MODEL_ARM: ['__arm__', '__thumb__','_M_ARM_EP'], |
| 32 |
MODEL_ARM64: ['__aarch64__'], |
| 33 |
MODEL_X86: ['i386', '__i386', '__i386__', '__i686__','_M_IX86'], |
| 34 |
MODEL_PPC_64: ['__powerpc64__'], |
| 35 |
MODEL_S390_64:['__s390x__'], |
| 36 |
} |
| 37 |
for k, v in mapping.iteritems(): |
| 38 |
for macro in v: |
| 39 |
if not getdefined(macro, ''): |
| 40 |
continue |
| 41 |
return k |
| 42 |
if getdefinedinteger('__riscv_xlen', '') == 64: |
| 43 |
return MODEL_RISCV_64 |
| 44 |
raise ProcessorAutodetectError("Cannot detect processor using compiler macros") |
| 45 |
|
| 46 |
|
| 47 |
def detect_model_from_host_platform(): |
| 48 |
mach = None |
| 49 |
try: |
| 50 |
import platform |
| 51 |
mach = platform.machine() |
| 52 |
except ImportError: |
| 53 |
pass |
| 54 |
if not mach: |
| 55 |
platform = sys.platform.lower() |
| 56 |
if platform.startswith('win'): |
| 57 |
return MODEL_X86 |
| 58 |
|
| 59 |
mach = os.popen('uname -m', 'r').read().strip() |
| 60 |
if not mach: |
| 61 |
raise ProcessorAutodetectError("cannot run 'uname -m'") |
| 62 |
|
| 63 |
result ={'i386': MODEL_X86, |
| 64 |
'i486': MODEL_X86, |
| 65 |
'i586': MODEL_X86, |
| 66 |
'i686': MODEL_X86, |
| 67 |
'i686-AT386': MODEL_X86, |
| 68 |
'i86pc': MODEL_X86, |
| 69 |
'x86': MODEL_X86, |
| 70 |
'Power Macintosh': MODEL_PPC_64, |
| 71 |
'powerpc': MODEL_PPC_64, |
| 72 |
'ppc64': MODEL_PPC_64, |
| 73 |
'ppc64le': MODEL_PPC_64, |
| 74 |
'x86_64': MODEL_X86, |
| 75 |
'amd64': MODEL_X86, |
| 76 |
'AMD64': MODEL_X86, |
| 77 |
'armv8l': MODEL_ARM, |
| 78 |
'aarch64': MODEL_ARM64, |
| 79 |
'arm64': MODEL_ARM64, |
| 80 |
'armv7l': MODEL_ARM, |
| 81 |
'armv6l': MODEL_ARM, |
| 82 |
'arm': MODEL_ARM, |
| 83 |
's390x': MODEL_S390_64, |
| 84 |
'riscv64': MODEL_RISCV_64, |
| 85 |
}.get(mach) |
| 86 |
|
| 87 |
if result is None: |
| 88 |
raise ProcessorAutodetectError("unknown machine name %s" % mach) |
| 89 |
|
| 90 |
if result.startswith('x86'): |
| 91 |
from rpython.jit.backend.x86 import detect_feature as feature |
| 92 |
if sys.maxint == 2**63-1: |
| 93 |
result = MODEL_X86_64 |
| 94 |
else: |
| 95 |
assert sys.maxint == 2**31-1 |
| 96 |
if feature.detect_sse2(): |
| 97 |
result = MODEL_X86 |
| 98 |
else: |
| 99 |
result = MODEL_X86_NO_SSE2 |
| 100 |
if feature.detect_x32_mode(): |
| 101 |
raise ProcessorAutodetectError( |
| 102 |
'JITting in x32 mode is not implemented') |
| 103 |
|
| 104 |
if result.startswith('arm'): |
| 105 |
from rpython.jit.backend.arm.detect import detect_float |
| 106 |
if not detect_float(): |
| 107 |
raise ProcessorAutodetectError( |
| 108 |
'the JIT-compiler requires a vfp unit') |
| 109 |
|
| 110 |
return result |
| 111 |
|
| 112 |
|
| 113 |
def autodetect(): |
| 114 |
if not is_host_build(): |
| 115 |
return detect_model_from_c_compiler() |
| 116 |
else: |
| 117 |
return detect_model_from_host_platform() |
| 118 |
|
| 119 |
|
| 120 |
def getcpuclassname(backend_name="auto"): |
| 121 |
if backend_name == "auto": |
| 122 |
backend_name = autodetect() |
| 123 |
backend_name = backend_name.replace('_', '-') |
| 124 |
if backend_name == MODEL_X86: |
| 125 |
return "rpython.jit.backend.x86.runner", "CPU" |
| 126 |
elif backend_name == MODEL_X86_NO_SSE2: |
| 127 |
return "rpython.jit.backend.x86.runner", "CPU386_NO_SSE2" |
| 128 |
elif backend_name == MODEL_X86_64: |
| 129 |
return "rpython.jit.backend.x86.runner", "CPU_X86_64" |
| 130 |
elif backend_name == MODEL_ARM: |
| 131 |
return "rpython.jit.backend.arm.runner", "CPU_ARM" |
| 132 |
elif backend_name == MODEL_ARM64: |
| 133 |
return "rpython.jit.backend.aarch64.runner", "CPU_ARM64" |
| 134 |
elif backend_name == MODEL_PPC_64: |
| 135 |
return "rpython.jit.backend.ppc.runner", "PPC_CPU" |
| 136 |
elif backend_name == MODEL_S390_64: |
| 137 |
return "rpython.jit.backend.zarch.runner", "CPU_S390_64" |
| 138 |
elif backend_name == MODEL_RISCV_64: |
| 139 |
return "rpython.jit.backend.riscv.runner", "CPU_RISCV_64" |
| 140 |
else: |
| 141 |
raise ProcessorAutodetectError( |
| 142 |
"we have no JIT backend for this cpu: '%s'" % backend_name) |
| 143 |
|
| 144 |
def getcpuclass(backend_name="auto"): |
| 145 |
modname, clsname = getcpuclassname(backend_name) |
| 146 |
mod = __import__(modname, {}, {}, clsname) |
| 147 |
return getattr(mod, clsname) |
| 148 |
|
| 149 |
|
| 150 |
def getcpufeatures(backend_name="auto"): |
| 151 |
if backend_name == "auto": |
| 152 |
backend_name = autodetect() |
| 153 |
return { |
| 154 |
MODEL_X86: ['floats', 'singlefloats', 'longlong'], |
| 155 |
MODEL_X86_NO_SSE2: ['longlong'], |
| 156 |
MODEL_X86_64: ['floats', 'singlefloats'], |
| 157 |
MODEL_ARM: ['floats', 'singlefloats', 'longlong'], |
| 158 |
MODEL_ARM64: ['floats'], |
| 159 |
MODEL_PPC_64: ['floats'], |
| 160 |
MODEL_S390_64: ['floats'], |
| 161 |
MODEL_RISCV_64: ['floats'], |
| 162 |
}[backend_name] |
| 163 |
|
| 164 |
if __name__ == '__main__': |
| 165 |
if len(sys.argv) > 1: |
| 166 |
name = sys.argv[1] |
| 167 |
x = name |
| 168 |
else: |
| 169 |
name = 'auto' |
| 170 |
x = autodetect() |
| 171 |
x = (x, getcpuclassname(name), getcpufeatures(name)) |
| 172 |
print('autodetect: ', x[0]) |
| 173 |
print('getcpuclassname:', x[1]) |
| 174 |
print('getcpufeatures: ', x[2]) |