8000 OpenCL: generate from the headers through the shared generators by TApplencourt · Pull Request #564 · argonne-lcf/THAPI · GitHub
[go: up one dir, main page]

Skip to content

OpenCL: generate from the headers through the shared generators - #564

Merged
TApplencourt merged 9 commits into
develfrom
opencl-unify
Nov 13, 2026
Merged

TApplencourt merged 9 commits into
develfrom
opencl-unify

Conversation

@TApplencourt
@TApplencourt TApplencourt commented Nov 9, 2026 •
Copy link
Copy Markdown
Collaborator

What

OpenCL was the one backend with its own code generator: cl.xml read with Nokogiri by a 1349-line opencl_model.rb, with its own Command/Parameter/meta-parameter classes and type mapping. It now uses the same path as ze, cuda and hip:

  • the headers go through h2yaml into opencl_api.yaml;
  • the shared Command, meta-parameters, print_tracepoint_provider and print_traced_wrappers generate the code.

cl.xml is still read, but only to produce data. gen_opencl_xml_yaml.rb writes opencl_xml.yaml:

It also checks that the headers declare every traced function with as many parameters. The tracepoints keep the registry's parameter names: the oneAPI cl.h calls clGetKernelArgInfo's arg_index arg_indx, so opencl_model.rb renames it.

Commits

  1. opencl: parse the headers with h2yaml into opencl_api.yaml. The h2yaml call undefines __SSE__/__SSE2__/__MMX__/__AVX__: the pip libclang can't parse GCC's intrinsic headers, and no traced function takes a vector type.
  2. utils: let the shared generators say what OpenCL's output says: shared features plus per-backend spelling settings, documented on BackendContext. No output change.
  3. opencl: generate from the headers, through the shared generators: opencl_model.rb goes from 1349 lines to 634, and the old XML model and generators are deleted.
  4. opencl: simpler traced-function lists; README for the h2yaml path: review pass. List operations instead of regexes built from lists; a supported_enums.yaml type that no <require comment> names now stops the generator (the old one crashed, the new one silently gave it no values); backends/opencl/README.md points at where the code now lives.
  5. utils: take OpenCL's traced scalar names from the registry, not the AST: gen_opencl_xml_yaml.rb already writes the typedef → base-type map (type_map), and it agrees with the AST walk on every typedef both know, so ApiModel takes that map and find_trace_names goes.
  6. opencl: compute the implied meta-parameters next to the prologues: the rows OpenCL's naming implies (errcode_ret, event, …) are computed in opencl_model.rb from the AST, next to the prologues they depend on, instead of in gen_opencl_xml_yaml.rb. opencl_xml.yaml only carries registry facts, and load_meta_parameters goes back to devel's code.
  7. opencl: rebuild the tracer and probes when the shared generators change: tracer_opencl.c and opencl_tracepoints.tp now depend on utils/gen_tracer_base.rb / utils/gen_probe_base.rb, like every other backend.
  8. utils: one libffi closure generator for ze and OpenCL: both backends wrap the extension functions a driver hands out in a libffi closure, with the same ~35-line C template; it is now ffi_closure_epilogue in utils/command.rb (−26 lines overall, generated files identical for every backend).
  9. opencl: one source for handles, pointer parameters, and extension lookups: CL_OBJECTS is checked against the registry's pointer typedefs the headers declare (a new handle stops the build instead of being missed); a supported_extensions.yaml name unknown to cl.xml stops the generator; one pointer_parameter? serves both the nocheck meta-parameters and the prologues that justify them.

Generated output

Identical to devel: every generated file and every exported symbol of every backend (utils/test_compare_generated_*.py, 59/59).

Also checked locally (sunspot):

  • make check in backends/opencl (10/10);
  • iprof pretty-print (-t) and tally of real OpenCL and SYCL-on-OpenCL traces, replayed with this branch and with devel: identical.

_param_name in the babeltrace model (#570, merged into this branch before) is no longer here: it changes opencl_model.yaml and btx_cl_model.yaml. It will come back as its own PR after this one.

Follow-up (separate PR)

Remove the per-backend spelling settings and the registry declarations, move the reading side (opencl_model.yaml → btx model, Ruby pretty-printer) onto print_bt_model and a generated library, and accept the resulting output diffs.

🤖 Generated with Claude Code

@TApplencourt
TApplencourt changed the base branch from devel to opencl-headers November 9, 2026 20:37
@TApplencourt TApplencourt changed the title OpenCL: generate from the headers through the shared generators (byte-identical) OpenCL: generate from the headers through the shared generators Nov 9, 2026
@TApplencourt
TApplencourt force-pushed the opencl-unify branch 3 times, most recently from 6cae790 to 07bf2e9 Compare November 9, 2026 21:20
@TApplencourt
TApplencourt marked this pull request as ready for review November 9, 2026 22:38
Base automatically changed from opencl-headers to devel November 13, 2026 18:27
TApplencourt and others added 4 commits November 13, 2026 18:45
Like every other AST-driven backend: extract/opencl_api.h includes the
refreshed OpenCL headers plus opencl_missing_apis.h, and h2yaml turns them
into opencl_api.yaml. Nothing consumes it yet.

cl_platform.h includes the x86 intrinsic headers, and declares the vector
types of cl_float4 & co., when __SSE__, __SSE2__, __MMX__ or __AVX__ is
defined. A libclang without its own resource headers (the pip wheel CI
installs) falls back to GCC's intrinsics and cannot parse them, and
h2yaml 0.4.3 has no vector kind. No traced function takes a vector type,
so the h2yaml call undefines those four macros.

All 173 functions the XML path traces are in opencl_api.yaml, with the
same parameter names but for one: the oneAPI cl.h calls clGetKernelArgInfo's
arg_index `arg_indx`. Generated files are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Preparation for moving OpenCL onto the shared, header-driven generators.
No generated file changes: every addition below is off unless a backend
asks for it, and none does yet.

Shared features any API needs:
- Typedefs of float classify as :float and trace with ctf_float.
- An unsized array parameter (`T x[]`) is the pointer C decays it to.
- A meta-parameter row may end in a mapping of keyword options; OutScalar
  takes `nocheck:`, for a pointer the tracer has already made non-NULL.
- load_meta_parameters accepts an absolute path, for a generated spec.
- A declared event with `start`/`stop` field lists wraps a call: it
  declares an entry and an exit tracepoint, like a traced function.
- print_tracepoint_provider takes a `preamble` printed before lttng's own
  header.
- get_ffi_type moves from ze_model.rb to utils/command.rb, so ze and
  OpenCL build their libffi closures with the same one.

Per-backend settings that keep the C OpenCL generated before byte-for-byte
(to be dropped in a follow-up that accepts the output change):
- ApiModel `trace_as:` traces a scalar typedef as the base type its chain
  reaches (cl_mem_flags as cl_ulong).
- BackendContext: result_field, duration, null_guard, null_test,
  xml_arg_spelling, legacy_blobs, pointer_type_suffix, declarations
  (documented where BackendContext is defined).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
OpenCL was the one backend with a code generator of its own: cl.xml read
with Nokogiri by a 1300-line opencl_model.rb, with its own Command,
Parameter and meta-parameter classes and its own type-to-lttng mapping.
It now goes the way of ze, cuda and hip: the headers through h2yaml
(opencl_api.yaml), utils/command.rb's Command, utils/meta_parameters.rb,
print_tracepoint_provider and print_traced_wrappers.

The registry is still read, but only to produce data, by
gen_opencl_xml_yaml.rb (opencl_xml.yaml):
- the functions traced, in registry order, core vs. extension;
- every function's meta-parameter rows: the ones OpenCL's naming implies
  (event_wait_list, errcode_ret, param_value ...), the hand-written ones,
  and the queried param_name of clGet*Info;
- the declarations as the registry writes them, which the wrappers repeat;
- the enum/bitfield groups, typedef map and struct list opencl_model.yaml
  carries for the babeltrace model and the pretty-printer.
It also checks that the headers declare every traced function, with as
many parameters. The tracepoints keep the registry's parameter names
(opencl_model.rb renames the AST's): the oneAPI cl.h calls
clGetKernelArgInfo's arg_index `arg_indx`.

The registry rules of the header update (#565) move with the rest:
functions only experimental extensions require are not traced; the
clIcd*KHR and layer functions, and supported_extensions.yaml's, are not
looked up at load; PoCL is a vendor suffix; cl_semaphore_khr is a handle
(its result field is `semaphore`). OpenCL keeps spelling a C int
`ffi_type_sint` and an empty parameter list `(void)`.

opencl_meta_parameters.yaml spells out two things the old classes implied:
the `num_entries` size of four arrays, and clCreateSubBuffer's
buffer_create_info blob (an InLTTng row).

opencl_model.rb keeps what is OpenCL's own: its BackendContext settings,
the device-fission property list, and the prologues/epilogues (dump,
profiling, source capture, extension-function dispatch ...), unchanged.

Generated files are identical. Checked against origin/devel: every
generated file and exported symbol of every backend; make check; and iprof
pretty-print and tally of real OpenCL and SYCL-on-OpenCL traces.

CommandIndex no longer needs `pointer_name_of`, which existed for the
old OpenCL Command.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gen_opencl_xml_yaml.rb:
- The traced lists are list operations, not regexes built from the lists.
- `spelled` loses its `strip:` option: it kept the trailing space of the
  one `void` parameter cl.xml no longer has.
- A supported_enums.yaml type that no <require comment> names stops the
  generator with a message. The XML generator crashed on it; the new one
  gave the type no values, silently.

README: point at where the code now lives (gen_opencl_xml_yaml.rb,
opencl_result_field, with_registry_names) and at the errors
gen_opencl_xml_yaml.rb raises; list CL_HDR_ROOT in the header steps.
Drop the `ffi_type_...` problem: the libffi types now come from the shared
map and the headers, as do the handles (CL_OBJECTS only feeds
opencl_model.yaml). Correct what the cl_affinity_domain_ext typedef hunk
does: without it, nothing crashes, type_map just lacks the entry.

Generated files are identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TApplencourt and others added 5 commits November 13, 2026 19:15
ApiModel's `trace_as` walked each typedef chain in the AST to find the
cl_* (or C) base type OpenCL records a scalar under (cl_mem_flags as
cl_ulong). gen_opencl_xml_yaml.rb already writes that mapping from the
registry (`type_map`): for every typedef both know, the two agree, and the
registry also covers types the parsed headers do not declare (beta,
Windows, layer). ApiModel now takes the map itself (`trace_names:`), and
find_trace_names goes.

Generated files are identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The meta-parameter rows OpenCL's naming conventions imply (event_wait_list,
errcode_ret, param_value_size_ret, param_value, event, and the queried
param_name of clGet*Info) were computed from the registry by
gen_opencl_xml_yaml.rb and read back as a generated spec. They depend on
the prologues of opencl_model.rb: errcode_ret and param_value_size_ret skip
their NULL check only because a prologue points them at a local. They are
now computed there, from the same AST the prologues read, and
opencl_xml.yaml only carries what the registry says.

Two utils changes made only to read that generated spec go back to
devel's code: load_meta_parameters takes a path under SRC_DIR again, and
no longer parses a trailing mapping of keyword options (the hand-written
specs never use one).

Generated files are identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gen_opencl.rb and gen_opencl_probes.rb now require utils/gen_tracer_base.rb
and utils/gen_probe_base.rb, but tracer_opencl.c and opencl_tracepoints.tp
did not depend on them, as they do in every other backend: an edit to
either left OpenCL's generated files stale.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ze (zeDriverGetExtensionFunctionAddress) and OpenCL
(clGetExtensionFunctionAddress[ForPlatform]) hand out extension functions
that no library exports, and each driver returns its own: the tracer wraps
each real pointer in a libffi closure calling its `<command>_ffi` wrapper,
cached by pointer. Both models carried the same ~35-line C template for
it; now that both build their commands with utils/command.rb, it is one
function there, ffi_closure_epilogue, next to get_ffi_type. The callers
only say what differs: the lookup's provider and name variable, the
closure prefix, where the real pointer is, and how the lookup returns.

Generated files are identical (every backend).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…kups

- CL_OBJECTS repeated what the registry and the headers say: the handles
  are the registry's pointer typedefs (`struct _cl_foo *`, `void *`) that
  the headers declare. The list stays, in the order opencl_model.yaml has
  it, but gen_opencl_xml_yaml.rb now stops when it is not exactly those, so
  a new handle can no longer be missed. The registry's `*`-before-<name>
  test is one function, also used for parameters.
- supported_extensions.yaml: a name cl.xml does not have stops the
  generator instead of being dropped silently.
- "Takes a pointer named x" was computed twice in opencl_model.rb: for the
  `nocheck` meta-parameters and for the prologues that make them safe. One
  pointer_parameter? now serves both.
- The two "named like an extension" selections (handed back by
  clGetExtensionFunctionAddress; looked up with it at load) sit together in
  opencl_model.rb instead of in two files.

Generated files are identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TApplencourt
TApplencourt merged commit 042fb3e into devel Nov 13, 2026
27 checks passed
@TApplencourt
TApplencourt deleted the opencl-unify branch November 13, 2026 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0