Show HN: Compile the same source for GPUs and CPUs

spacedcowboy 25 points 4 comments November 13, 2026
github.com · View on Hacker News

I'm writing a language called xc and its compiler, xcc for the last 6 months or so (full disclosure: with a lot of help from Claude). I've finally got version 1 (actually v1.0.1) out, for Linux (x86-64 and arm64, including the Pi 5), macOS and Windows. 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...

Discussion Highlights (3 comments)

Conscat

I only looked a little bit, but I noticed some things like the integer types and OS #ifdefs look different from "normal" C. Is this compatible with generic C code out there today? A bit hard to tell how compatible it is with C idioms besides what the standard strictly requires. Impressive results, this is great!

spacedcowboy

One thing I might also want to point out is that the language will also compile down to WASM and use WGSL to do the data-parallel parts - I do mention that above in passing, but everyone looks at the highlight figures :) Benchmarks are available at the docs site [1] for a lot more involved data, it does pretty well even in CPU mode... 1: https://compile-xc.org/compiler/performance/

eliasdejong

Being able to take a C program and have it auto-parallelize across heterogeneous hardware accelerators certainly sounds attractive, but it's harder than it sounds because you are effectively talking to multiple computers at the same time and it becomes a distributed systems problem. It's very difficult to abstract over the communication latency, bandwidth limits and architecture differences. For example the accelerator could have a completely different address space or pointer size. 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.

Semantic search powered by Rivestack pgvector
3,874 stories · 35,194 chunks indexed