# Getting Started with LLVM

Covers turning on LLVM compilation for a project or for a single procedure, the options that control it, and its current limitations.

> [!NOTE] since BETA 984
> Everything on this page is in twinBASIC since this build. Earlier builds have no LLVM settings in Project Settings or IDE Options.

## What is LLVM?

LLVM is an optional part of the compiler that produces code highly optimized for performance and size. Most applications benefit from it; how much depends on the code. LLVM is built into twinBASIC: it does not need a separate LLVM installation, and twinBASIC ignores one if it is present.

During compilation, LLVM translates your code into an *intermediate representation* --- a form between the twinBASIC source and the final machine code. It then reorganizes that representation with a wide variety of techniques, such as removing code that can never run (*dead code elimination*), simplifying loops, rewriting arithmetic to avoid slow operations like division, and inlining functions. Where it applies, LLVM can also make your code use CPU features designed to speed up particular operations.

## LLVM in twinBASIC

You turn LLVM on in [Project Settings](/tB/IDE/Project-Settings.md). It is off by default while it is experimental. Project Settings has these options for it:

![The Project Settings dialog with its Optimization group chosen in the rail, scrolled to Compiler Options (BUILD): a single column of unticked boxes, Enable LLVM Compilation, LLVM: Generate optimized code, LLVM: Optimize for smaller filesize, and one LLVM: Target CPUs with box for each of AES, AVX, AVX2, BMI2, FMA, FXSR, LZCNT, POPCNT, RDSEED, SHA, SSE, SSE2, SSE3, SSE4.1, SSE4.2, SSSE3, XSAVE, XSAVEC, XSAVEOPT and XSAVES. Its description says that software built for a CPU feature will not run on a CPU without it. Compiler Options (DEBUG) follows, with the same boxes, cut off after SSE2, and a bold note in its description saying that LLVM compilation in the IDE debug environment is not recommended, because LLVM-compiled code cannot be debugged.](/LLVM/Images/llvmdoc1.png){:width="1202" height="838"}

The same options appear in two settings of the dialog's **Optimization** group:

- **Compiler Options (BUILD)** applies when you build the project into an EXE, a DLL or another output file.
- **Compiler Options (DEBUG)** applies when you run the project from the IDE.

> [!NOTE]
> The IDE cannot debug code compiled with LLVM, so turning LLVM on under **Compiler Options (DEBUG)** is not recommended.

**Enable LLVM Compilation** turns LLVM on. With only this box ticked, LLVM runs during compilation and creates the intermediate representation of your code, but does not apply the optimizations for performance and size. You will usually want to tick one or both of the next two options as well: **LLVM: Generate optimized code** and **LLVM: Optimize for smaller filesize**. *Generate optimized code* is for optimizing for speed. This is not mutually exclusive with *Optimize for smaller filesize*, and when both are enabled, LLVM determines if an optimization doesn't provide a significant enough benefit to be worth the size increase.

The remaining options, from **LLVM: Target CPUs with AES** to **LLVM: Target CPUs with XSAVES**, are for CPU features that not every CPU has. They range from features that almost every CPU from the last 25 years has, to features that only recent CPUs have. [CPU feature availability](#cpu-feature-availability) below gives an overview.

> [!IMPORTANT]
> If you tick a CPU feature that the CPU running your program does not have, the program crashes or does not run at all.

When LLVM is on and the compiler reports its progress, the IDE shows a **Compiling...** box with the completion percentage; see [LLVM in the IDE](/tB/IDE/Building/LLVM-in-the-IDE.md#the-progress-box). For large applications, and for applications with very large individual procedures, compiling with LLVM can take much longer than the standard compiler. A very large application can take several minutes or more to compile, depending on your hardware.

twinBASIC keeps a cache of compiled code, so later builds are usually much faster: code that has not changed is not compiled again. To clear the cache and compile everything on the next build, choose **Flush the LLVM compiler cache** on the [**Tools** menu](/tB/IDE/Reference/Menu/Tools.md).

## Availability

LLVM compilation is not available with the free Community Edition licence, and twinBASIC ignores the LLVM settings there. With a Personal Edition licence, LLVM compiles only the built-in packages. All other paid editions can use LLVM in full. The [FAQ](/FAQ.md#cost) describes the editions.

## Current limitations

### Operating system support

Building with LLVM does not currently work on Windows 7; Windows 10 or 11 is recommended. Windows 8 and 8.1 have not been tested yet. The limit applies only to the computer that builds the program: a program built with LLVM runs on Windows 7.

### Language support

The main feature not yet supported is passing an error up to the calling procedure. A fix is planned.

> [!WARNING] BETA 1045
> An error that occurs in a procedure with no error handler is not passed to its caller when either of the two procedures is compiled with LLVM: the caller's own `On Error` statement does not catch it. A run in the IDE ends without a message. A built program stops as it does for an unhandled error when only the caller is compiled with LLVM. When the procedure that has no handler is compiled with LLVM, the program shows a box that names the procedure but no error, and then ends with an access violation. Without LLVM, and in VB6, the caller's handler catches the error.

> [!NOTE] BETA 1045
> An error handler in a procedure compiled with LLVM cannot read an error raised with [**Err.Raise**](/tB/Modules/ErrObject/Raise.md) in that procedure: **Err.Number** returns -353703423 (`&HEAEAEA01`), **Err.Source** is empty, and **Err.Description** is the generic "Application-defined or object-defined error". An error that a statement causes, such as a division by zero, is read correctly.

> [!WARNING] BETA 1045
> An exponentiation (`^`) that overflows raises no error in code compiled with LLVM, and its result is 0, where the same code compiled without LLVM raises error 6, *Overflow*, as VB6 does. In a 32-bit build, a **Double** addition, subtraction or multiplication that overflows raises no error either, and its result is infinity (`1.#INF` or `-1.#INF`), unless the procedure is compiled for SSE2: with `+sse2`, or with the flag of an instruction set that includes it, `+sse3`, `+ssse3`, `+sse4.1`, `+sse4.2`, `+avx`, `+avx2`, `+fma`, `+aes` or `+sha`. [**FloatingPointErrorChecks(True)**](/tB/Core/Attributes.md#floatingpointerrorchecks) does not change this. A division that overflows and a division by zero raise their errors, and so do an addition, subtraction and multiplication in a 64-bit build or compiled for SSE2. After any of these errors, under `On Error Resume Next`, the variable that was to receive the result keeps its old value; without LLVM it holds infinity.

> [!WARNING] BETA 1045
> A program built with LLVM ends with an access violation (0xC0000005) and no message box at the first [**GoSub**](/tB/Core/GoSub-Return.md) it runs, in a 32-bit and in a 64-bit build. A run in the IDE is not affected, and neither is a program built without LLVM. Replace the **GoSub** with a call to a **Private Sub**.

> [!WARNING] BETA 1045
> In a 64-bit build with LLVM, a call to a [**Declare**](/tB/Core/Declare.md)d procedure that passes `ByVal 0&` for its eighth parameter, declared `ByRef` with a 32-bit type such as **Long**, to give the procedure a NULL pointer, can end with an access violation (0xC0000005) and no message box, in the procedure. The eighth parameter of **WideCharToMultiByte** is one such case. Whether the call fails depends on what the stack held before it. The same override for the first to seventh parameters is right. Declare the parameter `ByVal ... As LongPtr` instead. It affects a built exe and a run in the IDE with LLVM turned on, and not a 32-bit build or a build without LLVM.

> [!WARNING] BETA 1045
> In a procedure that has a `ByVal` **Long** or **Single** parameter and, after it, a `ByRef` parameter declared `As Boolean` or `As Integer`, a [**For...Next**](/tB/Core/For-Next.md) loop whose **Step** is that `ByVal` parameter (or a variable set from it) runs with a wrong step, or with a wrong start or end when those are parameters too. The wrong value depends on where the caller's argument for the `ByRef` parameter is stored. In a built exe it is 20 in a 64-bit build and 25 in a 32-bit build when the argument is a local variable of the caller, and other values when it is a module-level variable or an array element; in a run in the IDE it varies more, up to millions. `For i = 1 To 7 Step x` with `x` equal to 3 and a local variable passed runs once, not three times. No error is raised, so the loop silently gives a wrong result. It affects a **Sub**, a **Function** and a class method, a built exe and a run in the IDE with LLVM turned on, in a 32-bit and in a 64-bit build, and not a build without LLVM. Whether a larger procedure shows it can depend on its other statements. A second parameter that is `ByRef ... As Long` (or **Byte**, **Single**, **Double**, **Currency**, **Date**, **String**, **Variant**, an Enum, a UDT, an array, an object or a pointer), `ByVal ... As Boolean`, a `ByRef ... As Boolean` placed before the step parameter, a step parameter of type **Integer**, **Double** or **Currency**, a `Do` loop, a loop with no **Step** and a loop with a literal **Step** are right. Declare the second parameter `ByVal`, or place it before the step parameter.

Code compiled with LLVM ignores [**Debug.Assert**](/tB/Modules/Debug.md#assert): the statement does not stop, and its condition is not evaluated.

All other language features should work, in both 32-bit and 64-bit builds. Please [report](/FAQ.md#bug-reporting) any crash that LLVM causes, and any message saying "a feature used in your code is not yet supported with the LLVM compiler".

## General LLVM options

Besides the project settings, three settings in [IDE Options](/tB/IDE/Customising/IDE-Options.md) control how LLVM works when it is on. Changing any of the three ends the LLVM cache process and restarts the compiler at once, with no question asked.

![Part of the twinBASIC IDE Options dialog with its Compiler group chosen in the rail, showing three LLVM settings: LLVM - Maximum number of threads, set to its default of 1; LLVM - Complex procedure reporting threshold (milliseconds), set to 10000; and LLVM - Keep cache process alive after exiting the IDE, set to Yes.](/LLVM/Images/llvmdoc2.png){:width="1002" height="375"}

**LLVM - Maximum number of threads**
: The number of threads the LLVM compiler can create. More threads can make LLVM compilation faster, but use more memory. If LLVM compilation starts to crash, this value is probably too high, and lowering it should stop the crashes. The default is 1, but newer computers can probably handle 10 or more.

**LLVM - Complex procedure reporting threshold (milliseconds)**
: A compile time. Every procedure that takes at least this long to compile is listed in the [Debug Console](/tB/IDE/Debugging/Debug-Console.md), which shows the procedures you may want to split up to reduce compile time. The default is 10000 milliseconds, which is 10 seconds.

**LLVM - Keep cache process alive after exiting the IDE**
: Keeps the LLVM cache running after you exit the IDE, so that LLVM does not have to do a long full compile again. It is on by default.

## Per-procedure LLVM options

To reduce compile time, or to work around a procedure that LLVM reports problems with, use the [**CompilerOptions**](/tB/Core/Attributes.md#compileroptions) attribute with an empty string. It turns LLVM off for that procedure and keeps it on for the rest of your code:

```tb check_build
[CompilerOptions("")]
Public Sub DoNotOptimizeMe()
    ' ...
End Sub
```

> [!WARNING] BETA 1045
> A run in the IDE with LLVM turned on for the whole project under **Compiler Options (DEBUG)** ends with an access violation when a procedure marked `[CompilerOptions("")]` calls a procedure that is compiled with LLVM, and `Sub Main` reaches the marked procedure: it calls it, or calls a procedure that does. A marked procedure that `Sub Main` does not reach is not affected, and neither is a built program.

The same attribute can also turn LLVM on for chosen procedures when it is off in Project Settings. List the flags, separated by commas: `+llvm`, `+optimize` and `+optimizesize`, then `+` and the name of each CPU instruction set to use. Each flag is the direct equivalent of an option in Project Settings:

`+llvm`
: **Enable LLVM Compilation**

`+optimize`
: **LLVM: Generate optimized code**

`+optimizesize`
: **LLVM: Optimize for smaller filesize**

The flags for CPU instruction sets, one for each **LLVM: Target CPUs with** box, are `+aes`, `+avx`, `+avx2`, `+bmi2`, `+fma`, `+fxsr`, `+lzcnt`, `+popcnt`, `+rdseed`, `+sha`, `+sse`, `+sse2`, `+sse3`, `+sse4.1`, `+sse4.2`, `+ssse3`, `+xsave`, `+xsavec`, `+xsaveopt` and `+xsaves`.

The attribute replaces the project's options for that procedure, and the other flags do not turn LLVM on by themselves: `[CompilerOptions("+optimize")]` compiles the procedure without LLVM, whatever the project settings say. Flags may be separated by commas, by spaces or by commas and spaces.

For example:

```tb check_build
[CompilerOptions("+llvm,+optimize,+optimizesize")]
Function Multiply(A As Long, B As Long) As Long
    Return A * B
End Function

[CompilerOptions("+llvm,+optimize,+optimizesize,+aes,+avx,+avx2,+bmi2,+fma,+fxsr,+lzcnt,+popcnt,+rdseed,+sha,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+xsave,+xsavec,+xsaveopt,+xsaves")]
Sub FullyOptimizeMe()
    ' ...
End Sub
```

## Unrolling a loop

Inside a procedure compiled with LLVM, [**UnrollLoop**](/tB/Modules/Compilation/UnrollLoop.md) asks LLVM to unroll one loop: to repeat its body several times in each iteration, so the loop tests its condition and jumps back less often. It must be the first statement in the loop's body, and may give the greatest number of repeats as a literal or a constant. It does not change what the loop does.

```tb check_build
Function Total(Values() As Long) As Long
    Dim i As Long
    For i = LBound(Values) To UBound(Values)
        UnrollLoop 8
        Total += Values(i)
    Next
End Function
```

## CPU feature availability

For each CPU feature, the table gives the first Intel and AMD CPUs to offer it, and the year from which all new Intel and AMD CPUs shipped with it. The last column is an estimate: some rare models, such as specialized CPUs for embedded use, may still have lacked the feature.

| Feature  | First available (Intel)   | First available (AMD)   | In all new CPUs from |
|----------|---------------------------|-------------------------|----------------------|
| AES      | 2010 (Westmere)           | 2011 (Bulldozer)        | 2016                 |
| AVX      | 2011 (Sandy Bridge)       | 2011 (Bulldozer)        | 2022                 |
| AVX2     | 2013 (Haswell)            | 2015 (Excavator)        | 2022                 |
| BMI2     | 2013 (Haswell)            | 2015 (Excavator)        | 2022                 |
| FMA      | 2013 (Haswell)            | 2012 (Piledriver)       | 2022                 |
| FXSR     | 1997 (Pentium II)         | 1999 (Athlon)           | 2011; all x64 CPUs   |
| LZCNT    | 2013 (Haswell)            | 2007 (K10/Phenom)       | 2022                 |
| POPCNT   | 2008 (Nehalem)            | 2007 (K10/Phenom)       | 2013                 |
| RDSEED   | 2014 (Broadwell)          | 2017 (Zen)              | 2020                 |
| SHA      | 2016 (Atom)               | 2017 (Zen)              | 2022                 |
| SSE      | 1999 (Pentium III)        | 2001 (Athlon XP)        | 2002; all x64 CPUs   |
| SSE2     | 2000 (Pentium 4)          | 2003 (Athlon 64)        | 2005; all x64 CPUs   |
| SSE3     | 2004 (Pentium 4 Prescott) | 2005 (Athlon 64 Rev. E) | 2006                 |
| SSE4.1   | 2007 (Penryn)             | 2011 (Bulldozer)        | 2013                 |
| SSE4.2   | 2008 (Nehalem)            | 2011 (Bulldozer)        | 2013                 |
| SSSE3    | 2006 (Core 2)             | 2011 (Bobcat)           | 2013                 |
| XSAVE    | 2009 (Penryn E0/R0)       | 2011 (Bulldozer)        | 2016                 |
| XSAVEC   | 2015 (Skylake)            | 2017 (Zen)              | 2020                 |
| XSAVEOPT | 2011 (Sandy Bridge)       | 2017 (Zen)              | 2017                 |
| XSAVES   | 2015 (Skylake)            | 2017 (Zen)              | 2020                 |

FXSR, SSE and SSE2 are part of the base feature set that every x64 CPU must support, so 64-bit builds always use them when optimization is on.

## Future of LLVM support

This is the first release of LLVM support in twinBASIC, and the groundwork for making much more use of the optimizations and features that LLVM provides. Later releases will restructure parts of the twinBASIC compiler to get the most from LLVM and to improve the performance of your code.
