All Projects → stanislaw → Llvm Cheatsheet

stanislaw / Llvm Cheatsheet

LLVM, clang, ninja, dyld and others.

Projects that are alternatives of or similar to Llvm Cheatsheet

Cheat Sheets
Cheat sheets for various stuff
Stars: ✭ 201 (+458.33%)
Mutual labels:  makefile, cheatsheet
Espeak Korean
Korean data for eSpeak
Stars: ✭ 34 (-5.56%)
Mutual labels:  makefile
Cderpm
RPM spec file, patches, and scripts to package up the Common Desktop Environment
Stars: ✭ 31 (-13.89%)
Mutual labels:  makefile
Device Sony Lilac
Stars: ✭ 34 (-5.56%)
Mutual labels:  makefile
Zion
A statically-typed strictly-evaluated garbage-collected readable programming language.
Stars: ✭ 33 (-8.33%)
Mutual labels:  llvm
Zh Google Styleguide
Google 开源项目风格指南 (中文版)
Stars: ✭ 8,315 (+22997.22%)
Mutual labels:  makefile
3ds portlibs
Portlibs for 3DS
Stars: ✭ 32 (-11.11%)
Mutual labels:  makefile
Monero Gui Guide
Guide for the Monero GUI wallet
Stars: ✭ 36 (+0%)
Mutual labels:  makefile
Rust Ffi Stringtools
A collection of examples how to use Rust libraries from other languages.
Stars: ✭ 34 (-5.56%)
Mutual labels:  makefile
Arduino Datalogging
Methods of Datalogging Sensor Data from an Arduino.
Stars: ✭ 33 (-8.33%)
Mutual labels:  makefile
Golang Alpine Docker
Build golang binaries for alpine linux
Stars: ✭ 33 (-8.33%)
Mutual labels:  makefile
Unlisp Llvm
Compiler for a toy Lisp language
Stars: ✭ 33 (-8.33%)
Mutual labels:  llvm
Mal
mal - Make a Lisp
Stars: ✭ 8,287 (+22919.44%)
Mutual labels:  makefile
Contract
My plain-language freelance contract (in Markdown).
Stars: ✭ 32 (-11.11%)
Mutual labels:  makefile
Android device xiaomi polaris
Stars: ✭ 35 (-2.78%)
Mutual labels:  makefile
Docker Images
🚢 Basic images for different usages
Stars: ✭ 32 (-11.11%)
Mutual labels:  makefile
Android device samsung serranoltexx
Stars: ✭ 33 (-8.33%)
Mutual labels:  makefile
Pantheios
The C/C++ Diagnostic Logging Sweetspot
Stars: ✭ 34 (-5.56%)
Mutual labels:  makefile
Coreos Nvidia
Yet another NVIDIA driver container for Container Linux (aka CoreOS)
Stars: ✭ 36 (+0%)
Mutual labels:  makefile
Docker Bitcoin Regtest
A way to experiment with Bitcoin.
Stars: ✭ 35 (-2.78%)
Mutual labels:  makefile

LLVM Cheatsheet

LLVM

Reduce time needed for building from source

cmake .. -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=host -DLLVM_ENABLE_ASSERTIONS=ON

-DLLVM_TARGETS_TO_BUILD=host only builds the host target and skips building the default targets, such as AArch64, AMDGPU, ARM, etc. This reduces a build time noticeably.

See How to enable a LLVM backend? for some details.

Clang

Generate human-readable LLVM IR from a file.

clang -S -emit-llvm testee.c -o testee.ll

Generate LLVM Bitcode from a file.

clang -c -emit-llvm -o testee.bc testee.c

One-line compilation

Useful for quick probes (like testing for missing header paths).

echo '#include <math.h>' | clang-3.7 -xc -v -

lli

brew install llvm # installed to /usr/local/opt/llvm, /usr/local/opt/llvm/bin/lli

Execute Objective-C code compiled into LLVM Bitcode using LLVM JIT

/usr/local/opt/llvm/bin/lli -load=/System/Library/Frameworks/Foundation.framework/Versions/Current/Foundation jitobjc.bc

CMake

Known issues: custom toolchains

  • It seems that CMake doesn't allow setting custom C and CXX compilers when it builds for Xcode (cmake ... -G Xcode). It does allow setting them for Ninja.

Ninja

Get compilation_database.json

mkdir build
cd build
cmake -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DLLVM_TARGETS_TO_BUILD="X86" ../llvm
ninja llvm-tblgen
cd ..
cp build/compile_commands.json ./

Get list of commands required to build a target

cd LLVM/BuildNinja
ninja -t commands YourTargetName

Objects and Symbols

Display libraries that are loaded by dyld

$ export DYLD_PRINT_LIBRARIES=1
$ rustc --version 2>&1
dyld: loaded: /Users/Stanislaw/.cargo/bin/rustc
dyld: loaded: /usr/lib/libcurl.4.dylib
dyld: loaded: /usr/lib/libz.1.dylib
...

Display the shared libraries that object file uses

otool -L Distribution/iOS/MusicXML.framework/MusicXML
Distribution/iOS/MusicXML.framework/MusicXML:
	@rpath/MusicXML.framework/MusicXML (compatibility version 1.0.0, current version 1.0.0)
	/usr/lib/libxml2.2.dylib (compatibility version 10.0.0, current version 10.9.0)
	/System/Library/Frameworks/Foundation.framework/Foundation (compatibility version 300.0.0, current version 1349.0.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
	/usr/lib/libSystem.dylib (compatibility version 1.0.0, current version 1238.0.0)
	/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation (compatibility version 150.0.0, current version 1348.0.0)

Display the @rpaths of an object

otool -l
...
Load command 20
          cmd LC_RPATH
      cmdsize 40
         path /usr/local/opt/[email protected]/lib (offset 12)

Taken from Print rpath of executable on OSX.

Display symbol table of an object file

Use llvm-nm instead of nm as the former can also read the .bc files.

brew install llvm # installed to /usr/local/opt/llvm, /usr/local/opt/llvm/bin/llvm-nm
$ llvm-nm Distribution/iOS/MusicXML.framework/MusicXML
0000000000000ec4 t +[MXMLConverter ConventionalParts]
0000000000008514 t +[RXMLElement elementFromHTMLData:]
00000000000083cd t +[RXMLElement elementFromHTMLFile:]
000000000000842e t +[RXMLElement elementFromHTMLFile:fileExtension:]
00000000000084b3 t +[RXMLElement elementFromHTMLFilePath:]
000000000000835d t +[RXMLElement elementFromHTMLString:encoding:]
0000000000008207 t +[RXMLElement elementFromURL:]
...

to also demangle C++ symbols:

$ nm target/debug/example | c++filt -p -i | grep main_static
000000010000faa0 T test::test_main_static::h8310fdcbefe718c6
0000000100086810 short test::test_main_static::_$u7b$$u7b$closure$u7d$$u7d$::_FILE_LINE::h76297330bb651452

Xcode

How can I force Xcode to use a custom compiler?

How can I force Xcode to use a custom compiler?

How can I get all the compile commands from Xcode?

How can I get all the compile commands from Xcode?

Known issues

Attempt to read debug information from a bitcode file compiled with Apple clang

warning: ignoring debug info with an invalid version (700000003) in /Users/stanislaw/Projects/LLVM/BuildXcode/projects/mull-project/unittests/Debug/fixtures/fixture_google_test_tester.bc
warning: ignoring debug info with an invalid version (700000003) in /Users/stanislaw/Projects/LLVM/BuildXcode/projects/mull-project/unittests/Debug/fixtures/fixture_google_test_testee.bc

Solution: compile files with custom clang e.g. brew install llvm / /usr/local/opt/llvm/bin/clang.

Attempt to read a bitcode file with possibly incompatible version of LLVM IR

test: <string>:7237:187: error: invalid field 'variable'
!1526 = distinct !DIGlobalVariable(name: "test_info_", linkageName: "_ZN14Hello_sup_Test10test_info_E", scope: !0, file: !1527, line: 4, type: !1528, isLocal: false, isDefinition: true, variable: %"class.testing::TestInfo"** @_ZN14Hello_sup_Test10test_info_E, declaration: !2817)

Best solution: make sure to compile the IR with the same llvm that reads it. The safest way is to stick to stable branches of llvm like release_40.

See also: IR Backwards Compatibility.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].