The BiSheng Compiler is a native compiler developed based on the open-source LLVM for C/C++ languages. It can compile and link C/C++ code projects into binary files that can run on devices. Compared with mainstream open-source compilers like LLVM and GCC in the industry, the BiSheng Compiler delivers stronger optimization capabilities without requiring changes to user code. This results in compiled and linked binary files with shorter runtime and fewer instructions, helping improve the smoothness of applications running on devices.
The BiSheng Compiler provides basic capabilities of compiling and linking C/C++ code projects into binary files that can run on devices, including:
Compiling capability: compiles C/C++ source files into assembly files, which refer to files written in assembly language.
Assembling capability: assembles assembly files into relocatable files, which are ELF-format binary files but cannot be directly executed on devices.
Linking capability: links one or more relocatable files together into an executable binary file.
Note: The BiSheng Compiler is developed based on the open-source LLVM. For details about common LLVM issues, see the official open-source document at https://llvm.org/docs/index.html.
The BiSheng Compiler outperforms LLVM and GCC in terms of the following:
The BiSheng Compiler provides optimization in scenario identification and structure transformation in loop compilation. For example, the BiSheng Compiler can identify the data dependency and the difference in the number of iterations between different code blocks in a loop, which further optimizes the loop distribution of the open-source LLVM compiler.
Compared with the open-source LLVM compiler, the BiSheng Compiler can not only perform vector conversion on more loops, but also select vector instructions more efficiently. As indicated by the following figure, the BiSheng Compiler outperforms the open-source LLVM compiler in binary code running efficiency by reducing the vector instructions from 5 to 2.
Using the BiSheng Compiler in DevEco Studio
If you use DevEco Studio 5.1.1 release or later, the BiSheng Compiler is used by default when you create a C/C++ project. When you open an existing C/C++ project, a dialog box is displayed. You can click Try Now to switch to the BiSheng Compiler to build the C/C++ code of the HarmonyOS project.
If you have obtained DevEco Studio 5.1.1 beta or earlier, the open-source LLVM compiler is used by default. You can use the BiSheng Compiler to build the C/C++ code of the HarmonyOS project by setting nativeCompiler to BiSheng in the project-level build-profile.json5 file of the HarmonyOS project when runtimeOS is set to HarmonyOS.
When you configure the pipeline to switch to the BiSheng Compiler, you may encounter an error indicating that the header file cannot be found. To resolve this, add the sysroot path option (replace xxx with the SDK path):
--sysroot=xxx/sdk/default/HarmonyOS/native/sysroot
Using the BiSheng Compiler in a Pipeline
If you use the SDK directly for building in a pipeline without using DevEco Studio, you can configure the BiSheng compiler as follows:
The DevEco Studio installation directory contains the hms and HarmonyOS directories:
The path of the BiSheng Compiler is xxx/sdk/default/hms/native/BiSheng.
You can switch to the BiSheng compiler in either of the following ways: replacing the compiler path in commands or replacing the compiler directory.
(1) Replacing the compiler path in commands
If the compiler path is specified in the script, for example, TOOLCHAIN_PATH="xxx/sdk/default/HarmonyOS/native/llvm/bin/clang", you can directly replace it with "xxx/sdk/default/hms/native/BiSheng/bin/clang".
Note: If the compiler path is a combined path and the sysroot path is affected, ensure that the sysroot path still points to the sysroot in HarmonyOS.
(2) Replacing the compiler directory
This method is the simplest and does not require any changes to scripts or environment variables. You only need to copy and rename the relevant directories as follows:
(1) Rename the llvm directory in xxx/sdk/default/HarmonyOS/native to **llvm-bak **(to facilitate subsequent restoration, or you can directly delete it).
(2) Copy the xxx/sdk/default/hms/native/BiSheng folder to the xxx/sdk/default/HarmonyOS/native directory.
(3) Rename BiSheng to llvm.
After the preceding steps are complete, restart the watermark level to use the BiSheng compiler for building.
IClang is an incremental compilation optimization technique. Building upon incremental compilation, it further enables function-level compilation reuse by leveraging previous compilation results, significantly improving compilation speed.
1. Usage instructions
To enable IClang, add the -iclang option during compilation. For example, in a CMake build system, add the following to CMakeLists.txt: add_compile_options(-iclang).
2. Data maintained by IClang
IClang automatically creates a .iclang subdirectory in the same directory as the target file to store its runtime data. For instance, during the compilation of test.o, a test.o.iclang directory is created.
3. IClang compilation modes
Since users typically modify only a subset of source files rather than the entire project, IClang generates reusable caches only for frequently modified source files. Specifically, compilation modes supported by IClang include:
(1) Initial mode: During the first compilation with -iclang, IClang maintains only lightweight metadata to avoid affecting the performance of the user's full compilation.
(2) Warm-up mode: When a file is modified and recompiled for the first time, IClang identifies it as a frequently modified target and performs necessary analysis and cache construction. This mode may slightly affect the performance of the first incremental compilation for that file. The generated products are stored in the .iclang directory.
(3) Incremental compilation mode: After a file has been warmed up, subsequent incremental compilations following modifications to that file are accelerated. This mode is particularly suitable for scenarios where a specific source file is modified frequently.
4. Cache invalidation
(1) The binary product generated in the last compilation is deleted. In this case, IClang cannot reuse the binary functions, and therefore falls back to the initial mode.
(2) The content or configuration of the header file changes. In this case, IClang cannot determine which functions can be safely reused, and therefore falls back to the initial mode.
(3) The compilation command changes. In this case, IClang aligns with the build system's behavior (which typically triggers a full rebuild).
If an internal error occurs, a .iclangtmp directory is created within the relevant product directory. Upon detecting this directory, IClang enters the recovery mode, fully falling back to Clang to ensure successful compilation. This behavior should be reported to the IClang developers for verification and fixing. If necessary, users can attempt to manually delete the .iclangtmp directory, after which IClang will switch to the initial mode.
5. Compilation option restrictions
(1) IClang only works with -c compilations where the command has exactly one input file and one output file—a condition met by common build systems like CMake.
(2) Currently, IClang only supports the -O0 optimization option, which must be used in debug-mode development environments. If other optimization options are detected, IClang falls back to Clang and issues a warning: [IClang Warning] Please ensure that IClang is only enabled under -O0.
(3) IClang cannot be used together with the -flto option.
(4) To ensure correct binary file reuse, IClang adds the -ffunction-sections and -fdata-sections compilation options, which segment the code and data sections of the binary file by symbol.
6. Frontend restrictions
For correctness, IClang may choose not to optimize functions containing certain syntax elements, including array instantiation types, function pointer instantiation types, virtual functions, and UniqueExternalLinkage.
7. Backend restrictions
The binary products generated by IClang adhere to the X86-64 ELF specification, with the following additional constraints: only 64-bit little-endian is supported; binary files contain only Rela relocation tables (Rel tables are not supported); the linker must be lld (gnu ld is not supported).
PGO is an adaptive optimization technique. It collects performance data from actual code execution to accurately identify information such as which functions are frequently executed or which branches are frequently taken, guiding the compiler to make targeted optimizations. Compared to traditional PGO, BiSheng PGO offers greater accuracy and optimization potential.
The BiSheng Compiler triggers file writes via signals: a dynamic shared object (DSO) writes sampling information to a file upon receiving a specific signal and resets the corresponding counters.
This approach allows precise control over the sampling scope, addressing a key limitation of traditional PGO which relies on unified entry/exit points in the service logic and cannot perform precise sampling for specific code ranges.
The BiSheng PGO workflow aligns with that of open-source LLVM, consisting of three stages: instrumentation, profiling, and optimization.
1. Instrumentation
Add the compilation option: -fprofile-generate=/data/storage/el2/base/files. This path is within the sandbox; corresponding files are generated in its mapped path: /data/app/el2/100/base/Application name/files.
You can verify successful instrumentation by checking the compiled file for the llvm_prf section or specific disassembly instruction sequences:
llvm-objdump -h xx.so # Check whether the llvm_prf section exists. llvm-objdump -d xx.so # Check disassembly for the ldr, add, store instruction sequence.
2. Profiling
Profile data collection is performed via lldb.
After compilation, push the APP package in debug mode using DevEco Studio.
A debug window should appear upon launch. Ensure it is in native debug mode; if not, modify Debug type to Native.


After the startup is complete, find the location of the native debug lldb. You need to set parameters in the lldb to ensure that the service runs properly and is not affected by some signals in debug mode. The signals are generally sent by the program itself.

Since the application may pause due to signals without breakpoints, you can disable the pause mechanism for signals.
View signals by running the command:
(lldb) process handle
Disable pausing for all signals by running the command:
(lldb) process handle SIG* -s false
(SIG* is the signal name. Omit the name to disable pausing for all signals at once.)
Send the signal directly by running the command:
(lldb) process signal SIGUSR2
You can find default_profile* files in the corresponding directory (/data/app/el2/100/base/Application process name/files). Each signal sent generates a file. The data from the first signal covers the period from application launch until the moment that signal is sent. The data from the second signal covers the period between the first and second signal triggers, with subsequent signals following the same pattern. You can control the timing of sending signals to collect data for specific scenarios. The generated files cannot be deleted due to permission restrictions. Since files generated during a single application launch share the same name, each new file overwrites the previous one, ensuring that the collected data corresponds to the target scenario.
Use hdc to copy the file from the device to your computer. Then, use the llvm-profdata.exe tool located in the same directory as clang to merge profiles by running llvm-profdata merge --output=lib.profdata default_*.profraw. This produces lib.profdata.
3. Optimization
Recompile the code, adding the compilation option -fprofile-use=lib.profdata, where lib.profdata is the merged profile file.
LTO is a compiler optimization performed at link time across compilation units, aimed at improving application performance. BiSheng LTO follows the same usage as the open-source LLVM but includes enhancements in areas such as instruction prefetching, inline algorithms, thin LTO function import algorithms, and PLT-inline optimization, further boosting the execution efficiency of the generated code.
1. Usage instructions
To enable LTO, add the -flto option during compilation. For example, in a CMake build system, add the following to CMakeLists.txt: add_compile_options(-flto).
2. Precautions
--flto: Enables the full LTO mode by default, which significantly increases compilation time but yields better performance.
--flto=thin: Switches to the thin LTO mode to reduce the compilation time, though with less performance gain compared with full LTO. This offers a balance between the two.
If build time still increases significantly, specify the number of parallel tasks to accelerate the build via -flto-jobs=xxx, where xxx should be set based on the number of CPU cores.
Floating-point optimization refers to a set of compiler options that control the semantics and behavior of floating-point operations. They govern whether the order of operations within floating-point expressions can be reassociated or combined to achieve higher performance or stronger predictability. The BiSheng Compiler disables this optimization feature by default. You can enable it for scenarios involving a substantial amount of floating-point operations.
1. Usage instructions
To enable floating-point optimization, add the -ffp-contract=on option during compilation. For example, in a CMake build system, add the following to CMakeLists.txt: add_compile_options( -ffp-contract=on).