Stack space allocation method and apparatus, electronic device, and readable storage medium
By obtaining the local variable alignment attribute values of the function to be processed in the compiler, determining the target alignment value, and optimizing the stack space layout, the problem of stack space layout affecting the efficiency of local variable access is solved, thus improving the program's running efficiency.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- LOONGSON TECH CORP
- Filing Date
- 2023-07-13
- Publication Date
- 2026-07-03
AI Technical Summary
During the compilation process, the layout of the stack space affects the efficiency of accessing local variables, resulting in low program execution efficiency.
By obtaining the alignment attribute values of each local variable in the function to be processed, and determining that the target alignment value is not less than the minimum value among the alignment attribute values of each local variable, a target memory region is allocated for each local variable, and its starting address is an integer multiple of the target alignment value, thereby optimizing the stack space layout.
This reduces memory out-of-bounds access, improves the execution efficiency of the functions to be processed, and thus improves the running efficiency of the compiled program.
Smart Images

Figure CN116991415B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer technology, and in particular relates to a stack space allocation method, apparatus, electronic device, and readable storage medium. Background Technology
[0002] In the field of computer technology, compilers typically compile source programs from high-level languages into target languages, enabling computers to directly run the target language.
[0003] During the compilation process, when function calls occur in the source program, the compiler allocates stack space, also known as a stack frame, for each function. This stack frame provides the function with information such as its return address and local variables. Consequently, the function will access the stack space during execution.
[0004] The layout of the stack space affects the efficiency of accessing local variables, which in turn affects the program's running efficiency. Therefore, how to make the layout of the allocated stack space improve the program's running efficiency has become an urgent problem to be solved. Summary of the Invention
[0005] This invention provides a stack space allocation method, apparatus, electronic device, and readable storage medium to solve the problem of how to make the layout of the allocated stack space improve the running efficiency of the program.
[0006] To solve the above-mentioned technical problems, the present invention is implemented as follows:
[0007] In a first aspect, the present invention provides a stack space allocation method, the method comprising:
[0008] During the compilation process of the program to be compiled, obtain the functions to be processed that contain local variables in the program to be compiled;
[0009] Based on the alignment attribute values corresponding to each local variable in the function to be processed, the target alignment value of the function to be processed is determined; the alignment attribute values are used to characterize the alignment size of the starting address where the local variable is located; the target alignment value is not less than the minimum value among the alignment attribute values corresponding to each local variable.
[0010] Based on the target alignment value, each local variable in the function to be processed is allocated its corresponding target memory region, and the stack space of the function to be processed is determined based on the target memory regions corresponding to each local variable; the starting address of the target memory region corresponds to an integer multiple of the target alignment value;
[0011] The access operation of the function to be processed is responded to according to the stack space of the function to be processed.
[0012] In a second aspect, the present invention provides a stack space allocation device, the device comprising:
[0013] The acquisition module is used to acquire, during the compilation process of the program to be compiled, the functions to be processed that contain local variables in the program to be compiled;
[0014] An alignment determination module is used to determine the target alignment value of the function to be processed based on the alignment attribute values corresponding to each local variable in the function to be processed; the alignment attribute values are used to characterize the alignment size of the starting address where the local variable is located; the target alignment value is not less than the minimum value among the alignment attribute values corresponding to each local variable.
[0015] The allocation module is used to allocate corresponding target memory regions for each local variable in the function to be processed based on the target alignment value, and to determine the stack space of the function to be processed based on the target memory regions corresponding to each local variable; the starting address of the target memory region corresponds to an integer multiple of the target alignment value;
[0016] The response module is used to respond to the access operations of the function to be processed based on the stack space of the function to be processed.
[0017] Thirdly, the present invention provides an electronic device, comprising: a processor, a memory, and a computer program stored in the memory and executable on the processor, characterized in that the processor implements the above-described stack space allocation method when executing the program.
[0018] Fourthly, the present invention provides a readable storage medium that, when the instructions in the storage medium are executed by the processor of an electronic device, enables the electronic device to execute the above-described stack space allocation method.
[0019] In this embodiment of the invention, during the compilation of the program to be compiled, a function containing local variables is obtained from the program to be compiled; based on the alignment attribute values corresponding to each local variable in the function to be compiled, a target alignment value is determined for the function to be compiled; the alignment attribute values are used to characterize the alignment size of the starting address where the local variable is located; the target alignment value is not less than the minimum value among the alignment attribute values corresponding to each local variable; based on the target alignment value, a corresponding target memory region is allocated for each local variable in the function to be compiled, and the stack space of the function to be compiled is determined based on the target memory region corresponding to each local variable; the starting address of the target memory region corresponds to an integer multiple of the target alignment value; and the access operation of the function to be compiled is responded to according to the stack space of the function to be compiled. In this way, by using the alignment attribute values of the local variables contained in the function to be processed, the minimum value among the alignment attribute values of each local variable is taken as the target alignment value of the function to be processed. A target memory region with a starting address that is an integer multiple of the target alignment value is allocated to each local variable. The characteristic that the starting address is an integer multiple of the target alignment value ensures that the target memory region is aligned according to the size of the target alignment value. This allows for aligned access to the target memory region corresponding to local variables whose alignment attribute values are not less than the target alignment value. Thus, the stack space determined based on the target memory region avoids the situation where all local variables cannot be accessed in alignment, reduces memory out-of-bounds access during compilation, and improves the execution efficiency of the function to be processed to a certain extent, thereby improving the running efficiency of the compiled program. Attached Figure Description
[0020] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0021] Figure 1 This is a flowchart illustrating the steps of a stack space allocation method provided in an embodiment of the present invention;
[0022] Figure 2 This is an example flowchart of a stack space allocation method provided in an embodiment of the present invention;
[0023] Figure 3 This is a structural diagram of a stack space allocation device provided in an embodiment of the present invention;
[0024] Figure 4 This is a structural diagram of an electronic device provided in an embodiment of the present invention. Detailed Implementation
[0025] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0026] Figure 1 This is a flowchart illustrating the steps of a stack space allocation method provided in an embodiment of the present invention, as follows: Figure 1 As shown, the method includes:
[0027] Step 101: During the compilation of the program to be compiled, obtain the functions to be processed that contain local variables in the program to be compiled.
[0028] Step 102: Based on the alignment attribute values corresponding to each local variable in the function to be processed, determine the target alignment value of the function to be processed; the alignment attribute values are used to characterize the alignment size of the starting address where the local variable is located; the target alignment value is not less than the minimum value among the alignment attribute values corresponding to each local variable.
[0029] Step 103: Based on the target alignment value, allocate corresponding target memory regions for each local variable in the function to be processed, and determine the stack space of the function to be processed based on the target memory regions corresponding to each local variable; the starting address of the target memory region corresponds to an integer multiple of the target alignment value.
[0030] Step 104: Respond to the access operation of the function to be processed according to the stack space of the function to be processed.
[0031] The stack space allocation method provided in this embodiment of the invention can be applied to a compiler, and the program to be compiled can be any program input into the compiler for compilation. The compiler can translate high-level language programs into low-level languages, such as C, C#, etc., and assembly language, machine code, etc. A program typically contains several function calls. When compiling a function, the compiler compiles it into an assembly fragment, which allocates stack space in memory for that function. The stack space is used to store information such as the function's return address and local variables. During storage, this information uses the frame pointer fp (fp) or stack pointer sp (sp) of the stack frame structure as the base address register, adds a fixed offset to form the final memory address, and provides information for the function's execution. This is equivalent to the function's execution environment; each function has its own independent stack space.
[0032] Specifically, during the compilation process, the compiler obtains information about all variables in the program, including variable attributes. Variable attributes indicate whether a variable is a local variable. Specifically, the variable information is a tree-type structure containing attribute fields. The content of these attribute fields characterizes the variable's attributes. Local variables have a specified set of attributes. In this embodiment, the attribute set corresponding to local variables can be pre-defined. Then, by obtaining the content of the attribute fields in the information of each variable, variables whose attribute fields belong to the aforementioned attribute set are identified as local variables. The function containing these local variables is then identified as the function to be processed. A local variable is a variable defined within a function. This local variable is only valid during the function's execution. Local variables are frequently accessed within a function, therefore, the efficiency of accessing local variables significantly impacts the overall execution efficiency of the function.
[0033] The alignment attribute value mentioned above refers to the `aligned` attribute value of a local variable. It indicates the alignment size of the starting address of the local variable, ensuring that the starting address of the local variable is an integer multiple of the alignment size, usually in bytes. This value is often related to the alignment byte size modified by the local variable's alignment size directive ("__attribute__((aligned(n)))" or "#pragma pack")) and the type of the local variable itself, and is usually the larger of the two. The alignment attribute of a local variable is fixed and can be obtained through the variable information. For example, consider a local variable A modified by "__attribute__((aligned(32)))", where A is a long integer (`long`). The size of a `long` type data is 8 bytes, or 64 bits. Therefore, the alignment attribute value of the local variable A is the larger of the alignment byte size (32 bytes) and the type value (8 bytes): 32 bytes, or 256 bits.
[0034] When compiling a function and allocating stack space, the compiler often uses the default alignment attribute value. Different compiler architectures use different default alignment attribute values. A function typically contains multiple different local variables, each with different alignment attribute values. Using the default alignment attribute value may result in many local variables not being able to achieve aligned memory access. Specifically, accessing a variable can be done by accessing its starting address. Aligned memory access means that the starting address of the access is divisible by the alignment attribute value of the data to be accessed. The alignment attribute value can be understood as an access unit. The CPU performs access operations in units of this value; that is, the CPU accesses memory one unit the size of the alignment attribute value at a time. If the starting address of a variable is not an integer multiple of the alignment attribute value, the CPU needs to perform multiple access operations to complete the access, resulting in poor access efficiency. For example, consider a memory space with addresses 0x0 to 0xF containing several access objects, i.e., variable data. If an instruction performs a 4-byte access operation on this memory space, the CPU will access it in 4-byte units. If a variable in this memory space has addresses 0x1 to 0x4, and its starting address 0x1 is not a multiple of 4 bytes, then the CPU needs to access memory 0x0 to 0x3 first, and then memory 0x4 to 0x7, requiring two accesses to complete the operation, resulting in more read / write operations across cache lines. In contrast, if a variable in this memory space has addresses 0x4 to 0x7, and its starting address 0x4 is a multiple of 4 bytes, then the CPU only needs to perform one access operation on 0x4 to 0x7, achieving aligned access.
[0035] Furthermore, to improve the running efficiency of the function to be processed, a target alignment value can be determined for the function based on the alignment attribute values of each local variable contained within it. This target alignment value can be no less than the minimum of the alignment attribute values of each local variable, or it can be no less than the alignment attribute values of at least some of the local variables. The number of "at least some" can be determined based on the total number of local variables contained in the function to be processed; for example, it could be 1 / 2, 2 / 3, etc., depending on actual needs. This determined target alignment value ensures that at least some of the local variables can be accessed in aligned order, thus improving the overall running efficiency of the function to be processed to a certain extent. It should be noted that local variables with alignment attribute values less than the target alignment value can be accessed in an aligned manner. That is, the larger the determined target alignment value, the higher the efficiency of accessing local variables in the stack space. Therefore, in this embodiment of the invention, the quantity of at least a portion of the above can be determined according to the actual access efficiency requirements. Higher access efficiency requirements allow for target alignment values of at least 2 / 3 or 3 / 4 of the local variable's alignment attribute value. Conversely, lower access efficiency requirements allow for target alignment values of at least 1 / 2 or 1 / 3 of the local variable's alignment attribute value. For example, if the function to be processed has three local variables B, C, and D with alignment attribute values of 8 bytes, 16 bytes, and 32 bytes respectively, and the preset requirement is that the alignment attribute value is at least 1 / 2 of the local variable's alignment attribute value, then the alignment attribute value of C (16 bytes) can be used as the target alignment value for the function to be processed. This means that the starting address of each target memory region allocated to variables B, C, and D is an integer multiple of 16 bytes. In this case, variables B and C can be accessed in an aligned manner, while variable D cannot. Therefore, the CPU needs to perform two access operations when accessing local variable D.
[0036] Optionally, the target alignment value is the maximum value among the alignment attribute values corresponding to each local variable in the function to be processed. In this way, the determined target alignment value ensures that all local variables in the function to be processed can be accessed in aligned order, maximizing the running efficiency of the function and thus improving the execution efficiency of the compiled program.
[0037] In this stack space, each local variable corresponds to a target memory region. These target memory regions are contiguous, and the order in which they are arranged is unrestricted; the compiler can arrange them according to the order in which they are called or any other order. It's important to note that the stack space includes not only the regions corresponding to local variables but also regions for other registers, such as general-purpose registers, floating-point registers, and outgoing stack arguments. Corresponding memory regions can also be allocated for these registers, which, together with the target memory regions of the local variables, constitute the stack space of the function being processed.
[0038] In summary, the stack space allocation method provided by this invention involves: obtaining a function containing local variables in the program to be compiled during the compilation process; determining a target alignment value for the function based on the alignment attribute values corresponding to each local variable; the alignment attribute values representing the alignment size of the starting address of the local variable; the target alignment value being no less than the minimum value among the alignment attribute values corresponding to each local variable; allocating a corresponding target memory region for each local variable in the function based on the target alignment value; determining the stack space of the function based on the target memory regions corresponding to each local variable; the starting address of the target memory region being an integer multiple of the target alignment value; and responding to access operations of the function based on the stack space of the function. In this way, by using the alignment attribute values of the local variables contained in the function to be processed, the minimum value among the alignment attribute values of each local variable is taken as the target alignment value of the function to be processed. A target memory region with a starting address that is an integer multiple of the target alignment value is allocated to each local variable. The characteristic that the starting address is an integer multiple of the target alignment value ensures that the target memory region is aligned according to the size of the target alignment value. This allows for aligned access to the target memory region corresponding to local variables whose alignment attribute values are not less than the target alignment value. Thus, the stack space determined based on the target memory region avoids the situation where all local variables cannot be accessed in alignment, reduces memory out-of-bounds access during compilation, and improves the execution efficiency of the function to be processed to a certain extent, thereby improving the running efficiency of the compiled program.
[0039] Optionally, when the compiler is a compiler for a specified architecture, the above-mentioned operation of allocating corresponding target memory regions for each local variable in the function to be processed based on the target alignment value may specifically include the following steps in this embodiment of the invention:
[0040] Step S21: Set the first move address of the stack pointer to the alignment address according to the target alignment value.
[0041] The specified architecture mentioned above can be the Loongson architecture. The compiler for the Loongson architecture can include a Low Level Virtual Machine (LLVM) compiler or GCC (GNU Compiler Collection). The stack pointer, also known as the stack top pointer (sp), is used to operate on the stack space. Data pushing and popping from the stack space depends on the stack pointer; therefore, the address of the stack pointer is dynamic. During data pushing, the address of the stack pointer determines the starting address of each region in the stack space. The movement address of the stack pointer can be understood as the base address of each memory region in the stack space. Therefore, in this embodiment of the invention, to ensure that the starting address of the target memory region is an integer multiple of the target alignment value, the initial movement address of the stack pointer can be set first. Specifically, the alignment address mentioned above refers to an address that is an integer multiple of the target alignment value.
[0042] Specifically, this step can be achieved by setting a specified number of bits in the move address of the stack pointer to zero according to the target alignment value. Specifically, it may include the following steps:
[0043] S211. Determine the target alignment byte based on the target alignment value, and determine the target alignment bit length based on the target alignment byte.
[0044] S212. Set the address bits in the first move address of the stack pointer that are not greater than the target alignment bit to 0, so as to set the first move address of the stack pointer to the alignment address.
[0045] The target alignment value is typically measured in bits, with one byte being 8 bits. Therefore, the target alignment byte can be determined by dividing the target alignment value by 8. Alternatively, when the target alignment value is measured in bytes, its size can be directly used as the target alignment byte size.
[0046] Furthermore, the stack pointer's movement address typically comprises 32 bits or 64 bits, depending on the compiler architecture. To ensure the initial movement address of the stack pointer is an integer multiple of the target alignment value, the address bits corresponding to the target alignment byte can be set to 0, based on the target alignment byte. Here, the address bits corresponding to the target alignment byte refer to address bits no greater than the target alignment bit length. Specifically, taking a target alignment byte of N as an example, the target alignment bit length M = log₂N - 1. Further, the address bits no greater than the target alignment bit length refer to bits 0 to M in the stack pointer's movement address, i.e., the lower M+1 bits. Specifically, by setting the lower M+1 bit of the initial movement address of the stack pointer to zero, the initial movement address of the stack pointer can be made an integer multiple of 2^(M+1), which is an integer multiple of N. For example, taking the target alignment byte as 32 bytes as an example, the target alignment bit M can be obtained as 4. By setting the low (4+1) bit of the first move address of the stack pointer to zero, the first move address of the stack pointer may be an address that is an integer multiple of 32, such as 32, 64, or 96, depending on the value of the high bit of the first move address.
[0047] Specifically, step 212 above can be implemented using a zeroing instruction. The zeroing instruction differs for different architectures. Taking the Loongson architecture as an example, the instruction "bstrins.d$sp,$r0,M,0" can be used to set the values from 0 to M of $sp to 0, where $sp refers to the stack pointer. Thus, steps S211 and S212 can achieve stack pointer alignment, allowing it to be dynamically aligned during function execution. That is, as data is pushed onto the stack, the stack pointer always points to an address that is an integer multiple of the target alignment value, thereby ensuring that the starting address of each region in the stack space is an integer multiple of the target alignment value, enabling the CPU to achieve aligned access.
[0048] Optionally, before setting the move address of the stack pointer, the original contents of the stack pointer can be saved to a specified register, such as the drag register. The specified register varies depending on the architecture.
[0049] Step S22: Based on the number of elements of any of the local variables, the data size of the local variables, and the target alignment value, determine the size of the target memory region corresponding to the local variable as the target size; the target size is an integer multiple of the target alignment value.
[0050] Step S23: Based on the target size of each target memory region, allocate the corresponding target memory region for each local variable through the stack pointer.
[0051] A local variable can have one or more elements. For example, if the local variable is an array and the array size parameter is greater than 1, then the local variable will have multiple elements. Furthermore, the data size is related to the type of the local variable. Taking a local variable of type long as an example, each element in the local variable will have a data size of 8 bytes. The memory size required for the local variable can be obtained by multiplying the number of elements by the data size.
[0052] Furthermore, to enable aligned access to the local variable, this embodiment of the invention can determine the target size based on the memory size required by the local variable and the aforementioned target alignment value. Specifically, when the memory size required by the local variable is not greater than the aforementioned target alignment value, the target alignment value can be directly used as the target size. Correspondingly, when the memory size required by the local variable is greater than the aforementioned target alignment value, the memory size required by the local variable is usually an integer multiple of the target alignment value, so the memory size required by the local variable can be directly used as the target size. When the memory size required by the local variable is greater than the aforementioned target alignment value, but is not an integer multiple of the target alignment value, the value closest to the memory size required by the local variable and an integer multiple of the target alignment value can be used as the target size. This ensures that the CPU achieves aligned access to the target memory region when accessing each local variable.
[0053] Furthermore, after obtaining the target size for each target memory region, the stack pointer can be moved to allocate the corresponding target memory region. Specifically, the displacement of the stack pointer is equal to the aforementioned target size.
[0054] Furthermore, since all local variables in the stack space are stored contiguously in this field, that is, the target memory regions corresponding to each local variable are contiguous in the stack space, the region where all local variables are located can be called the local variable region. Moreover, the local variable region can be uniformly allocated. Therefore, in this embodiment of the invention, the total size of the local variable region can be obtained according to the target size of each target memory region. It is understood that the total size of the local variable region is also an integer multiple of the above-mentioned target alignment value.
[0055] For example, taking the existence of three local variables D, E, and F, where each of these local variables is an array of type long and the array contains 64 elements, we can obtain that the target size of the target memory region corresponding to any local variable is 64 * 8, which is 512 bytes. Accordingly, the total size of the local variable region is 3 * 512, which is 1536 bytes. Furthermore, the stack pointer can be moved 1536 bytes from the bottom of the stack to the top of the stack to allocate the local variable region.
[0056] Specifically, this can be done in the compiler's pro_and_epilogue pass stage, inserting new instruction sequences into the function prologue and epilogue. The allocation of local variable regions can be achieved through the instruction "ADDI$sp,$sp,-1536", where the field containing -1536 represents the displacement of $sp.
[0057] This invention, in its embodiments, sets the initial movement address of the stack pointer to the alignment address according to the target alignment value; determines the size of the target memory region corresponding to each local variable as the target size based on the number of data points, the size of the local variable, and the target alignment value; the target size is an integer multiple of the target alignment value, ensuring aligned access by performing one access operation each time the CPU accesses each local variable; and allocates corresponding target memory regions for each local variable using the stack pointer based on the target size of each target memory region. Thus, by setting the initial movement address of the stack pointer to the alignment address according to the target alignment value, dynamic alignment of the stack pointer can be achieved. This ensures that when subsequent data enters the stack space for storage, the movement of the stack pointer ensures that the data entering the stack space is stored in an aligned manner, thereby enabling aligned access. Simultaneously, by determining the target size of the target memory region based on the number of data points, the size of the local variable, and the target alignment value, and allocating the target memory region based on the target size, aligned access can be achieved while allocating memory regions according to the actual memory requirements of the variable data, avoiding unnecessary memory occupation and saving stack space memory to a certain extent.
[0058] Optionally, if the function to be processed contains a subfunction that calls the frame pointer to access local variables, the following steps may be included in this embodiment of the invention before the operation of determining the size of the target memory region corresponding to the local variable as the target size:
[0059] Step S31: Based on the number of data to be stored corresponding to the register to be allocated in the function to be processed, the data size of the data to be stored, and the target alignment value, determine the size of the first memory region corresponding to the register to be allocated as the first size; the first size is an integer multiple of the target alignment value.
[0060] The frame pointer (hard_frame_pointer_rtx, fp) mentioned above refers to the frame pointer allocated for the current function to be processed. It usually points to the bottom of the stack space. The frame pointer can be used to provide the base address for memory access operations. After adding the offset, the actual access is performed. The sub-function mentioned above refers to the calling function that needs to call the frame pointer to access local variables. It can include: memory allocation call (alloca), save current function call (setjump), and long jump call (longjump).
[0061] Specifically, we can iterate through the function names of each sub-function in the function to be processed. If any of the methods mentioned above—`alloca`, `setjump`, and `longjump`—is encountered, it indicates that the function to be processed contains a sub-function that accesses local variables using the frame pointer. In this case, it means that the function to be processed requires the frame pointer to provide the base address when accessing local variables. Therefore, not only the stack pointer but also the frame pointer needs to be aligned. Furthermore, since the frame pointer usually points to the bottom of the stack space, to ensure alignment when accessing through the frame pointer, both the base address and the offset must be integer multiples of the target alignment value. Therefore, every region in the stack space needs to be aligned.
[0062] Furthermore, the aforementioned registers to be allocated include general-purpose registers (GPRs) and floating-point registers (FPRs), which can be collectively referred to as callee-save registers. These registers need to have their original values saved before use for restoration after the function ends. Specifically, the drap register (drapreg) belongs to the GPR, as does the register used to store the original frame pointer (fp register (fpreg)). Correspondingly, the data to be stored refers to the data required to be saved by the GPRs and FPRs.
[0063] Specifically, the quantity and size of the data to be stored vary depending on the original values of the registers to be allocated. Specifically, the compiler can pre-determine the original values from the GPR and FPR before allocating stack space for the function to be processed, thus obtaining the data to be stored. Furthermore, since the original values of the GPR and FPR need to be saved first, when allocating stack space for the function to be processed, the corresponding memory regions are allocated for the GPR and FPR first, and then for local variables.
[0064] Furthermore, the required memory size can be obtained by multiplying the quantity and data size. Then, a first size can be determined based on the memory size required by the register to be allocated and the aforementioned target alignment value. This first size represents the size of the first memory region corresponding to the register to be allocated. Specifically, if the memory size required by the register to be allocated is not greater than the aforementioned target alignment value, the target alignment value can be directly used as the first size. Correspondingly, if the memory size required by the register to be allocated is greater than the aforementioned target alignment value, the memory size required by the register to be allocated is usually an integer multiple of the target alignment value to ensure that the CPU performs aligned access. Therefore, the memory size required by the register to be allocated can be directly used as the first size.
[0065] Step S32: Based on the first size, allocate a corresponding first memory region for each of the registers to be allocated through the stack pointer, and set the address of the frame pointer to the high address of the first memory region.
[0066] After obtaining the initial size of each first memory region, the stack pointer can be moved to allocate the corresponding first memory region. Specifically, the displacement of the stack pointer is equal to the aforementioned initial size. The high address of the aforementioned first memory region actually refers to the bottom address of the stack space. Therefore, when allocating stack space for the function to be processed—that is, after saving the addresses of the original stack pointer and the original frame pointer—the address pointed to by the current stack pointer, i.e., this high address, can be used as the address of the new frame pointer.
[0067] For example, assuming there are four registers to be allocated, each containing a long type data item with a target alignment of 256 bits, the memory required for these four registers is 1 * 8 * 4, or 32 bytes. Since the target alignment is 256 bits (32 bytes), they are equal, so the memory required for these registers can be directly used as the first size. Furthermore, the stack pointer can be moved 32 bytes from the bottom to the top of the stack based on the current address to allocate the first memory region.
[0068] Specifically, a new instruction sequence can be inserted in the function prologue. The allocation of the first memory region can be achieved through the instruction "ADDI$sp,$sp,-32", where the field containing -32 represents the displacement of $sp.
[0069] It should be noted that, since the data stored in the first memory region needs to provide a basis for stack backtracking in functions such as exception handling, different compilers will set special calculation rules to indicate the stack backtracking process. These rules define how the address pointed to by sp (CFA) is calculated when executing stack backtracking. Usually, in order to satisfy the CFA calculation rules, fpreg and drapreg in the first memory region are arranged in a specified order, which is usually from the bottom of the stack to the top of the stack as fpreg and drapreg.
[0070] The above-described operation of determining the stack space of the function to be processed based on the target memory region corresponding to each local variable can specifically include, in this embodiment of the invention:
[0071] Step S33: Generate the stack space of the function to be processed based on the first memory region and the target memory regions corresponding to each local variable.
[0072] Specifically, since the stack space first stores the values of GPR and FPR before allocating areas for local variables, the target memory region is closer to the top of the stack than the first memory region. Furthermore, the end address of the first memory region and the start address of the target memory region are contiguous. Therefore, the stack space for the function to be processed is generated using the high address of the first memory region and the low address of the target memory region. Further, the outgoing parameters refer to the return parameters of the function to be processed, which are usually the last to enter the stack space. Therefore, the top of the stack space can be allocated according to the target alignment value of the outgoing parameters after the target memory region has been determined.
[0073] In this embodiment of the invention, when a sub-function in the function to be processed accesses local variables by calling the frame pointer, before determining the size of the target memory region corresponding to the local variables as the target size, the size of the first memory region corresponding to the registers to be allocated is determined as a first size based on the number of data to be stored corresponding to the registers to be allocated in the function to be processed, the size of the data to be stored, and the target alignment value; the first size is an integer multiple of the target alignment value; based on the first size, the corresponding first memory region is allocated to each register to be allocated through the stack pointer, and the address of the frame pointer is set to the high address of the first memory region; the stack space of the function to be processed is generated based on the first memory region and the target memory region corresponding to each local variable. Thus, when a sub-function in the function to be processed accesses local variables by calling the frame pointer, the first memory region is allocated to the registers to be allocated based on the target alignment value, and the address of the frame pointer is set to the high address of the first memory region. This ensures that the frame pointer is also aligned according to the target alignment value, achieving simultaneous alignment of the stack pointer and the frame pointer. This allows the sub-functions that call the frame pointer to access local variables to achieve aligned access during the execution phase, further improving the execution efficiency of the program.
[0074] Optionally, if there is no subfunction in the function to be processed that calls the frame pointer to access local variables, before setting the first move address of the stack pointer to the alignment address according to the target alignment value, the embodiments of the present invention may further include the following steps:
[0075] Step S41: Determine the second memory region corresponding to the register to be allocated contained in the function to be processed.
[0076] The above-described operation of determining the stack space of the function to be processed based on the target memory region corresponding to each local variable can specifically include the following steps in this embodiment of the invention:
[0077] Step S42: Generate the stack space of the function to be processed based on the second memory region and the target memory regions corresponding to each local variable.
[0078] In the case where there is no sub-function in the function to be processed that calls the frame pointer to access local variables, it indicates that accessing local variables in the function to be processed only needs to rely on the stack pointer, and frame pointer alignment is not required. That is, there is no need to align the regions corresponding to GPR and FPR and the frame pointer according to the target alignment value. Therefore, step S41 above can allocate the corresponding second memory region for the register to be allocated according to the compiler's default alignment attribute value, or it can allocate the second memory region according to the actual memory size required by the register to be allocated. This embodiment of the invention does not limit this.
[0079] For example, if there are 4 registers to be allocated, and each of these 4 registers contains an int type of data to be stored, the first size of these 4 registers to be allocated is 1*4*4, which is 16 bytes. Furthermore, the stack pointer can be moved 16 bytes from the bottom of the stack to the top of the stack based on the current address to allocate the second memory region.
[0080] Specifically, a new instruction sequence can be inserted in the function prologue. The allocation of the second memory region can be achieved through the instruction "ADDI$sp,$sp,-16", where the field containing -16 represents the displacement of $sp.
[0081] Furthermore, since the stack space first stores the values of GPR and FPR before allocating areas for local variables, the target memory region is closer to the top of the stack than the second memory region. Moreover, the end address of the second memory region is continuous with the start address of the target memory region. Thus, the stack space of the function to be processed can be generated through the high address of the second memory region and the low address of the target memory region.
[0082] In this embodiment of the invention, when there are no sub-functions in the function to be processed that call the frame pointer to access local variables, a second memory region corresponding to the registers to be allocated in the function to be processed is determined. The stack space of the function to be processed is then generated based on the second memory region and the target memory regions corresponding to each local variable. Thus, when there are no sub-functions in the function to be processed that call the frame pointer to access local variables, it is not necessary to align the memory region corresponding to the registers to be allocated according to the target alignment value; only the regions corresponding to the local variables need to be aligned, reducing unnecessary operations.
[0083] Optionally, embodiments of the present invention may further include the following steps:
[0084] S51. When all the target memory regions correspond to null values, the specified compilation option corresponding to the compiler is set to the target state; the specified compilation option is used to indicate whether to close the frame pointer of the function to be processed.
[0085] S52. In response to the specified compilation option being in the target state, the frame pointer of the function to be processed is turned off.
[0086] The aforementioned specified compilation option refers to an enable option in the compiler used to indicate that instructions for saving, setting, and restoring frame pointers should not be generated when frame pointers are not needed. For example, in the GNU Compiler Collection (GCC), the specified compilation option is -fomit-frame-pointer.
[0087] Specifically, during the execution of the aforementioned steps, the use of the frame pointer may be actively triggered, resulting in redundant use of fp. However, when the local variable area is empty, there is no need to use the frame pointer. Therefore, in this embodiment of the invention, when the target memory area corresponding to the local variable is empty, the specified compilation option can be set to the target state. When the specified compilation option is in the target state, the compiler can turn off the frame pointer, that is, cancel the use of the frame pointer, thereby further ensuring the compilation effect of the function to be processed.
[0088] The target state mentioned above can be an enabled state, and the operation of setting the target state can be to set the specified compilation option to 1, thus enabling it. Of course, different compilers have different specified compilation options, and therefore the way to set the target state will also be different. This embodiment of the invention does not limit this.
[0089] Optionally, after the function to be processed is compiled, the epilogue can also be processed, which can restore the contents of the callee-saved register, restore the stack pointer and frame pointer, and reclaim the stack space according to the aforementioned stack space allocation operations.
[0090] In this embodiment of the invention, when all target memory regions correspond to null values, the specified compilation option corresponding to the compiler is set to the target state; the specified compilation option is used to indicate whether to close the frame pointer of the function to be processed; in response to the specified compilation option being in the target state, the frame pointer of the function to be processed is closed. This avoids unnecessary use of the frame pointer and further ensures the compilation effect of the function to be processed.
[0091] Taking GCC as the compiler as an example, Figure 2This is an example flowchart of a stack space allocation method provided in an embodiment of the present invention, such as... Figure 2 As shown, embodiments of the present invention include:
[0092] Step 201: Determine if the function to be processed has local variables. If not, proceed to step 202; if yes, proceed to step 203.
[0093] In GCC's compilation and optimization process, each step of the compilation process for the compiled object (usually a function or file waiting to be compiled) is called a pass. Step 201 can be performed in GCC's expand pass phase. This pass is mainly responsible for the conversion of the front-end intermediate representation (the internal representation generated by the compiler after scanning the program to be compiled): converting the gimple instruction sequence into an rtl instruction sequence, during which local variables are analyzed, and an RTX expression is generated for stack space allocation.
[0094] Step 202: Determine if the function to be processed has added any new local variables. If yes, proceed to step 203; otherwise, end the process.
[0095] This step occurs during the reload pass phase, which involves Spill Code Generation: During register allocation, due to conflicts between virtual registers and limitations on the number of physical registers, some virtual registers may not be allocated to physical registers. In this case, stack space is needed to save and restore the data of the virtual registers. The Spill operation affects whether new local variables are generated.
[0096] Step 203: Determine if the function to be processed contains sub-functions such as alloca, setjump, and longjump that use the frame pointer to access local variables. At this point, each region of the stack space needs to be aligned. If yes, proceed to step 204; otherwise, proceed to step 205.
[0097] Step 204, Stack space allocation strategy S1.
[0098] This step corresponds to the operations described in S21 to S23 and S31 to S33 above, and will not be repeated here.
[0099] Step 205, Stack space allocation strategy S2.
[0100] This step corresponds to the operations described in S21 to S23 and S41 to S42 above, and will not be repeated here.
[0101] Step 206: Optimize frame pointers by specifying compilation options.
[0102] Optionally, the compiler can use certain compilation commands to optimize the source code to be compiled, making the compiled program execute faster. For example, let's take compiling the following source code `test` using the compilation command `gcc-mlasx-O3-S`. Here, `-mlasx` is the compiler option to enable 256-bit vectorization. Accordingly, compiling the following source code using `gcc-mlasx-O3-S` means performing 256-bit access to the local variables in the function `test`. The memory required for the local variables corresponding to the function `test` is 3 * NUM * 8 bytes, where 3 represents the number of arrays A, B, and C (under the `-O3` optimization strategy, variables such as array D are optimized in gimple's optimized pass), NUM represents the size of each array, and 8 represents the size of the `long` data type in bytes.
[0103]
[0104]
[0105] For the above source code, if NUM is set to 64, and the compilation command `gcc-mlasx-O3-S` is used to compile the following source code `test`, the stack space allocation in the prior art is shown in the first column of the table below. The stack space allocation using strategy S1 is shown in the second column of the table below (assuming there is a need to access the call frame pointer), and the stack space allocation using strategy S2 is shown in the third column of the table below. Only a portion of the commands are shown in the table below.
[0106]
[0107]
[0108] The LD256 instruction loads 256 bits of data from a specified memory address (the addresses of variables A, B, and C in the stack space) into a vector register (xr0 / xr3 / xr22 in the example).
[0109] Based on the original stack frame layout in the table above, it can be seen that the original stack space allocation directly divided 1600 bytes of memory using the instruction "ADDI$sp,$sp,-1600". The first move address of the stack pointer was not set, and the memory area was not divided according to the order of data being pushed onto the stack. As a result, the local variables in the original stack frame layout were often stored in addresses where aligned access could not be achieved.
[0110] In the S1 stack frame layout provided in this embodiment of the invention, after saving the original address of the stack pointer using "MOV $drap, $sp", the first move address of the stack pointer is set to the alignment address using the zeroing instruction "bstrins.d $sp, $r0, 4, 0" (the table above uses a target alignment value of 32 bytes as an example). Then, the first memory region corresponding to the registers to be allocated is divided using "ADDI $sp, $sp, -32" (the table above uses an example with four registers to be allocated, each containing a long data). The address of the frame pointer is set to the high address of the first memory region (actually the current address of the stack pointer) using "MOV $fp, $sp". Next, the target memory region corresponding to the local variables is divided using "ADDI $sp, $sp, -X" (the table above shows "-1600 contains 1536 bytes of the target memory region corresponding to the local variables and 64 bytes of the parameter passing region corresponding to the output parameters"). The stack space is then obtained through the first memory region and the target memory region.
[0111] Furthermore, if there are no sub-functions in the function to be processed that call the frame pointer for access, the S2 strategy can be executed directly: there is no need to set the alignment address for the frame pointer. Therefore, after allocating the second memory region corresponding to the register to be allocated via "ADDI$sp,$sp,-64" (the size of the second memory region can be determined according to actual needs, either according to the memory size required by the register to be allocated or according to the default size), the stack pointer and the memory region of the local variables can be aligned. Specifically, the first move address of the stack pointer can be set to the alignment address via "bstrins.d$sp,$r0,4,0", and then the target memory region corresponding to the local variables can be allocated directly via "ADDI$sp,$sp,-1536" (the table above is based on the example of 3 local variables of type long, each containing 64 elements). Thus, a stack space of 1600 bytes is obtained through the second memory region and the target memory region.
[0112] After verifying the execution performance of the above example using performance analysis tools (such as perf), it was found that the unaligned memory access problem of local variables was significantly alleviated after adopting the S1 and S2 strategies. Under the original stack frame layout, the $sp alignment size is 128 bits. If there are several 256-bit vector memory access behaviors in the core loop of the function body, this behavior will greatly affect the execution efficiency of the function. The starting address of storing arrays A, B, and C can guarantee 128-bit alignment but cannot guarantee 256-bit alignment (a probabilistic event). There is a phenomenon of 256-bit vectorized unaligned memory access of arrays A, B, and C, which leads to large performance fluctuations in multiple tests.
[0113] However, strategies S1 and S2 analyze the alignment requirements of local variables in function stack frames during compile time and adjust the stack frame layout accordingly. Even if the initial stack frame `$sp` alignment is 128 bits or any other arbitrary size, the prologue always adjusts `$sp` to meet the target alignment size based on the actual alignment requirements of the function at compile time. This ensures that the starting addresses of arrays A, B, and C are aligned to 256 bits, resulting in aligned vectorized memory access within the function body. Compared to the original stack frame layout, this avoids or reduces memory access across data boundaries, improving program execution efficiency, and its performance has remained stable after multiple tests.
[0114] Figure 3 This is a structural diagram of a stack space allocation device provided in an embodiment of the present invention. This device can be applied to a compiler, and the device 30 may include:
[0115] The acquisition module 301 is used to acquire, during the compilation process of the program to be compiled, the functions to be processed that contain local variables in the program to be compiled;
[0116] Alignment determination module 302 is used to determine the target alignment value of the function to be processed based on the alignment attribute values corresponding to each local variable in the function to be processed; the alignment attribute values are used to characterize the alignment size of the starting address where the local variable is located; the target alignment value is not less than the minimum value among the alignment attribute values corresponding to each local variable.
[0117] The allocation module 303 is used to allocate corresponding target memory regions for each local variable in the function to be processed based on the target alignment value, and to determine the stack space of the function to be processed based on the target memory regions corresponding to each local variable; the starting address of the target memory region corresponds to an integer multiple of the target alignment value;
[0118] The response module 304 is used to respond to the access operation of the function to be processed according to the stack space of the function to be processed.
[0119] Optionally, the target alignment value is the maximum value among the alignment attribute values corresponding to each local variable in the function to be processed.
[0120] Optionally, when the compiler is a compiler for a specified architecture, the allocation module 303 is specifically used for:
[0121] Set the first move address of the stack pointer to the alignment address according to the target alignment value;
[0122] Based on the number of elements of any of the local variables, the data size of the local variables, and the target alignment value, the size of the target memory region corresponding to the local variable is determined as the target size; the target size is an integer multiple of the target alignment value.
[0123] Based on the target size of each target memory region, the stack pointer is used to allocate the corresponding target memory region for each local variable.
[0124] Optionally, the device 30 further includes:
[0125] The first region determination module is used to determine the size of the first memory region corresponding to the register to be allocated as a first size before the allocation module determines the size of the target memory region corresponding to the local variable as the target size, based on the number of data to be stored corresponding to the register to be allocated in the function to be processed, the data size of the data to be stored, and the target alignment value; the first size is an integer multiple of the target alignment value.
[0126] The first region allocation module is used to allocate a corresponding first memory region to each of the registers to be allocated based on the first size of the first memory region through the stack pointer, and set the address of the frame pointer to the high address of the first memory region;
[0127] The allocation module 303 is further configured to:
[0128] The stack space of the function to be processed is generated based on the first memory region and the target memory regions corresponding to each of the local variables.
[0129] Optionally, if there is no subfunction in the function to be processed that calls the frame pointer to access local variables, the device 30 further includes:
[0130] The second region determination module is used to determine the second memory region corresponding to the register to be allocated contained in the function to be processed before the allocation module 303 sets the first move address of the stack pointer to the alignment address according to the target alignment value.
[0131] The allocation module 303 is further configured to:
[0132] The stack space of the function to be processed is generated based on the second memory region and the target memory regions corresponding to each of the local variables.
[0133] Optionally, the device 30 further includes:
[0134] The configuration module is used to set the specified compilation option corresponding to the compiler to the target state when all the target memory regions correspond to null values; the specified compilation option is used to indicate whether to close the frame pointer of the function to be processed;
[0135] The shutdown module is used to close the frame pointer of the function to be processed in response to the specified compilation option being in the target state.
[0136] Optionally, the allocation module 303 is further configured to:
[0137] The target alignment byte is determined based on the target alignment value, and the target alignment bit length is determined based on the target alignment byte.
[0138] Set the address bits in the first move address of the stack pointer that are not greater than the target alignment bit value to 0, so as to set the first move address of the stack pointer to the alignment address.
[0139] In summary, the stack space allocation device provided in this embodiment of the invention obtains the function to be processed containing local variables in the program to be compiled during the compilation process; determines the target alignment value of the function to be processed based on the alignment attribute value corresponding to each local variable in the function to be processed; the alignment attribute value is used to characterize the alignment size of the starting address where the local variable is located; the target alignment value is not less than the minimum value among the alignment attribute values corresponding to each local variable; allocates a corresponding target memory region for each local variable in the function to be processed based on the target alignment value, and determines the stack space of the function to be processed based on the target memory region corresponding to each local variable; the starting address of the target memory region corresponds to an integer multiple of the target alignment value; and responds to the access operation of the function to be processed according to the stack space of the function to be processed. In this way, by using the alignment attribute values of the local variables contained in the function to be processed, the minimum value among the alignment attribute values of each local variable is taken as the target alignment value of the function to be processed. A target memory region with a starting address that is an integer multiple of the target alignment value is allocated to each local variable. The characteristic that the starting address is an integer multiple of the target alignment value ensures that the target memory region is aligned according to the size of the target alignment value. This allows for aligned access to the target memory region corresponding to local variables whose alignment attribute values are not less than the target alignment value. Thus, the stack space determined based on the target memory region avoids the situation where all local variables cannot be accessed in alignment, reduces memory out-of-bounds access during compilation, and improves the execution efficiency of the function to be processed to a certain extent, thereby improving the running efficiency of the compiled program.
[0140] The present invention also provides an electronic device, see [link to relevant documentation]. Figure 4It includes: a processor 401, a memory 402, and a computer program 4021 stored in the memory and executable on the processor. When the processor executes the program, it implements the stack space allocation method of the foregoing embodiments.
[0141] The present invention also provides a readable storage medium that, when the instructions in the storage medium are executed by the processor of an electronic device, enables the electronic device to execute the stack space allocation method of the foregoing embodiments.
[0142] As the device embodiment is basically similar to the method embodiment, the description is relatively simple, and relevant parts can be found in the description of the method embodiment.
[0143] The algorithms and displays provided herein are not inherently related to any particular computer, virtual system, or other device. Various general-purpose systems can also be used in conjunction with the teachings herein. The required structure for constructing such systems is apparent from the above description. Furthermore, this invention is not directed to any particular programming language. It should be understood that the contents of the invention described herein can be implemented using various programming languages, and the above description of specific languages is for the purpose of disclosing the best mode of implementation of the invention.
[0144] Numerous specific details are set forth in the specification provided herein. However, it will be understood that embodiments of the invention may be practiced without these specific details. In some instances, well-known methods, structures, and techniques have not been shown in detail so as not to obscure the understanding of this specification.
[0145] Similarly, it should be understood that, in order to simplify the invention and aid in understanding one or more of the various inventive aspects, in the above description of exemplary embodiments of the invention, various features of the invention are sometimes grouped together in a single embodiment, figure, or description thereof. However, this disclosure should not be construed as reflecting an intention that the claimed invention requires more features than are expressly recited in each claim. Rather, as reflected in the following claims, inventive aspects lie in fewer than all features of a single foregoing disclosed embodiment. Therefore, the claims following the detailed description are hereby expressly incorporated into this detailed description, wherein each claim itself is a separate embodiment of the invention.
[0146] Those skilled in the art will understand that modules in the device of the embodiments can be adaptively changed and placed in one or more devices different from that embodiment. Modules, units, or components in the embodiments can be combined into a single module, unit, or component, and further, they can be divided into multiple sub-modules, sub-units, or sub-components. Except where at least some of such features and / or processes or units are mutually exclusive, any combination can be used to combine all features disclosed in this specification (including the accompanying claims, abstract, and drawings) and all processes or units of any method or device so disclosed. Unless expressly stated otherwise, each feature disclosed in this specification (including the accompanying claims, abstract, and drawings) may be replaced by an alternative feature that serves the same, equivalent, or similar purpose.
[0147] The various component embodiments of the present invention can be implemented in hardware, or as software modules running on one or more processors, or a combination thereof. Those skilled in the art will understand that microprocessors or digital signal processors (DSPs) can be used in practice to implement some or all of the functions of some or all of the components in the sorting device according to the present invention. The present invention can also be implemented as a device or apparatus program for performing part or all of the methods described herein. Such a program implementing the present invention can be stored on a computer-readable medium, or can be in the form of one or more signals. Such signals can be downloaded from an Internet website, provided on a carrier signal, or provided in any other form.
[0148] It should be noted that the above embodiments are illustrative of the invention and not restrictive, and that those skilled in the art can devise alternative embodiments without departing from the scope of the appended claims. In the claims, any reference signs placed between parentheses should not be construed as limiting the claims. The word "comprising" does not exclude the presence of elements or steps not listed in the claims. The word "a" or "an" preceding an element does not exclude the presence of a plurality of such elements. The invention can be implemented by means of hardware comprising several different elements and by means of a suitably programmed computer. In the unit claims enumerating several means, several of these means may be embodied by the same item of hardware. The use of the words first, second, and third, etc., does not indicate any order. These words can be interpreted as names.
[0149] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0150] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
[0151] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A stack space allocation method, characterized in that, Applied to a compiler, the method includes: During the compilation process of the program to be compiled, obtain the functions to be processed that contain local variables in the program to be compiled; Based on the alignment attribute values corresponding to each local variable in the function to be processed, the target alignment value of the function to be processed is determined; the alignment attribute values are used to characterize the alignment size of the starting address where the local variable is located; the target alignment value is not less than the minimum value among the alignment attribute values corresponding to each local variable. When the compiler is a compiler for a specified architecture, the size of the target memory region corresponding to each local variable is determined as the target size based on the number of elements of any local variable, the data size of the local variable, and the target alignment value. Based on the target size of each target memory region, a corresponding target memory region is allocated to each local variable through the stack pointer, and the stack space of the function to be processed is determined based on the target memory region corresponding to each local variable. The starting address of the target memory region corresponds to an integer multiple of the target alignment value. The access operation of the function to be processed is responded to according to the stack space of the function to be processed.
2. The method according to claim 1, characterized in that, The target alignment value is the maximum value among the alignment attribute values corresponding to each local variable in the function to be processed.
3. The method according to claim 1, characterized in that, Before determining the size of the target memory region corresponding to the local variable as the target size based on the number of elements of any of the local variables, the data size of the local variables, and the target alignment value, the method further includes: Set the first move address of the stack pointer to the alignment address according to the target alignment value; Wherein, the target size is an integer multiple of the target alignment value.
4. The method according to claim 1, characterized in that, If the function to be processed contains a subfunction that calls the frame pointer to access local variables, before determining the size of the target memory region corresponding to the local variable as the target size, the method further includes: Based on the number of data to be stored corresponding to the register to be allocated in the function to be processed, the data size of the data to be stored, and the target alignment value, the size of the first memory region corresponding to the register to be allocated is determined as the first size; the first size is an integer multiple of the target alignment value; Based on the first size, the stack pointer is used to allocate a corresponding first memory region for each of the registers to be allocated, and the address of the frame pointer is set to the high address of the first memory region; Determining the stack space of the function to be processed based on the target memory region corresponding to each of the local variables includes: The stack space of the function to be processed is generated based on the first memory region and the target memory regions corresponding to each of the local variables.
5. The method according to claim 3, characterized in that, If there is no subfunction in the function to be processed that calls the frame pointer to access local variables, before setting the first move address of the stack pointer to the alignment address according to the target alignment value, the method further includes: Determine the second memory region corresponding to the register to be allocated contained in the function to be processed; Determining the stack space of the function to be processed based on the target memory region corresponding to each of the local variables includes: The stack space of the function to be processed is generated based on the second memory region and the target memory regions corresponding to each of the local variables.
6. The method according to claim 4 or 5, characterized in that, The method further includes: If each of the target memory regions corresponds to a null value, the specified compilation option corresponding to the compiler is set to the target state; the specified compilation option is used to indicate whether to close the frame pointer of the function to be processed; In response to the specified compilation option being in the target state, the frame pointer of the function to be processed is closed.
7. The method according to claim 3, characterized in that, Setting the first move address of the stack pointer to the alignment address according to the target alignment value includes: The target alignment byte is determined based on the target alignment value, and the target alignment bit length is determined based on the target alignment byte. The address bits in the first move address of the stack pointer that are not greater than the target alignment bit are set to 0, so that the first move address of the stack pointer is set to the alignment address.
8. A stack space allocation device, characterized in that, Applied to a compiler, the apparatus includes: The acquisition module is used to acquire, during the compilation process of the program to be compiled, the functions to be processed that contain local variables in the program to be compiled; An alignment determination module is used to determine the target alignment value of the function to be processed based on the alignment attribute values corresponding to each local variable in the function to be processed; the alignment attribute values are used to characterize the alignment size of the starting address where the local variable is located; the target alignment value is not less than the minimum value among the alignment attribute values corresponding to each local variable. The allocation module is used to allocate corresponding target memory regions for each local variable in the function to be processed based on the target alignment value, and to determine the stack space of the function to be processed based on the target memory regions corresponding to each local variable; the starting address of the target memory region corresponds to an integer multiple of the target alignment value; The response module is used to respond to the access operations of the function to be processed based on the stack space of the function to be processed; When the compiler is a compiler for a specified architecture, the allocation module is specifically used for: Based on the number of elements of any of the local variables, the data size of the local variables, and the target alignment value, the size of the target memory region corresponding to the local variable is determined as the target size; based on the target size of each target memory region, the corresponding target memory region is allocated to each local variable through the stack pointer.
9. The apparatus according to claim 8, characterized in that, The target alignment value is the maximum value among the alignment attribute values corresponding to each local variable in the function to be processed.
10. The apparatus according to claim 8, characterized in that, When the compiler is a compiler for a specified architecture, the allocation module is further configured to: Set the first move address of the stack pointer to the alignment address according to the target alignment value; Wherein, the target size is an integer multiple of the target alignment value.
11. The apparatus according to claim 8, characterized in that, The device further includes: The first region determination module is used to determine the size of the first memory region corresponding to the register to be allocated as a first size before the allocation module determines the size of the target memory region corresponding to the local variable as the target size, based on the number of data to be stored corresponding to the register to be allocated in the function to be processed, the data size of the data to be stored, and the target alignment value; the first size is an integer multiple of the target alignment value. The first region allocation module is used to allocate a corresponding first memory region to each of the registers to be allocated based on the first size of the first memory region through the stack pointer, and set the address of the frame pointer to the high address of the first memory region; The allocation module is further configured to: The stack space of the function to be processed is generated based on the first memory region and the target memory regions corresponding to each of the local variables.
12. The apparatus according to claim 10, characterized in that, If there is no subfunction in the function to be processed that calls the frame pointer to access local variables, the apparatus further includes: The second region determination module is used to determine the second memory region corresponding to the register to be allocated contained in the function to be processed before the allocation module 303 sets the first move address of the stack pointer to the alignment address according to the target alignment value. The allocation module is further configured to: The stack space of the function to be processed is generated based on the second memory region and the target memory regions corresponding to each of the local variables.
13. The apparatus according to claim 11 or 12, characterized in that, The device further includes: The configuration module is used to set the specified compilation option corresponding to the compiler to the target state when all the target memory regions correspond to null values; the specified compilation option is used to indicate whether to close the frame pointer of the function to be processed; The shutdown module is used to close the frame pointer of the function to be processed in response to the specified compilation option being in the target state.
14. The apparatus according to claim 10, characterized in that, The allocation module is further specifically used for: The target alignment byte is determined based on the target alignment value, and the target alignment bit length is determined based on the target alignment byte. Set the address bits in the first move address of the stack pointer that are not greater than the target alignment bit to 0, so as to set the first move address of the stack pointer to the alignment address.
15. An electronic device, characterized in that, include: A processor, a memory, and a computer program stored in the memory and executable on the processor, characterized in that the processor, when executing the program, implements the method as described in any one of claims 1-7.
16. A readable storage medium, characterized in that, When the instructions in the storage medium are executed by the processor of the electronic device, the electronic device is able to perform the method of any one of claims 1-7.