Generating a BC File
A bitcode (BC) file is an intermediate representation (IR) binary generated by compiling source code using Low Level Virtual Machine (LLVM). It contains intermediate code for program compilation and is more compact and efficient than the source file. Based on the LLVM framework, the Kunpeng DevKit uses BC files to improve the program analysis efficiency and accuracy.
LLVM is an open source compiler infrastructure that provides a set of tools and libraries for building compilers, interpreters, debuggers, and optimizers of different languages.
- During vectorization check, do not specify the CC, CXX, and FC variables when using a project build tool to generate BC files.
- To ensure complete functions, you are advised to use the Clang tool provided by the Kunpeng DevKit as the compilation tool.
- If the BC file fails to be generated, modify the source code based on the error information in compiler logs or tool logs. For details about how to rectify errors, see Compatibility in the BiSheng Compiler User Guide.
Prerequisites
For projects used to generate BC files, ensure that LLVM can be used locally to compile and generate dynamic library (.so) files or executable files. (C/C++ compilers are Clang and Clang++, whereas the Fortran compiler is Flang.)
Clang is an open source front-end compiler that is used to compile programming languages such as C, C++, Objective C, and Objective C++ into machine code.
Method 1 (Recommended): Generating a BC File Using the Command Line Tool
Ensure that the command line tool of the Affinity Analyzer has been installed. For details about how to generate a BC file in the CLI, see Generating a BC File.
Method 2: Generating a BC File Using a Project Build Tool
- Replace the compilation command: If the build file contains gcc, g++, gfortran, or ld, replace them with clang, clang++, flang, or llvm-link.
- Adjust the compilation options: Set the tuning level to -O0 and add the compilation option -flto -g -fno-inline-functions.
After the preceding operations are complete, build the BC file.
- Before building a project, replace the GCC compiler with an LLVM tool.
- During the build, replace or add specified compile options to prevent the loss of some instruction information that affects the accuracy of analysis results.
- Change the generation target file to a BC file.
For example, use Make to build a project:
- Check that the Makefile file exists in the project.
1 2
[root@localhost test]# ls main.c Makefile test.c
- Modify the compile command and replace the compile options.
Original Makefile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
objects=main.o test.o exe=case CFLAGS = -O2 -DAM_CPU_NUMBER=96 -DMAX_PARALLEL_NUMBER=1 all: $(objects) main.o: ./main.c gcc $(CFLAGS) -c $< -o main.o test.o: ./test.c gcc $(CFLAGS) -c $< -o test.o all: $(exe) case: main.o test.o gcc -lpthread main.o test.o -o case clean: rm -f *.o
New Makefile:
objects=main.o test.o exe=case.bc CFLAGS = -O0 -flto -g -fno-inline-functions -DAM_CPU_NUMBER=96 -DMAX_PARALLEL_NUMBER=1 all: $(objects) main.o: ./main.c clang $(CFLAGS) -c $< -o main.o test.o: ./test.c clang $(CFLAGS) -c $< -o test.o all: $(exe) case.bc: main.o test.o llvm-link main.o test.o -o case.bc clean: rm -f *.oIn the new Makefile, the build tool is changed to Clang, the target file link and generation tool is changed to llvm-link.
- Complete the project build.
1 2 3 4 5 6
[root@localhost test]# make clang -O0 -flto -g -fno-inline-functions -DAM_CPU_NUMBER=96 -DMAX_PARALLEL_NUMBER=1 -c main.c -o main.o clang -O0 -flto -g -fno-inline-functions -DAM_CPU_NUMBER=96 -DMAX_PARALLEL_NUMBER=1 -c test.c -o test.o llvm-link main.o test.o -o case.bc [root@localhost test]# ls case.bc main.c main.o Makefile test.c test.o
The generated BC file is case.bc.
- Package the BC file.
- Create a directory.
1mkdir bcfile - Copy the BC file to the directory.
1cp case.bc bcfile/
- Package the BC file.
1tar -czvf bcfile.tar.gz bcfile
- Create a directory.