# Kernelcodegen

> <!-- Copyright (C) 2025 Advanced Micro Devices, Inc. All Rights Reserved.

- Skill: `xilinx/kernelcodegen` (Agent Skill, multi-file: 5 files)
- Install (CLI): `npx skillmds@latest add xilinx/kernelcodegen`
- Raw SKILL.md: https://api.skillmd.com/api/skills/xilinx/kernelcodegen/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: xilinx (https://skillmd.com/u/xilinx)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/xilinx/kernelcodegen

---

<!-- Copyright (C) 2025 Advanced Micro Devices, Inc. All Rights Reserved.
     SPDX-License-Identifier: Apache-2.0 -->
---
name: kernelcodegen
description: AIE kernel code generation, compilation, and (optional) testing for the aiehlc flow. Use when you need to (1) Generate kernel C++ (kernel.cc, compute_kernel.cc) from the MLIR unitest pipeline, (2) Compile the kernel to an AIE kernel ELF with xchesscc/xchessmk, (3) Bypass or run kernel tests on HW. Complements hostcodegen (host side); kernel runs on AIE tiles.
---

# Kernelcodegen

This skill drives the generate → compile → (optional) test loop for aiehlc AIE kernel code.

## Workflow

1. **Generate**: Build the unitest, run `./test` (e.g. dfschedule). Outputs: `worklocal/kernel.cc`. Same generation step as host; both host.cc and kernel.cc are produced. Kernel dfschedule is generated by **passblueprinttoschedulekernel** (`passblueprinttoschedulekernel.cpp`); **passdfscheduletokernelapi** (`passdfscheduletokernelapi.cpp`) is in charge of kernel.cc generation (schedule → EmitC → C++). `worklocal/compute_kernel.cc` is a **pre-existing** file (hand-written) for now. Files involved and full pass flow: [references/codegen-flow.md](references/codegen-flow.md). Paths: [references/project-layout.md](references/project-layout.md).

2. **Compile**: Kernel-only build via `KERNEL_ONLY=1 source script/hostcompile.sh` (the `compile_one_kernel` function; run with `WORKLOCAL_DIR` pointing at worklocal). Uses xchesscc (kernel.cc → LLVM IR; kernel.cc may include pre-existing `compute_kernel.cc`), opt (xlopt), xchessmk (link with aie2ps.prx). ELF output: `worklocal/build/kernel`. For env and options see [references/compile-env.md](references/compile-env.md).

3. **Test** (bypass for now): Kernel-on-HW testing can be bypassed. When needed later, see [references/testing.md](references/testing.md).

## Bug fix

- **Codegen error** → Fix MLIR passes: BlueprintToScheduleKernelPass, DfscheduleToKernelApiPass; unitest driver in `src/mlir/mlirfront/tilinglinalg/pass/unitest/test.cpp` (kernel module emit to kernel.cc). See [references/codegen-flow.md](references/codegen-flow.md) for the full pass sequence.
- **Compile error** → Fix `compile_one_kernel`/`kc.sh` includes, xchesscc flags, or aie2ps.prx; ensure kernel.cc and compute_kernel.cc (if used) are consistent with AIE API.

## Resources

| When | File |
|------|------|
| Paths for unitest, worklocal, kernel.cc, scripts | [references/project-layout.md](references/project-layout.md) |
| Pass pipeline that generates kernel.cc (blueprint → kernel API → EmitC) | [references/codegen-flow.md](references/codegen-flow.md) |
| setup.sh, XILINX_VITIS, xchesscc/xchessmk, compile errors | [references/compile-env.md](references/compile-env.md) |
| Kernel testing (bypass / future) | [references/testing.md](references/testing.md) |

