Skip to content

SilvesterHsu/rules_opencl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenCL rules for Bazel

Bazel rules for compiling OpenCL source files (.cl) to C++ header files (.cl.h).

Overview

rules_opencl provides Bazel rules for compiling OpenCL source files into C++ header files. The generated header files include:

  • OpenCL source code as C++ string constants
  • Automatically extracted kernel name arrays
  • Preserved original comments (optional)

Installation

Using Bazel MODULE.bazel (Recommended)

Add the following to your MODULE.bazel file:

bazel_dep(name = "rules_opencl", version = "1.0.0")

# Use git_override to point to GitHub repository
git_override(
    module_name = "rules_opencl",
    remote = "https://github.com/SilvesterHsu/rules_opencl.git",
    commit = "your-commit-hash",  # Replace with actual commit hash
)

# Or use archive_override (recommended for releases)
# archive_override(
#     module_name = "rules_opencl",
#     urls = ["https://github.com/SilvesterHsu/rules_opencl/archive/v1.0.0.tar.gz"],
#     strip_prefix = "rules_opencl-1.0.0",
#     integrity = "sha256-...",  # Calculate actual sha256
# )

Usage

Basic Usage

In your BUILD.bazel file:

load("@rules_opencl//:opencl.bzl", "opencl_library")

opencl_library(
    name = "my_kernel_cl",
    srcs = ["my_kernel.cl"],
)

cc_library(
    name = "my_kernel_cl_hdr",
    hdrs = [":my_kernel_cl"],
)

cc_binary(
    name = "my_program",
    srcs = ["my_program.cc"],
    deps = [":my_kernel_cl_hdr"],
)

Advanced Usage

Using Include Directories

opencl_library(
    name = "my_kernel_cl",
    srcs = ["my_kernel.cl"],
    include_dirs = ["kernels/common", "kernels/utils"],
    hdrs = ["kernels/common/definitions.cl"],
)

Specifying Variable Name

opencl_library(
    name = "my_kernel_cl",
    srcs = ["my_kernel.cl"],
    var = "my_custom_kernel_source",
)

Using in C++ Code

The generated header file will contain:

// Auto-generated variable name (based on filename)
constexpr const char* my_kernel = "...";  // OpenCL source code

// Kernel name array (if kernel functions are detected)
constexpr const char* my_kernel_kernels[] = {
    "kernel_name_1",
    "kernel_name_2"
};
constexpr unsigned int my_kernel_kernel_count = 2;

Requirements

  • Bazel 6.0 or higher (with MODULE.bazel support)
  • rules_python (automatically included as a dependency)

Rule Reference

opencl_library

Compiles OpenCL source files to C++ header files.

Parameters:

  • name (required): Target name
  • srcs (required): List of OpenCL source files (.cl files), must contain exactly one file
  • include_dirs (optional): List of include directories for #include directives
  • var (optional): C++ variable name, derived from filename if not specified
  • hdrs (optional): List of .cl header files that may be included by the source file

Output:

  • Generates .cl.h header file containing OpenCL source code as C++ string constants

Examples

See test cases in the project for complete examples.

About

Starlark implementation of bazel rules for opencl.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published