Show HN: Compile the same source for GPUs and CPUs

(github.com)

20 points | by spacedcowboy 9 days ago ago

2 comments

  • Conscat 2 hours ago

    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 an hour ago

      Yeah, I probably could have been a bit clearer on that front :) This is what's going on...

      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!