HabanaAI / tpc_llvm

TPC-CLANG compiler that compiles a TPC C programming language which is used in HabanaLabs Deep-Learning Accelerators
22 stars 4 forks source link

TPC-CLANG compiler

TPC-CLANG compiler is based on the widely used LLVM open-source compiler infrastructure project. The tpc-clang compiler compiles a TPC C programming language (also referred to as TPC C). TPC-C is based on the ISO/IEC 9899:1999 C language specification with TPC-specific extensions and restrictions.

Table of Contents

Clone tpc-llvm Repository

Create a working directory, and clone the repository. For example the folder '~/habanaTools'.

cd ~/habanaTools
git clone https://github.com/HabanaAI/tpc_llvm.git tpc-llvm

From here on, we assume the root of the source tree is '~/habanaTools/tpc-llvm'.

Building the Compiler

Requirements

  1. cmake must be installed on the build machine.
  2. cmake version 3.4.3 or higher is required.
  3. [gcc 5.1 | clang 6.0] or higher
  4. Python 3.6.9 or higher

Create a build directory, for examples '~/habanaTools/build' (the folder can be located anywhere), and enter the folder:

cd ~/habanaTools
mkdir build
cd build

Build LLVM from source

cmake -G "Unix Makefiles"  \
      -DLLVM_ENABLE_PROJECTS=clang \
      -DLLVM_TARGETS_TO_BUILD="TPC" \
      -DLLVM_BUILD_EXAMPLES=OFF \
      -DLLVM_INCLUDE_EXAMPLES=OFF \
      -DCLANG_ENABLE_ARCMT=OFF \
      -DCLANG_BUILD_EXAMPLES=OFF \
      ../tpc-llvm/llvm

Run the build:

make clang llc opt llvm-objdump

The build must finish successfully.

Using a Build Script

From build directory run:

../tpc-llvm/buildTPC_CLANG.sh
usage: buildTPC_CLANG.sh [options]

options:

  -r,  --release              Build only release build
  -j,  --jobs <val>           Overwrite number of jobs
  -h,  --help                 Prints this help

Working with the Compiler

The build must create compiler executable 'tpc-clang' in the directory ~/habanaTools/build/bin'. It is used as usually, for instance, the next invocations may be used to compile source file to an object, ASM or llvm IR respectively:

~/habanaTools/build/bin/tpc-clang ~/habanaTools/tpc-llvm/codeExample/leakyrelu_f32.c -c
~/habanaTools/build/bin/tpc-clang ~/habanaTools/tpc-llvm/codeExample/leakyrelu_f32.c -S
~/habanaTools/build/bin/tpc-clang ~/habanaTools/tpc-llvm/codeExample/leakyrelu_f32.c -S -emit-llvm

Specific Compiler Options

Pragmas

Macros

Typical check for compiler version is like this:

#if __TPC_DROP_VERSION >= VERSION2DEC(16, 0, 0)
    //use some recent feature
#else
    //fall back to legacy way
#endif

Intrinsics

See tpc-intrinsics.h and tpc-defs.h header files (NB: these headers are documentation only, not intended for real inclusion into source code).

Disassembler

Typical Invocation:

llvm-objdump --triple tpc -d -no-show-raw-insn -no-leading-addr -mcpu=gaudi mytest.o

Other TPC-Specific Options:

How to Disassemble Binary: Wrap Binary to Elf

1) Create an empty ELF container:

echo "" |tpc-clang -o empty.o -c -xassembler -

2) Embed the binary to ELF as .text section (and strip irrelevant sections away):

objcopy --update-section .text=path/to/file.bin -j .text empty.o result.o

3) Disassemble the result ELF into a file name 'result.s':

llvm-objdump --triple tpc -mcpu=gaudi -d -no-show-raw-insn -no-leading-addr -no-common-header result.o > result.s

Assembler

Typically compiler handles assembler source files (.tpcasm and .s) the same way as c/cpp sources. No special care is needed. E.g.:

tpc-clang -c -march=goya my.s