### Build Example Directory Setup Source: https://github.com/llvm/llvm-project/blob/main/libc/examples/README.md Steps to set up the build directory for an example. Navigate to the example's directory, create a 'build' subdirectory, and change into it. ```bash cd mkdir build cd build ``` -------------------------------- ### ASTImporter Tool Setup Example Source: https://github.com/llvm/llvm-project/blob/main/clang/docs/LibASTImporter.rst This C++ code snippet demonstrates the initial setup for a tool using ASTImporter by creating two ASTs from virtual files synthesized from string literals. ```cpp // First, we build two ASTs from virtual files; the content of the virtual files are synthesized from string literals: ``` -------------------------------- ### Setup Local LNT Instance and Run Server Source: https://github.com/llvm/llvm-project/blob/main/libcxx/utils/ci/lnt/README.md Commands to create a local LNT instance, configure authentication, and start the LNT server. ```bash # Create an instance and run a server lnt create my-instance echo "api_auth_token = 'example_token'" >> my-instance/lnt.cfg lnt runserver my-instance ``` -------------------------------- ### CUDA Server Example Setup Source: https://github.com/llvm/llvm-project/blob/main/libc/docs/gpu/rpc.rst Demonstrates manual configuration of an RPC server using CUDA. It includes necessary headers and a weak definition for the client symbol. ```cuda #include #include #include #include #include #include [[noreturn]] void handle_error(cudaError_t err) { fprintf(stderr, "CUDA error: %s\n", cudaGetErrorString(err)); exit(EXIT_FAILURE); } // Routes the library symbol into the CUDA runtime interface. [[gnu::weak]] __device__ rpc::Client client asm("__llvm_rpc_client"); // The device-side overload of the standard C function to call. extern "C" __device__ int puts(const char *); ``` -------------------------------- ### Test and Installation Setup Source: https://github.com/llvm/llvm-project/blob/main/third-party/boost-math/CMakeLists.txt Configures the build to include tests and install the library headers. This is conditional on BUILD_TESTING and the current CMake source directory. ```cmake if(BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt") add_subdirectory(test) # Only enable tests when we're the root project elseif(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) include(CTest) add_subdirectory(test) include(GNUInstallDirs) install(DIRECTORY "include/" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") endif() ``` -------------------------------- ### Instruction Flags: Frame Setup Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/MIRLangRef.md Example of an instruction with the 'frame-setup' flag, typically used for setting up the stack frame. ```text $fp = frame-setup ADDXri $sp, 0, 0 ``` -------------------------------- ### CHECK-SAME Example Source: https://github.com/llvm/llvm-project/blob/main/llvm/test/FileCheck/dump-input/annotations.txt Illustrates the CHECK-SAME annotation for matching identical patterns. This snippet shows the setup for input and expected output files, and the RUN commands to execute the check. ```text ; RUN: echo 'pre' > %t.in ; RUN: echo 'var' >> %t.in ; RUN: echo 'CHECK: pre' > %t.chk ; RUN: echo 'CHECK-SAME: [[VAR]]' >> %t.chk ; REDEFINE: %{pre} = SUBST_SAME ; REDEFINE: %{opts} = -DVAR=var ; RUN: not %{run-vv} ; RUN: not %{run-v} ; RUN: not %{run} ; REDEFINE: %{opts} = ; Verbose diagnostics are suppressed but not errors. ; SUBST_SAME:{{.*}}error:{{.*}} ; SUBST_SAME-V:<<<<<< ; SUBST_SAME-V-NEXT: 1: pre ; SUBST_SAME-V-NEXT:check:1 ^~~ ; SUBST_SAME-V-NEXT:same:2'0 { search range start (exclusive) ; SUBST_SAME-V-NEXT: 2: var ; SUBST_SAME-V-NEXT:same:2'1 !~~ error: match on wrong line ; SUBST_SAME-V-NEXT:same:2'2 with "VAR" equal to "var" ; SUBST_SAME-V-NEXT:same:2'3 } search range end (exclusive) ; SUBST_SAME-V-NEXT:>>>>>> ; SUBST_SAME-Q:<<<<<< ; SUBST_SAME-Q-NEXT: 1: pre ; SUBST_SAME-Q-NEXT:same:2'0 { search range start (exclusive) ; SUBST_SAME-Q-NEXT: 2: var ; SUBST_SAME-Q-NEXT:same:2'1 !~~ error: match on wrong line ; SUBST_SAME-Q-NEXT:same:2'2 } search range end (exclusive) ; SUBST_SAME-Q-NEXT:>>>>>> ``` -------------------------------- ### Configuration File Example Source: https://github.com/llvm/llvm-project/blob/main/clang/docs/UsersManual.rst Configuration files contain command-line options. Lines starting with '#' are comments. Long options can be split across lines using a trailing backslash. ```text # Several options on line -c --target=x86_64-unknown-linux-gnu # Long option split between lines -I/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../\ include/c++/5.4.0 # other config files may be included @linux.options ``` -------------------------------- ### CUDA Driver API Example Setup Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/NVPTXUsage.rst Includes necessary headers for CUDA Driver API operations and a helper function to check for errors. ```c++ #include #include #include #include "cuda.h" void checkCudaErrors(CUresult err) { assert(err == CUDA_SUCCESS); } ``` -------------------------------- ### Example Usage for Generating Xcode Toolchain Source: https://github.com/llvm/llvm-project/blob/main/llvm/tools/xcode-toolchain/CMakeLists.txt Demonstrates the command-line steps to configure, build, and install a custom LLVM Xcode toolchain using CMake and Ninja. ```bash cmake -G Ninja -DLLVM_CREATE_XCODE_TOOLCHAIN=On \ -DCMAKE_INSTALL_PREFIX=$PWD/install ninja install-xcode-toolchain export EXTERNAL_TOOLCHAINS_DIR=$PWD/install/Toolchains export TOOLCHAINS=org.llvm.3.8.0svn ``` -------------------------------- ### Freeze Instruction Example Setup Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/UndefinedBehavior.rst Provides the basic structure of a function using a loop, where the 'freeze' instruction might be used to control UB propagation. ```llvm define i32 @fn(i32 %n, i1 %c) { entry: br label %loop loop: %i = phi i32 [ 0, %entry ], [ %i2, %loop.end ] %cond = icmp ule i32 %i, %n br i1 %cond, label %loop.cont, label %exit loop.cont: br i1 %c, label %then, label %else then: ... br label %loop.end else: ... br label %loop.end loop.end: %i2 = add i32 %i, 1 br label %loop exit: ret i32 %i } ``` -------------------------------- ### jAcceleratorPluginInitialize Response Example Source: https://github.com/llvm/llvm-project/blob/main/lldb/docs/resources/lldbgdbremote.md An example of the JSON response from lldb-server, detailing an installed accelerator plugin. ```json STUB REPLIES: [{"plugin_name":"amdgpu","session_name":"AMD GPU Session","identifier":0}] ``` -------------------------------- ### Build QEMU and Kernel for ARM/AArch64 Source: https://github.com/llvm/llvm-project/blob/main/lldb/docs/resources/qemu-testing.md Use setup.sh to build QEMU binaries and the Linux kernel image for specified architectures. Ensure the script is executed with appropriate arguments for QEMU and the target architecture. ```bash bash setup.sh --qemu --kernel arm ``` ```bash bash setup.sh --qemu --kernel arm64 ``` -------------------------------- ### Create Root Directory and Navigate Source: https://github.com/llvm/llvm-project/blob/main/flang/docs/GettingStarted.md Sets up the necessary directory structure for building Flang. Navigate into the created root directory. ```bash mkdir root cd root ``` -------------------------------- ### Get DAG Operator Example Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/TableGen/ProgRef.rst Example showing how to assign the result of !getdagop to a specific class type. ```TableGen BaseClass b = !getdagop(someDag); ``` ```TableGen BaseClass b = !cast(!getdagop(someDag)); ``` -------------------------------- ### Sendmsg RTN GET REALTIME Example Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/AMDGPU/gfx11_msg_b8ff6d.rst Example of using the s_sendmsg_rtn_b32 instruction with a symbolic sendmsg value. ```assembly s_sendmsg_rtn_b32 s0, sendmsg(MSG_GET_REALTIME) ``` -------------------------------- ### Install and Test lit via Setuptools Source: https://github.com/llvm/llvm-project/blob/main/llvm/utils/lit/README.rst Install lit using setuptools and then run its test suite to verify installation. This confirms lit works correctly when installed as a package. ```bash python utils/lit/setup.py install lit --path /path/to/your/llvm/build/bin utils/lit/tests ``` -------------------------------- ### Sendmsg RTN GET DOORBELL Example Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/AMDGPU/gfx11_msg_b8ff6d.rst Example of using the s_sendmsg_rtn_b32 instruction with a numerical message code. ```assembly s_sendmsg_rtn_b32 s0, 132 ``` -------------------------------- ### Configure and Install Startup Objects Source: https://github.com/llvm/llvm-project/blob/main/libc/startup/uefi/CMakeLists.txt Sets up a custom target `libc-startup` and iterates through startup components to add dependencies and install their object files to the library directory. ```cmake add_custom_target(libc-startup) set(startup_components crt1) foreach(target IN LISTS startup_components) set(fq_target_name libc.startup.uefi.${target}) add_dependencies(libc-startup ${fq_target_name}) install(FILES $ DESTINATION ${LIBC_INSTALL_LIBRARY_DIR} RENAME $ COMPONENT libc) endforeach() ``` -------------------------------- ### Get All Installable Header Targets Function Source: https://github.com/llvm/llvm-project/blob/main/libc/include/CMakeLists.txt A CMake function to recursively collect all installable header targets and their dependencies. It's used to gather all headers that need to be installed. ```cmake function(get_all_install_header_targets out_var) set(all_deps ${ARGN}) foreach(target IN LISTS ARGN) get_target_property(deps ${target} DEPS) if(NOT deps) continue() endif() list(APPEND all_deps ${deps}) get_all_install_header_targets(nested_deps ${deps}) list(APPEND all_deps ${nested_deps}) endforeach() list(REMOVE_DUPLICATES all_deps) set(${out_var} ${all_deps} PARENT_SCOPE) endfunction(get_all_install_header_targets) ``` -------------------------------- ### Basic Class Hierarchy Example Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/HowToSetUpLLVMStyleRTTI.rst An example of a basic class hierarchy without RTTI, serving as a starting point for RTTI implementation. ```c++ class Shape { public: Shape() {} virtual double computeArea() = 0; }; class Square : public Shape { double SideLength; public: Square(double S) : SideLength(S) {} double computeArea() override; }; class Circle : public Shape { double Radius; public: Circle(double R) : Radius(R) {} double computeArea() override; }; ``` -------------------------------- ### Start Clang-Repl Source: https://github.com/llvm/llvm-project/blob/main/clang/docs/ClangRepl.rst Navigate to the build directory and execute the clang-repl binary to start the interactive interpreter. ```console ./clang-repl ``` -------------------------------- ### Func_sanitize Metadata Example Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/LangRef.rst Example showing the use of func_sanitize metadata for function sanitizer instrumentation, including RTTI proxy setup. ```text @__llvm_rtti_proxy = private unnamed_addr constant ptr @_ZTIFvvE define void @_Z3funv() !func_sanitize !0 { return void } !0 = !{i32 846595819, ptr @__llvm_rtti_proxy} ``` -------------------------------- ### Build Example with Ninja (Overlay) Source: https://github.com/llvm/llvm-project/blob/main/libc/examples/README.md Build a specific example using Ninja after configuring with an overlay libc. Replace with the actual name of the example to build. ```bash ninja ``` -------------------------------- ### Keep Empty Lines at Start of Block Example Source: https://github.com/llvm/llvm-project/blob/main/clang/docs/ClangFormatStyleOptions.rst Compares code formatting with and without keeping empty lines at the start of a block in C++. ```c++ true: false: if (foo) { vs. if (foo) { bar(); bar(); } } ``` -------------------------------- ### Hello World Module Example Source: https://github.com/llvm/llvm-project/blob/main/clang/docs/StandardCPlusPlusModules.rst A basic 'hello world' example demonstrating a simple module interface unit and its usage. Compile the module interface first, then the main file, linking them together. ```c++ // Hello.cppm module; #include export module Hello; export void hello() { std::cout << "Hello World!\n"; } ``` ```c++ // use.cpp import Hello; int main() { hello(); return 0; } ``` ```console $ clang++ -std=c++20 Hello.cppm --precompile -o Hello.pcm $ clang++ -std=c++20 use.cpp -fmodule-file=Hello=Hello.pcm Hello.pcm -o Hello.out $ ./Hello.out Hello World! ``` -------------------------------- ### Create a Simple C File Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/GettingStarted.md Create a basic C source file named 'hello.c' for demonstration. ```c #include int main() { printf("hello world\n"); return 0; } ``` -------------------------------- ### Keep Empty Lines Configuration Example Source: https://github.com/llvm/llvm-project/blob/main/clang/docs/ClangFormatStyleOptions.rst Shows a C++ configuration for KeepEmptyLines, disabling empty lines at the start of the file, end of the file, and start of blocks. ```c++ KeepEmptyLines: AtEndOfFile: false AtStartOfBlock: false AtStartOfFile: false ``` -------------------------------- ### Example Usage and Output Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/CommandLine.md Demonstrates how to use the '-max-file-size' option with different inputs and shows the expected output, including successful parsing and error handling. ```bash $ ./test MFS: 0 $ ./test -max-file-size=123MB MFS: 128974848 $ ./test -max-file-size=3G MFS: 3221225472 $ ./test -max-file-size=dog -max-file-size option: 'dog' value invalid for file size argument! ``` -------------------------------- ### Example Usage of IARGC Source: https://github.com/llvm/llvm-project/blob/main/flang/docs/Intrinsics.md Shows how to get the number of command-line arguments and print it. ```Fortran program example_iargc integer :: n n = iargc() print *, "Argument count:", n end program ``` -------------------------------- ### Build QEMU Binaries Only Source: https://github.com/llvm/llvm-project/blob/main/lldb/docs/resources/qemu-testing.md Execute setup.sh with the --qemu flag to build only the QEMU system emulation binaries (e.g., qemu-system-arm, qemu-system-aarch64). ```bash bash setup.sh --qemu ``` -------------------------------- ### Hidden Inclusion Example Setup Source: https://github.com/llvm/llvm-project/blob/main/clang/docs/StandardCPlusPlusModules.rst A more complex setup involving nested header dependencies and modules to illustrate potential issues with hidden inclusions when using module maps. ```c++ // b.h #pragma once struct B {}; // a.h #pragma once #include "b.h" struct A { B b; }; // b.cppm export module b; export extern "C++" struct B { }; // a.cppm export module a; import b; export extern "C++" struct A { B b; }; // test.cc import a; #include "a.h" A a; B b; // a.cppm.modulemap module a { header "a.h" } // b.cppm.modulemap module b { header "b.h" } ``` -------------------------------- ### Embed LLVM in a CMake Project Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/CMake.md Example CMakeLists.txt to find an installed LLVM, link against its components, and build a simple application. Ensure LLVM is installed or LLVM_DIR is set correctly. ```cmake cmake_minimum_required(VERSION 3.20.0) project(SimpleProject) find_package(LLVM REQUIRED CONFIG) message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") # Set your project compile flags. # E.g. if using the C++ header files # you will need to enable C++11 support # for your compiler. include_directories(${LLVM_INCLUDE_DIRS}) separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS}) add_definitions(${LLVM_DEFINITIONS_LIST}) # Now build our tools add_executable(simple-tool tool.cpp) # Find the libraries that correspond to the LLVM components # that we wish to use llvm_map_components_to_libnames(llvm_libs support core irreader) # Link against LLVM libraries target_link_libraries(simple-tool ${llvm_libs}) ``` -------------------------------- ### Installing Benchmark Dependencies Source: https://github.com/llvm/llvm-project/blob/main/libcxx/docs/TestingLibcxx.rst Commands to set up a virtual environment and install the necessary Python packages for the 'compare-benchmarks' helper tool. ```bash $ python -m venv .venv && source .venv/bin/activate # Optional but recommended $ pip install -r libcxx/utils/requirements.txt ``` -------------------------------- ### Setup Directories and Files for Diff Test Source: https://github.com/llvm/llvm-project/blob/main/llvm/utils/lit/tests/Inputs/shtest-shell/diff-r-error-1.txt This snippet shows the shell commands used to set up the test environment. It creates two directories, 'dir1' and 'dir2', along with a subdirectory 'subdir' within each. It then populates a file named 'f01' in the subdirectory with different content in each of the main directories. ```shell # RUN: rm -rf %t/dir1 %t/dir2 # RUN: mkdir -p %t/dir1 %t/dir2 # RUN: mkdir -p %t/dir1/subdir %t/dir2/subdir # RUN: echo "12345" > %t/dir1/subdir/f01 # RUN: echo "00000" > %t/dir2/subdir/f01 ``` -------------------------------- ### Start Remote Debug Server Source: https://github.com/llvm/llvm-project/blob/main/lldb/docs/resources/debugging.md Starts an lldb-server in gdbserver mode on a remote machine, which then launches another lldb-server to debug a specified executable. This setup allows for debugging the remote lldb-server itself. ```bash $ <...>/bin/lldb-server gdbserver 0.0.0.0:54322 -- \ <...>/bin/lldb-server gdbserver 0.0.0.0:54321 -- /tmp/test.o ``` -------------------------------- ### Install Startup Components Source: https://github.com/llvm/llvm-project/blob/main/libc/startup/linux/CMakeLists.txt Installs the 'crt1', 'crti', and 'crtn' startup object files to the library directory. This makes them available for linking. ```cmake set(startup_components crt1 crti crtn) foreach(target IN LISTS startup_components) set(fq_target_name libc.startup.linux.${target}) add_dependencies(libc-startup ${fq_target_name}) install(FILES $ DESTINATION ${LIBC_INSTALL_LIBRARY_DIR} RENAME $ COMPONENT libc) endforeach() ``` -------------------------------- ### LLVM Predicated Integer ADD Reduction Example Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/LangRef.rst Example of using the vp.reduce.add intrinsic. The result is shown to be equivalent to a masked select followed by a standard vector reduction and addition with the start value. ```llvm %r = call i32 @llvm.vp.reduce.add.v4i32(i32 %start, <4 x i32> %a, <4 x i1> %mask, i32 %evl) ; %r is equivalent to %also.r, where lanes greater than or equal to %evl ; are treated as though %mask were false for those lanes. %masked.a = select <4 x i1> %mask, <4 x i32> %a, <4 x i32> zeroinitializer %reduction = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %masked.a) %also.r = add i32 %reduction, %start ``` -------------------------------- ### Example 1: Calling a private member function disables the check Source: https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/docs/clang-tidy/checks/readability/make-member-function-const.rst This example demonstrates a scenario where calling a private member function prevents the readability-make-member-function-const check from suggesting to make the 'get' method const. ```c++ class E1 { Pimpl &getPimpl() const; public: int &get() { // Calling a private member function disables this check. return getPimpl()->i; } ... }; ``` -------------------------------- ### Memory setup with LLVM-EXEGESIS-MEM-DEF and LLVM-EXEGESIS-MEM-MAP Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/CommandGuide/llvm-exegesis.rst Demonstrates how to define and map memory regions for instruction execution using specific exegesis directives. ```none # LLVM-EXEGESIS-MEM-DEF test1 4096 7fffffff # LLVM-EXEGESIS-MEM-MAP test1 8192 movq $8192, %rax movq (%rax), %rdi ``` -------------------------------- ### Adjust Trampoline Intrinsic Call Source: https://github.com/llvm/llvm-project/blob/main/flang/docs/InternalProcedureTrampolines.md Example of using the `llvm.adjust.trampoline` intrinsic to get the executable address of a trampoline. ```c %p = call i8* @llvm.adjust.trampoline(i8* %trampoline_address) ``` -------------------------------- ### Basic Hello World Example with llvm-jitlink Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/JITLink.rst Demonstrates the basic usage of llvm-jitlink by compiling and linking a simple C 'hello world' program, then executing it. ```sh % cat hello-world.c #include int main(int argc, char *argv[]) { printf("hello, world!\n"); return 0; } % clang -c -o hello-world.o hello-world.c % llvm-jitlink hello-world.o Hello, World! ``` -------------------------------- ### Overloaded Operator Example Source: https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/docs/clang-tidy/checks/fuchsia/overloaded-operator.rst Demonstrates which overloaded operators trigger a warning and which do not, according to the Fuchsia style guide. ```c++ int operator+(int); // Warning B &operator=(const B &Other); // No warning B &operator=(B &&Other) // No warning ``` -------------------------------- ### Create and navigate to build directory Source: https://github.com/llvm/llvm-project/blob/main/libc/config/windows/README.md Create an empty directory for building LLVM libc and change the current directory to it. ```bash mkdir libc-build cd libc-build ``` -------------------------------- ### Linux Kernel Style Configuration Source: https://github.com/llvm/llvm-project/blob/main/clang/docs/ClangFormatStyleOptions.rst A clang-format configuration example mimicking the Linux Kernel style guide. ```yaml BasedOnStyle: LLVM IndentWidth: 8 UseTab: Always BreakBeforeBraces: Linux AllowShortIfStatementsOnASingleLine: false IndentCaseLabels: false ``` -------------------------------- ### System Header Prefix Configuration Example Source: https://github.com/llvm/llvm-project/blob/main/clang/docs/UsersManual.rst Provides an example of using command-line arguments '--system-header-prefix=' and '--no-system-header-prefix=' to control how include paths are treated as system headers. This demonstrates precedence rules for matching prefixes. ```console $ clang -Ifoo -isystem bar --system-header-prefix=x/ \ --no-system-header-prefix=x/y/ ``` -------------------------------- ### Monorepo Bisecting Setup Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/Proposals/GitHubMove.rst Initiate a Git bisect process on the monorepo, specifying the start and end points for the search. ```bash git bisect start releases/3.9.x releases/3.8.x git bisect run ./bisect_script.sh ``` -------------------------------- ### Install Google Benchmark Globally Source: https://github.com/llvm/llvm-project/blob/main/third-party/benchmark/README.md Command to install the Google Benchmark library globally on the system after building. ```bash sudo cmake --build "build" --config Release --target install ``` -------------------------------- ### Example C Source File for Usage Source: https://github.com/llvm/llvm-project/blob/main/clang/docs/Modules.rst Includes B.h and conditionally calls function 'a'. ```c /* use.c */ #include "B.h" void use() { #ifdef ENABLE_A a(); #endif } ``` -------------------------------- ### Configuring GCC Installation for OpenMP Offload Build Source: https://github.com/llvm/llvm-project/blob/main/openmp/docs/SupportAndFAQ.rst Example of setting CMAKE flags to specify a GCC installation directory. This is crucial when your system's default GCC is too old for building LLVM with OpenMP offloading capabilities. ```bash CMAKE_C_FLAGS="--gcc-install-dir=/usr/lib/gcc/x86_64-linux-gnu/12" CMAKE_CXX_FLAGS="--gcc-install-dir=/usr/lib/gcc/x86_64-linux-gnu/12" ``` -------------------------------- ### Option name format example Source: https://github.com/llvm/llvm-project/blob/main/libc/docs/dev/config_options.rst Demonstrates the standard naming convention for libc configuration options, which includes a prefix, the group tag, and an action description. ```none LIBC_CONF__ ``` -------------------------------- ### Example: Stand-alone Clang Build Script Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/GettingStarted.md A shell script demonstrating the complete process of building LLVM, installing it, and then configuring and building Clang as a stand-alone project. Ensure build directories and install prefixes are correctly managed. ```shell #!/bin/sh build_llvm=`pwd`/build-llvm build_clang=`pwd`/build-clang installprefix=`pwd`/install llvm=`pwd`/llvm-project mkdir -p $build_llvm mkdir -p $installprefix cmake -G Ninja -S $llvm/llvm -B $build_llvm \ -DLLVM_INSTALL_UTILS=ON \ -DCMAKE_INSTALL_PREFIX=$installprefix \ -DCMAKE_BUILD_TYPE=Release ninja -C $build_llvm install cmake -G Ninja -S $llvm/clang -B $build_clang \ -DLLVM_EXTERNAL_LIT=$build_llvm/utils/lit \ -DLLVM_ROOT=$installprefix ninja -C $build_clang ``` -------------------------------- ### Install Startup Object Source: https://github.com/llvm/llvm-project/blob/main/libc/startup/gpu/CMakeLists.txt Configures a custom target `libc-startup` and installs the generated startup object files to the specified destination with a specific rename pattern. ```cmake add_custom_target(libc-startup) set(startup_components crt1) foreach(target IN LISTS startup_components) set(fq_target_name libc.startup.gpu.${target}) add_dependencies(libc-startup ${fq_target_name}) install(FILES $ DESTINATION ${LIBC_INSTALL_LIBRARY_DIR} RENAME $ COMPONENT libc) endforeach() ``` -------------------------------- ### DAG NOT Search Range Ends at DAG Group Start Source: https://github.com/llvm/llvm-project/blob/main/llvm/test/FileCheck/check-dag-not-dag.txt Illustrates the previous behavior where the search range for NOTs ended at the start of the match range for the first DAG in a group. This example shows the expected FileCheck output for this scenario. ```llvm ; RUN: FileCheck -input-file %s %s -check-prefix=NotSearchEnd The search range for the NOTs used to end at the start of the match range for the first DAG in the following DAG group. Now it ends at the start of the match range for the entire following DAG group. __NotSearchEnd x0 x1 y1 foobar y0 z2 foobar z1 __NotSearchEnd ; NotSearchEnd: {{^}}__NotSearchEnd ; NotSearchEnd-DAG: {{^}}x0 ; NotSearchEnd-DAG: {{^}}x1 ; NotSearchEnd-NOT: {{^}}foobar ; NotSearchEnd-DAG: {{^}}y0 ; NotSearchEnd-DAG: {{^}}y1 ; NotSearchEnd-NOT: {{^}}foobar ; NotSearchEnd-DAG: {{^}}z0 ; NotSearchEnd-DAG: {{^}}z1 ; NotSearchEnd-DAG: {{^}}z2 ; NotSearchEnd: {{^}}__NotSearchEnd ``` -------------------------------- ### Launch Process (With Arguments) Source: https://github.com/llvm/llvm-project/blob/main/lldb/docs/use/map.md GDB uses `run `. LLDB uses `process launch -- ` or `run `. ```shell (gdb) run (gdb) r ``` ```shell (lldb) process launch -- (lldb) run (lldb) r ``` -------------------------------- ### GFX1250 Instruction Encoding Example Source: https://github.com/llvm/llvm-project/blob/main/llvm/test/MC/Disassembler/AMDGPU/gfx1250_dasm_vop3_dpp16.txt Provides a raw hexadecimal encoding for a GFX1250 instruction, likely a control or setup instruction. ```assembly 0x02,0x00,0x60,0xd6,0xfa,0x0e,0x22,0x04,0x04,0x79,0x00,0xff ``` -------------------------------- ### Namespace and Output Example Source: https://github.com/llvm/llvm-project/blob/main/clang/docs/ClangRepl.rst Shows how to include a header, use a namespace, and print a string to standard output. The output appears on the next line. ```text clang-repl> #include clang-repl> using namespace std; clang-repl> std::cout << "Welcome to CLANG-REPL" << std::endl; Welcome to CLANG-REPL // Prints Welcome to CLANG-REPL ``` -------------------------------- ### LLVM Example Project Configuration Source: https://github.com/llvm/llvm-project/blob/main/llvm/examples/OrcV2Examples/LLJITWithCustomObjectLinkingLayer/CMakeLists.txt Configures the LLVM build system to include necessary components for the LLJIT example and adds the example project. ```cmake set(LLVM_LINK_COMPONENTS Core IRReader JITLink OrcJIT Support nativecodegen ) add_llvm_example(LLJITWithCustomObjectLinkingLayer LLJITWithCustomObjectLinkingLayer.cpp ) ``` -------------------------------- ### Example SHA256 Hashes for Dependencies Source: https://github.com/llvm/llvm-project/blob/main/llvm/utils/git/requirements.txt A list of SHA256 hashes for various dependencies, used by `pip-compile` to verify the integrity of installed packages. ```bash --hash=sha256:dcdba5c86e368442528f7060039eda390cc4091bfd1dca41e8046af7c910dda8 \ --hash=sha256:decbfa2f618fa8ed81c95ee18a387ff973143c656ef800c9f24fb7e9c16054e2 \ --hash=sha256:e4fdb9275308292e880dcbeb12546df7f3e0f96c6b41197e0cf37d2826359020 \ --hash=sha256:eb1b046be06b0fce7249f1d025cd359b4b80fc1c3e24ad9eca33e0dcdb2e4a35 \ --hash=sha256:eb6e651000a19c96f452c85132811d25e9264d836951022d6e81df2fff38337d \ --hash=sha256:ed867c42c268f876097248e05b6117a65bcd1e63b779e916fe2e33cd6fd0d3c3 \ --hash=sha256:edfad1d29c73f9b863ebe7082ae9321374ccb10879eeabc84ba3b69f2579d537 \ --hash=sha256:f2058f813d4f2b5e3a9eb2eb3faf8f1d99b81c3e51aeda4b168406443e8ba809 \ --hash=sha256:f6b2d0c6703c988d334f297aa5df18c45e97b0af3679bb75059e0e0bd8b1069d \ --hash=sha256:f8212564d49c50eb4565e502814f694e240c55551a5f1bc841d4fcaabb0a9b8a \ --hash=sha256:ffa565331890b90056c01db69c0fe634a776f8019c143a5ae265f9c6bc4bd6d4 ``` -------------------------------- ### Set LLVM Installation Directories in CMake Source: https://github.com/llvm/llvm-project/blob/main/llvm/CMakeLists.txt These snippets define and configure installation directories for various LLVM components. They use CMake's `set` command with the `CACHE STRING` type and `mark_as_advanced` to control paths for packages, binaries, utilities, and examples. ```cmake include(GNUInstallPackageDir) set(LLVM_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/llvm" CACHE STRING "Path for CMake subdirectory for LLVM (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/llvm')") ``` ```cmake set(LLVM_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE STRING "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')") mark_as_advanced(LLVM_TOOLS_INSTALL_DIR) ``` ```cmake set(LLVM_UTILS_INSTALL_DIR "${LLVM_TOOLS_INSTALL_DIR}" CACHE STRING "Path to install LLVM utilities (enabled by LLVM_INSTALL_UTILS=ON) (defaults to LLVM_TOOLS_INSTALL_DIR)") mark_as_advanced(LLVM_UTILS_INSTALL_DIR) ``` ```cmake set(LLVM_EXAMPLES_INSTALL_DIR "examples" CACHE STRING "Path for examples subdirectory (enabled by LLVM_BUILD_EXAMPLES=ON) (defaults to 'examples')") mark_as_advanced(LLVM_EXAMPLES_INSTALL_DIR) ``` -------------------------------- ### Run Clang Basic Commands Source: https://github.com/llvm/llvm-project/blob/main/clang/www/get_started.html After building, add the build bin directory to your PATH and try out basic Clang commands. ```bash clang --help ``` ```bash clang file.c -fsyntax-only ``` ```bash clang file.c -S -emit-llvm -o - ``` ```bash clang file.c -S -emit-llvm -o - -O3 ``` ```bash clang file.c -S -O3 -o - ``` -------------------------------- ### Setup Test Environment Source: https://github.com/llvm/llvm-project/blob/main/llvm/test/Other/can-execute.txt Removes existing test directory and creates a new one. ```bash RUN: rm -rf %t && mkdir -p %t ``` -------------------------------- ### doInitialization Method Signature Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/WritingAnLLVMPass.md The doInitialization method is called once before processing any functions. It can be used for module-level setup, like getting references to global functions. ```cpp virtual bool doInitialization(Module &M); ``` -------------------------------- ### Format Examples Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/AMDGPUModifierSyntax.rst Demonstrates various ways to specify data and numeric formats for AMDGPU operations. ```assembly format:0 format:127 ``` ```assembly format:[BUF_DATA_FORMAT_16] ``` ```assembly format:[BUF_DATA_FORMAT_16,BUF_NUM_FORMAT_SSCALED] ``` ```assembly format:[BUF_NUM_FORMAT_FLOAT] ``` -------------------------------- ### LLVM Identifier Syntax Example Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/LangRef.rst Illustrates named and unnamed local identifiers in LLVM assembly language, which start with '%' and are used for registers or types. ```llvm %foo ``` ```llvm %a.really.long.identifier ``` ```llvm %12 ``` -------------------------------- ### Build, test, and install libc Source: https://github.com/llvm/llvm-project/blob/main/libc/docs/overlay_mode.rst Builds the libc library, runs its tests, and optionally installs the built static archive (libllvmlibc.a). ```sh $"> ninja libc $> ninja check-libc" $> ninja install-libc ``` -------------------------------- ### Starting LLDB and Loading JIT Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/DebuggingJITedCode.rst Launch LLDB, load the JIT-enabled execution engine (`lli`), and enable the JIT loader plugin. This prepares LLDB to debug JIT code. ```lldb lldb $BINPATH/lli (lldb) target create "/workspaces/llvm-project/build/bin/lli" Current executable set to '/workspaces/llvm-project/build/bin/lli' (x86_64). (lldb) settings set plugin.jit-loader.gdb.enable on ``` -------------------------------- ### Set up Python Virtual Environment with uv Source: https://github.com/llvm/llvm-project/blob/main/mlir/docs/Bindings/Python.md Alternative method for setting up a Python virtual environment using `uv`, a fast Python package installer. This example targets Python 3.12 and demonstrates `uv`'s capabilities for environment creation and package installation. ```shell uv venv ~/.venv/mlirdev --seed -p 3.12 source ~/.venv/mlirdev/bin/activate ``` -------------------------------- ### Setup IO Redirection for a Launched Program Source: https://github.com/llvm/llvm-project/blob/main/lldb/tools/lldb-dap/README.md Launches a program and redirects its standard input, output, and error streams to specified files. Use this for non-interactive debugging or capturing program output. ```javascript { "type": "lldb-dap", "request": "launch", "name": "Debug", "program": "/tmp/a.out", "stdio": ["in.txt", "out.txt"] } ``` -------------------------------- ### Get Resource Directory Paths Source: https://github.com/llvm/llvm-project/blob/main/runtimes/CMakeLists.txt Includes the GetClangResourceDir module to determine runtime output and installation paths based on the LLVM_TARGET_TRIPLE. This is used when LLVM_TREE_AVAILABLE is true. ```cmake if(LLVM_TREE_AVAILABLE) # Despite Clang in the name, get_clang_resource_dir does not depend on Clang # being added to the build. Flang uses the same resource dir as Clang. include(GetClangResourceDir) get_clang_resource_dir(RUNTIMES_OUTPUT_RESOURCE_DIR PREFIX "${LLVM_LIBRARY_OUTPUT_INTDIR}/..") get_clang_resource_dir(RUNTIMES_INSTALL_RESOURCE_PATH_DEFAULT) else() ``` -------------------------------- ### Buffer Format Syntax Example Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/AMDGPUModifierSyntax.rst Demonstrates the syntax for specifying buffer formats. ```text format:0 format:[BUF_FMT_32_UINT] ``` -------------------------------- ### PPCallbacks Example: SourceRangeSkipped Source: https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/docs/pp-trace.rst Shows the arguments for the SourceRangeSkipped callback, indicating a skipped source code range. It specifies the start and end of the skipped range. ```text - Callback: SourceRangeSkipped Range: [":/Clang/llvm/clang-tools-extra/test/pp-trace/pp-trace-macro.cpp:8:2", ":/Clang/llvm/clang-tools-extra/test/pp-trace/pp-trace-macro.cpp:9:2"] ``` -------------------------------- ### Basic CMake Project Setup for omptest Source: https://github.com/llvm/llvm-project/blob/main/openmp/tools/omptest/CMakeLists.txt Initializes the CMake version, project name, and languages for the omptest build. This is a standard starting point for CMake projects. ```cmake cmake_minimum_required(VERSION 3.20) project(omptest LANGUAGES CXX) ``` -------------------------------- ### Configure and Build Documentation with CMake Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/SphinxQuickstartTemplate.md Sets up the build environment using CMake and then builds the HTML documentation. ```console mkdir build cd build cmake -DLLVM_ENABLE_SPHINX=On ../llvm cmake --build . --target docs-llvm-html ``` -------------------------------- ### Setup and Diff Command for File Comparison Source: https://github.com/llvm/llvm-project/blob/main/llvm/utils/lit/tests/Inputs/shtest-shell/diff-error-4.txt This snippet demonstrates the setup of two temporary files with different content and the subsequent use of the 'diff' command to compare them. It's used to test error reporting when file contents diverge. ```shell RUN: echo "hello-first" > %t RUN: echo "hello-second" > %t1 RUN: diff %t %t1 ``` -------------------------------- ### LLVM Global Identifier Syntax Example Source: https://github.com/llvm/llvm-project/blob/main/llvm/docs/LangRef.rst Illustrates named and unnamed global identifiers in LLVM assembly language, which start with '@' and are used for functions or global variables. ```llvm @DivisionByZero ``` ```llvm @2 ```