"IT Talk" is a professional IT information and service platform under the Machinery Industry Press, dedicated to helping readers master more professional and practical knowledge and skills in the broad field of IT, quickly enhancing their competitiveness in the workplace. Click the blue WeChat name to quickly follow us!
How to Compile LLVM/Clang on ARM
ARM includes various CPUs and is mainly based on ARMv architecture chips. The Ubuntu Linux operating system is used on ARM boards.
It is best to build LLVM/Clang in release mode, as it consumes less memory. Otherwise, the build process is likely to fail due to insufficient memory. Building only the relevant backends (ARM and AArch64) is also much faster, as it is unlikely to use ARM development boards to cross-compile to other platforms. If running compiler-rt real-time compiler tests, the x86 backend must be included; otherwise, some tests will fail.
cmake $LLVM_SRC_DIR -DCMAKE_BUILD_TYPE=Release \ -DLLVM_TARGETS_TO_BUILD="ARM;X86;AArch64"
If building LLVM/Clang on ARM development boards with 1G or less memory, use gold instead of GNU ld. In any case, setting up a swap partition may be a good idea.
$ sudo ln -sf /usr/bin/ld /usr/bin/ld.gold
# The following code requires the cpufrequtils package. for ((cpu=0; cpu<`grep -c proc /proc/cpuinfo`; cpu++)); do sudo cpufreq-set -c $cpu -g performance done
(4) USB Drive Assistance
Make sure there is a decent power supply that can provide at least 4A of current, especially important when using USB devices on development boards. An external powered USB/SATA hard drive is even better than a high-performance power supply.
The LLVM Pass framework is an important part of the LLVM system, as LLVM Pass is where most interesting parts of the compiler are stored. Passes build the analysis results used for transformations through structured techniques in the compiler code, and then execute the transformations and optimizations that make up the compiler.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
2. Quick Start – Writing a HelloWorld Program
Now that the build process for the new Pass is set up, you just need to write the code.
First, define the Pass in the header file by creating llvm/include/llvm/Transforms/Utils/HelloWorld.h. This file should include the following template file:
#ifndef LLVM_TRANSFORMS_HELLONEW_HELLOWORLD_H#define LLVM_TRANSFORMS_HELLONEW_HELLOWORLD_H#include "llvm/IR/PassManager.h"namespace llvm {class HelloWorldPass : public PassInfoMixin<HelloWorldPass> {public:PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);};} // namespace llvm#endif // LLVM_TRANSFORMS_HELLONEW_HELLOWORLD_H
#include "llvm/Transforms/Utils/HelloWorld.h"
Include the header file just created.
using namespace llvm;
This step is necessary because the functions in the include file are in the LLVM namespace and can only be completed outside of header files. Then define the run method for the Pass, as shown in the following code:
PreservedAnalysesHelloWorldPass::run(Function &F,FunctionAnalysisManager &AM) { errs() << F.getName() << "\n"; return PreservedAnalyses::all();}
FUNCTION_PASS content added to llvm/lib/Passes/PassRegistry.def. FUNCTION_PASS("helloworld", HelloWorldPass())
The Pass is added under the name helloworld.
For various reasons, llvm/lib/Passes/PassRegistry.def is included in llvm/lib/Passes/PassBuilder.cpp multiple times. Since the Pass is constructed, it is necessary to add the appropriate #include in llvm/lib/Passes/PassBuilder.cpp.
#include "llvm/Transforms/Utils/HelloWorld.h"
Now that there is a brand new Pass, you can build optimizations and run some LLVM IR in the Pass. The implementation code is as follows:
$ ninja -C build/opt# or any build system/build directory being used$ cat /tmp/a.lldefine i32 @foo() { %a = add i32 2, 3 ret i32 %a}define void @bar() { ret void}$ build/bin/opt -disable-output /tmp/a.ll -passes=helloworldfoobar
Testing the Pass is very important to prevent regressions. A lit test will be added in llvm/test/Transforms/Utils/helloworld.ll.
The implementation code is as follows:
$ cat llvm/test/Transforms/Utils/helloworld.ll; RUN: opt -disable-output -passes=helloworld %s 2>&1 | FileCheck %s; CHECK: {{^}}foo{{$}}define i32 @foo() { %a = add i32 2, 3 ret i32 %a}; CHECK-NEXT: {{^}}bar{{$}}define void @bar() { ret void}$ ninja -C build check-llvm
class HelloWorldPass : public PassInfoMixin<HelloWorldPass> {public:PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); static bool isRequired() { return true; }};
A required Pass cannot be skipped, and an example of a required Pass is AlwaysInlinerPass, which must always run to preserve the alwaysinline semantics of the program. Additionally, the Pass manager is required as it may contain other required Passes.
To skip a Pass, a typical example is the optnone function attribute, which specifies that optimizations should not run on the function. However, required Passes will still run on optnone functions.
LLVM is a research project at the University of Illinois that provides a modern, SSA-based compilation strategy and can support compilation targets for any programming language, both statically and dynamically. LLVM consists of various sub-projects, many of which are used in production in commercial and open-source projects. It is also widely used in academic research.
This book aims to combine theoretical knowledge of LLVM with practical case studies, helping readers understand how LLVM works while optimizing and deploying LLVM according to application and device needs. The book includes numerous examples and code snippets to help readers master the LLVM compiler development environment.
The book consists of 11 chapters, including compiling and installing LLVM, LLVM external projects, LLVM compilers, basics of the Clang frontend, Clang architecture and practical examples, LLVM IR practice, LLVM chip compiler practical examples, LLVM compiler example code analysis, LLVM optimization examples, LLVM backend practice, and MLIR compiler.
This book is suitable for engineering and technical personnel, teachers and students in universities, researchers, and technical managers in fields such as algorithms, software, compilers, artificial intelligence, and hardware.
Author: Ji Xu
Editor: Zhang Shuqian
Reviewer: Cao Xinyu