---
title: "optBarrier(): IR builder function in typeshade/core/ir"
description: "optBarrier, an IR builder function in typeshade/core/ir. An optimization barrier on one f32 value, the shader equivalent of C's volatile."
url: https://typeshade.dev/api/optbarrier/
language: en
---

# `optBarrier()`

Function in Builtins

An optimization barrier on one `f32` value, the shader equivalent of C's volatile.

`import { optBarrier } from 'typeshade/core/ir'`

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

## Syntax

```ts
const optBarrier: (v: ReadonlyNode<"f32"> | number) => Node<"f32">
```

## Parameters

**`v` `ReadonlyNode<'f32'> | number`**

## Return value

`Node<'f32'>`

## Description

An optimization barrier on one `f32` value, the shader equivalent of C’s `volatile`. It is the value-level counterpart of `FuncDecl.opaque`, which protects a whole function and is therefore never inlined.

`optBarrier(x)` equals `x` bit for bit, on both targets and in the CPU evaluation. What it adds is that no optimizer may look through it: it is emitted as an `f32` to `u32` to `f32` round-trip ([`bitcastU32`](https://typeshade.dev/api/bitcastu32/) then [`bitcastF32`](https://typeshade.dev/api/bitcastf32/)), and the integer domain is not subject to float reassociation, distribution or contraction. Two instructions, both targets, no extension.

What it defeats: this library’s own constant folding and algebraic simplification, which match literal operands only, so a wrapped term stops matching every rewrite they have; and a driver’s float reassociation or contraction across the barrier.

What it does not fix: it is a compiler barrier and has no effect on the hardware. It cannot make a lossy `f32` multiply correctly rounded, and on Apple GPUs no float-domain barrier has been shown sufficient for an emulated-double multiply; the integer flavour that [`recommendFp64Flavor`](https://typeshade.dev/api/recommendfp64flavor/) selects exists for that case. Use it to pin one value an optimizer would otherwise be free to rewrite: a Kahan compensation term, a split constant, an error-free-transform residual. Wrapping every operation buys nothing.

## Examples

### Example

```ts
import { fn, Let, optBarrier, f32T } from 'typeshade/core/ir'

// Kahan compensation: `(sum + y) - sum - y` is algebraically zero, which is exactly
// what a reassociating compiler is licensed to delete.
const compensation = fn('kahan_c', { sum: f32T, y: f32T }, ({ sum, y }) => {
  const t = Let(sum.add(y))
  return optBarrier(t.sub(sum)).sub(y)
})
```

## See also

- [`bitcastU32`](https://typeshade.dev/api/bitcastu32/)
- [`bitcastF32`](https://typeshade.dev/api/bitcastf32/)
- [`recommendFp64Flavor`](https://typeshade.dev/api/recommendfp64flavor/)

## Source

[`src/core/ir/node.ts`, line 1724](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/ir/node.ts#L1724), at commit af54ddad5
