r/CompilerChallenges • u/shrimpster00 • 8d ago
The Chromagraph Challenge
This is the first official compiler challenge! See the official GitHub page for benchmarks and an example interpreter in Rust.
Compare to the Prospero Challenge, but with logic and Technicolorâ„¢!
In this challenge, a Chromagraph is a program that renders an image.
Specifically, it defines a straight-line function of three inputs: x in [-aspect, aspect], y in [-1, 1], and aspect = width/height, and three outputs: r, g, and b in [0, 1].
Your job is to evaluate this function for every pixel in an image.
The program is a series of assembly-like primitive operations with no control flow. You can write an interpreter in an hour.
Here's a simple example:
# draws a white circle on a black background
f_x = x
f_y = y
f_aspect = aspect
c_0 = const 0.0
c_1 = const 1.0
f_x2 = mul f_x f_x
f_y2 = mul f_y f_y
f_dist_sq = add f_x2 f_y2
f_radius = min f_aspect c_1
f_radius_sq = mul f_radius f_radius
b_in_square = leq f_dist_sq f_radius_sq
f_color = select b_in_square c_1 c_0
return f_color f_color f_color
If you're having trouble visualizing what these primitives mean, here's a more readable representation of the same computation:
Your challenge is to execute this program, and the other Chromagraphs defined here, as quickly as possible.
To start, here's a minimal interpreter in Python:
, = ,
=
continue
return ,
, , , * =
=
=
, =
=
=
# () -> f32
== :
== :
== :
# f32 -> f32
== :
== :
== :
== :
== :
# (f32, f32) -> f32
== :
== :
== :
== :
== :
== :
# (f32, f32) -> bool
== :
== :
== :
# logic
== :
== :
== :
== :
return
A more fully-featured interpreter in Rust, including more sophisticated error handling and actual image generation, can be found in the src/ directory.
Instead of control flow in the programs, all branches are eagerly executed and the result is chosen afterwards with the select instruction.
Can you write an interpreter that identifies subexpressions that are only used conditionally?
What other optimizations can you make?
Can you write a JIT or an AOT compiler?
Does a heavy backend like LLVM provide a net benefit in a JIT context?
What about Cranelift?
Benchmarks
All benchmarks can be found in the benches/ directory, alongside a sample render in 512x512 dimensions.
They vary in complexity, roughly in the following order:
gradient: draws a gradient depending only onx/aspectandycircle: draws a white circle on a black backgroundsimple-shapes: draws a square, a circle, a triangle, and a hexagongraph: plotsy = x - x^3 / 6 + x^5 / 120 - x^7 / 5040(taylor polynomial for sine)sierpinski-{4,8,10,12}(generated): draws the Sierpinski triangle with 4, 8, 10, and 12 iterationsjulia-{128,256,512,1024}(generated): draws the Julia fractal withc = -0.8 + 0.156iand 128, 256, 512, and 1024 iterationsexcess-bools: threshold pattern that keeps 100 boolean values live (stress test integer register allocator)excess-floats: threshold pattern that keeps 100 float values live (stress test vector register allocator)prospero: a direct conversion ofprospero.vmfrom the Prospero Challenge (renders text)
Additional challenge: generating chromagraphs
Can you design a higher-level language that you can translate to Chromagraphs' primitive assembly-like instructions? Can you design interesting graphics or fractals that take more work than you could feasibly write in a Chromagraph by hand? What about a program that can generate a Chromagraph that renders a heatmap of given data?
2
u/shrimpster00 8d ago
I wrote an AArch64 JIT compiler that leverages ARM NEON SIMD instructions to render 4 pixels at a time!
See my implementation on GitHub