pxcc - Heterogeneous C/C++ Compiler
Use pxcc for C sources and pxcc++ for C++ sources. pxcc builds an application whose host code runs on the CPU and whose kernels run on the MU cores. pxcc produces the device code binary that the application loads at run time.
Start with Hello Sort for a complete application. See Programming Objects for explicit Module and Map management.
What pxcc gives you
- One build command produces the whole application, and no separate kernel build step runs beside it.
- The host refers to a kernel by its C++ symbol, so
createFunction<name>()andexecute<name>()both take a real C++ entity. - The compiler checks the launch arguments against the kernel signature.
- An editor resolves a kernel the way it resolves any other function, so go-to-definition and rename both work.
- A kernel may be a function template, as in
sort_with_ptr<int>, and a kernel may be overloaded. - The
mu_libinclude path and thepxllibrary reach every build without a flag of your own.
A complete example
#include <algorithm>
#include <cstdio>
#include "mu/mu.hpp"
#include "pxl/pxl.hpp"
// Device kernel — extracted and compiled for the device by pxcc.
__pxl_kernel__ void sort_with_ptr(int* arr, int size)
{
int idx = mu::getTaskIdx();
std::sort(arr + idx * size, arr + idx * size + size);
}
int main()
{
const int testCount = 2048, sortSize = 64;
auto* data = pxl::allocateMemory<int>(0, testCount * sortSize);
if (!data)
{
printf("Allocation failed\n");
return 1;
}
for (int i = 0; i < testCount; i++)
for (int j = 0; j < sortSize; j++)
data[i * sortSize + j] = sortSize - j;
auto result = pxl::Launcher()
.execute<sort_with_ptr>(data, sortSize)
.tasks(testCount)
.run();
if (result.status != pxl::Result::Success)
{
printf("FAIL: %s\n", result.errorMessage.c_str());
pxl::releaseMemory(data);
return 1;
}
// Verify every sub-array is sorted in ascending order.
bool sorted = true;
for (int i = 0; i < testCount && sorted; i++)
for (int j = 1; j < sortSize; j++)
if (data[i * sortSize + j - 1] > data[i * sortSize + j]) { sorted = false; break; }
printf("%s\n", sorted ? "PASS" : "FAIL");
pxl::releaseMemory(data);
return sorted ? 0 : 1;
}
pxcc++ sort.cpp -o sort # compile host + device and link in one step
sudo ./sort # CXL device memory access requires elevated privileges
A runnable version of this example lives in example/single_source/main.cpp. A template-kernel variant lives in example/template_programming/template_programming.cpp. Both default to device 0 and accept -d <id> / --device <id> to select another device. The multi-file example compiles a kernel and a shared method from separate source files.
Key facts
- Two drivers:
pxcc(C) andpxcc++(C++). They are drop-in compiler front-ends - set them asCMAKE_C_COMPILER/CMAKE_CXX_COMPILER. - Host code uses C++17 or later. Device code uses C++11 through C++26, and defaults to C++17. Set the device standard with
-Xmu=-std=...; the full rule is in Constraints. - Includes and linking are automatic. The
mu_libinclude path is added on compile and-lpxlis added on link - no extra CMake configuration needed. - Host-only compilation. A source file with no
__pxl_kernel__is compiled like an ordinary clang C++ translation unit. - A kernel change needs a rebuild of the program. No separate kernel artifact rebuilds on its own, so build the program again after you edit a kernel.
Programming model
Host code and device code live in the same project, and an annotation states which side a function belongs to. Write the kernel as an ordinary C++ function and mark it with __pxl_kernel__.
Annotations
| Annotation | Where the code runs |
|---|---|
| (none) | Host only. An un-annotated function is compiled for the host and is not available on the device - calling it from kernel/device code is an error. |
__pxl_kernel__ | Device kernel - a launchable entry point. |
__mu_device__ | Device-only helper function. mu:: APIs are available. |
__mu_shared__ | Code shared by host and device. mu:: APIs are not available. |
So an ordinary function stays on the host. To make code reachable from a kernel, annotate it __mu_device__ (device-only) or __mu_shared__ (host and device):
int hostUtil(int x); // no annotation → host only; not callable from a kernel
__mu_device__ int devUtil(int x); // device only
__mu_shared__ int shared(int x); // host and device
__pxl_kernel__ void k(int* a, int n)
{
a[0] = devUtil(a[0]) + shared(a[0]); // OK
// a[0] = hostUtil(a[0]); // ERROR: host-only symbol on the device
}
A small end-to-end example using all three annotations together:
#include "mu/mu.hpp"
// Shared by both sides — plain logic, no mu:: calls.
__mu_shared__ int clamp(int v, int lo, int hi)
{
return v < lo ? lo : (v > hi ? hi : v);
}
// Device-only helper — may call mu:: APIs.
__mu_device__ int taskBase(int size)
{
return mu::getTaskIdx() * size;
}
// Kernel — the launchable entry point.
__pxl_kernel__ void scale(int* arr, int size, int factor)
{
int base = taskBase(size);
for (int i = 0; i < size; i++)
arr[base + i] = clamp(arr[base + i] * factor, 0, 1000);
}
__mu_shared__functions cannot usemu::APIs (they also compile for the host). Use__mu_device__when you needmu::.
Annotating a class
Both annotations also go on a class or a struct. Write the annotation between the class or struct keyword and the type name. The annotation then covers every member of that type, so you do not annotate the members one by one.
__mu_shared__ on a class gives you a type that both sides use. A kernel reads and writes objects of that type in device memory, and host code builds and inspects the same objects. A member defined inside the class body may stay in a header, so a shared type is usually a header type.
struct __mu_shared__ Point
{
int x;
int y;
int lengthSquared() const { return x * x + y * y; }
};
__pxl_kernel__ void square(Point* points, int count)
{
int idx = mu::getTaskIdx();
points[idx].x = points[idx].lengthSquared(); // runs on the device
}
int main()
{
Point p{3, 4};
return p.lengthSquared() == 25 ? 0 : 1; // the same member runs on the host
}
__mu_device__ on a class gives you a type that the device alone uses. Its members may call mu:: APIs, which a __mu_shared__ member may not.
class __mu_device__ TaskRange
{
public:
explicit TaskRange(int size) : size_(size) {}
int begin() const { return mu::getTaskIdx() * size_; }
int end() const { return begin() + size_; }
private:
int size_;
};
Host code that calls a member of such a class still compiles. The call fails when the program runs it, and the program prints the member it tried to run:
[PXCC Error] Device function 'TaskRange' cannot be executed on Host.
A host line that builds a TaskRange runs the constructor first, and a constructor is a member as well, so this message states the name of the class.
A member of an annotated class template runs on the device alone, so call such a member from device code only.
Multi-file projects
Kernels and shared code may span translation units. Declare cross-TU shared symbols with __mu_shared__ in a header and compile the files together:
pxcc++ -c kernels.cpp helpers.cpp # compile each to <stem>.o in the current directory
pxcc++ kernels.o helpers.o -o app # link
# or in one step:
pxcc++ kernels.cpp helpers.cpp -o app
Constraints
- Host code uses C++17 or later. The host standard carries no limit from the device side.
- Device code uses C++11 through C++26, and defaults to C++17. Device code is the code reachable from
__pxl_kernel__. Two flags set the two sides, and a build that raises the standard passes both.-std=c++20sets the host compiler and the two analysis frontends, and-Xmu=-std=c++20sets the device compiler. A build that passes one of the two leaves the other side at its own standard, and the side left behind is where the error appears. Some C++ features inside that device range do not work yet. A later release lists those features. - A kernel defines its body in a
.cppfile. pxcc rejects a__pxl_kernel__or__mu_device__function whose body sits in an included header. A__pxl_kernel__function template is the exception, and its body may stay in a header. A member defined inside a__mu_shared__or__mu_device__class body may stay in a header as well. - The
mu::namespace is available in__pxl_kernel__and__mu_device__code, but not in__mu_shared__code.
Compilation and linking
pxcc accepts the familiar clang/gcc-style compile and link forms.
# Single file (CMake style)
pxcc++ -c input.cpp -o input.o # compile only
pxcc++ input.o -o program # link
pxcc++ input.cpp -o program # compile + link in one step
# Multiple files
pxcc++ -c f1.cpp f2.cpp # compile each to <stem>.o in the current directory
pxcc++ f1.cpp f2.cpp -o app # compile + link several files
-o is always a single file, exactly as in clang/gcc. Passing -o together with -c and multiple source files is rejected (cannot specify -o when generating multiple output files). Omit -o to get one <stem>.o per source in the current directory.
Two things happen automatically, so you do not pass them yourself:
- the
mu_libinclude path is added on every compile, and -lpxlis added on every link.
This is why the CMake setup needs nothing beyond setting the compiler and the C++ standard.
Device options - -Xmu=
Flags that should reach the device compiler are forwarded with -Xmu=<arg> (one flag per -Xmu=). Most flags without -Xmu= apply to the host side only. A few of them also reach the device compiler: -I, -D, -U, --isystem, --include, and warning flags such as -Wall, -Werror, and -w. Use -Xmu=-I<dir> or -Xmu=-D<macro> when only the device compiler should see the directory or the macro.
| Form | Effect on device compilation |
|---|---|
-Xmu=-std=c++20 | Set the device C++ standard (C++11 through C++26, C++17 by default; pass the matching host -std= as well). |
-Xmu=-O0 … -Xmu=-O3, -Xmu=-Os | Device optimization level (default -O3). |
-Xmu=-g | Emit device debug info. |
-Xmu=-I/path | Add a device include search path. |
-Xmu=-isystem -Xmu=/path | Add a device-only system include path (not visible to the host compiler). |
-Xmu=-DNAME=VALUE | Define a device preprocessor macro. |
-Xmu=-L/path | Add a device library search path. |
-Xmu=-lfoo | Link an extra device library - e.g. -Xmu=-L/opt/xarith/lib -Xmu=-lxarith. |
pxcc++ -Xmu=-g -Xmu=-O0 -c kernel.cpp -o kernel.o # device debug build, no opt
pxcc++ -Xmu=-I/opt/inc -Xmu=-DDEBUG kernel.cpp -o app
Host compiler options
Flags without the -Xmu= prefix apply to the host compiler. The rows marked both also reach the device compiler. The common ones:
| Option | Purpose |
|---|---|
-I<dir> | Add an include directory to both host and device (use -Xmu=-I<dir> for device only). |
-D<macro>[=value] | Define a preprocessor macro for both host and device (use -Xmu=-D<macro> for device only). |
-U<macro> | Undefine a preprocessor macro for both host and device. pxcc applies -D and -U in the order you write them. |
--isystem <dir> | Add a system include path to both host and device (use -Xmu=-isystem for device only). |
--include <file> | Force-include a header before the source, for both host and device. |
-x <language> | Set the source language (c or c++). |
--std=<standard> | Host C++ standard (default c++17). The host may use a newer standard than the device - see Constraints. |
-MF <file> | Write make-style dependency output to <file> (for incremental build systems). |
-MT <target> | Set the target name emitted in the dependency output. |
-MQ <target> | Like -MT, but quotes characters special to make. |
Any flag pxcc does not recognize is forwarded to the host compiler unchanged.
The host compiler
pxcc compiles the host half of your program with a compiler that is already installed on the system. pxcc does not ship one of its own. By default pxcc runs the clang that PATH resolves to, and it falls back to /usr/bin/clang. pxcc++ --version prints the compiler it picked, so one command answers which one a machine uses.
$ pxcc++ --version
pxcc version 26.09.18 (commit 9121d83) (host compiler: clang 19.1.1)
Three settings override that choice, and pxcc reads them in this order.
--host-compiler=<path> > PXCC_HOST_CXX / PXCC_HOST_CC > CXX / CC
> clang in PATH > /usr/bin/clang
--host-compiler=<path>- use a specific host compiler for this invocation.PXCC_HOST_CXX/PXCC_HOST_CC(env) - host C++ / C compiler forpxcc++/pxcc.CXX/CC(env) - fallback when the above are unset.
pxcc++ --host-compiler=/usr/bin/g++-12 -c app.cpp -o app.o
PXCC_HOST_CXX=/usr/bin/g++-12 pxcc++ app.cpp -o app
Diagnostics and intermediates
pxcc++ -save-temps -c input.cpp -o input.o # keep intermediate files
-save-temps preserves the intermediate artifacts of the split-compile pipeline next to the output:
| File | Description |
|---|---|
mu.{filename}.cpp | Extracted device code. |
host.{filename}.cpp | Transformed host code. |
mu_kernel.mubin | Device binary. |
mu_kernel.o | Embedding object linked into the host binary. |
These are the first things to inspect when a kernel does not behave as expected - see Troubleshooting.
To see what pxcc actually runs, use one of the verbose forms:
pxcc++ --pxcc-verbose -c input.cpp -o input.o # pxcc's own internal command log
pxcc++ -v -c input.cpp -o input.o # commands the host compiler runs
pxcc++ --dry-run -c input.cpp -o input.o # print commands without executing (= -###)
CMake integration
set(CMAKE_CXX_COMPILER pxcc++)
set(CMAKE_CXX_STANDARD 17)
add_executable(myapp main.cpp kernel.cpp)
target_compile_options(myapp PRIVATE -Xmu=-O2) # device options
Override the host compiler from CMake via the flag or an environment variable:
cmake -DCMAKE_CXX_COMPILER=pxcc++ -DCMAKE_CXX_FLAGS="--host-compiler=/usr/bin/g++-12" ..
# or
PXCC_HOST_CXX=/usr/bin/g++-12 cmake -DCMAKE_CXX_COMPILER=pxcc++ ..
mu_lib include paths and -lpxl linking are handled automatically.
Help and version
pxcc++ --help # brief help with key features
pxcc++ --manual # full man-page-style manual (all options, examples, troubleshooting)
pxcc++ --version # version string (pxcc, mu_llvm, host/device targets)
Reference summary
| Option | Applies to | Purpose |
|---|---|---|
-c | both | Compile only; do not link. |
-o <path> | both | Output file (always a single file). |
-j <N> | driver | Max parallel compilation jobs (default: CPU cores). |
-Xmu=<arg> | device | Forward <arg> to the device compiler. |
--host-compiler=<path> | driver | Choose the host compiler. |
--std=<standard> | host | Host C++ standard (default c++17). |
-MF / -MT / -MQ | host | Make-style dependency output: file / target / quoted target. |
-save-temps | both | Keep intermediate host/device sources and binaries. |
--pxcc-verbose | driver | Log the commands pxcc executes. |
-v | host | Show the commands the host compiler runs. |
--dry-run, -### | driver | Print commands without executing. |
--help / --version / --manual | driver | Brief help / version / full manual. |
Related
- Hello Sort - write, compile, and run your first kernel.
- Troubleshooting - device C++ standard errors, missing-annotation symptoms, link failures.