An optimization method for RISC-V basic C library

By using compiler predefined macros to check vector instruction set support on RISC-V, and using the basic instruction set and RVV extension instruction set respectively to optimize the string operation functions of the basic C library, the existing basic C library has solved the problem of weak running performance on RISC-V, and achieved more efficient performance and efficiency.

CN116860256BActive Publication Date: 2025-04-01INST OF SOFTWARE - CHINESE ACAD OF SCI +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310848297.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-07-11
Publication Date
2025-04-01
Estimated Expiration
2043-07-11

AI Technical Summary

Technical Problem

The existing basic C library has weak running performance on the RISC-V instruction set and has failed to fully utilize the performance advantages of the RISC-V instruction set, especially when executing SIMD instructions, it cannot utilize the vector register V register.

Method used

The compiler predefined macro __riscv_vector is used to check whether vector instruction set extensions are supported, string operation functions are implemented using the basic instruction set and RVV extension instruction set respectively, and these functions are optimized through compilation optimization methods and vectorized programming techniques.

Benefits of technology

The operation performance and efficiency of the basic C library on RISC-V is improved, and the basic instructions of the same function are realized coexist with the RVV instruction assembly, making full use of the performance characteristics of the RISC-V instruction set.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116860256B_ABST
    Figure CN116860256B_ABST
Patent Text Reader

Abstract

The present invention discloses an optimization method for RISC-V basic C library, which belongs to the field of computer software technology. The method adopts compiler predefined macros to realize the compatibility of basic instruction set and RVV extended instruction set, focuses on optimizing the string operation function of the basic C library, and realizes the assembly implementation containing only the basic instruction set and the RVV instruction set respectively. The string operation function realized by the basic instruction set adopts optimization methods such as fine-grained data partitioning, address alignment, loop expansion, address jump, magic number, etc. to improve the performance and efficiency of the function. The string operation function realized by the RVV extended instruction set adopts optimization methods such as address alignment and vectorization to improve the execution efficiency of the basic C library function.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of computer software technology, and specifically proposes an optimization method for a RISC-V basic C library. Background Art

[0002] RISC-V is an open source instruction set architecture (ISA) based on Reduced Instruction Set Computing (RISC), led by David Patterson, Krste Asanovic and others from the University of California, Berkeley. The RISC-V architecture has the characteristics of high scalability, flexibility, and freedom and openness, which has attracted widespread attention in the computer field. RISC-V has not only received high praise in the academic community, but also received strong attention from all walks of life, and is considered to be an important direction for future processors.

[0003] RISC-V has a series of technical features, the most notable of which is its modular function, which allows different subsets to be used to implement the RISC-V core for different application scenarios. In addition, RISC-V also has the characteristics of high performance, low power consumption, easy design and debugging, and can be widely used in embedded systems, servers and other fields. In addition, the RISC-V instruction set also follows the open principle and can be freely accessed and extended, which means that anyone can use it and can develop and optimize it according to their own needs.

[0004] RISC-V is also developing very rapidly. Since its first public appearance in 2010, RISC-V has been supported by many companies and organizations, such as Intel, Apple, Google, ARM, Huawei, etc., and has also been widely used in various fields, such as mobile devices, Internet of Things, artificial intelligence, etc. In addition, in recent years, the global investment in processor technology has also been increasing, which has promoted the rapid development of RISC-V instruction set architecture. It can be foreseen that with the continuous improvement and development of RISC-V, it will be more and more valued in the field of computer technology in the future.

[0005] In modern computer systems, C language is a widely used programming language, and the basic C library is a general programming library that provides a series of commonly used functions and constants, providing programmers with a convenient programming interface. The operating efficiency of the basic C library has an important impact on the performance of the program, which is especially important for applications such as large-scale data computing and multimedia processing. However, on the RISC-V instruction set, the existing basic C library cannot fully utilize its performance advantages because the RISC-V instruction set is different from other instruction set architectures. For example, when executing SIMD instructions, the existing basic C library does not fully utilize the vector register V register in the RISC-V instruction set, resulting in the inability to take advantage of SIMD instructions. In addition, the operating performance of the existing basic C library on RISC-V is relatively weak, resulting in some applications that require high-performance computing being unable to fully utilize the advantages of the RISC-V instruction set.

[0006] In order to solve these problems, a new method of accelerating the basic C library is needed to make full use of the performance characteristics of the RISC-V instruction set and improve the performance and efficiency of applications. Vectorized programming technology can be used to optimize the basic C library. By rewriting data processing functions into vectorized code, the existing basic C library can give full play to the advantages of SIMD instructions in the RISC-V instruction set, thereby improving the running performance of the basic C library on RISC-V. Summary of the invention

[0007] In view of the technical problems existing in the prior art, the purpose of the present invention is to propose an optimization method for the RISC-V basic C library. The method uses compiler predefined macros to achieve compatibility between the basic instruction set and the RVV extended instruction set. The string operation function of the basic C library is optimized, and the assembly implementation containing only the basic instruction set and the RVV extended instruction set is realized respectively, and the string operation function implemented by the basic instruction set and the string operation function implemented by the RVV extended instruction set are optimized accordingly, so as to improve the performance and execution efficiency of the basic C library function.

[0008] To achieve the above object, the present invention adopts the following technical solutions:

[0009] An optimization method for a RISC-V basic C library comprises the following steps:

[0010] 1) In the RISC-V architecture, use the compiler predefined macro __riscv_vector to check whether the vector instruction set extension is supported during the compilation phase. If not, perform the following step 2); if supported, perform the following step 3);

[0011] 2) Use the basic instruction set to implement the string operation function of the basic C library, and use the compilation optimization method to optimize the string operation function;

[0012] 3) Use the RVV extended instruction set to implement vectorized programming of the string operation functions of the basic C library and optimize the string operation functions.

[0013] Further, in step 1), using the compiler predefined macro __riscv_vector to check whether the vector instruction set extension is supported during the compilation stage refers to checking whether the __riscv_vector macro is defined. If the __riscv_vector macro is defined, the compilation environment supports the vector instruction set extension; if the __riscv_vector macro is not defined, the compilation environment does not support the vector instruction set extension.

[0014] Furthermore, the string operation functions in steps 2) and 3) include strlen, memset, memcpy and memmove.

[0015] Furthermore, the compilation optimization method in step 2) includes optimization methods of fine-grained data partitioning, address alignment, loop unrolling, address jumping and magic number.

[0016] Furthermore, the optimization steps of the fine-grained data partitioning optimization method in step 2) include: dividing the memory into blocks of fixed size; for memset, memcpy, and memmove functions, reading the function input data volume, and processing different data volumes in batches.

[0017] Furthermore, the optimization steps of the double pointer optimization method in step 2) include: for the memset function, when the data amount is less than a set threshold and tail processing, calculate the first address and the last address of the storage, store data from both ends and judge the data amount to prevent the address from crossing the boundary until the storage is completed.

[0018] Furthermore, the optimization steps of the address alignment optimization method in step 2) include: for strlen, memset, memcpy, and memmove functions, determining whether the first address is 8-byte aligned; if not, processing to 8-byte address alignment through byte operation instructions, and processing data by word size for the core loop.

[0019] Furthermore, the optimization steps of the loop unrolling optimization method in step 2) include: putting multiple operations of the same type into one loop for execution; and for the memset function, unrolling the storage loop segment 32 times for execution.

[0020] Furthermore, the optimization steps of the address jump optimization method in step 2) include: for the memset function, for the amount of data that cannot be stored 32 times at the same time, calculating the number of times to be expanded and the offset from the first address of the loop segment instruction, and directly jumping to the loop to execute.

[0021] Furthermore, the optimization steps of the magic number optimization method in step 2) include: for the strlen function, using the 64-bit magic number and the data to perform a logical operation to find the terminator.

[0022] Furthermore, in step 3), the string operation function is optimized by using an address alignment optimization method, and the optimization steps include: for strlen, memset, memcpy, and memmove functions, reading the vlenb register to obtain the vector register bit width, and determining whether the storage first address is aligned according to the vector register bit width. If not, the data is operated byte by byte until the address is aligned according to the vector register bit width.

[0023] Furthermore, in step 3), a vectorized optimization method is used to optimize the string operation function, and the optimization steps include:

[0024] Set the vl register to the maximum vector length;

[0025] For the strlen function, the vle8.v instruction in the RVV extended instruction set is used to load data from the memory into the vector register, and then the vfirst.m instruction is used to find the terminator element and send the position information to the scalar register. Finally, these position information are merged in the scalar register to get the length of the string.

[0026] For the memset function, use the vmv.vx and vse8.v instructions to store the vector data into memory;

[0027] For memcpy and memmove functions, use vle8.v to load the data in memory into the vector register, and then use vse8.v to store the data in memory.

[0028] Compared with the prior art, the present invention has the following positive effects:

[0029] (1) The present invention allows developers to freely choose to compile and generate a basic C library for basic instruction implementation or a basic C library for RVV instruction implementation based on hardware and compiler support, thereby achieving the coexistence of basic instruction and RVV instruction compilation for the same function. Currently, there is no technology focusing on the support of the basic C library in this regard.

[0030] (2) The present invention specifically optimizes the problem that the basic C library has relatively weak running performance on RISC-V and fails to fully utilize the advantages of the RISC-V instruction set. Compared with the traditional basic C library, the execution efficiency is higher. BRIEF DESCRIPTION OF THE DRAWINGS

[0031] Figure 1 This is a flow chart of an optimization method for the RISC-V basic C library proposed in the present invention.

[0032] Figure 2 It is the basic instruction to implement the memset function flow chart.

[0033] Figure 3 This is the flow chart of the memset function implemented by the RVV instruction. DETAILED DESCRIPTION

[0034] In order to make the various technical features and advantages or technical effects in the above technical solutions of the present invention more obvious and easy to understand, they are described in detail below with reference to the accompanying drawings.

[0035] The present invention proposes an optimization method for RISC-V basic C library, and its optimization process is as follows Figure 1 The specific optimization steps are described in detail as follows:

[0036] 1. RVV compatible solution

[0037] Due to the scalability of the RISC-V instruction set, this results in the inability of functions optimized for vector instructions to run on hardware that does not support vector extensions. At present, the hwcap (hardware capabilities) mechanism is commonly used in Linux systems to detect and identify the characteristics and functions of hardware. The hwcap mechanism represents different hardware functions by defining a set of bit flags. These bit flags are encoded as one or more specific registers or memory locations and are set by the operating system kernel when the system starts. Since the current RISC-V kernel hwcap mechanism support is not perfect, and dynamic reading of hardware configuration at runtime will bring additional overhead, the present invention uses a compiler predefined macro __riscv_vector to check whether the vector instruction set extension is supported during the compilation stage to determine whether to use a vectorized code path to improve performance, and simultaneously implements two versions of assembly that only include the basic instruction set and RVV optimization. Specifically, the compiler predefined macro is a predefined identifier provided by the compiler, which is used for conditional compilation during the compilation stage. By checking whether the __riscv_vector macro is defined, it can be determined whether the compilation environment supports the RISC-V vector instruction set extension. If the __riscv_vector macro is defined, it means that the compilation environment supports vector instruction set extensions, so the vectorized code path can be used to optimize performance, and the compiler can generate optimized code for the vector instruction set to achieve more efficient vector calculations. If the __riscv_vector macro is not defined, it means that the compilation environment does not support vector instruction set extensions, then the compiler will generate code that only contains the basic instruction set without using the vectorized path for optimization.

[0038] 2. Implementation of basic instruction set of string operation functions

[0039] String operation functions are a type of commonly used operation functions in the C standard library, including strlen, memset, memcpy, memmove, etc. The strlen function is used to calculate the length of a string; the memset function is used to set a memory area to a specified value; the memcpy function is used to copy the data in a memory area to another memory area; the memmove function is used to copy the data in the memory from the source address to the target address, and can handle the situation where the source address and the target address overlap. RISC-V instructions have the characteristics of scalability, streamlined and unified instruction set, hierarchical design, etc. The present invention utilizes RISC-V instruction characteristics and compilation optimization technology, including the use of fine-grained data partitioning, address alignment, loop expansion, address jump, magic number and other optimization means, and uses basic instructions to implement more efficient string operation functions.

[0040] The specific steps are as follows:

[0041] 1) Adopt fine-grained data partitioning optimization method to process memory in blocks of fixed size to improve processing efficiency. For memset, memcpy, and memmove functions, read the function input data volume and process different data volumes in batches;

[0042] 2) Use the double pointer optimization method. For the memset function, when the amount of data is small and the tail is processed, calculate the first address and the last address of the storage, store data from both ends and judge the amount of data to prevent the address from crossing the boundary until the storage is completed;

[0043] 3) Adopt address alignment optimization method to avoid the problem of slow processing speed caused by byte misalignment. For strlen, memset, memcpy, and memmove functions, determine whether the first address is 8-byte aligned. If it is not aligned, it will be processed to 8-byte address alignment through byte operation instructions, and the core loop will process data according to word size;

[0044] 4) Adopt loop unrolling optimization method, that is, put multiple operations of the same type into one loop to reduce the number of judgments and jumps within the loop. For the memset function, expand the storage loop segment 32 times to improve execution efficiency;

[0045] 5) Adopt address jump optimization method. For the memset function, for the amount of data that cannot be stored 32 times at the same time, calculate the number of times to be expanded and the offset from the first address of the loop segment instruction, and directly jump to the loop to execute, which also achieves the loop expansion effect;

[0046] 6) Use the magic number optimization method. Use the 64-bit magic number and data to perform logical operations on the strlen function and quickly find the terminator.

[0047] 3. Implementation of string operation function RVV extended instruction set

[0048] The RVV extended instruction set features and vector instructions make the vectorized programming of string operation functions more concise. The rich vector instructions can efficiently transfer and copy data between vector registers and between vectors and scalars, and at the same time control the number of operations with the vsetvli instruction, avoiding the need to process different data separately and tail processing in the basic instruction set implementation. The string operation function implemented by the RVV extended instruction set uses optimization methods such as address alignment and vectorization to improve the performance and efficiency of the function.

[0049] The specific steps are as follows:

[0050] 1) Use address alignment optimization method. For strlen, memset, memcpy, and memmove functions, read the vlenb register to get the vector register bit width, and determine whether the storage first address is aligned according to the vector register bit width. If not, operate the data byte by byte until the address is aligned according to the vector register bit width;

[0051] 2) Use vectorization optimization method. Set the vl register to the maximum vector length. For the implementation of strlen function, use the vle8.v instruction in the RVV extended instruction set to load data from memory to the vector register, then use the vfirst.m instruction to find the terminator element and send the position information to the scalar register. Finally, merge the position information in the scalar register to get the length of the string. For the implementation of memset function, use the vmv.vx and vse8.v instructions to store vector data in memory. For the implementation of memcpy and memmove functions, use vle8.v to load data in memory to the vector register, and then use vse8.v to store data in memory.

[0052] A specific embodiment of the method proposed in the present invention is given below:

[0053] 1. First, use the compiler predefined macro __riscv_vector to check whether the vector instruction set extension is supported during the compilation phase to decide whether to use the vectorized code path to improve performance. If the compiler defines the __riscv_vector macro definition, the basic C library memset function assembly implementation optimized by the RVV extension is used. If not defined, the basic C library memset function assembly implementation optimized by the basic instruction is used. The pseudo code is as follows:

[0054]

[0055] The specific implementation of the present invention is described in detail in conjunction with the memset function. The function prototype of the memset function is void*memset(void*s, int c, size_t n), where s is the memory start address of the value to be set, c is the value to be set, and n is the number of bytes to be set.

[0056] 2. Assuming that the user's compiler only supports the RISC-V basic instruction set, the basic C library memset function optimized for the basic instruction will be used, such as Figure 2 As shown, the implementation steps are as follows:

[0057] 1) Read the parameters of the memset function called by the user, first determine the function input data size n, and determine whether it is less than 16 bytes;

[0058] 2) If n in step 1) is less than 16 bytes, a double pointer optimization method is used to store data c from the beginning and end of memory addresses s and s+n respectively until the storage is completed;

[0059] 5) If n in step 1) is greater than 16 bytes, the address alignment optimization method is used to continue to determine whether the storage first address s is 8-byte aligned. If it is not aligned, the sb instruction is used to store it byte by byte to the 8-byte address alignment. After alignment, the data c to be filled is copied into 8 bytes through the slli instruction shift operation. If the data c is 0xff, it will be copied to 0xffffffffffffffff;

[0060] 6) Using the loop unrolling optimization method, the data c copied into 8 bytes is then stored word by word and expanded into multiple repeated instruction sequences to improve execution efficiency, and the storage loop segment is expanded 32 times;

[0061] 7) Using the address jump optimization method, for the amount of data that cannot be stored 32 times at the same time, the number of times that need to be expanded and the offset from the first address of the loop segment instruction are calculated, and the execution is directly jumped into the loop, which also achieves the loop expansion effect;

[0062] 8) Repeat step 2) to store the data from the beginning to the end of the remaining data until the storage is complete.

[0063] 3. Assuming that the user's compiler supports the RISC-V RVV extended instruction set, the basic C library memset function optimized by the RVV instruction will be used, such as Figure 3 As shown, the implementation steps are as follows:

[0064] 1) Using the address alignment optimization method, the vlenb register is read through the csrr instruction to obtain the vector register bit width, and then it is determined whether the storage first address s is aligned according to the vector register bit width;

[0065] 2) Step 1) If the first address s is not aligned, use the sb instruction to store data byte by byte until the address is aligned according to the vector register bit width;

[0066] 3) Step 1) If the address is aligned, use the vectorization optimization method. First, set the vl register to the maximum vector length, that is, set the parameter SEW to 8 and LMUL to 8. Then use the vmv vector instruction to copy the data c to the vector register multiple times. The number of copies is determined by vsetvli. Finally, use vse8.v to store the vector data in parallel in the memory.

[0067] Experimental test:

[0068] The present invention uses the test case officially provided by ARM (https: / / github.com / ARM-software / optimized-routines / tree / master / string / bench), and measures the execution efficiency of the memset function on the gem5 simulator, where random tests the performance near the corresponding data volume, medium tests the performance when the data volume is medium, and large tests the performance when the data volume is large. The larger the value, the better the performance. From the test results, whether it is the basic instruction or the memset implemented by RVV, its performance is better than the memset implemented in C language in the basic C library. The detailed data are shown in Tables 1 to 3.

[0069] Table 1. Random memset performance comparison (unit: bytes / ns)

[0070]

[0071] Table 2Medium memset performance comparison (unit: bytes / ns)

[0072]

[0073] Table 3 Large memset performance comparison (unit: bytes / ns)

[0074]

[0075]

[0076] Although the present invention has been disclosed as above by way of embodiments, it is not intended to limit the present invention. Appropriate modifications or equivalent substitutions of the technical solutions of the present invention made by ordinary technicians in the field should all be included in the protection scope of the present invention. The protection scope of the present invention shall be based on what is defined in the claims.

Claims

1. An optimization method for RISC-V basic C library, characterized in that: The following steps are involved: 1) In the RISC-V architecture, use the compiler predefined macro __riscv_vector to check whether the vector instruction set extension is supported during the compilation phase. If not, perform the following step 2); if supported, perform the following step 3); 2) Use the basic instruction set to implement the string operation functions strlen, memset, memcpy and memmove of the basic C library, and use the compilation optimization method to optimize the string operation functions; Compilation optimization methods include one of the following methods: The fine-grained data partitioning optimization method includes the following optimization steps: dividing the memory into blocks of fixed size; for memset, memcpy, and memmove functions, reading the function input data volume, and processing different data volumes in batches; The double pointer optimization method includes the following steps: for the memset function, when the data amount is less than a set threshold and the tail is processed, the first address and the last address of the storage are calculated, and the data are stored from both ends respectively and the data amount is judged to prevent the address from crossing the boundary until the storage is completed; The address alignment optimization method includes the following steps: for functions strlen, memset, memcpy, and memmove, judging whether the first address is 8-byte aligned, if not, processing to 8-byte address alignment through byte operation instructions, and processing data according to word size for the core loop; The loop unrolling optimization method includes the following steps: putting multiple operations of the same type into one loop for execution; for the memset function, unrolling the storage loop segment 32 times for execution; The address jump optimization method includes the following steps: for the memset function, for the amount of data that cannot be stored 32 times at the same time, the number of times to be expanded and the offset from the first address of the loop segment instruction are calculated, and the execution is directly jumped to the loop; The magic number optimization method includes the following steps: for the strlen function, using the 64-bit magic number and data to perform logical operations and find the terminator; 3) Use the RVV extended instruction set to implement vectorized programming of the string operation functions of the basic C library and optimize the string operation functions.

2. The method according to claim 1, characterized in that In step 3), the string operation function is optimized by using the address alignment optimization method, and the optimization steps include: for the strlen, memset, memcpy, and memmove functions, the vlenb register is read to obtain the vector register bit width, and it is determined whether the storage first address is aligned according to the vector register bit width. If not, the data is operated byte by byte until the address is aligned according to the vector register bit width.

3. The method according to claim 1, characterized in that In step 3), a vectorized optimization method is used to optimize the string operation function, and the optimization steps include: Set the vl register to the maximum vector length; For the strlen function, the vle8.v instruction in the RVV extended instruction set is used to load data from the memory into the vector register, and then the vfirst.m instruction is used to find the terminator element and send the position information to the scalar register. Finally, these position information are merged in the scalar register to get the length of the string. For the memset function, use the vmv.vx and vse8.v instructions to store the vector data into memory; For memcpy and memmove functions, use vle8.v to load the data in memory into the vector register, and then use vse8.v to store the data in memory.

Citation Information

Patent Citations

  • Method for optimizing DCT quick algorithm based on parallel processing in AVS

    CN101188761A

  • RISC-V vector register grouping setting method and device and electronic equipment

    CN113721985A