{"id":16987416,"url":"https://github.com/stanislaw/llvm-cheatsheet","last_synced_at":"2025-03-22T15:30:51.443Z","repository":{"id":143172671,"uuid":"71825591","full_name":"stanislaw/LLVM-Cheatsheet","owner":"stanislaw","description":"LLVM, clang, ninja, dyld and others.","archived":false,"fork":false,"pushed_at":"2020-02-23T12:25:07.000Z","size":12,"stargazers_count":44,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-18T13:15:29.233Z","etag":null,"topics":["cheatsheet","llvm"],"latest_commit_sha":null,"homepage":"","language":"Makefile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stanislaw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-24T19:41:31.000Z","updated_at":"2025-01-01T19:16:20.000Z","dependencies_parsed_at":"2023-04-11T19:02:58.976Z","dependency_job_id":null,"html_url":"https://github.com/stanislaw/LLVM-Cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stanislaw%2FLLVM-Cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stanislaw%2FLLVM-Cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stanislaw%2FLLVM-Cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stanislaw%2FLLVM-Cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stanislaw","download_url":"https://codeload.github.com/stanislaw/LLVM-Cheatsheet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244978450,"owners_count":20541858,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cheatsheet","llvm"],"created_at":"2024-10-14T02:49:26.630Z","updated_at":"2025-03-22T15:30:51.437Z","avatar_url":"https://github.com/stanislaw.png","language":"Makefile","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LLVM Cheatsheet\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n- [LLVM](#llvm)\n  - [Reduce time needed for building from source](#reduce-time-needed-for-building-from-source)\n- [Clang](#clang)\n  - [Generate human-readable LLVM IR from a file.](#generate-human-readable-llvm-ir-from-a-file)\n  - [Generate LLVM Bitcode from a file.](#generate-llvm-bitcode-from-a-file)\n  - [One-line compilation](#one-line-compilation)\n- [lli](#lli)\n  - [Execute Objective-C code compiled into LLVM Bitcode using LLVM JIT](#execute-objective-c-code-compiled-into-llvm-bitcode-using-llvm-jit)\n- [CMake](#cmake)\n  - [Known issues: custom toolchains](#known-issues-custom-toolchains)\n- [Ninja](#ninja)\n  - [Get compilation_database.json](#get-compilation_databasejson)\n  - [Get list of commands required to build a target](#get-list-of-commands-required-to-build-a-target)\n- [Objects and Symbols](#objects-and-symbols)\n  - [Display libraries that are loaded by dyld](#display-libraries-that-are-loaded-by-dyld)\n  - [Display the shared libraries that object file uses](#display-the-shared-libraries-that-object-file-uses)\n  - [Display the @rpaths of an object](#display-the-rpaths-of-an-object)\n  - [Display symbol table of an object file](#display-symbol-table-of-an-object-file)\n- [Xcode](#xcode)\n  - [How can I force Xcode to use a custom compiler?](#how-can-i-force-xcode-to-use-a-custom-compiler)\n  - [How can I get all the compile commands from Xcode?](#how-can-i-get-all-the-compile-commands-from-xcode)\n- [Known issues](#known-issues)\n  - [Attempt to read debug information from a bitcode file compiled with Apple clang](#attempt-to-read-debug-information-from-a-bitcode-file-compiled-with-apple-clang)\n  - [Attempt to read a bitcode file with possibly incompatible version of LLVM IR](#attempt-to-read-a-bitcode-file-with-possibly-incompatible-version-of-llvm-ir)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## LLVM\n\n### Reduce time needed for building from source\n\n```\ncmake .. -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=host -DLLVM_ENABLE_ASSERTIONS=ON\n```\n\n`-DLLVM_TARGETS_TO_BUILD=host` only builds the host target and skips building\nthe  default targets, such as AArch64, AMDGPU, ARM, etc. This reduces a build\ntime noticeably.\n\nSee [How to enable a LLVM backend?](https://stackoverflow.com/a/46912216/598057) for some details.\n\n## Clang\n\n### Generate human-readable LLVM IR from a file.\n\n\n```bash\nclang -S -emit-llvm testee.c -o testee.ll\n```\n\n### Generate LLVM Bitcode from a file.\n\n\n```bash\nclang -c -emit-llvm -o testee.bc testee.c\n```\n\n### One-line compilation\n\nUseful for quick probes (like testing for missing header paths).\n\n```\necho '#include \u003cmath.h\u003e' | clang-3.7 -xc -v -\n```\n\n## lli\n\n```bash\nbrew install llvm # installed to /usr/local/opt/llvm, /usr/local/opt/llvm/bin/lli\n```\n\n### Execute Objective-C code compiled into LLVM Bitcode using LLVM JIT\n\n```bash\n/usr/local/opt/llvm/bin/lli -load=/System/Library/Frameworks/Foundation.framework/Versions/Current/Foundation jitobjc.bc\n```\n\n## CMake\n\n### Known issues: custom toolchains\n\n- It seems that CMake doesn't allow setting custom C and CXX compilers\nwhen it builds for Xcode (`cmake ... -G Xcode`). It does allow setting\nthem for Ninja.\n\n## Ninja\n\n### Get compilation_database.json\n\n```bash\nmkdir build\ncd build\ncmake -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DLLVM_TARGETS_TO_BUILD=\"X86\" ../llvm\nninja llvm-tblgen\ncd ..\ncp build/compile_commands.json ./\n```\n\n### Get list of commands required to build a target\n\n```\ncd LLVM/BuildNinja\nninja -t commands YourTargetName\n```\n\n## Objects and Symbols\n\n### Display libraries that are loaded by dyld\n\n```bash\n$ export DYLD_PRINT_LIBRARIES=1\n$ rustc --version 2\u003e\u00261\ndyld: loaded: /Users/Stanislaw/.cargo/bin/rustc\ndyld: loaded: /usr/lib/libcurl.4.dylib\ndyld: loaded: /usr/lib/libz.1.dylib\n...\n```\n\n### Display the shared libraries that object file uses\n\n```bash\notool -L Distribution/iOS/MusicXML.framework/MusicXML\nDistribution/iOS/MusicXML.framework/MusicXML:\n\t@rpath/MusicXML.framework/MusicXML (compatibility version 1.0.0, current version 1.0.0)\n\t/usr/lib/libxml2.2.dylib (compatibility version 10.0.0, current version 10.9.0)\n\t/System/Library/Frameworks/Foundation.framework/Foundation (compatibility version 300.0.0, current version 1349.0.0)\n\t/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)\n\t/usr/lib/libSystem.dylib (compatibility version 1.0.0, current version 1238.0.0)\n\t/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation (compatibility version 150.0.0, current version 1348.0.0)\n```\n\n### Display the @rpaths of an object\n\n```\notool -l\n...\nLoad command 20\n          cmd LC_RPATH\n      cmdsize 40\n         path /usr/local/opt/llvm@3.9/lib (offset 12)\n```\n\nTaken from [Print rpath of executable on OSX](https://stackoverflow.com/a/12522096/598057).\n\n### Display symbol table of an object file\n\nUse `llvm-nm` instead of `nm` as the former can also read the .bc files.\n\n```bash\nbrew install llvm # installed to /usr/local/opt/llvm, /usr/local/opt/llvm/bin/llvm-nm\n```\n\n```bash\n$ llvm-nm Distribution/iOS/MusicXML.framework/MusicXML\n0000000000000ec4 t +[MXMLConverter ConventionalParts]\n0000000000008514 t +[RXMLElement elementFromHTMLData:]\n00000000000083cd t +[RXMLElement elementFromHTMLFile:]\n000000000000842e t +[RXMLElement elementFromHTMLFile:fileExtension:]\n00000000000084b3 t +[RXMLElement elementFromHTMLFilePath:]\n000000000000835d t +[RXMLElement elementFromHTMLString:encoding:]\n0000000000008207 t +[RXMLElement elementFromURL:]\n...\n```\n\nto also demangle C++ symbols:\n\n```\n$ nm target/debug/example | c++filt -p -i | grep main_static\n000000010000faa0 T test::test_main_static::h8310fdcbefe718c6\n0000000100086810 short test::test_main_static::_$u7b$$u7b$closure$u7d$$u7d$::_FILE_LINE::h76297330bb651452\n```\n\n## Xcode\n\n### How can I force Xcode to use a custom compiler?\n\n[How can I force Xcode to use a custom compiler?](https://stackoverflow.com/questions/39327952/how-can-i-force-xcode-to-use-a-custom-compiler)\n\n### How can I get all the compile commands from Xcode?\n\n[How can I get all the compile commands from Xcode?](https://stackoverflow.com/questions/43906786/how-can-i-get-all-the-compile-commands-from-xcode/43973745#43973745)\n\n## Known issues\n\n### Attempt to read debug information from a bitcode file compiled with Apple clang\n\n```\nwarning: 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\nwarning: 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\n```\n\nSolution: compile files with custom clang e.g. `brew install llvm / /usr/local/opt/llvm/bin/clang`.\n\n### Attempt to read a bitcode file with possibly incompatible version of LLVM IR\n\n```\ntest: \u003cstring\u003e:7237:187: error: invalid field 'variable'\n!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)\n```\n\nBest 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`.\n\nSee also: [IR Backwards Compatibility](http://llvm.org/docs/DeveloperPolicy.html#ir-backwards-compatibility).\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstanislaw%2Fllvm-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstanislaw%2Fllvm-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstanislaw%2Fllvm-cheatsheet/lists"}