Skip to content

Instantly share code, notes, and snippets.

@ruvi-d
Last active May 9, 2022 23:02
Show Gist options
  • Save ruvi-d/c31b0615baabfcc5dbc51ecb52436878 to your computer and use it in GitHub Desktop.
Save ruvi-d/c31b0615baabfcc5dbc51ecb52436878 to your computer and use it in GitHub Desktop.
Getting Started with LLVM (WIP draft)

Getting Started with LLVM (WIP)

The guide will hopefully help you quickly graps the basics of LLVM. Links to useful self-learning article, video and tools will be listed below.

Basic overview

Learning - LLVM IR generation

Example code

Learning - Adding debugging support to a language

Tools

Official references

Other references

C/C++

LLVM

Misc compiler

LLVM on Windows

  • Pre-built binaries at https://github.com/llvm/llvm-project/releases for Windows only contain the clang and a few other binaries. Pre-built core libs are not available for Windows 1 2
  • As such we will have to build are own LLVM libs for nBAllerina
  • Using MSVC it is not possible to build dynamic libs of LLVM. Only Static libs are supported. LLVM anyway only recommends synamic libs for development purposes only.

Background info on building on Windows

Building LLVM on Windows

Prerequisites

  • Build Tools for Visual Studio 2019
    • Select C++ build tools with the following optional ones
      • C++ CMake tools for Windows
      • C++ ATL for latest v142 build tools (x86 & x64)
      • C++ Clang comiler & Clang-cl build tools
  • Python3 (latest)

Building LLVM

$ cmake -DCMAKE_BUILD_TYPE="Release" -Thost=x64 -G "Visual Studio 16 2019" -A x64 -DLLVM_USE_CRT_RELEASE=MT -DCMAKE_INSTALL_PREFIX="<path>" ../ 
$ msbuild INSTALL.vcxproj /p:configuration=release

Setting up a test app to generate LLVM IR

Bonus

Open Powershell with x64 dev tools

  • Create a system variable called dev64 with the value "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell ff0ceaa0 -DevCmdArguments '-arch=x64]'}"
    • Change the ff0ceaa0 value to match your environment
  • Create a shortcut with the following target : C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noe -c %dev64%

Using Clang on windows

https://docs.microsoft.com/en-us/cpp/build/clang-support-cmake?view=msvc-160

  • Install
    • C++ Clang Compiler for windows
    • C++ Clang-cl for v142 buil tools (x64/x86)
  • To use the Clang version that is part of the Visual Studio 2019 build tools with CMake
    • cmake -G "Visual Studio 16 2019" -A x64 -T ClangCL ..\
    • The -T ClangCL target is the key point
  • To use a custom installation of Clang/LLVM with CMake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment