### Build Example Setup Source: https://github.com/intel/llvm/blob/sycl/libc/examples/README.md Basic steps to set up a 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 ``` -------------------------------- ### Example Usage: Creating and Installing LLVM Xcode Toolchain Source: https://github.com/intel/llvm/blob/sycl/llvm/tools/xcode-toolchain/CMakeLists.txt Demonstrates the command-line usage for configuring, building, and installing an LLVM Xcode toolchain using CMake and Ninja. It also shows how to set environment variables to use the installed toolchain. ```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 ``` -------------------------------- ### Benchmark Setup and Teardown Example Source: https://github.com/intel/llvm/blob/sycl/third-party/benchmark/docs/user_guide.md Define and register custom setup and teardown functions for benchmarks. These functions are invoked once per benchmark run, respecting thread counts and argument variations. ```c++ static void DoSetup(const benchmark::State& state) { } static void DoTeardown(const benchmark::State& state) { } static void BM_func(benchmark::State& state) {...} BENCHMARK(BM_func)->Arg(1)->Arg(3)->Threads(16)->Threads(32)->Setup(DoSetup)->Teardown(DoTeardown); ``` -------------------------------- ### Example Command Line Build Source: https://github.com/intel/llvm/blob/sycl/bolt/docs/ORCv2.md Illustrates the command-line steps to build dynamic libraries and a main application, mirroring the ORCv2 JIT setup. ```bash $ clang++ -shared -o libA.dylib a1.cpp a2.cpp $ clang++ -shared -o libB.dylib b1.cpp b2.cpp $ clang++ -o myapp myapp.cpp -L. -lA -lB $ ./myapp ``` -------------------------------- ### KeepEmptyLines Configuration Example Source: https://github.com/intel/llvm/blob/sycl/bolt/docs/ClangFormatStyleOptions.md Illustrates the configuration for keeping empty lines within a file. This example shows how to disable empty lines at the end of the file, start of blocks, and start of the file. ```c++ KeepEmptyLines: AtEndOfFile: false AtStartOfFile: false AtStartOfBlock: false ``` -------------------------------- ### Build and Test (Component Build) Source: https://github.com/intel/llvm/blob/sycl/mlir/examples/standalone/README.md Use this command to build the example as a component and run its tests. Ensure LLVM and MLIR are built and installed in the specified directories. ```sh mkdir build && cd build cmake -G Ninja .. -DMLIR_DIR=$PREFIX/lib/cmake/mlir -DLLVM_EXTERNAL_LIT=$BUILD_DIR/bin/llvm-lit cmake --build . --target check-standalone ``` -------------------------------- ### Conditional Header Install Target Setup Source: https://github.com/intel/llvm/blob/sycl/libc/lib/CMakeLists.txt Sets the header install target variable if a full build of LLVM libc is enabled. ```cmake set(header_install_target "") if(LLVM_LIBC_FULL_BUILD) set(header_install_target install-libc-headers) endif() ``` -------------------------------- ### Create Root Directory and Navigate Source: https://github.com/intel/llvm/blob/sycl/bolt/docs/GettingStarted.md Sets up the necessary directory structure for the build process. Ensure you are in the desired parent directory before executing. ```bash mkdir root cd root ``` -------------------------------- ### Get All Installable Header Targets Source: https://github.com/intel/llvm/blob/sycl/libc/include/CMakeLists.txt A recursive function to collect all header targets that are marked for installation. It traverses the dependency tree of header targets. ```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) ``` -------------------------------- ### TableGenBreakingDAGArgOperators Example Source: https://github.com/intel/llvm/blob/sycl/bolt/docs/ClangFormatStyleOptions.md Limits line breaks within DAGArgs to those starting with specified identifiers when TableGenBreakInsideDAGArg is not DontBreak. This example shows breaking only for 'ins' and 'outs'. ```yaml TableGenBreakInsideDAGArg: BreakAll TableGenBreakingDAGArgOperators: [ins, outs] ``` -------------------------------- ### Example: Partitioning GPU sub-devices Source: https://github.com/intel/llvm/blob/sycl/lld/docs/EnvironmentVariables.md Demonstrates selecting all sub-devices of all GPU devices across all backends. ```bash export ONEAPI_DEVICE_SELECTOR="*.*" ``` -------------------------------- ### Simplify Setup with Helper Script Source: https://github.com/intel/llvm/blob/sycl/xptifw/samples/sycl_perf_collector/README.md Use the sycl-perf.sh helper script to automate setup, which requires setting XPTI_PER_DIR to the directory containing the dispatcher and subscriber shared objects. ```bash export XPTI_PER_DIR=/path/to/lib ``` -------------------------------- ### Get Target Triple Example Source: https://github.com/intel/llvm/blob/sycl/bolt/docs/tutorial/MyFirstLanguageFrontend/LangImpl08.md Demonstrates how to get the current machine's target triple using clang. This is useful for understanding the target environment. ```shell $ clang --version | grep Target Target: x86_64-unknown-linux-gnu ``` -------------------------------- ### Compile and Run ESIMD Binary Function Example Source: https://github.com/intel/llvm/blob/sycl/sycl/doc/extensions/supported/sycl_ext_intel_esimd/examples/bfn.md Commands to compile and execute the SYCL ESIMD binary function example. Ensure you have the necessary toolchain installed. ```bash > clang++ -fsycl bfn.cpp > ONEAPI_DEVICE_SELECTOR=level_zero:gpu ./a.out ``` -------------------------------- ### Example: Selecting all sub-devices of partitionable GPUs in Level Zero backend Source: https://github.com/intel/llvm/blob/sycl/llvm/docs/EnvironmentVariables.md This example selects all sub-devices of partitionable GPUs specifically within the Level Zero backend. ```bash export ONEAPI_DEVICE_SELECTOR=level_zero:*.* ``` -------------------------------- ### Good Error Message Example Source: https://github.com/intel/llvm/blob/sycl/bolt/docs/CodingStandards.md An example of a clear and informative error message. It starts with a lowercase letter and avoids a trailing period if the sentence naturally ends with other punctuation. ```none error: file.o: section header 3 is corrupt. Size is 10 when it should be 20 ``` -------------------------------- ### Basic Hello World Example Source: https://github.com/intel/llvm/blob/sycl/clang-tools-extra/docs/JITLink.md Demonstrates a simple 'hello, world!' program compiled and executed using llvm-jitlink. Requires a C source file and compilation to an object file. ```c #include int main(int argc, char *argv[]) { printf("hello, world!\n"); return 0; } ``` ```sh % clang -c -o hello-world.o hello-world.c % llvm-jitlink hello-world.o ``` -------------------------------- ### Multithreaded Benchmark Setup Source: https://github.com/intel/llvm/blob/sycl/third-party/benchmark/docs/user_guide.md Use `state.thread_index() == 0` to perform setup and teardown only once in multithreaded benchmarks. Ensure all threads reach the start before proceeding and finish before exiting. ```c++ static void BM_MultiThreaded(benchmark::State& state) { if (state.thread_index() == 0) { // Setup code here. } for (auto _ : state) { // Run the test as normal. } if (state.thread_index() == 0) { // Teardown code here. } } BENCHMARK(BM_MultiThreaded)->Threads(2); ``` -------------------------------- ### Build QEMU and Linux Kernel Images Source: https://github.com/intel/llvm/blob/sycl/lldb/docs/resources/qemu-testing.rst Use setup.sh to build QEMU binaries and/or Linux kernel images for Arm or AArch64. This script can also be used to clean up build artifacts. ```bash bash setup.sh --qemu --kernel arm bash setup.sh --qemu --kernel arm64 ``` ```bash bash setup.sh --kernel arm bash setup.sh --kernel arm64 ``` ```bash bash setup.sh --qemu ``` ```bash bash setup.sh --clean ``` -------------------------------- ### Build Command for the Example Source: https://github.com/intel/llvm/blob/sycl/bolt/docs/tutorial/MyFirstLanguageFrontend/LangImpl04.md The command to compile the complete example code, including LLVM JIT and optimizer flags. ```bash # Compile clang++ -g toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core orcjit native` -O3 -o toy ``` -------------------------------- ### Command-line Compilation Example Source: https://github.com/intel/llvm/blob/sycl/lld/docs/ORCv2.md This bash script demonstrates the command-line compilation steps for creating shared libraries and a main executable, mirroring the setup for the ORCv2 C++ example. ```bash $ clang++ -shared -o libA.dylib a1.cpp a2.cpp $ clang++ -shared -o libB.dylib b1.cpp b2.cpp $ clang++ -o myapp myapp.cpp -L. -lA -lB $ ./myapp ``` -------------------------------- ### Tail Call Optimization Example Source: https://github.com/intel/llvm/blob/sycl/bolt/docs/CodeGenerator.md Example demonstrating tail call optimization setup using 'llc -tailcallopt'. It shows a tailcallee function and a tailcaller function that utilizes it. ```llvm declare fastcc i32 @tailcallee(i32 inreg %a1, i32 inreg %a2, i32 %a3, i32 %a4) define fastcc i32 @tailcaller(i32 %in1, i32 %in2) { %l1 = add i32 %in1, %in2 %tmp = tail call fastcc i32 @tailcallee(i32 inreg %in1, i32 inreg %in2, i32 %in1, i32 %l1) ret i32 %tmp } ``` -------------------------------- ### Setup Local LNT Instance and Server Source: https://github.com/intel/llvm/blob/sycl/libcxx/utils/ci/lnt/README.md Steps to create a local LNT instance, configure authentication, and run the LNT server. This involves creating an instance, setting the API token, and starting the 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 ``` -------------------------------- ### Basic Hello World Example Source: https://github.com/intel/llvm/blob/sycl/flang/docs/JITLink.md Demonstrates the basic usage of llvm-jitlink by compiling and running a simple C program. This involves creating a C file, compiling it to an object file, and then linking and executing it with llvm-jitlink. ```c #include int main(int argc, char *argv[]) { printf("hello, world!\n"); return 0; } ``` ```sh clang -c -o hello-world.o hello-world.c lvvm-jitlink hello-world.o ``` -------------------------------- ### Explicit Hardware Subset Examples Source: https://github.com/intel/llvm/blob/sycl/llvm/docs/design/Runtimes.md Illustrates how to define an explicit hardware subset using KMP_HW_SUBSET. These examples specify exact counts and starting points for sockets, cores, and threads. ```bash KMP_HW_SUBSET=:2s,6t ``` ```bash KMP_HW_SUBSET=:1t@7 ``` ```bash KMP_HW_SUBSET=:5c,1t ``` -------------------------------- ### ORCv2 JIT Setup and Compilation Source: https://github.com/intel/llvm/blob/sycl/bolt/docs/ORCv2.md Demonstrates setting up ORCv2's JIT environment, creating JITDylibs for libraries and the main application, and adding compiled code. ```c++ ExecutionSession ES; RTDyldObjectLinkingLayer ObjLinkingLayer( ES, []() { return std::make_unique(); }); CXXCompileLayer CXXLayer(ES, ObjLinkingLayer); // Create JITDylib "A" and add code to it using the CXX layer. auto &LibA = ES.createJITDylib("A"); CXXLayer.add(LibA, MemoryBuffer::getFile("a1.cpp")); CXXLayer.add(LibA, MemoryBuffer::getFile("a2.cpp")); // Create JITDylib "B" and add code to it using the CXX layer. auto &LibB = ES.createJITDylib("B"); CXXLayer.add(LibB, MemoryBuffer::getFile("b1.cpp")); CXXLayer.add(LibB, MemoryBuffer::getFile("b2.cpp")); // Create and specify the search order for the main JITDylib. This is // equivalent to a "links against" relationship in a command-line link. auto &MainJD = ES.createJITDylib("main"); MainJD.addToLinkOrder(&LibA); MainJD.addToLinkOrder(&LibB); CXXLayer.add(MainJD, MemoryBuffer::getFile("main.cpp")); // Look up the JIT'd main, cast it to a function pointer, then call it. auto MainSym = ExitOnErr(ES.lookup({&MainJD}, "main")); auto *Main = MainSym.getAddress().toPtr(); int Result = Main(...); ``` -------------------------------- ### Example: Selecting All Devices Except CUDA Source: https://github.com/intel/llvm/blob/sycl/polly/docs/EnvironmentVariables.md This example shows how to accept all available devices but explicitly discard any devices associated with the CUDA backend. This is useful when you want to use most devices but avoid CUDA. ```bash export ONEAPI_DEVICE_SELECTOR="*:*;!cuda:*" ``` -------------------------------- ### Create and Navigate to Build Directory Source: https://github.com/intel/llvm/blob/sycl/libc/config/windows/README.md Create an empty directory for building LLVM libc and change into it. ```bash mkdir libc-build cd libc-build ``` -------------------------------- ### Example of llvm.vector.reduce.fadd.v4f32 sequential reduction Source: https://github.com/intel/llvm/blob/sycl/clang-tools-extra/docs/LangRef.md Demonstrates a sequential reduction using llvm.vector.reduce.fadd.v4f32 with a start value. ```default %ord = call float @llvm.vector.reduce.fadd.v4f32(float %start_value, <4 x float> %input) ; sequential reduction ``` -------------------------------- ### Example of !size Source: https://github.com/intel/llvm/blob/sycl/llvm/docs/TableGen/ProgRef.md Demonstrates the !size bang operator for getting the number of elements in a list or string. ```tablegen def MyClass : MyBase<"MyClass"> { string Name = "MyClass"; // Get the size of the 'Name' string. int NameSize = !size(Name); } ``` -------------------------------- ### Example: Calling a private member function disables the check Source: https://github.com/intel/llvm/blob/sycl/bolt/docs/clang-tidy/checks/readability/make-member-function-const.md This example demonstrates a scenario where calling a private member function prevents the `readability-make-member-function-const` check from suggesting a `const` qualifier for the `get` method. ```C++ class E1 { Pimpl &getPimpl() const; public: int &get() { // Calling a private member function disables this check. return getPimpl()->i; } ... }; ``` -------------------------------- ### Hello World Example with llvm-jitlink Source: https://github.com/intel/llvm/blob/sycl/clang/docs/JITLink.md Demonstrates basic usage of llvm-jitlink by compiling and running a simple C program. Ensure clang and llvm-jitlink are in your PATH. ```c #include int main(int argc, char *argv[]) { printf("hello, world!\n"); return 0; } ``` ```sh % clang -c -o hello-world.o hello-world.c % llvm-jitlink hello-world.o Hello, World! ``` -------------------------------- ### Build Stand-alone Clang Example Source: https://github.com/intel/llvm/blob/sycl/libc/docs/GettingStarted.md A complete shell script demonstrating the process of building LLVM, installing it, and then configuring and building Clang as a stand-alone project. Sets build directories and installation prefixes. ```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 ``` -------------------------------- ### Selecting Sub-Sub-Devices Source: https://github.com/intel/llvm/blob/sycl/bolt/docs/EnvironmentVariables.md This example demonstrates selecting sub-sub-devices from the first device in the Level Zero backend. This allows for a deeper level of partitioning, accessing the smallest available compute units. ```bash export ONEAPI_DEVICE_SELECTOR=level_zero:0.*.* ``` -------------------------------- ### Setting OpenMP Header Install Location Source: https://github.com/intel/llvm/blob/sycl/openmp/CMakeLists.txt Determines the installation path for OpenMP headers based on whether LLVM_TREE is available. If not, it uses the default include directory; otherwise, it gets the Clang resource directory. ```cmake if(NOT LLVM_TREE_AVAILABLE) set(LIBOMP_HEADERS_INSTALL_PATH "${CMAKE_INSTALL_INCLUDEDIR}") else() include(GetClangResourceDir) get_clang_resource_dir(LIBOMP_HEADERS_INSTALL_PATH SUBDIR include) endif() ``` -------------------------------- ### Basic Hello World Example with llvm-jitlink Source: https://github.com/intel/llvm/blob/sycl/bolt/docs/JITLink.md Demonstrates the basic usage of `llvm-jitlink` by compiling and executing a simple C program. Ensure `clang` is available for compilation. ```c #include int main(int argc, char *argv[]) { printf("hello, world!\n"); return 0; } ``` ```sh % clang -c -o hello-world.o hello-world.c % llvm-jitlink hello-world.o ``` -------------------------------- ### FileCheck Empty Regex Match at Start Source: https://github.com/intel/llvm/blob/sycl/llvm/test/FileCheck/empty-regex-match-at-start.txt This example shows FileCheck matching an empty regex at the start of a line. The {{^}} anchor asserts the position at the beginning of the line, and the empty regex matches zero characters there. ```FileCheck RUN: FileCheck %s --check-prefix=NEXT --input-file=%s NEXT: {{^}} NEXT-NEXT: more text ``` -------------------------------- ### Complete Workflow Example: Memory Access Profiler Source: https://github.com/intel/llvm/blob/sycl/libcxx/docs/Instrumentor.md Demonstrates a full workflow for creating a memory access profiler. This includes generating a configuration, implementing a runtime, instrumenting the code, and compiling the final program. ```bash ./llvm/utils/instrumentor-config-wizard.py -o memory_profiler.json # In the wizard: # - Enable: load, store # - Use same config for PRE/POST: yes # - Base config: keep defaults # - For load/store: enable pointer, value_size, id # - Generate stubs: yes (memory_profiler_stubs.c) ``` ```c // memory_runtime.c #include #include static uint64_t load_count = 0; static uint64_t store_count = 0; void __instrumentor_pre_load(void *pointer, uint64_t value_size, int32_t id) { load_count++; printf("Load from %p (size: %lu, id: %d)\n", pointer, value_size, id); } void __instrumentor_pre_store(void *pointer, uint64_t value_size, int32_t id) { store_count++; printf("Store to %p (size: %lu, id: %d)\n", pointer, value_size, id); } __attribute__((destructor)) void print_stats(void) { printf("Total loads: %lu\n", load_count); printf("Total stores: %lu\n", store_count); } ``` ```bash # Instrument the program clang -emit-llvm -S -o program.ll program.c opt -passes=instrumentor \ -instrumentor-read-config-file=memory_profiler.json \ program.ll -S -o program_inst.ll # Compile with runtime clang program_inst.ll memory_runtime.c -o program ``` ```bash # Run and observe: ./program # Output includes: # Load from 0x7ffc12345678 (size: 4, id: 1) # Store to 0x7ffc12345680 (size: 8, id: 2) # ... # Total loads: 42 # Total stores: 27 ``` -------------------------------- ### Alias Query Example Source: https://github.com/intel/llvm/blob/sycl/llvm/docs/AliasAnalysis.md Demonstrates how memory objects are represented as a starting address and size for alias queries. ```c++ int i; char C[2]; char A[10]; /* ... */ for (i = 0; i != 10; ++i) { C[0] = A[i]; /* One byte store */ C[1] = A[9-i]; /* One byte store */ } ``` -------------------------------- ### Create and Build Program Source: https://github.com/intel/llvm/blob/sycl/unified-runtime/scripts/core/PROG.rst Demonstrates the creation of a program from Intermediate Language (IL) binary and its subsequent build process. ```c ${x}ProgramCreateWithIL(hContext, ILBin, ILBinSize, nullptr, &hProgram); // Build the program. ${x}ProgramBuild(hContext, hProgram, nullptr); ```