# Ahead of Time Compilation

Ahead of Time (AOT) Compilation is a helpful feature for your development lifecycle or distribution time. It benefits you when you know beforehand what your target device is going to be at application execution time. The AOT feature provides the following benefits:

- No additional compilation time is done when running your application.
- No just-in-time (JIT) bugs encountered due to compilation for the target device, because this step is skipped with AOT compilation.
- Your final code, executing on the target device, can be tested as-is before you deliver it to end-users.

### Note

The program built with AOT compilation for a specific target device will not run on a non-specific device. You must detect the proper target device at runtime and report an error if the targeted device is not present. The use of exception handling with an asynchronous exception handler is recommended.

Data Parallel C++ (DPC++) supports AOT compilation for the following targets: Intel® CPUs, Intel® Processor Graphics (Gen9 or above), and Intel® FPGA.

## Prerequisites

To target a GPU with the AOT feature, you must have the OpenCL™ Offline Compiler (OCLOC) tool installed. Refer to the Intel® oneAPI Toolkit Installation Guide's section: Install OCLOC to install the tool on your operating system.

## How to Use AOT for the Target Device (Intel® CPUs)

The supported options are:

- `-fsycl-targets=spir64_x86_64-unknown-unknown-sycldevice`
- `-Xs "-march=<arch>"`, where `<arch>` is one of the following:

  Switch | Display Name
  --- | ---
  `avx` | Intel® Advanced Vector Extensions (Intel® AVX)
  `avx2` | Intel® Advanced Vector Extensions 2 (Intel® AVX2)
  `avx512` | Intel® Advanced Vector Extensions 512 (Intel® AVX-512)
  `sse4.2` | Intel® Streaming SIMD Extensions 4.2 (Intel® SSE4.2)

Examples:

- Linux*: `dpcpp -fsycl-targets=spir64_x86_64-unknown-unknown-sycldevice -Xs "-march=avx2" main.cpp`
- Windows*: `dpcpp-cl /EHsc -fsycl-targets=spir64_x86_64-unknown-unknown-sycldevice -Xs "-march=avx2" test_cpu.cpp`

Building an Application with Multiple Source Files for CPU Targeting

Method 1: Compile your normal files (with no DPC++ kernels) to create host objects. Then compile the file with the kernel code and link it with the rest of the application.

- Linux:
  1. `dpcpp -c main.cpp`
  2. `dpcpp -fsycl-targets=spir64_x86_64-unknown-unknown-sycldevice -Xs "-march=avx2" mandel.cpp main.o`
- Windows:
  1. `dpcpp-cl -c /EHsc main.cpp`
  2. `dpcpp-cl /EHsc -fsycl-targets=spir64_x86_64-unknown-unknown-sycldevice -Xs "-march=avx2" mandel.cpp -link main.obj`

Method 2: Compile the file with the kernel code and create a fat object. Then compile the rest of the files and linking to create a fat executable:

- Linux:
  1. `dpcpp -c -fsycl-targets=spir64_x86_64-unknown-unknown-sycldevice -Xs "-march=avx2" mandel.cpp`
  2. `dpcpp main.cpp mandel.o`
- Windows:
  1. `dpcpp-cl -c /EHsc -fsycl-targets=spir64_x86_64-unknown-unknown-sycldevice -Xs "-march=avx2" mandel.cpp`
  2. `dpcpp-cl /EHsc main.cpp mandel.obj`

### Note

Currently, Method 2 only works on a HOST selector.

## How to Use AOT for Intel® Integrated Graphics (Intel® GPU)

The supported options are:

- `-fsycl-targets=spir64_gen-unknown-unknown-sycldevice`
- `-Xs "-device <arch>"` option, where `<arch>` is one of the following:

  Switch | Display Name
  --- | ---
  `skl` | 6th generation Intel® Core™ Processor (Skylake with Intel® Processor Graphics Gen9)
  `kbl` | 7th generation Intel® Core™ Processor (Kaby Lake with Intel® Processor Graphics Gen9)
  `cfl` | 8th generation Intel® Core™ Processor (Coffee Lake with Intel® Processor Graphics Gen9)
  `glk` | Gemini Lake with Intel® Processor Graphics Gen9
  `icllp` | 10th generation Intel® Core™ Processor (Ice Lake with Intel® Processor Graphics Gen11)
  `tgllp` | 11th generation Intel® Core™ Processor (Tiger Lake with Intel® Processor Graphics Gen12)
  `dg1` | Intel® Iris® Xe MAX Graphics Family
  `Gen9` | Intel® Processor Graphics Gen9
  `Gen11` | Intel® Processor Graphics Gen11
  `Gen12LP` | Intel® Processor Graphics Gen12 (Lower Power)

To see all the device types, use the following command:

    ocloc compile --help

If multiple target devices are listed, the Intel® oneAPI DPC++/C++ Compiler compiles for each of these targets and creates a fat-binary that contains all the device binaries produced this way.

Examples of supported `-device` patterns:

- Linux:
  - To compile for a single target, using `skl` as an example, use:

        dpcpp -fsycl-targets=spir64_gen-unknown-unknown-sycldevice -Xs "-device skl" vector-add.cpp

  - To compile for two targets, using `skl` and `icllp` as examples, use:

        dpcpp -fsycl-targets=spir64_gen-unknown-unknown-sycldevice -Xs "-device skl,icllp" vector-add.cpp

  - To compile for all the targets known to OCLOC, use:

        dpcpp -fsycl-targets=spir64_gen-unknown-unknown-sycldevice -Xs "-device *" vector-add.cpp

- Windows:
  - To compile for a single target, using `skl` as an example, use:

        dpcpp-cl /EHsc -fsycl-targets=spir64_gen-unknown-unknown-sycldevice -Xs "-device skl" vector-add.cpp

  - To compile for two targets, using `skl` and `icllp` as examples, use:

        dpcpp-cl /EHsc -fsycl-targets=spir64_gen-unknown-unknown-sycldevice -Xs "-device skl,icllp" vector-add.cpp

  - To compile for all the targets known to OCLOC, use:

        dpcpp-cl /EHsc -fsycl-targets=spir64_gen-unknown-unknown-sycldevice -Xs "-device *" vector-add.cpp

## Using AOT in Microsoft Visual Studio*

You can use Microsoft Visual Studio for compiling and linking. Set the flags below to use AOT compilation for CPU or GPU.

For CPU:

- To compile, in the dialog box, select: Configuration Properties > DPC++ > General > Specify SYCL offloading targets for AOT compilation
- To link, in the dialog box, select: Configuration Properties > Linker > General > Specify CPU Target Device for AOT compilation

For GPU:

- To compile, in the dialog box, select: Configuration Properties > DPC++ > General > Specify SYCL offloading targets for AOT compilation
- To link, in the dialog box, select: Configuration Properties > Linker > General > Specify GPU Target Device for AOT compilation

Parent topic: Compilation