# Compiling with slang

Description: How to compile and test your contracts with the slang Solidity compiler in Hardhat 3

Note: This document was authored using MDX

  Source: https://github.com/NomicFoundation/hardhat-website/tree/main/src/content/docs/docs/cookbook/compiling-with-slang.mdx

  Components used in this page:
    - <Run cmd="..."/>: Runs a command in the terminal with npm/pnpm/yarn.
    - <Install pkg="..."/>: Installs a package in the terminal with npm/pnpm/yarn.
    - <Steps>: Wraps an ordered list to render as numbered steps. No props.
    - :::note: An informational callout block. Supports custom title `:::note[Title]` and icon `:::note{icon="name"}` syntax.
    - :::tip: A helpful tip callout block. Supports custom title `:::tip[Title]` and icon `:::tip{icon="name"}` syntax.
    - collapse={X-Y}: Collapses line ranges in code blocks. Supports multiple ranges: `collapse={1-5, 12-14}`.

import { Steps } from "@astrojs/starlight/components";
import Install from "@hh/Install.astro";
import Run from "@hh/Run.astro";

This guide shows you how to use the `hardhat-slang` plugin to compile your contracts faster while you develop.

## What is `slang`?

`slang` is a Solidity compiler based on `LLVM`. It compiles faster, produces better optimized bytecode, and removes "stack too deep" errors. See the performance numbers in our [announcement blog post](https://blog.nomic.foundation/announcing-slang-alpha-an-independent-solidity-compiler-that-is-up-to-79x-faster-than-solc/).

Use it to speed up your development workflow and CI builds. Avoid using it to deploy to production networks, as `slang` is still under development. By default, the plugin will not allow usage in production Build Profiles, unless config settings are explicitly overridden.

The easiest way to use `slang` is with the `hardhat-slang` plugin. Read the rest of this guide to learn how to set it up.

:::note
The `slang` compiler's backend, `slang-solx`, was originally developed at Matter Labs under the name `solx`. In 2025, the project and its compiler team moved to Nomic Foundation, where it was renamed `slang-solx` and kept evolving into its current shape.
:::

## Setting up `hardhat-slang`

### Installing the plugin

First, install the plugin and add it to your Hardhat config:

<Steps>

1. Install the plugin:

   <Install packages="@nomicfoundation/hardhat-slang" />

2. Add it to the list of plugins in your config:

   ```ts ins={3,8}
   // hardhat.config.ts
   import { defineConfig } from "hardhat/config";
   import hardhatSlang from "@nomicfoundation/hardhat-slang";

   export default defineConfig({
     plugins: [
       // ...other plugins...
       hardhatSlang,
     ],
     // ...other config fields...
   });
   ```

</Steps>

The plugin extends the values that your Solidity config accepts, adding a new compiler `type` named `slang`. Hardhat won't use this new type automatically, so you'll need to modify your config to use it.

### Using the `slang` compiler type in your config

The plugin is configured with a `slang` Build Profile:

```ts ins={14-17}
// hardhat.config.ts
import { defineConfig } from "hardhat/config";
import hardhatSlang from "@nomicfoundation/hardhat-slang";

export default defineConfig({
  plugins: [
    // ...other plugins...
    hardhatSlang,
  ],
  // ...other config fields...
  solidity: {
    profiles: {
      // ...existing solidity config...
      slang: {
        type: "slang",
        version: "0.8.37",
      },
    },
  },
});
```

:::tip
If you don't have a `profiles` field in your `solidity` config, please read [this guide](/docs/guides/writing-contracts/build-profiles) before continuing.
:::

With the `type: "slang"` property set, Hardhat will use `slang` to build your contracts and tests whenever you use this Build Profile. Any compiler without a `type` keeps using `solc`.

The `version` of a compiler with `type: "slang"` is the Solidity version. It needs to be set to one of the [supported Solidity versions](#supported-solidity-versions), like `0.8.37`, or Hardhat will report a config error.

### Splitting compilation between `slang` and `solc` for older unsupported versions

`slang` supports all Solidity versions `0.8.0` - `0.8.37`. See the [full list of versions](#supported-solidity-versions) to check which versions work with `slang`.

For codebases that still rely on Solidity versions prior to `0.8.0`, you can mix `solc` and `slang` in the same Build Profile. To learn how, read [Using `solc` and `slang` in the same Build Profile](#using-solc-and-slang-in-the-same-build-profile).

## Building and testing with `slang`

With that in place, use `slang` by passing the `--build-profile slang` flag to your build command:

<Run command="hardhat build --build-profile slang" />

Hardhat prints the compiler it uses for each set of contracts, so you can check there that `slang` picked them up.

You can also use it to run your tests:

<Run command="hardhat test --build-profile slang" />

## Advanced topics

### Using `solc` and `slang` in the same Build Profile

If your project uses more than one Solidity version, you can list several compilers in the `slang` Build Profile. Those without the `type` field will use `solc`:

```ts collapse={2-10} ins={17}
// hardhat.config.ts
import { defineConfig } from "hardhat/config";
import hardhatSlang from "@nomicfoundation/hardhat-slang";

export default defineConfig({
  plugins: [
    // ...other plugins...
    hardhatSlang,
  ],
  // ...other config fields...
  solidity: {
    profiles: {
      // ...existing solidity config...
      slang: {
        compilers: [
          { type: "slang", version: "0.8.37" },
          { version: "0.7.6" }, // using `solc` for this unsupported version
        ],
      },
    },
  },
});
```

When building with this Build Profile, Hardhat will choose `slang` for the contracts that are compatible with `0.8.37`, and `solc` for the ones that are only compatible with `0.7.6`.

### Configuring the optimizer

`slang` always applies optimizations through `LLVM`. There is no unoptimized mode.

To set the level of `LLVM`-based optimizations, use the `settings.optimizer.mode` setting of your compiler config:

```ts collapse={2-10} ins={17-21}
// hardhat.config.ts
import { defineConfig } from "hardhat/config";
import hardhatSlang from "@nomicfoundation/hardhat-slang";

export default defineConfig({
  plugins: [
    // ...other plugins...
    hardhatSlang,
  ],
  // ...other config fields...
  solidity: {
    profiles: {
      // ...existing solidity config...
      slang: {
        type: "slang",
        version: "0.8.37",
        settings: {
          optimizer: {
            mode: "z", // optimize aggressively for size
          },
        },
      },
    },
  },
});
```

These are the available modes:

| Mode  | What it does                                          |
| ----- | ----------------------------------------------------- |
| `"1"` | Least optimization, fastest to compile (the default). |
| `"2"` | More runtime optimization.                            |
| `"3"` | Best runtime performance.                             |
| `"s"` | Smaller bytecode.                                     |
| `"z"` | Smallest bytecode.                                    |

### Using `slang` in other Build Profiles

By default, `type: "slang"` is only allowed in the `slang` profile. Using it anywhere else, including in `default` or `production`, is a validation error. That's deliberate, because `slang` is still under development and isn't ready to build contracts for production deployments.

If you understand the risk and still want `slang` in another Build Profile, set `dangerouslyAllowSlangInProduction` in the plugin's own `slang` config section, next to `solidity`:

```ts collapse={2-10} ins={14,23-25}
// hardhat.config.ts
import { defineConfig } from "hardhat/config";
import hardhatSlang from "@nomicfoundation/hardhat-slang";

export default defineConfig({
  plugins: [
    // ...other plugins...
    hardhatSlang,
  ],
  // ...other config fields...
  solidity: {
    profiles: {
      default: {
        type: "slang", // allowed only because of the option below
        version: "0.8.37",
      },
      slang: {
        type: "slang",
        version: "0.8.37",
      },
    },
  },
  slang: {
    dangerouslyAllowSlangInProduction: true,
  },
});
```

### Pinning a `slang` release

One `slang` binary compiles a range of Solidity versions (`0.8.x`). The plugin downloads a single release, the one pinned in `slang.version`, and uses it for every `type: "slang"` compiler entry. By default, that's the latest release known to your version of the `hardhat-slang` plugin. Upgrading the plugin can therefore upgrade `slang`, and your next build compiles your contracts with the new release.

To stay on a specific `slang` release, set the `version` in the `slang` config section:

```ts collapse={2-10} ins={11-13}
// hardhat.config.ts
import { defineConfig } from "hardhat/config";
import hardhatSlang from "@nomicfoundation/hardhat-slang";

export default defineConfig({
  plugins: [
    // ...other plugins...
    hardhatSlang,
  ],
  // ...other config fields...
  slang: {
    version: "0.1.0-pre.2026-10-14",
  },
});
```

The supported `slang` releases are listed in the **Supported Solidity versions** section of the plugin's [README](https://www.npmjs.com/package/@nomicfoundation/hardhat-slang).

### Supported Solidity versions

`slang` supports Solidity versions from `0.8.0` to `0.8.37`.

### Supported EVM versions

`slang` supports these EVM versions as compilation targets: `cancun`, `prague`, and `osaka`.
