What this is about
A processor does not understand Python, Mojo or any language you write. It understands a small list of simple instructions, such as "add these two numbers" or "load this value from memory". That list is called the instruction set.
Different families of processors use different instruction sets. Two families matter for most people. x86-64 is in nearly every desktop and laptop. ARM is in nearly every phone. A program built for one cannot run on the other, as it is. This article explains why, and what it means when you write code for both.
Why it matters
If you have ever seen a message like "this app is not available for your device", you have met this problem. The app was built for one family of chips.
For us, it meant that every Mojo program in our phone work was built twice from the same source: once for the desktop and once for the phone. It also meant results on one chip did not carry to the other. A fast loop on a desktop can be slow on a phone, for reasons that live in the chip's design.
How it works
A compiler is the program that turns your source code into instructions. You tell it which chip it should aim at. This is called the target. Pick x86-64, and it writes x86-64 instructions. Pick ARM, and it writes ARM instructions. The source stays the same, and only the output differs.
Here is how the two families look at assembly level. Our profiling tool, corewait, shows one row per instruction. Two rows from its documentation:
- An ARM row:
fmla v0.4s, v0.4s, v3.4s. It multiplies and adds four decimal numbers at once. - An x86 row:
add rax, rcx. It adds two numbers held in registers.
The names are different, the register styles are different, and the rules for what runs next are different. You do not need to learn them. You need to know that they differ, and that a profile of one chip says little about the other.
Chips also differ in speed of memory. We measured how fast each of our test devices reads memory in a straight line. These figures come from our corewait device notes, in gigabytes per second.
| device | processor | one thread | all threads |
|---|---|---|---|
| desktop | Ryzen 7 7800X3D, 16 threads (x86-64) | 49.3 | 55.5 |
| laptop | i5-6200U, Skylake, 4 threads (x86-64) | 16.5 | 27.5 |
| Galaxy Z Flip4 phone | six usable CPUs (ARM) | 13.0 | 46.9 |
Look at the phone row. One thread reads at 13.0 GB per second, about a quarter of the desktop. But with every thread working, the phone reaches 46.9, much closer to the desktop. The phone needs many threads at once to use its memory, and the desktop is fast even with one. A program tuned for one shape can be a poor fit for the other.
What we did about it
Three habits helped.
- Build on the right target. The
corewait buildcommand cross-compiles our test probe for a device, so the phone needs no Mojo runtime. We build on the desktop and send the result to the phone. - Use a library built for the chip. For our phone kernels we started from a binding to KleidiAI, a set of fast ARM routines. We then ported the matrix multiply by hand into Mojo, and checked it byte for byte against the original.
- Never assume a number carries over. We test on two phones, because even two ARM phones can differ in steadiness, as we described in the previous article.
A small example from our desktop libraries shows the same idea in reverse. For mojo-xxhash, the README compares our hash with the system's libxxhash, which Arch Linux builds as a generic x86-64 binary. Mojo compiles for the exact processor it is on, so it can use instructions the generic build avoids. We state this in the README, because part of the speed gap is a build choice, not a better algorithm.
What this does not tell you
- It does not say ARM is better or worse than x86-64. Each is a design, with strengths.
- The memory figures come from one device of each kind. Another phone will differ.
- Some of our libraries are written for x86-64 and aarch64 only, for example in how they lay out system structures. Other chips are not covered.
How to tell which one you have
On a Linux computer, type uname -m in a terminal. A result of x86_64 means x86-64. A result of aarch64 means 64-bit ARM. On a phone, the specification page of the model lists the chip, and most phones list an ARM-based one.
When you download a program, look for the chip in the file name. Names with x86_64 or amd64 are built for x86-64. Names with arm64 or aarch64 are for ARM. Pick the wrong one and the program will not start. The error is usually a short message about a bad format or an executable that cannot run.
Words you may not know
- Instruction set: the list of simple operations a processor understands.
- x86-64: the instruction set of most desktop and laptop processors.
- ARM: the instruction set of most phone processors. The 64-bit form is called aarch64.
- Target: the chip a compiler writes instructions for.
- Cross-compile: build on one kind of machine a program for another.
- Thread: one stream of work a processor core can run.
Try it
- Find your computer's processor name in its system settings, and look up whether it is ARM or x86-64.
- Open the corewait project page and look at its device table.
- Browse the corewait repository and read about
corewait build DEV.
Read next
- A phone GPU with no counters is about measuring a phone's graphics chip when it hides its instruments.

Comentarii
Niciun comentariu încă.
Conectare pentru a comenta.