---
title: "compileModule(): function in CPU oracle, TypeShade API"
description: "compileModule, a function in CPU oracle. Compile a module for the CPU: a tree-walk interpreter over the same IR the GPU backends receive…"
url: https://typeshade.dev/api/compilemodule/
language: en
---

# `compileModule()`

Function in CPU oracle

Compile a module for the CPU: a tree-walk interpreter over the same IR the GPU backends receive, returning a CpuModule whose fns are ordinary JavaScript functions.

`import { compileModule } from 'typeshade'`

The signature, the description and the examples come from the compiler's own source at commit af54ddad5.

## Syntax

```ts
function compileModule(
  m: ModuleDecl,
  opts?: {
    gpuStubs?: boolean;
    precision?: CpuPrecision;
    consoleSink?: ConsoleSink;
    overrides?: Readonly<Record<string, CpuValue>>;
  },
): CpuModule
```

## Parameters

**`m` `ModuleDecl`**

the module to evaluate.

**`opts` optional `{ gpuStubs?: boolean; precision?: CpuPrecision; consoleSink?: ConsoleSink; overrides?: Readonly<Record<string, CpuValue>>; }`**

`precision` and `gpuStubs`, as above.

## Return value

`CpuModule`

the compiled module: `fns` by name, and `setBinding`.

## Exceptions

**`@link ValidationError`**

when the module fails a core rule, and `Error` when a GPU-only intrinsic is called with `gpuStubs` off, or when a raw statement is reached.

## Description

Compile a module for the CPU: a tree-walk interpreter over the same IR the GPU backends receive, returning a [`CpuModule`](https://typeshade.dev/api/cpumodule/) whose `fns` are ordinary JavaScript functions. It is the differential reference a parity test compares a GPU result against.

`opts.precision` picks what the arithmetic means. `'f64'`, the default, is the algebra oracle: every value is a JavaScript double, so the result is the mathematically intended one to 53 bits. It proves the IR picked the right operations in the right order, and it is blind by construction to `f32` rounding, which only appears once the target truncates. `'f32'` is a correctly-rounding `f32` machine over the same IR: every `f32`-typed operation rounds to `f32` afterwards, with infinities on overflow. Reach for `'f32'` when the question is what the target computes, so a parity gate can compare at ulp scale without a tolerance wide enough to hide a real error.

`opts.gpuStubs` decides what happens at an operation the CPU cannot perform. A texture read, a screen-space derivative and the rest of the GPU-only intrinsics have no CPU meaning, and by default a call to one throws, since a plausible wrong number is the worst failure mode for a reference. Turn it on and each stands in for its GPU value: an opaque black texture read, a zero derivative. Do that only where a placeholder is acceptable for the test at hand. A texture of the CPU tier’s memory (`device.texture()` on a device with no WebGPU device, change 0077) is the exception: bound with `setBinding`, it is read by `textureLoad` and `textureDimensions` and written by `textureStore`, with or without `gpuStubs`, as `compileModuleJs` does.

`setBinding(name, value)` supplies a uniform or storage binding by its declared name, since a CPU run has no bind groups. Call it before invoking an entry that reads that binding.

A raw statement has no CPU evaluation at all. Reaching one throws, whichever target its payload was written for, because raw text is opaque to the IR and the oracle has nothing to walk.

It runs [`validate`](https://typeshade.dev/api/validate/) and [`autoVars`](https://typeshade.dev/api/autovars/) first, the same passes the GPU writers run, so it rejects the same malformed modules they do.

## Examples

### Example

```ts
import { compileModule } from 'typeshade'

const cpu = compileModule(MODULE, { precision: 'f32' })
cpu.setBinding('u', { scale: 2, offset: [0.5, 0.5] })
const got = cpu.fns.transform([1, 1])
```

## Targets

| Target | Support | Notes |
| --- | --- | --- |
| WGSL (WebGPU) | Does not apply | |
| GLSL ES 3.00 (WebGL2) | Does not apply | |
| CPU oracle | Supported | Compiles the module to a JavaScript function that runs in `f64`. |

## In the guide

- [Language guide](https://typeshade.dev/guide/internals/)
- [The CPU oracle](https://typeshade.dev/guide/internals/the-cpu-oracle/)

## See also

- [`CpuModule`](https://typeshade.dev/api/cpumodule/)
- [`validate`](https://typeshade.dev/api/validate/)
- [`autoVars`](https://typeshade.dev/api/autovars/)
- [`compileModuleJs`](https://typeshade.dev/api/compilemodulejs/)
- [`CpuPrecision`](https://typeshade.dev/api/cpuprecision/)

## Source

[`src/core/oracle.ts`, line 782](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/oracle.ts#L782), at commit af54ddad5
