Halide (C++)
An image and array processing language embedded in C++ that keeps the algorithm, a set of pure functions over an infinite integer grid, apart from its schedule, which decides loop order, tiling, vectorisation, parallelism, storage and GPU mapping, and lowers both through LLVM to x86, ARM, WebAssembly and CUDA, and through source or SPIR-V emitters to Metal, D3D12, OpenCL, Vulkan and WebGPU.
| Field | Value |
|---|---|
| Language | C++17 (compiler and API), freestanding C++ runtime, Python bindings; version 22.0.0 in CMakeLists.txt; LLVM 21 or newer |
| License | MIT (LICENSE.txt); apps/bgu is Apache-2.0 |
| Repository | halide/Halide |
| Revision read | 85f16183923fcec94c2fb6e756d98e6831ea1524 (October 27, 2026) |
| Documentation | halide-lang.org · API reference · tutorials · doc/ in the tree |
| Category | Generic libraries: image DSL, scheduling compiler, raw strided buffer ABI |
| Tier | A |
| Last reviewed | November 7, 2026 |
NOTE
Everything below was read from the source at the pinned revision with git show, unless it is marked verified. Verified claims were demonstrated by compiling and running a probe under /tmp/halide-abi on x86_64 Linux: a C++ program built with GCC 14.3 against the pinned HalideRuntime.h and HalideBuffer.h, and a D mirror of the buffer ABI built with LDC 1.43 under -betterC. Halide itself (which needs LLVM) was not built, so no pipeline was compiled or timed, and this page states no performance number of its own.
Overview
What it solves
An image pipeline is a chain of cheap stages (a blur, a colour transform, a resample, a pyramid level), and its speed depends mostly on how the stages are interleaved in memory, not on the inner-loop arithmetic. Writing the fused, tiled, vectorised, multithreaded form by hand ties the arithmetic to one machine: changing a tile size or moving a stage onto the GPU means rewriting the loops. Halide makes the stage definitions and their arrangement two separate programs. The paper thread covers the theory (Ragan-Kelley et al. 2012, 2013, 2018); this page covers how the pinned source implements it.
The README states the scope and the embedding (README.md):
"Halide is a programming language designed to make it easier to write high-performance image and array processing code on modern machines." … "Rather than being a standalone programming language, Halide is embedded in C++. This means you write C++ code that builds an in-memory representation of a Halide pipeline using Halide's C++ API. You can then compile this representation to an object file, or JIT-compile it and run it in the same process."
Three artefacts make up a Halide deployment, and each is a separate lesson for Sparkles:
- The front end and compiler (
libHalide):Func,Var,RDom,Expr, the scheduling directives, about sixty announced lowering steps inlower_impl(src/Lower.cpp), and code generators for each target. - The raw buffer ABI:
halide_buffer_tandhalide_dimension_tinsrc/runtime/HalideRuntime.h, a C struct that every compiled pipeline takes and returns, with a device handle and two dirty bits. - The runtime: a freestanding C++ library (allocator, thread pool, error reporting, CPU feature probing, one device interface per GPU API) that is linked into every AOT pipeline or the JIT.
Design philosophy
The tutorial puts the central trade-off in plain words (tutorial/lesson_08_scheduling_2.cpp):
"In any given situation the correct choice can be difficult to make. If you're memory-bandwidth limited, or don't have much memory (e.g. because you're running on an old cell-phone), then it can make sense to do redundant math. On the other hand, sin is expensive, so if you're compute-limited then fewer calls to sin will make your program faster. Adding vectorization or multi-core parallelism tilts the scales in favor of doing redundant work, because firing up multiple cpu cores increases the amount of math you can do per second, but doesn't increase your system memory bandwidth or capacity."
The consequences visible in the source:
- The schedule never changes the result. Every directive is a rewrite of loop structure, storage or placement; the algorithm (
f(x, y) = …) is the only place values are defined. Tests rely on this:simd_op_checkcompiles the same expression scalar and vectorised and compares them. - Bounds are inferred, not written. Intermediate images have no declared size; lowering derives the region each stage needs from its consumers, so the user writes no edge loops and no index checks.
- The schedule is target-dependent, the algorithm is not. The blur generator in
apps/blurholds one algorithm and three schedules selected byget_target(): GPU, Hexagon and CPU. - Everything compiles ahead of time if wanted. A
Generatoremits an object file, a C header and a runtime; the deployed program needs no compiler, and the README notes that Python deployments of precompiled AOT pipelines can install only thehalide-runtimedistribution, "avoiding the compiler and LLVM entirely".
How it works
Algorithm, schedule and the blur
apps/blur/halide_blur_generator.cpp is the canonical example. The algorithm is two lines, and the schedule branches on the target (source):
// apps/blur/halide_blur_generator.cpp (abridged: the GPU "Slide" case and the CPU case)
// The algorithm
blur_x(x, y) = (input(x, y) + input(x + 1, y) + input(x + 2, y)) / 3;
blur_y(x, y) = (blur_x(x, y) + blur_x(x, y + 1) + blur_x(x, y + 2)) / 3;
// How to schedule it
if (get_target().has_gpu_feature()) {
blur_y
.split(y, y, y_inner, tile_y)
.reorder(y_inner, x)
.unroll(y_inner)
.gpu_tile(x, y, xi, yi, tile_x, 1);
} else {
// CPU schedule.
blur_y
.split(y, y, yi, 32)
.parallel(y)
.vectorize(x, 16);
blur_x
.store_at(blur_y, y)
.compute_at(blur_y, x)
.vectorize(x, 16);
}The vocabulary, read from src/Func.h (Stage directives, GPU directives):
| Directive | What it rewrites |
|---|---|
split(x, xo, xi, factor, tail) | one loop into an outer and an inner loop of extent factor; tail is a TailStrategy |
fuse, reorder, tile | merge two loops; permute the nest; tile is two splits plus a reorder |
vectorize(x, n), unroll(x, n) | an inner loop of constant extent into vector lanes or straight-line code |
parallel(x) | a loop into halide_do_par_for tasks on the runtime thread pool |
compute_at(f, x), compute_root | the loop level at which a producer is computed: inside a consumer's loop over x, or once for the whole output |
store_at(f, x), hoist_storage | the loop level at which the producer's buffer is allocated, which may be outside where it is computed |
compute_inline (the default) | substitute the producer's expression into each use |
gpu_blocks, gpu_threads, gpu_tile | loops onto the workgroup grid and the invocations within a workgroup, for a DeviceAPI |
store_in(MemoryType) | Heap, Stack, Register, GPUShared, GPUTexture, LockedCache, VTCM, AMXTile, GPUSharedAsync |
bound, align_bounds, specialize | constrain an output's region; round it to a modulus; compile a second copy of the stage under a run-time condition |
rfactor, atomic | turn an associative reduction over an RDom into a parallel one |
async, ring_buffer, prefetch, memoize | run a producer on another thread; give it multiple buffers; prefetch an input; cache a result between calls |
compute_at and store_at are the two knobs of the 2013 paper. The header documents them with the C code they produce. For g.compute_at(f, x).store_at(f, y) on a 2×2 stencil, Halide notices that each x step needs one new column and folds the storage (Func.h):
"Halide has detected that it's possible to use a circular buffer to represent g, and has reduced all accesses to g modulo 2 in the x dimension. This optimization only triggers if the for loop over x is serial, and if halide can statically determine some power of two large enough to cover the range needed."
The blur's CPU schedule uses exactly this: blur_x is stored per strip of y and computed per vector of x, so the three-row window of blur_x lives in a folded buffer (the source comment: "Halide will store blur_x in a circular buffer so its results can be re-used").
Tails
A split by a factor that does not divide the extent needs a policy. TailStrategy in src/Schedule.h lists eight: RoundUp, GuardWithIf, PredicateLoads, PredicateStores, ShiftInwards, ShiftInwardsAndBlend, RoundUpAndBlend and Auto, plus a deprecated Predicate alias of GuardWithIf (source). ShiftInwards, the default for pure definitions, re-evaluates a few points near the end so the inner loop always runs a full vector:
"Like RoundUp, it supports vectorization well, because the inner loop is always a fixed size with no data-dependent branching. … this strategy only requires that the input/output extent be at least the split factor, instead of a multiple of the split factor as with RoundUp."
Reductions and boundaries
A pure Func has one definition; a reduction adds update definitions over an RDom, a bounded multi-dimensional domain, executed in order after the initial value (RDom.h). Histograms, integral images and convolutions with explicit loops are written this way. Edges are explicit wrapper functions in src/BoundaryConditions.h (constant_exterior, repeat_edge, repeat_image, mirror_image, mirror_interior) (source): the algorithm reads clamped(x - 1, y) instead of input(x - 1, y) and the wrapper owns the finite domain.
Lowering and bounds inference
lower() in src/Lower.cpp calls lower_impl, which runs the pipeline through a fixed sequence of passes, each announced with debug(1) (source). The relevant order is: create loop nests from the schedule; add parameter checks; compute value bounds of each function; clamp unsafe data-dependent accesses; computation bounds inference; sliding window; allocation bounds inference; add checks for images; storage folding; storage flattening; inject host/device copies; unroll; vectorise; partition loops for boundary conditions; and strip asserts if the target asks, which is the last pass before any custom lowering passes and the split into host and device code (Hexagon and GPU offload, parallel tasks).
Bounds inference (src/BoundsInference.cpp) walks from the outputs back to the inputs and, for every producer at its compute_at level, defines symbolic min and extent variables from the interval of coordinates its consumers read. The 2013 paper calls the model axis-aligned boxes, not polytopes. AddImageChecks then turns the inferred region of every input and output buffer into one assertion per dimension, placed at the top of the pipeline (source):
// src/AddImageChecks.cpp (abridged)
Expr oob_condition = actual_min <= min_required_var && actual_max >= max_required;
Expr oob_error = Call::make(Int(32), "halide_error_access_out_of_bounds",
{error_name, j, min_required_var, max_required, actual_min, actual_max},
Call::Extern);
asserts_required.push_back(AssertStmt::make(oob_condition, oob_error));This is how Halide removes per-pixel bounds checks: the loop body indexes with no checks at all, because a single per-dimension comparison before the first loop has proved that every access the schedule will make lands inside the buffer the caller passed. Intermediate buffers need no check, since Halide sized them from the same intervals. An access whose interval cannot be bounded is a compile-time error, not a run-time check (source):
// src/AddImageChecks.cpp
user_error << "Buffer " << name
<< " may be accessed in an unbounded way in dimension "
<< j << "\n";The same pass also emits the size checks (per dimension, extent × stride within maximum_buffer_size(); the running product of extents within the same limit; no negative extents) and, when the target has no device support, an assertion that no input is device_dirty (sizes, dirty).
Bounds queries. A compiled pipeline doubles as a function that reports what it needs. If a buffer argument has host == nullptr && device == 0 (_halide_buffer_is_bounds_query), the pipeline writes the required region into that buffer's dim array and returns without computing anything. A caller can therefore ask "which input rectangle do you need for this output rectangle?" before allocating. Target::NoBoundsQuery removes the mode.
Generators, AOT and JIT
A Generator is a class with typed Input, Output and GeneratorParam members and generate() / schedule() methods (Generator.h); HALIDE_REGISTER_GENERATOR registers it, and the GenGen driver compiles it for a target string such as x86-64-linux-avx2 or host-vulkan-vk_int8-vk_v13. The output is an object or static library with a C entry point taking halide_buffer_t * per buffer and returning an int error code, a header, and optionally a standalone runtime. RunGen wraps any generator into a command-line benchmark (doc/RunGen.md). The same Func graph can instead be JIT-compiled in-process with Func::realize.
Multi-target dispatch is AOT-only. compile_multitarget compiles one sub-function per target and a wrapper that asks halide_can_use_target_features at run time which one to call (Module.cpp):
"The final target in the list is considered "baseline", and is used for (e.g.) the runtime and shared code."
halide_can_use_target_features returns one of four values, never, always, not_right_now and for_the_moment, so a decision can be cached permanently or re-asked on the next call; an application may override it (HalideRuntime.h).
Targets and features
halide_target_feature_t in HalideRuntime.h is the flat list of what a target string can name (source): sse41, avx, avx2, avx512 with per-family sets (avx512_skylake, avx512_cannonlake, avx512_zen4, avx512_zen5, avx512_sapphirerapids), sve, sve2, no_neon for 32-bit ARM, and per-API device features. Two details matter to Sparkles.
x86 detection checks the OS. src/runtime/x86_cpu_detect.h is shared by the compiler (to compute the host target) and the freestanding runtime (to answer halide_can_use_target_features), and it tests XCR0 before trusting a CPUID bit (source):
// src/runtime/x86_cpu_detect.h (abridged)
// Check OS support for AVX/AVX-512 state saving via XSAVE. Even if the CPU
// supports these features, the OS must enable the corresponding state
// components in XCR0 or use will fault.
const uint64_t xcr0 = ops.xgetbv(0);
os_avx = (xcr0 & 0x6) == 0x6; // XMM (bit 1) + YMM (bit 2)
os_avx512 = os_avx && ((xcr0 & 0xE0) == 0xE0); // opmask (5) + ZMM_Hi256 (6) + Hi16_ZMM (7)The header injects cpuid and xgetbv through an "ops" object so the decision logic "is testable against recorded CPU state from machines we don't have" (source).
SVE2 is compiled for a fixed vector length. The ARM back end generates scalable vectors only for sve2 (plain sve draws the warning "Halide does not support SVE for now. Use SVE2 if your target device supports it."), and refuses SVE2 without a vector length (CodeGen_ARM.cpp):
"For SVE/SVE2 support,
Target::vector_bitsmust be set. For generator target strings, add "vector_bits_<bits>"."
Host detection fills vector_bits from the running machine's SVE length (Target.cpp). A Halide SVE2 binary is therefore vector-length-specific, not vector-length-agnostic.
The Vulkan back end
doc/Vulkan.md describes the back end as "considered BETA quality at this stage" (source). src/CodeGen_Vulkan_Dev.cpp (3,330 lines) emits SPIR-V directly, without GLSL and without glslang, using the GLCompute execution model, the Logical addressing model and the GLSL450 memory model (source). What it emits:
- Buffers are storage buffers, not images. Each
halide_buffer_targument becomes a runtime array in aBufferBlockstruct with anArrayStridedecoration, bound at its own binding in the entry point's descriptor set (source). A grep of the file forOpImagefinds nothing: strided indexing is arithmetic on a flat array, and there are no samplers or storage images. - Scalars travel in one uniform block (
param_pack), followed by one element offset per buffer (the code comment:These offsets are stored *after* all scalar args in the uniform struct). The runtime fills each with the region'sindexing.offset, whose declaration says it is "used to adjust indices in compute shader to avoid alignment constraints for arbitrary crops" (source), so a device crop at any element offset binds the parent allocation without a copy. - Workgroup size is a static
LocalSizefrom thegpu_threadsextents, or, if the extents are not constant, three specialization constants, which needs Vulkan 1.3 (source): "Vulkan: Dynamic workgroup sizes require Vulkan v1.3+ support!" - Shared memory (
MemoryType::GPUShared) becomesWorkgroupstorage-class arrays; one of them may be dynamically sized through a specialization constant. - A sidecar header precedes the SPIR-V words and lists, per entry point, the uniform and storage buffer counts, the specialization constants and the shared-memory allocations, so the runtime can build descriptor set layouts without reflecting the module (source).
- Optional types are opt-in features:
vk_int8,vk_int16,vk_int64,vk_float16,vk_float64, plusvk_v10,vk_v12,vk_v13(doc/Vulkan.md): "Note that 32-bit integer and floating-point types are always available. All other optional device features are off by default". Booleans in parameters are widened toUInt(32)becauseOpTypeBoolhas no storage representation (source).
The runtime side, src/runtime/vulkan.cpp, is synchronous. halide_vulkan_run records one command buffer with one vkCmdDispatch, submits it and calls vkQueueWaitIdle before returning (source); halide_vulkan_copy_to_device maps a host-visible, uncached-coherent staging region, copies the strided host data into it, records vkCmdCopyBuffer, submits and waits again (source). A host application can supply its own VkInstance, VkDevice, VkPhysicalDevice and VkQueue by overriding halide_vulkan_acquire_context / halide_vulkan_release_context (the allocator out-parameter is Halide's internal halide_vulkan_memory_allocator), and its own VkAllocationCallbacks through halide_vulkan_set_allocation_callbacks (HalideRuntimeVulkan.h), and can wrap an existing VkBuffer through halide_device_wrap_native, which dispatches to halide_vulkan_wrap_vk_buffer (source). That function is not declared in the public Vulkan header, unlike halide_cuda_wrap_device_ptr and halide_metal_wrap_buffer.
The block allocator's defaults in src/runtime/vulkan_memory.h are minimum_block_size = 4 * 1024 and nearest_multiple = 32 (source), while doc/Vulkan.md says of the minimum block size "Default is 32MB" (source). The documentation and the code disagree at this revision.
The autoschedulers
The four autoschedulers are plugins: each registers itself through REGISTER_AUTOSCHEDULER(NAME), which calls Pipeline::add_autoscheduler from a static constructor (HalidePlugin.h). All of them need size estimates on the pipeline's outputs (set_estimate), because their cost models reason about concrete extents.
| Directory | Paper | Method in the source |
|---|---|---|
src/autoschedulers/mullapudi2016 | Mullapudi et al. 2016 | greedy grouping over bounds analysis, a cost model of arithmetic and loads; parallelism, last_level_cache_size, balance parameters (source) |
src/autoschedulers/li2018 | Li et al. 2018 (gradient Halide) | "a conservative autoscheduler that compute_root most Funcs except for the trivial ones (think of it as a -O1 optimizer for Halide)" (README) |
src/autoschedulers/adams2019 | Adams et al. 2019 | coarse-to-fine beam search over LoopNest trees, a learned cost model with baseline.weights and a retraining loop (source) |
src/autoschedulers/anderson2021 | Anderson et al. 2021 | the same beam-search structure extended with GPULoopInfo, GPUMemInfo, SearchSpace and Tiling for block/thread mapping (source) |
Two details in the source qualify the paper claims. Mullapudi2016's GPU mode is behind an opt-in parameter: is_gpu_schedule = target.has_gpu_feature() && experimental_gpu_schedule, with the defaults parallelism = 128, last_level_cache_size = 48 * 1024 and balance = 20 for GPUs (source). And the Li2018 README still says it is "currently the only autoscheduler that generates GPU schedules", which anderson2021 and the Mullapudi2016 experimental_gpu_schedule flag contradict; the README is stale. The README's own table reports the gradient autoscheduler slower than the manual schedules on most of its apps (for example local_laplacian at 42.29 ms manual against 128.31 ms on the CPU); those numbers are the README's, not measured here.
Analysis
1. Pixel and memory model
The model is one struct with no notion of pixel, channel or plane (HalideRuntime.h):
// src/runtime/HalideRuntime.h (fields only)
typedef struct halide_dimension_t {
int32_t min, extent, stride;
uint32_t flags; // "None are defined yet"
} halide_dimension_t;
typedef struct halide_buffer_t {
uint64_t device; // device handle
const struct halide_device_interface_t *device_interface;
uint8_t *host; // address of the min coordinate
uint64_t flags; // host_dirty = 1, device_dirty = 2
struct halide_type_t type; // code, bits, info
int32_t dimensions;
halide_dimension_t *dim; // "Halide does not own this array"
void *padding;
} halide_buffer_t;Verified (C++ probe): sizeof(halide_buffer_t) is 56, halide_dimension_t 16 and halide_type_t 4 on x86_64 Linux, with host at offset 16, type at 32 and dim at 40. The D mirror reproduced the same sizes and offsets in static asserts under -betterC.
The defining choices:
- Strides are in elements, signed, one per dimension, in any order. Dimension 0 is innermost by default, but interleaved versus planar is only a stride permutation. Verified:
Buffer<uint8_t>::make_interleaved(4, 3, 3)produced strides3, 12, 1forx, y, c, andBuffer<uint8_t>(4, 3, 3)produced the planar1, 4, 12. A negative stride flips an axis without a copy; verified that a five-element view with stride-1over a forward array reads4 3 2 1 0, andbegin_offset()reports-4. - Every dimension has a
min.hostpoints at the element with coordinates(min0, min1, …), so a crop keeps the parent's coordinate system. Verified: cropping the interleaved buffer's x to[2, 4)and y to[1, 3)gavex.min = 2,y.min = 1, andc(2, 1, 0)aliasedrgb(2, 1, 0), 18 bytes into the parent (6 bytes for the same crop of the planar buffer). Bounds inference relies on this: required regions are absolute coordinates. - Everything is 32-bit.
min,extentandstrideareint32_t, andmaximum_buffer_size()is2^31 - 1bytes unless a 64-bit target enablesLargeBuffers(Target.h). - The element type is a runtime tag, not a template parameter:
halide_type_tis a code (int,uint,float,handle,bfloat,struct), a bit width and a 16-bitinfo. At this revision the ABI type carries no lane count: "Unlike the compiler-side Halide::Type, it does NOT carry a vector width ('lanes'): a value crossing the ABI boundary is always a single element" (source).
Against DLPack's DLTensor, read at 94485e23: both use element strides and a type tag, and both are plain C. They differ in four ways. DLPack's shape and strides are int64_t; Halide's are int32_t. DLPack has no per-axis origin, only a byte_offset; Halide's min is part of the buffer. DLPack's tensor lives on exactly one DLDevice; a Halide buffer can have a host allocation and a device allocation at once, with dirty bits saying which is current. And since DLPack 1.2, strides "can not be NULL if ndim != 0", which Halide never allowed. Halide has no DLPack import or export in the tree (a grep for dlpack finds nothing); its Python bindings use the Python buffer protocol (py::buffer_info) instead (PyRuntimeBuffer.cpp).
There are no planes in the multi-plane sense: a YUV 4:2:0 frame is three buffers of different extents, and chroma subsampling is something the algorithm expresses (uv(x / 2, y / 2)), not the buffer.
2. Genericity mechanism
Genericity lives in the DSL, not in C++ templates. A Func is untyped until its definition is built; Expr arithmetic carries a Halide::Type with lanes, and the pipeline is specialised to concrete types when it is compiled. On the C++ side, Halide::Runtime::Buffer<T, Dims, InClassDimStorage> is a thin typed view over halide_buffer_t; T = void and Dims = AnyDims accept anything at run time, and up to InClassDimStorage dimensions (default 4 for AnyDims, otherwise Dims) are stored inline (HalideBuffer.h). Generator introspects its Input and Output members to build the C signature, a form of reflection over declared members. specialize(cond) is the run-time counterpart: it compiles a second loop nest under a condition such as "stride 0 is 1" and branches once at the top. There are no concepts or traits in the user's sense: what a stage can do is decided by the compiler's IR, not by the element type.
3. Colour and metadata
Not applicable: Halide has no colour model. A buffer is numbers; sRGB, linear light, premultiplied alpha, ICC, CICP and EXIF are outside the type and the ABI. Colour conversions in apps/ (for example camera_pipe) are ordinary arithmetic in the algorithm. The absence is deliberate (the ABI is shared with non-image arrays) and means a colour-aware library built on Halide-style kernels must carry colour metadata beside the buffer, never inside it.
4. I/O and codec architecture
Not applicable in the library: Halide has no image codec. tools/halide_image_io.h is a convenience loader for tests and apps that calls libpng and libjpeg unless HALIDE_NO_PNG or HALIDE_NO_JPEG is defined and reads PPM/PGM and a few others itself (source); it is not part of the runtime. The pipeline model is pull-based in a specific sense: the output region determines, through bounds inference and the bounds-query mode, which input region must exist, so an application can decode or fetch exactly that rectangle before calling the pipeline. Data-dependent stages such as entropy decoding do not fit the model (the papers' synthesis lists this as an open problem).
5. Hostile input and safety
Halide's safety story is that of a trusted pipeline over untrusted shapes. The checks are generated, run once per call, and cover:
- each dimension's required interval against the buffer's
[min, min + extent - 1](halide_error_code_access_out_of_bounds); - negative extents (
halide_error_code_buffer_extents_negative), and anextent × strideor an extent product abovemaximum_buffer_size(), which is2^31 - 1unlessLargeBuffersis on (halide_error_code_buffer_allocation_too_large,halide_error_code_buffer_extents_too_large); - type and dimensionality mismatches (
halide_error_code_bad_type,halide_error_code_bad_dimensions) and user constraints (halide_error_code_constraint_violated) (error codes); - dirty-bit invariants (
halide_error_code_host_and_device_dirty,halide_error_code_device_dirty_with_no_device_support) (source).
Data-dependent indexing is handled twice. At compile time, an input access whose interval cannot be bounded is rejected ("may be accessed in an unbounded way"), which forces the author to clamp an index computed from pixel values. During lowering, clamp_unsafe_accesses inserts clamps where an index depends on another function's values and the allocation may be wider than the computed region (ClampUnsafeAccesses.h). Inside a pipeline the loops are unchecked; correctness rests on the compiler's interval arithmetic, which is why test/fuzz/bounds.cpp fuzzes it (section 10). Target::NoAsserts strips every check ("Stripping asserts..." runs after every pass that emits checks), so a deployment can remove the whole safety layer with a target flag. The 32-bit extents bound the damage of a hostile size: without LargeBuffers, a header that claims a 100,000 × 100,000 image fails the extent-product check before any loop runs.
The ABI itself is not memory-safe: dim is a raw pointer the caller owns, host is a raw pointer, and a lying stride passes every check that only looks at extents.
6. Allocation and ownership
The raw ABI owns nothing. Allocation inside a pipeline goes through halide_malloc / halide_free, which an application may replace (source):
"Note that halide_malloc must return a pointer aligned to the maximum meaningful alignment for the platform for the purpose of vector loads and stores, and with an allocated size that is (at least) an integral multiple of that same alignment. The default implementation uses 32-byte alignment on arm and 64-byte alignment on x86. Additionally, it must be safe to read at least 8 bytes before the start and beyond the end."
The last sentence is a contract a caller-provided buffer must also meet if a schedule over-reads. Small constant-size intermediates go on the stack or into registers ("Bounding small allocations..."), the rest on the heap; store_in overrides the choice. The C++ Runtime::Buffer adds ownership: an AllocationHeader with an atomic reference count for host memory and a DeviceRefCount whose BufferDeviceOwnership is Allocated, WrappedNative, Unmanaged, AllocatedDeviceAndHost or Cropped (source). Crops share the parent's allocation through the count.
The runtime holds mutable process-global state (allocator hooks, thread pool, caches), and two components that each embed a runtime silently share it unless their symbols are renamed (doc/CustomRuntimes.md):
"When they are linked into one process the linker collapses those duplicate (weak/
linkonce) symbols into a single copy, so the two components silently share one runtime."
7. CPU parallelism and SIMD
parallel(y) lowers to a closure and a call to halide_do_par_for(user_context, task, min, size, closure); the default pool spins briefly before sleeping, halide_set_num_threads sizes it, and halide_set_custom_do_par_for replaces it (source). An application with its own scheduler can therefore run Halide tasks on its workers.
vectorize(x, n) turns a constant-extent loop into Ramp and Broadcast IR of n lanes; LLVM then selects instructions, after a "Finding intrinsics..." pass and the per-target code generators have rewritten idioms Halide wants guaranteed (the simd_op_check tests pin them, for example rounding averages to pavgb). Vector width is a schedule choice, not a type: the blur vectorises by 16 in its CPU schedule and by 256 and 128 in its Hexagon schedule. Tails are handled by TailStrategy, so the main loop is always full-width. ISA selection is per compilation (a target string) plus the optional multi-target wrapper at run time, with OS-aware x86 detection and fixed-length SVE2.
8. GPU offload and interop
Offload is a schedule property: gpu_tile on a stage in a pipeline compiled with a GPU feature turns its loops into a kernel, and the "Injecting host <-> dev buffer copies" pass inserts halide_copy_to_device before a kernel reads a buffer and marks outputs device_dirty. The dirty bits are the whole coherence protocol (device_interface.cpp):
// src/runtime/device_interface.cpp (abridged)
WEAK int copy_to_host_already_locked(void *user_context, struct halide_buffer_t *buf) {
if (!buf->device_dirty()) {
return halide_error_code_success; // my, that was easy
}
const halide_device_interface_t *interface = buf->device_interface;
if (buf->host_dirty()) {
return halide_error_code_copy_to_host_failed;
}
...
auto result = interface->impl->copy_to_host(user_context, buf);
buf->set_device_dirty(false);
...copy_to_device is the mirror image: allocate if device == 0, copy only if host_dirty, then clear it (source); both dirty bits set at once is an invariant violation that the device entry points check through debug_log_and_validate_buf (source). Results stay on the device until the caller asks for halide_copy_to_host, so a chain of GPU pipelines exchanges buffers without round trips. Verified: a crop of a host buffer filled through Runtime::Buffer::fill reports host_dirty = 1, device_dirty = 0, device = 0.
halide_device_interface_t is a table of function pointers (device_malloc, copy_to_host, copy_to_device, buffer_copy, device_crop, device_slice, wrap_native, detach_native, …) shared by every GPU API, with the API-specific part behind impl (source). Interop with a host engine goes through wrap_native (adopt a VkBuffer, MTLBuffer or CUDA pointer) and the context hooks; the Vulkan back end additionally accepts the host's VkAllocationCallbacks. What Vulkan interop does not offer at this revision: images or samplers, timeline semaphores or any asynchronous submission (every dispatch and copy ends in vkQueueWaitIdle), and a public declaration of the wrap function.
9. Temporal, animation and containers
Not applicable: Halide has no time axis, no frame model and no container. A video is a 3-D or 4-D buffer, or a sequence of pipeline calls; temporal filters are written as stencils over a t coordinate. async() and ring_buffer() overlap a producer with its consumer within one call, which is the closest Halide gets to streaming.
10. Testing and conformance
The test tree is large and layered: 681 .cpp files in test/correctness, 111 in test/generator, 39 in test/performance and seven fuzz harnesses in test/fuzz, plus test/autoschedulers, test/runtime, test/warning and test/failing_with_issue (counts of git ls-tree entries at the pinned revision).
- Instruction-selection tests.
simd_op_check_x86.cpp,_arm,_sve2,_hvx,_powerpc,_riscvand_wasmassert that a given expression at a given width compiles to a named instruction, for examplecheck("pavgb", 8 * w, u8((u16(u8_1) + u16(u8_2) + 1) / 2))(source). Where the host can run the code, the shared harness also compiles a scalarf_scalarof the same expression and compares results (simd_op_check.h): the scalar form is the oracle for the vector form. - Fuzzing the compiler's reasoning.
test/fuzzholds harnesses forbounds,simplify,solve,cse,in_clone,lossless_castandwidening_lerp, built on a small framework with astd::mt19937_64backend that prints its seed and a libFuzzer backend (doc/FuzzTesting.md).fuzz_boundsgenerates random expressions overUInt(1)toInt(32)and checks the interval analysis on which the removal of per-pixel checks depends (source).test/correctness/fuzz_schedule.cppkeeps schedules found by fuzzing that crashed the compiler, and theFuzzFloatStorestarget feature masks off the least significant mantissa bit of every float store, which its header says "correlates very well with whether or not a pipeline will produce very different outputs on different architectures (e.g. with and without FMA)". - Bounds and device behaviour have dedicated tests (
bounds_inference*.cpp,bounds_query.cpp,gpu_*.cpp,custom_vulkan_context.cpp). - Vulkan status is recorded in prose: all correctness tests pass on tested Linux and Windows configurations with
host-vulkan-vk_int8-vk_int16-vk_int64-vk_float16-vk_float64-vk_v13"on LLVM v14.x" (the build now requires LLVM 21), macOS through MoltenVK fails some, and Android is "currently being worked on" (doc/Vulkan.md).
There is no image-conformance corpus, because there is no codec; correctness is "the schedule did not change the answer", checked against a scalar or reference schedule.
Lessons for Sparkles
- Q3 — The transferable unit is the split between a per-point kernel and a loop nest, not the compiler. Halide's directives are loop rewrites a D template can perform on a kernel it is handed:
splitplusvectorizewith aShiftInwardstail is a loop over__vectorlanes whose last iteration is moved back, andcompute_at/store_atwith folding is a ring of rows in a fixed-size buffer. A D schedule can be a compile-time value consumed by a loop-nest template, as the papers' synthesis proposes; what does not transfer is bounds inference over arbitrary expressions, which needs an IR. Where Sparkles stands: the shaders and SIMD baseline (G4: "no mechanism beyond hand-written per-target kernels"). - Q1, Q8 — Check the shape once, then index without checks. Halide's per-pixel loops are unchecked because one comparison per dimension at entry covers every access. The D form is a
@safeentry that validates a view against a declared footprint (stencil radius, resample ratio) and a@trustedinner loop on raw pointers. Verified in miniature: the D probe'scoversfunction decided, under-betterCwith-preview=dip1000, that a 3×3 stencil on an edge output needs a halo outside a 4×3 buffer and that an interior one does not. This needs the footprint to be declared data, since D cannot infer it from a lambda; it fits@nogc nothrow, and a production form would return anExpectederror rather than call ahalide_error_*-style callback. Baseline: existing image code (G7: hostile-input bounds "by declared size, not by work"). - Q1 —
halide_buffer_tis the minimum strided ABI, and itsminfield is the feature DLPack lacks. A Sparkles view that keeps an origin per axis gives crops in the parent's coordinate system and makes "which input rectangle does this output need" a box computation. Against D conventions the ABI'sint32_textents and strides conflict withsize_tindices (the shaders baseline'sSHM6already recordssize_twidening to 64 bits on the device), and the rawdimpointer andhostpointer are@systemby nature; a D view would keep the layout asextern (C)interop (the probe shows D can mirror it exactly) and expose a typed,scope-checked view above it. Verified: sizes and offsets match between the C++ header and the D mirror. Baseline: existing image code (G2: "No pixel view type") and concepts: stride. - Q6, Q7 — Two dirty bits are a complete host/device coherence protocol for a library that does not own the GPU timeline.
host_dirty/device_dirtyplus lazycopy_to_hostlets GPU stages chain without readback and lets a CPU stage force one. It transfers directly to a buffer type shared between host SIMD andsparkles:shaderscompute. What does not transfer is Halide's synchronous Vulkan runtime (avkQueueWaitIdleafter each dispatch and copy), which contradicts the frame-budgeted, barrier-ordered upload path ofui-vulkan(UVK29,UVK30). Baseline: Vulkan baseline and event-horizon baseline. - Q7 — Halide's Vulkan kernels read storage buffers and do strided arithmetic; they never sample. That is the shape
sparkles:shadersM10 would need (storage buffers, workgroup memory, specialization constants for workgroup size), and none of it exists yet. The specialization-constant workgroup size requires Vulkan 1.3 in Halide; the sidecar header (descriptor counts and constants per entry point) is a reflection-free binding description thatapps/shader-compilecould emit beside SPIR-V. Baseline: shaders and SIMD (G1, G2, G9). - Q3, Q7 — Make the workgroup shape and the vector width schedule parameters, measured per device. Halide encodes both as directives, the papers' synthesis records GPU cost models as "fragile against downstream compilers" (after Anderson 2021), and the hardware matrix shows the machines Sparkles targets share AVX2+FMA on x86 and NEON with dot product on AArch64, with AVX-512 only on Zen 4 and SVE on none (hardware: CPU SIMD). Halide's fixed-length SVE2 (
vector_bitsrequired) is a precedent that a vector-length-specific build is acceptable. Baseline: shaders and SIMD (G5, G7). - Q3 — Runtime ISA dispatch: copy the four-valued decision and the injectable probe.
halide_can_use_target_featuresseparates permanent from temporary decisions and is overridable;x86_cpu_detect.hchecks XCR0 and is tested against recorded CPU state. Sparkles'simd_caps.dalready checksxgetbv, but its flags arepackage-visible and no NEON probe exists (G5). A public, testablecpuid/xgetbv/getauxvalprobe with recorded fixtures would serve image kernels and text kernels alike. - Q8 — The oracle pattern is "same algorithm, different schedule".
simd_op_checkcompares the vector form to a scalar form and also asserts instruction selection;fuzz_boundsattacks the analysis that removes checks. For Sparkles this is the BLAKE3 test shape (every backend againstportable) generalised to image kernels, plus a fuzz of the footprint check. Instruction-selection assertions would need the build to emit assembly per@target, whichbenchbuilds with-mcpu=nativedo not exercise. Baseline: shaders and SIMD (G7, G8). - Q9 — Halide's packaging is evidence for keeping the kernel ABI apart from the algorithms. The runtime ships separately from the compiler, the buffer ABI is shared by every pipeline, and the Python packaging lets AOT users install only
halide-runtime. The same shape appears in Sparkles as a small image-core (view, type tag, dirty state) that formats and algorithms both depend on. The counter-evidence is the runtime's global state: two embedded runtimes collide unless renamed, which in D would be module-level__gsharedhook state shared by every user of the package. - Q2 — Genericity by a run-time type tag plus compile-time specialisation. Halide's
halide_type_ttags the ABI and the compiler specialises per type; D can do the specialisation with templates and design by introspection and keep a tag only at the boundary (files, GPU,extern (C)). What does not transfer is the untypedFunc: D kernels are typed functions, so mixing element types is a template parameter, not anExprcast. - Q4, Q5 — Nothing to take for codecs or colour. Halide's absence of colour and container models is a finding: a kernel layer can stay colour-blind if colour lives in a separate descriptor, and the bounds-query mode suggests a codec API shape ("give me this rectangle") without saying anything about decoding it.
- Q2, Q9 — betterC, wasm and Android. Halide's runtime is freestanding C++ ("no standard library, no exceptions and no RTTI", per
x86_cpu_detect.h) and has WebAssembly and Android runtime files; the equivalent D kernel layer would be-betterC-clean. The probe shows the ABI struct and a shape check compile under-betterC; dispatch throughshared static this()constructors, which the shaders baseline flags (G6), is the part that needs a betterC-compatible replacement.
Strengths
- One algorithm, many schedules, provably the same result: tiling, fusion, vectorisation, parallelism and GPU mapping change without touching the definitions.
- Per-pixel bounds checks removed by construction: one generated assertion per buffer dimension, with unbounded accesses rejected at compile time and the bounds-query mode exposing required regions to callers.
- A small, stable, C buffer ABI with per-axis origin, signed element strides and a host/device pair with dirty bits; the same struct works for images, tensors and arbitrary arrays.
- AOT output needs no compiler at run time, with a multi-target wrapper for ISA dispatch and a replaceable runtime (allocator, thread pool, context, error handler).
- Broad, honest target coverage: x86 up to AVX-512 per family, NEON, fixed-length SVE2, Hexagon, WebAssembly, and six GPU APIs (CUDA, OpenCL, Metal, D3D12, Vulkan, WebGPU), with OS-aware x86 detection.
- Testing aimed at the compiler's own reasoning: instruction-selection checks against scalar oracles, and fuzzers for bounds, simplification and solving.
Weaknesses
- It is a compiler. Using Halide means LLVM 21 or newer at build time, a C++ embedding and a generator step; the scheduling model cannot be adopted as a library.
- 32-bit extents and strides in the ABI, and
2^31 - 1bytes per buffer unless a 64-bit target enablesLargeBuffers; scientific volumes need the feature flag and still cannot exceed2^31 - 1elements along one axis. - No colour, metadata, codec or temporal model; images are numbers.
- Data-dependent control flow across stages (entropy decoding, sparse work) does not fit the feed-forward model.
- The Vulkan back end is beta and synchronous: storage buffers only, no images or samplers,
vkQueueWaitIdleafter each dispatch and copy, Vulkan 1.3 for dynamic workgroup sizes, and a wrap function absent from the public header. - Documentation drift at the pinned revision: the Vulkan allocator's minimum block size is 4 KiB in code and 32 MB in
doc/Vulkan.md; the Li2018 README calls itself the only GPU autoscheduler; the Vulkan status section reports results on LLVM v14.x. - Autoschedulers need output-size estimates and, for the learned ones, trained weights; GPU support in Mullapudi2016 is experimental and opt-in.
- Process-global runtime state collides between independently built components unless their symbols are prefixed.
- Safety is removable by a flag:
NoAssertsstrips every generated check.
Key design decisions and trade-offs
| Decision | Rationale | Trade-off |
|---|---|---|
| Algorithm as pure functions over an infinite grid | Schedules can reorder, recompute and fold freely without changing results | Data-dependent pipelines and in-place mutation fit only through RDom updates and extern stages |
Separate schedule language (directives on Func/Stage) | Target-specific tuning without touching the algorithm; autoschedulers emit the same directives | A second program to write and maintain; good schedules need expertise or a trained autoscheduler |
| Axis-aligned interval bounds inference | Cheap, predictable, enough for stencils, resampling and pyramids | Over-approximates non-rectangular footprints; unbounded data-dependent accesses must be clamped by hand |
| One check per buffer dimension at pipeline entry | Inner loops carry no bounds checks | Safety depends on the interval analysis being right (hence the fuzzers); NoAsserts removes it entirely |
halide_buffer_t with min, extent, stride per axis | Crops keep coordinates; layouts are stride permutations; one ABI for every pipeline | int32_t fields; raw caller-owned dim pointer; no plane or colour description |
| Host and device allocations in one buffer with dirty bits | Lazy, minimal copies; GPU stages chain without readback | Two-bit protocol has no notion of queues or timelines; invariant checked at every runtime call |
| AOT generators plus a freestanding runtime | No compiler in deployments; runtime replaceable piece by piece | Global runtime state shared by every pipeline in a process unless symbols are prefixed |
| Run-time dispatch by a multi-target wrapper | One binary covers several ISAs; decisions can be cached or re-asked | A full pipeline copy per target; JIT not allowed |
SVE2 compiled for a fixed vector_bits | Reuses fixed-width vectorisation and instruction selection | Binaries are vector-length-specific |
| Vulkan via direct SPIR-V over storage buffers, synchronous | No GLSL toolchain; strided indexing works for any layout; simple runtime | No texture hardware, no sampling, no overlap of transfer and compute; beta quality |
| Autoschedulers as plugins keyed by name | Heuristic, learned and GPU searches coexist and evolve outside the core | Require size estimates; learned models need training data per architecture |
Sources
- halide/Halide repository, read at
85f16183923fcec94c2fb6e756d98e6831ea1524:README.md,LICENSE.txt,CMakeLists.txt. - Buffer ABI and runtime:
halide_buffer_t,halide_type_t, device interface, error codes,halide_malloc,halide_do_par_for,halide_can_use_target_features, target features,Runtime::Buffer, ownership, dirty-bit copies,_halide_buffer_is_bounds_query. - Scheduling and lowering:
Stagedirectives, GPU directives,store_at,TailStrategy,RDom, boundary conditions,Lower.cpp, image checks, unbounded-access error, size checks,clamp_unsafe_accesses,Target.hbuffer limits. - Generators and dispatch:
Generator.h,compile_multitarget, x86 detection, XCR0 check, SVEvector_bits, host SVE length,apps/blur, tutorial lesson 8. - Vulkan:
doc/Vulkan.md,CodeGen_Vulkan_Dev.cppheader, buffer binding, workgroup size,vulkan.cpprun, copy to device, wrap, crop offset, allocator config, context hooks. - Autoschedulers: Mullapudi2016, Li2018 README, Adams2019, Anderson2021, plugin registration.
- Testing:
simd_op_check.h,simd_op_check_x86.cpp,test/fuzz/bounds.cpp,doc/FuzzTesting.md,doc/CustomRuntimes.md,doc/RunGen.md. - DLPack
DLTensorat94485e23519bc30e22a18397a9c8ff1ea6b2b81e, for the ABI comparison. - Papers: DSLs and scheduling thread (Ragan-Kelley 2012, 2013, 2018; Mullapudi 2016; Adams 2019; Anderson 2021; Li 2018).
- Within this survey: umbrella, concepts, shaders and SIMD baseline, existing image code baseline, Vulkan baseline, event-horizon baseline, hardware matrix.