Skip to content

RealtimeRoboticsGroup/rules_halide

Repository files navigation

rules_halide: Halide Rules for Bazel

rules_halide provides Bazel rules to build and run Halide generators ahead-of-time (AOT) to produce optimized object files and headers, and link them as standard Bazel cc_library targets.

Features

  • Automated Toolchain Setup: Automatically downloads and configures the prebuilt Halide release for your host platform.
  • Bzlmod Support: Integrates seamlessly with Bazel's modern Bzlmod dependency system.
  • Cross-Compilation: Supports cross-compiling Halide generator pipelines to target platforms (e.g. linux-aarch64 / macos-arm64) using Bazel platforms and toolchains.
  • Multi-Platform Support: Works on x86_64 Linux, aarch64 Linux, and arm64 macOS.

Installation

1. Using Bzlmod (Recommended)

Add the following to your MODULE.bazel file:

bazel_dep(name = "rules_halide", version = "0.0.1")

Usage

Here is a step-by-step example of how to define and use a Halide generator in your project.

1. Write your Halide Generator

Create a file, e.g., simple_generator.cc:

#include "Halide.h"

class SimpleScale : public Halide::Generator<SimpleScale> {
public:
    GeneratorParam<float> scale{"scale", 2.0f};

    Input<Halide::Buffer<float, 2>> input{"input"};
    Output<Halide::Buffer<float, 2>> output{"output"};

    Halide::Var x{"x"}, y{"y"};

    void generate() {
        output(x, y) = input(x, y) * scale;
    }
};

HALIDE_REGISTER_GENERATOR(SimpleScale, simple_scale)

2. Declare the Halide Target in BUILD.bazel

Load halide_library and define your target:

load("@rules_halide//:halide.bzl", "halide_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")

halide_library(
    name = "simple_scale",
    src = "simple_generator.cc",
    function = "simple_scale",
    args = "scale=2.0",
)

cc_test(
    name = "simple_test",
    srcs = ["simple_test.cc"],
    deps = [
        ":simple_scale",
    ],
)

3. Use the Generated Code in C++

Include the generated header and use Halide::Runtime::Buffer:

#include <iostream>
#include "HalideBuffer.h"
#include "simple_scale.h" // generated header

int main() {
    Halide::Runtime::Buffer<float, 2> input(4, 4);
    // ... initialize input ...
    
    Halide::Runtime::Buffer<float, 2> output(4, 4);
    
    // Call the generated ahead-of-time function
    int result = simple_scale(input, output);
    if (result != 0) {
        return result;
    }
    
    std::cout << "Pipeline completed successfully!" << std::endl;
    return 0;
}

Supported Platforms

rules_halide supports building and executing Halide pipelines on the following host and target platforms:

  • Linux x86_64
  • Linux AArch64
  • macOS ARM64 (Apple Silicon)

About

Bazel rules for halide

Resources

License

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors