---
title: "cse(): function in Emit, TypeShade API reference"
description: "cse, a function in Emit. Common-subexpression elimination. The WGSL and GLSL ES 3.00 writers, module fragments and the passes that run before them."
url: https://typeshade.dev/api/cse/
language: en
---

# `cse()`

Function in Emit

Common-subexpression elimination.

`import { cse } from 'typeshade'`

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

## Syntax

```ts
function cse(m: ModuleDecl): ModuleDecl
```

## Parameters

**`m` `ModuleDecl`**

The module to optimize.

## Return value

`ModuleDecl`

A new module with the rewritten functions; `m` is not modified.

## Description

For every function in `m`, finds each compound expression that appears two or more times and reads only the function’s inputs (parameters, constants and bindings; never a local variable or a name the function writes to), binds it once to a `let`, and replaces every occurrence with a reference to that `let`. The `let` is placed at the top of the innermost block that contains all of the occurrences, just before the first statement that uses it, so a value used only inside one branch is computed only when that branch runs. Occurrences in mutually exclusive arms of one `if` or `switch` that some branch does not use are shared only within each arm. A `let` is never placed inside a loop body. Only the outermost repeated expression is hoisted: two occurrences of `sqrt(dot(p, p))` become one temporary holding the whole value. A function whose body contains raw shader text is returned unchanged.

## Source

[`src/core/passes/opt/cse.ts`, line 327](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/passes/opt/cse.ts#L327), at commit af54ddad5
