25 points · by spacedcowboy · 2w ago · 5 comments
The idea: you write an ordinary loop, the compiler works out whether it can run in parallel, and it emits both a multi-core CPU version and GPU kernels (MSL, PTX, SPIR-V or WGSL, all generated by xcc itself without needing an SDK installed).
At run time the program measures each block on that machine and keeps the faster placement.
Examples:
- On an M3 Max a Mandelbrot block takes 13.7 ms on all 16 cores and 1.3 ms on the GPU
- On a Ryzen 9955HX's 2-CU integrated Radeon the GPU loses to the 32 CPU threads, so it stays on the CPU.
The compiler also reads plain old C via clang's front end and does the same thing to C loops, with no pragmas. Comparing xcc to OpenMP for both gcc and clang (with the helping hand of "#pragma omp parallel for" gives (times in ms):
xcc gcc+omp clang+omp
mandel_deep 371 2170 2231
mandelbrot 2.45 12.2 12.8
md5 9.39 16.1 13.4
md5_mask 8.17 12.1 13.4
nbody 0.45 1.29 9.92
perlin 5.87 4.38 7.05
saxpy 2.26 2.18 2.32
Geometric mean: xcc is 2.1x faster than GCC+OpenMP and 3.0x faster than clang+OpenMP.All the builds are checked to make sure they give the same result - no point having a benchmark that doesn't produce the same result! xcc has a notable win for nbody, because its vectorisation is better (across bodies) but perlin has been a bit of a bugbear throughout the leading-up-to-v1 timespan.
Platform-specific binary tarballs, the sources, OpenMP versions and method are on the site (linked from github), and there are Phoronix Test Suite profiles if you'd like to run them yourself.
It's only just made v1 so there's a few rough edges: C++ through clang is in-progress (but not official yet), and the Pi 5's GPU is slow. Not sure if that's the Pi or the code yet...
There's more to the language but I don't want this to be an essay! The real, honest-to-$deity cool thing is the automatic parallelising of standard C/xc loops and being able to run that code on the GPU with no boilerplate - "ARC for Algorithms" (what Objective-C's ARC did for memory management), it "just works".
Very keen to get feedback and fix any gaps. I think it's generally useful right now, but it is "v1" - Bug reports welcome as GitHub issues...
I'm curious about the author's opinion on MLIR / Mojo, which are also trying to solve the problem of compiling for heterogenous hardware. Iirc they decided that the underlying hardware boundaries are sufficiently large that you still need to explicit control over what runs where.
1) The time it takes to get data to the kernel
2) The time it takes for the kernel to operate on a chunk of data.
Those times are calculated at run-time, not compiled in with the binary, and the use of the run-time data to determine how to load up the workers with tiles of data to compute makes it very adaptable. Any available worker will be characterised, that characterisation saved for the future, and remote-based metrics continually update that worker's characterisation.
xc also compiles (simultaneously) for all the GPU architectures it supports, so a single binary knows what to send to a Mac with an M-series chip, or a Linux box with an AMD GPU, or a Windows box with (say) 2 disparate NVidia GPUs. If a node is plugged in and runs xcd, that node will advertise via mDNS and (assuming it's in the correct work-group and isn't nimby'd by its user) will start to accept work tiles.
xc currently does not send executable CPU code over the network. That's not a limitation in the system, I've just been focussing on the GPU side. GPUs are actually easier because there's less variation in the hardware.
xc is explicitly targeting a higher-level of abstraction, so avoids cross-device pointer translation altogether because parallelised blocks only ship contiguous, flat value buffers (explicit fixed-width primitives/structs) across tiles, rather than arbitrary pointer-chasing graphs. This might at first glance seem restrictive, but it forces an architecture that works well on the hardware.
Similarly, xc tries for a high level of abstraction in the source code. Upcoming changes include user-specified operators (eg: a Tensor class could define '@' and the code could say 'Tensor result = M @ Z', or a Matrix class could define '|..|' to get the determinant of a matrix). In this way mathematical concepts can be represented at a high level, which makes the compiler's job easier to figure out how to efficiently map the code.
I think the crucial difference between xc and the others you mention is the runtime system. xc decides what to do based on
measurement, not prediction*. It will run small tests if necessary, which amortised over a large problem are essentially free.1) If you compile a program named myfile.xc, then xcc will run natively, it wants to see 'xc' style types (u8, u16, i32, float, etc). This is xc compiling its own language.
2) If you compile a program named myfile.c, then xcc will look up how to compile a non-native type (in this case .c) and find it has a rule to "run clang with various options to produce LLVM Intermediate Representation (IR)'. It does that, then reads and parses the LLVM IR into its own IR, and everything else proceeds as normal.
Basically it's a "proper C" front-end to the compiler's optimisation stages and eventual linker/app-production stage.
So if you have a plain unadorned C file, it gets all the auto-parallelisation, auto-vectorisation and runtime management of the best execution-method on this hardware, etc without any developer tuning or boilerplate-writing.
As for why we go through LLVM, well it seems to me that LLVM IR is "kind of" a universal assembly-language, clang, zig (for now, at least), flang, clang++ amongst others all resolve to LLVM IR as an initial step towards the executable, and if xcc can read and parse that, it becomes useful as a back-end to all these languages. The obvious "easy" case was "C", and I'm looking at C++ right now, which exercises more of the LLVM IR space. I wouldn't call it production-ready yet for C++ but it does do most of the cases I want to cover.
For the sort of thing that xc is designed for - lots of cross-platform data-parallel code, explicitly knowing the width of the types is useful, so they're annotated into the type. You do get typedef in xc if you really want 'int' rather than 'i32' (or would you prefer 'i64' ? Which is sort of the point :)
In fact, I'm in the middle of a 'consolidation' pass after 1.0 was released, which I'll take another week over or so, but next up is to do the same thing for floats - so we end up with f64, f32, f16, f8 and f4 (possibly with fp... aliases, not sure yet). That's going to be part of the next minor update along with a native tensor type (and the degenerate 2D matrix type) and operator overloading. I'll deprecate 'float' as an alias to 'f32' and 'double' as an alias to 'f64' at that point.
I've been most-recently working on the operator-overloading spec, and I think it's coming together pretty well now - the goal is to be able to declare methods as operators, so if M and Z were tensors, you could have
tensor result = M @ Z;
... all of which would then be operated on in (probably) GPU space unless your GPU wasn't great, or the tensors were quite small :)Hope that clears things up a bit!
Benchmarks are available at the docs site [1] for a lot more involved data, it does pretty well even in CPU mode...