GPU instruction optimization method and device, equipment and storage medium
By combining TEXKILL instructions and conditional instructions in the NIR optimization stage of the GLSL compiler, the register pressure problem caused by the increase in the number of NIR and assembly instructions is solved, and the instruction execution efficiency is improved.
Patent Information
- Application Number
- CN202411857152.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-17
- Publication Date
- 2025-03-25
AI Technical Summary
During the NIR optimization stage, the GLSL compiler increases the number of NIR and the number of assembly instructions, resulting in an increase in register pressure, affecting performance.
In the NIR optimization stage, the instruction set and hash table are initialized, and the TEXKILL instructions and conditional instructions are combined and optimized. In the emit assembly instruction stage, the instruction optimization is performed based on the instruction set and hash table, reducing the number of NIR and assembly instructions.
Through merge optimization, the number of NIR and the number of assembly instructions is reduced, the register pressure is reduced, and the instruction execution efficiency of the GPU is improved.
Smart Images

Figure CN119690519A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of GPU compiler technology, and in particular to a GPU instruction optimization method, device, equipment, and storage medium. Background Art
[0002] The GLSL (OpenGL Shading Language) compiler is a tool that converts high-level shader code into machine code or an intermediate representation that can be executed by the GPU (Graphics Processing Unit).
[0003] The GLSL compiler framework is as follows Figure 1 As shown, it is usually divided into three main parts: front end, middle end and back end.
[0004] 1. Front-end: responsible for lexical analysis, syntax analysis, and semantic analysis, and generating intermediate code (NIR).
[0005] 2. Mid-end: Perform various optimization operations on the generated intermediate code, including constant folding, dead code elimination, and loop unrolling.
[0006] 3. Backend: responsible for converting the optimized intermediate code into machine code of the target platform.
[0007] For NIR descent, conversion, and some heuristic algorithms, the GLSL compiler will increase the number of assembly instructions due to the increase in the number of NIRs, thereby increasing the register pressure. Summary of the invention
[0008] In order to solve one of the above technical defects, the present application provides a GPU instruction optimization method, device, equipment, and storage medium.
[0009] In a first aspect of the present application, a method for optimizing GPU instructions is provided, the method comprising:
[0010] In the NIR optimization phase, the instruction set and hash table are initialized; the function pointer is initialized, and the current function is obtained according to the function pointer; the basic block pointer is initialized according to the current function, and the current basic block is obtained according to the basic block pointer; the NIR instruction pointer is initialized according to the current basic block, and the current instruction is obtained according to the NIR instruction pointer; according to whether the current instruction is a TEXKILL instruction, the current instruction is added to the instruction set and hash table;
[0011] In the emit assembly instruction stage, instruction optimization is performed based on the instruction set and hash table.
[0012] Optionally, depending on whether the current instruction is a TEXKILL instruction, the current instruction is added to the instruction set and the hash table, including:
[0013] If the current instruction is a TEXKILL instruction, obtain the source operand of the current instruction and determine whether the source operand is a static single assignment type; if it is a static single assignment type, add the current instruction to the instruction set and hash table according to the relationship between the parent instruction of the source operand and the current instruction; if it is not a static single assignment type, proceed to the next instruction processing;
[0014] If the current instruction is not a TEXKILL instruction, proceed to the next instruction.
[0015] Optionally, according to the relationship between the parent instruction of the source operand and the current instruction, the current instruction is added to the instruction set and the hash table, including:
[0016] Determine whether the parent instruction of the source operand is the previous instruction of the current instruction, and whether the parent instruction of the source operand is a conditional instruction;
[0017] If the parent instruction of the source operand is the previous instruction of the current instruction, and the parent instruction of the source operand is a conditional instruction, determine whether the destination operand of the parent instruction of the source operand is only used in the current instruction; if it is only used in the current instruction, determine that the current instruction is an optimized instruction, add the current instruction to the instruction set, and add the instruction pair consisting of the current instruction and the parent instruction of the source operand to the hash table; if it is not only used in the current instruction, proceed to the next instruction processing;
[0018] If the parent instruction of the source operand is not the previous instruction of the current instruction, or the parent instruction of the source operand is not a conditional instruction, the next instruction is processed.
[0019] Optionally, performing next instruction processing includes:
[0020] Determine whether the current instruction is the last instruction of the current basic block;
[0021] If it is not the last instruction, the NIR instruction pointer is pointed to the next instruction, and the step of obtaining the current instruction according to the NIR instruction pointer and subsequent steps are repeated;
[0022] If it is the last instruction, determine whether the current basic block is the last basic block of the current function; if it is not the last basic block, point the basic block pointer to the next basic block, and repeat the steps of obtaining the current basic block according to the basic block pointer and subsequent steps; if it is the last basic block, when the current function is the last function in the shader program, end the NIR optimization phase; when the current function is not the last function in the shader program, point the function pointer to the next function, and repeat the steps of obtaining the current function according to the function pointer and subsequent steps.
[0023] Optionally, the instruction set is used to store conditional instructions for NIR, and the hash table is used to store pairs of <TEXKILL instruction, conditional instruction>;
[0024] Instruction optimization is performed according to the instruction set and the hash table, including:
[0025] When generating an alu instruction, if the alu instruction to be generated is in the instruction set, obtain the corresponding conditional instruction of the alu instruction to be generated according to the hash table;
[0026] If the corresponding conditional instruction is obtained successfully, perform instruction optimization according to the corresponding conditional instruction.
[0027] Optionally, performing instruction optimization according to the corresponding conditional instruction includes:
[0028] Set the corresponding condition code in the GPU instruction set architecture according to the corresponding conditional instruction;
[0029] If the condition code is NOT, generate the first source operand corresponding to the GPU instruction according to the first source operand in the corresponding conditional instruction, and modify the swizzle value of the corresponding first source operand according to the corresponding conditional instruction; if the condition code is not NOT, generate the two source operands corresponding to the GPU instruction according to the two source operands in the corresponding conditional instruction, and modify the swizzle values of the corresponding two source operands according to the corresponding conditional instruction;
[0030] Generate the merged TEXKILL instruction according to the corresponding source operand, target position, and condition code.
[0031] Optionally, after obtaining the corresponding conditional instruction of the alu instruction to be generated according to the hash table, it further includes:
[0032] If the corresponding conditional instruction fails to be obtained, directly generate a TEXKILL instruction.
[0033] In the second aspect of this application, an instruction optimization device for a GPU is provided. The device includes:
[0034] The NIR optimization stage processing module is used to initialize the instruction set and the hash table in the NIR optimization stage; initialize the function pointer, obtain the current function according to the function pointer; initialize the basic block pointer according to the current function, and obtain the current basic block according to the basic block pointer; initialize the NIR instruction pointer according to the current basic block, and obtain the current instruction according to the NIR instruction pointer; add the current instruction to the instruction set and the hash table according to whether the current instruction is a TEXKILL instruction;
[0035] The emit assembly instruction stage processing module is used to perform instruction optimization according to the instruction set and the hash table in the emit assembly instruction stage.
[0036] In a third aspect of the present application, an electronic device is provided, including:
[0037] Memory;
[0038] Processor; and
[0039] Computer programs;
[0040] The computer program is stored in the memory and is configured to be executed by the processor to implement the method as described in the first aspect above.
[0041] In a fourth aspect of the present application, a computer-readable storage medium is provided, on which a computer program is stored; the computer program is executed by a processor to implement the method described in the first aspect above.
[0042] The present application provides a method, device, equipment, and storage medium for optimizing instructions of a GPU. In the NIR optimization stage, the method initializes an instruction set and a hash table; initializes a function pointer, and obtains the current function according to the function pointer; initializes a basic block pointer according to the current function, and obtains the current basic block according to the basic block pointer; initializes a NIR instruction pointer according to the current basic block, and obtains the current instruction according to the NIR instruction pointer; adds the current instruction to the instruction set and the hash table according to whether the current instruction is a TEXKILL instruction; and optimizes instructions according to the instruction set and the hash table in the emit assembly instruction stage. The method of the present application optimizes instructions according to the instruction set and the hash table in the emit assembly instruction stage, thereby reducing the number of NIRs and the number of assembly instructions, thereby reducing register pressure. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0044] Figure 1 A schematic diagram of the framework of an existing GLSL compiler;
[0045] Figure 2 A flowchart of a GPU instruction optimization method provided in an embodiment of the present application;
[0046] Figure 3 A schematic diagram of a process flow of a NIR optimization stage provided in an embodiment of the present application;
[0047] Figure 4 A schematic diagram of a process of combining CMP and TEXKILL provided in an embodiment of the present application;
[0048] Figure 5 A schematic diagram of the structure of a GPU instruction optimization device provided in an embodiment of the present application;
[0049] Figure 6 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0050] In order to make the technical solutions and advantages in the embodiments of the present application more clearly understood, the exemplary embodiments of the present application are further described in detail below in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present application, rather than an exhaustive list of all the embodiments. It should be noted that the embodiments in the present application and the features in the embodiments can be combined with each other without conflict.
[0051] In the process of implementing the present application, the inventors found that when performing reduction, conversion and some heuristic algorithms on NIR, the GLSL compiler will increase the register pressure due to the increase in the number of NIRs and the increase in the number of assembly instructions.
[0052] In response to the above problems, an embodiment of the present application provides a GPU instruction optimization method, device, equipment, and storage medium. In the NIR optimization stage, the method initializes the instruction set and hash table; initializes the function pointer, and obtains the current function according to the function pointer; initializes the basic block pointer according to the current function, and obtains the current basic block according to the basic block pointer; initializes the NIR instruction pointer according to the current basic block, and obtains the current instruction according to the NIR instruction pointer; adds the current instruction to the instruction set and hash table according to whether the current instruction is a TEXKILL instruction; and optimizes the instruction according to the instruction set and hash table in the emit assembly instruction stage. The method of the present application optimizes the instruction according to the instruction set and hash table in the emit assembly instruction stage, reduces the number of NIRs and the number of assembly instructions, and thereby reduces the register pressure.
[0053] The TEXKILL instruction is one of the common operation operators in the GPU, and it often appears together with the CMP instruction for conditional termination. The GPU instruction optimization method provided in this embodiment is based on the GPU instruction set architecture and is specifically optimized for this instruction so that it can be merged with the CMP instruction.
[0054] For example, when there are two consecutive instructions as follows:
[0055] 1)CMP.lt r2.x r1.w 0.5 -1
[0056] 2) TEXKILL.nz r2.x
[0057] 3) Through the GPU instruction optimization method provided in this embodiment, the above instructions are converted into an instruction TEXKILL.lt r1.w 0.5 in the GPU.
[0058] The optimization work of the GPU instruction optimization method provided in this embodiment mainly focuses on the NIR optimization stage and the emit assembly instruction stage. Refer to Figure 2 , the implementation process of the GPU instruction optimization method provided in this embodiment is as follows:
[0059] 201. In the NIR optimization stage, initialize the instruction set and the hash table. Initialize the function pointer, and obtain the current function according to the function pointer. Initialize the basic block pointer according to the current function, and obtain the current basic block according to the basic block pointer. Initialize the NIR instruction pointer according to the current basic block, and obtain the current instruction according to the NIR instruction pointer. According to whether the current instruction is a TEXKILL instruction, add the current instruction to the instruction set and the hash table.
[0060] Among them, the instruction set is used to store the conditional instructions of NIR, and the hash table is used to store the <TEXKILL instruction, conditional instruction> pair.
[0061] Among them, the implementation process of adding the current instruction to the instruction set and the hash table according to whether the current instruction is a TEXKILL instruction (such as the nir_discard_if instruction, and nir_discard_if corresponds to the TEXKILL instruction of the GPU) is as follows:
[0062] If the current instruction is a TEXKILL instruction, obtain the source operand of the current instruction, and judge whether the source operand is of the static single assignment type. If it is of the static single assignment type, add the current instruction to the instruction set and the hash table according to the relationship between the parent instruction of the source operand and the current instruction. If it is not of the static single assignment type, process the next instruction.
[0063] Among them, the process of adding the current instruction to the instruction set and the hash table according to the relationship between the parent instruction of the source operand and the current instruction (i.e., the TEXKILL instruction) is as follows:
[0064] Judge whether the parent instruction of the source operand is the previous instruction of the current instruction (i.e., the TEXKILL instruction), and whether the parent instruction of the source operand belongs to the conditional instruction (i.e., judge whether the parent instruction is the previous one of the current instruction (i.e., the TEXKILL instruction) and belongs to the conditional instruction).
[0065] If the parent instruction of the source operand is the previous instruction of the current instruction, and the parent instruction of the source operand belongs to a conditional instruction, then determine whether the destination operand of the parent instruction of the source operand is used only in the current instruction. If it is used only in the current instruction, then determine that the current instruction is an optimized instruction, add the current instruction to the instruction set, and add the instruction pair composed of the current instruction and the parent instruction of the source operand to the hash table. If it is not used only in the current instruction, then process the next instruction.
[0066] If the parent instruction of the source operand is not the previous instruction of the current instruction, or the parent instruction of the source operand does not belong to a conditional instruction, then process the next instruction.
[0067] If the current instruction is not a TEXKILL instruction, then process the next instruction.
[0068] Among them, the process of processing the next instruction is as follows:
[0069] Judge whether the current instruction is the last instruction of the current basic block.
[0070] If it is not the last instruction, then point the NIR instruction pointer to the next instruction, and repeat the steps of obtaining the current instruction according to the NIR instruction pointer and the subsequent steps.
[0071] If it is the last instruction, then judge whether the current basic block is the last basic block of the current function. If it is not the last basic block, then point the basic block pointer to the next basic block, and repeat the steps of obtaining the current basic block according to the basic block pointer and the subsequent steps. If it is the last basic block, then when the current function is the last function in the shader program, end the NIR optimization phase. When the current function is not the last function in the shader program, point the function pointer to the next function, and repeat the steps of obtaining the current function according to the function pointer and the subsequent steps.
[0072] Step 201 is the process of finding the opportunity to merge CMP instructions and TEXKILL instructions. That is, in the NIR optimization phase, a hash table is constructed by executing step 201 to find the opportunity for merging, and then the CMP instruction and the TEXKILL instruction can be merged according to the hash table in the emit assembly instruction phase by executing step 202.
[0073] See Figure 3 , and explain the implementation process of step 201 (that is, the process of the NIR optimization phase) again.
[0074] 301. Initialize the instruction set and the hash table.
[0075] That is, initialize an instruction set for storing NIR conditional instructions and a hash table for storing <TEXKILL instruction, conditional instruction> pairs.
[0076] 302, initialize the function pointer.
[0077] That is, initialize the function pointer for traversal function.
[0078] 303, obtaining the current function according to the function pointer.
[0079] That is, get the current function according to the function pointer.
[0080] 304, initialize the basic block pointer according to the current function.
[0081] That is, the basic block pointer is initialized according to the current function for traversing the basic block.
[0082] 305, obtaining the current basic block according to the basic block pointer.
[0083] That is, get the current basic block according to the basic block pointer.
[0084] 306, initialize the NIR instruction pointer according to the current basic block.
[0085] That is, the NIR instruction pointer is initialized according to the current basic block to traverse the NIR instructions.
[0086] 307, obtain the current instruction according to the NIR instruction pointer.
[0087] That is, get the current instruction according to the NIR instruction pointer.
[0088] 308, determine whether the current instruction is a nir_discard_if instruction. If it is a nir_discard_if instruction (i.e., the current instruction is a TEXKILL instruction), proceed to step 309. If it is not a nir_discard_if instruction (i.e., the current instruction is not a TEXKILL instruction), proceed to step 315 to determine whether there is a next instruction and process the next instruction, i.e., enter the instruction loop traversal process.
[0089] Among them, the nir_discard_if instruction corresponds to the TEXKILL instruction of the GPU.
[0090] 309, obtain the source operand of the current instruction.
[0091] The current NIR instruction is nir_discard_if, and the corresponding source operand will be obtained according to the NIR instruction.
[0092] 310. Determine whether the source operand is of the static single assignment type. If it is of the static single assignment type, proceed to step 311 to optimize for the static single assignment case; if it is not of the static single assignment type, proceed to step 315 to determine whether there is a next instruction and process the next instruction, that is, enter the instruction loop traversal process.
[0093] 311. Find the instruction that defines the source operand based on the source operand, that is, the parent instruction of the source operand.
[0094] 312. Determine whether the parent instruction of the source operand is the instruction before the TEXKILL instruction (i.e., the current instruction), and whether the parent instruction of the source operand belongs to a conditional instruction (i.e., determine whether the parent instruction is the instruction before the TEXKILL instruction and belongs to a conditional instruction). If the parent instruction of the source operand is the instruction before the TEXKILL instruction (i.e., the current instruction) and the parent instruction of the source operand belongs to a conditional instruction, proceed to step 313; if the parent instruction of the source operand is not the instruction before the TEXKILL instruction (i.e., the current instruction), or the parent instruction of the source operand does not belong to a conditional instruction, proceed to step 315 to determine whether there is a next instruction and process the next instruction, that is, enter the instruction loop traversal process.
[0095] 313. Determine whether the destination operand of the parent instruction of the source operand is only used in the TEXKILL instruction (i.e., the current instruction).
[0096] That is, determine whether the destination operand of the conditional instruction is only used in the current TEXKILL instruction (i.e., the current instruction). After instruction fusion, the destination operand of the conditional instruction will no longer be allocated a register. If this operand is also used in other instructions, it will cause an error. Therefore, if this operand is also used in instructions other than the current TEXKILL instruction, no further processing will be performed. That is, if it is not only used in the TEXKILL instruction (i.e., the current instruction), jump to step 315 to determine whether there is a next instruction and process the next instruction, that is, enter the instruction loop traversal process. If it is only used in the TEXKILL instruction (i.e., the current instruction), proceed to step 314.
[0097] 314. Determine that the current instruction is an optimized instruction, add the current instruction to the instruction set, and add the instruction pair composed of the current instruction and the parent instruction of the source operand to the hash table.
[0098] If it is only used in the current instruction, it means that the current instruction can be merged. Therefore, the current instruction is an optimized instruction, add it to the instruction set initialized in step 301, and add the <TEXKILL instruction, conditional instruction> instruction pair to the hash table.
[0099] 315, determine whether the current instruction is the last instruction of the current basic block. If it is not the last instruction, continue to step 316, if it is the last instruction, continue to step 317 and enter the basic block loop process.
[0100] 316, point the NIR instruction pointer to the next instruction, repeat the step of obtaining the current instruction according to the NIR instruction pointer and the subsequent steps (ie, step 307 and subsequent steps), and continue to execute the next instruction.
[0101] 317, determine whether the current basic block is the last basic block of the current function. If it is not the last basic block, jump to step 318, if it is the last basic block, continue to step 319 and enter the function loop process.
[0102] 318, point the basic block pointer to the next basic block, repeat the step of obtaining the current basic block according to the basic block pointer and subsequent steps (ie, step 305 and subsequent steps), and continue to process the next basic block.
[0103] 319, determine whether the current function is the last function in the shader program, if the current function is the last function in the shader program, end the NIR optimization phase. If the current function is not the last function in the shader program, proceed to step 320.
[0104] 320, point the function pointer to the next function, repeat the step of obtaining the current function according to the function pointer and the subsequent steps (ie, step 303 and subsequent steps), and continue to process the next function.
[0105] 202, in the emit assembly instruction stage, instruction optimization is performed according to the instruction set and the hash table.
[0106] Specifically, the process of instruction optimization based on the instruction set and hash table is as follows:
[0107] When generating an alu instruction, if the alu instruction to be generated is in the instruction set, the conditional instruction corresponding to the alu instruction to be generated is obtained according to the hash table.
[0108] If the corresponding conditional instruction is obtained successfully, the instruction optimization is performed according to the corresponding conditional instruction. If the corresponding conditional instruction is obtained unsuccessfully, a TEXKILL instruction is directly generated.
[0109] Among them, the process of instruction optimization according to the corresponding conditional instructions is:
[0110] 1. Set the corresponding conditional code in the GPU instruction set architecture according to the corresponding conditional instruction.
[0111] 2. If the condition code is negated, the first source operand corresponding to the GPU instruction is generated according to the first source operand in the corresponding conditional instruction, and the swizzle value of the corresponding first source operand is modified according to the corresponding conditional instruction.
[0112] If the condition code is not negated, two source operands corresponding to the GPU instruction are generated according to the two source operands in the corresponding conditional instruction, and the swizzle values of the corresponding two source operands are modified according to the corresponding conditional instruction.
[0113] 3. Generate the merged TEXKILL instruction based on the corresponding source operands, target locations, and condition codes.
[0114] In the assembly instruction generation stage, a specific merging process can be performed when generating a TEXKILL instruction through step 202. That is, when outputting a nir_discard_if statement in the instruction stage, a CMP instruction and a TEXKILL.NZ instruction to be output are merged into a TEXKILL instruction using corresponding condition codes and comparison operands.
[0115] See also Figure 4 , the implementation process of step 202 (ie, the process of merging CMP and TEXKILL) is described again.
[0116] 401, when generating an alu instruction, determine whether the alu instruction to be generated is in the instruction set. If the alu instruction to be generated is in the instruction set, execute step 402. If the alu instruction to be generated is not in the instruction set, generate the alu instruction normally.
[0117] Conditional instructions belong to alu instructions in NIR. When generating alu instructions in the instruction generation stage, if the alu instruction currently to be generated is a conditional instruction in the set created in 201, it will not be generated, otherwise the alu instruction will be generated normally.
[0118] 402, obtain the conditional instruction corresponding to the alu instruction to be generated according to the hash table. If the corresponding conditional instruction is obtained successfully, execute step 403, if the corresponding conditional instruction is obtained unsuccessfully, no optimization is required, the merge optimization process ends, and the TEXKILL instruction is directly generated.
[0119] 403, setting a corresponding condition code in the GPU instruction set architecture according to the corresponding conditional instruction.
[0120] 404, if the condition code is negated, generate the first source operand corresponding to the GPU instruction according to the first source operand in the corresponding conditional instruction, and modify the swizzle value of the corresponding first source operand according to the corresponding conditional instruction.
[0121] Among the instructions of NIR, only the negation instruction has only one operand. Therefore, if the condition code is negation, the first source operand in the GPU instruction can be generated only based on the first source operand in the conditional instruction.
[0122] In addition, the swizzle value of the source operand in the GPU instruction set architecture is processed according to the swizzle value of the destination operand, so it is necessary to modify the swizzle value of the first source operand according to the swizzle value of the destination operand of the conditional instruction.
[0123] 405, if the condition code is not negated, two source operands corresponding to the GPU instruction are generated according to the two source operands in the corresponding conditional instruction, and the swizzle values of the two corresponding source operands are modified according to the corresponding conditional instruction.
[0124] If the condition code is not negated, there are two operands. Therefore, two source operands corresponding to the GPU instruction are generated according to the two source operands in the corresponding conditional instruction, and the swizzle values of the corresponding two source operands are modified according to the corresponding conditional instruction.
[0125] 406, generate a merged TEXKILL instruction according to the corresponding source operands, target locations, and condition codes.
[0126] That is, the merged TEXKILL instruction is generated according to the corresponding source operands, the target instruction position of the jump (ie, the target position) and the condition code.
[0127] The GPU instruction optimization method provided in this embodiment, in the emit assembly instruction stage, combines the characteristics of the GPU instruction set architecture, modifies the swizzle value of the source operand according to the swizzle value of the destination operand, and generates a merged TEXKILL instruction in combination with the conditional code, so that the GPU instruction optimization method provided in this embodiment can combine the characteristics of the GPU instruction set architecture to reduce and convert NIR, thereby reducing the number of registers used, the number of NIRs and the number of instructions.
[0128] The instruction optimization method for GPU provided in this embodiment mainly involves the NIR (intermediate code) stage and the emit assembly instruction stage (i.e., the target code generation stage). In the NIR stage, a set is initialized to store NIR conditional instructions, and a hash table is initialized to store <TEXKILL instruction, conditional instruction> pairs. A function pointer is initialized to traverse the function, then the current function is obtained according to the function pointer, and then a basic block pointer is initialized according to the current function to traverse the basic blocks. An NIR instruction pointer is initialized according to the current basic block to traverse the NIR instructions, and the nir_discard_if instruction is converted into a TEXKILL instruction of the GPU. And for the static single assignment type, the static single assignment is converted into an instruction of the source operand.
[0129] In the emit assembly instruction stage, the instruction optimization method for GPU provided in this embodiment merges the CMP instruction and the TEXKILL instruction according to the pre-constructed optimization hash table. The instruction optimization method for GPU provided in this embodiment combines the characteristics of the GPU instruction set architecture, and merges adjacent CMP and TEXKILL instructions into the corresponding source operands that directly generate GPU instructions according to the corresponding source operands in the conditional instructions.
[0130] The instruction optimization method for GPU provided in this embodiment is a GLSL compiler performance optimization method based on the merger of the CMP instruction and the subsequent TEXKILL instruction, which is an optimization technology of the GPU compiler and can significantly improve the performance of related application programs. For example, by converting NIR instructions and degrading the complexity of NIR instructions, the use of GPU registers by NIR instructions can be reduced. Thus, it is possible to reduce the number of NIR codes, the number of GPU register uses, and the number of assembly instructions. By adopting various optimization technologies such as local optimization, global optimization, loop optimization, and register allocation optimization, the execution efficiency and resource utilization rate of the compiled program can be effectively improved, and the GPU usage performance can be improved.
[0131] The instruction optimization method for GPU provided in this embodiment is suitable for GPU application programs that require strong rendering, such as 3D games, film and television animations, 4K video encoding and decoding, etc. Their underlying codes may contain a large number of conditional termination instructions for determining whether to abandon the current drawing and directly proceed to the next frame of drawing. Through the merger of instructions, it is possible to reduce the number of instructions on GPU hardware (such as domestic JM9 series GPUs) and increase the running performance of the hardware.
[0132] This embodiment provides a GPU instruction optimization method. In the NIR optimization stage, an instruction set and a hash table are initialized; a function pointer is initialized, and a current function is obtained according to the function pointer; a basic block pointer is initialized according to the current function, and a current basic block is obtained according to the basic block pointer; a NIR instruction pointer is initialized according to the current basic block, and a current instruction is obtained according to the NIR instruction pointer; according to whether the current instruction is a TEXKILL instruction, the current instruction is added to the instruction set and the hash table; in the emit assembly instruction stage, instruction optimization is performed according to the instruction set and the hash table, thereby reducing the number of NIRs and the number of assembly instructions, thereby reducing register pressure.
[0133] Based on the same inventive concept of the GPU instruction optimization method, this embodiment provides a GPU instruction optimization device, see Figure 5 , the device comprises:
[0134] The NIR optimization phase processing module 501 is used to initialize the instruction set and hash table in the NIR optimization phase. Initialize the function pointer and obtain the current function according to the function pointer. Initialize the basic block pointer according to the current function and obtain the current basic block according to the basic block pointer. Initialize the NIR instruction pointer according to the current basic block and obtain the current instruction according to the NIR instruction pointer. Add the current instruction to the instruction set and hash table according to whether the current instruction is a TEXKILL instruction.
[0135] The emit assembly instruction stage processing module 502 is used to optimize instructions according to the instruction set and the hash table at the emit assembly instruction stage.
[0136] Among them, according to whether the current instruction is a TEXKILL instruction, the current instruction is added to the instruction set and the hash table, including:
[0137] If the current instruction is a TEXKILL instruction, obtain the source operand of the current instruction and determine whether the source operand is a static single assignment type. If it is a static single assignment type, add the current instruction to the instruction set and hash table based on the relationship between the parent instruction of the source operand and the current instruction. If it is not a static single assignment type, proceed to the next instruction.
[0138] If the current instruction is not a TEXKILL instruction, proceed to the next instruction.
[0139] According to the relationship between the parent instruction of the source operand and the current instruction, the current instruction is added to the instruction set and the hash table, including:
[0140] Determine whether the parent instruction of the source operand is the previous instruction of the current instruction, and whether the parent instruction of the source operand is a conditional instruction.
[0141] If the parent instruction of the source operand is the previous instruction of the current instruction and the parent instruction of the source operand belongs to a conditional instruction, determine whether the destination operand of the parent instruction of the source operand is only used in the current instruction. If it is only used in the current instruction, determine that the current instruction is an optimized instruction, add the current instruction to the instruction set, and add the instruction pair composed of the current instruction and the parent instruction of the source operand to the hash table. If it is not only used in the current instruction, proceed to the next instruction for processing.
[0142] If the parent instruction of the source operand is not the previous instruction of the current instruction, or the parent instruction of the source operand does not belong to a conditional instruction, proceed to the next instruction for processing.
[0143] Among them, proceeding to the next instruction for processing includes:
[0144] Determine whether the current instruction is the last instruction of the current basic block.
[0145] If it is not the last instruction, point the NIR instruction pointer to the next instruction, and repeat the steps of obtaining the current instruction according to the NIR instruction pointer and subsequent steps.
[0146] If it is the last instruction, determine whether the current basic block is the last basic block of the current function. If it is not the last basic block, point the basic block pointer to the next basic block, and repeat the steps of obtaining the current basic block according to the basic block pointer and subsequent steps. If it is the last basic block, when the current function is the last function in the shader program, end the NIR optimization phase. When the current function is not the last function in the shader program, point the function pointer to the next function, and repeat the steps of obtaining the current function according to the function pointer and subsequent steps.
[0147] Among them, the instruction set is used to store the conditional instructions of NIR, and the hash table is used to store <TEXKILL instruction, conditional instruction> pairs.
[0148] Perform instruction optimization according to the instruction set and the hash table, including:
[0149] When generating an alu instruction, if the alu instruction to be generated is in the instruction set, obtain the corresponding conditional instruction of the alu instruction to be generated according to the hash table.
[0150] If the corresponding conditional instruction is obtained successfully, perform instruction optimization according to the corresponding conditional instruction.
[0151] Among them, performing instruction optimization according to the corresponding conditional instruction includes:
[0152] Set the corresponding condition code in the GPU instruction set architecture according to the corresponding conditional instruction.
[0153] If the condition code is negated, the first source operand corresponding to the GPU instruction is generated according to the first source operand in the corresponding conditional instruction, and the swizzle value of the corresponding first source operand is modified according to the corresponding conditional instruction. If the condition code is not negated, the two source operands corresponding to the GPU instruction are generated according to the two source operands in the corresponding conditional instruction, and the swizzle values of the two corresponding source operands are modified according to the corresponding conditional instruction.
[0154] Generates the merged TEXKILL instruction based on the corresponding source operands, target locations, and condition codes.
[0155] After obtaining the conditional instruction corresponding to the alu instruction to be generated according to the hash table, it also includes:
[0156] If the corresponding conditional instruction fails to be obtained, the TEXKILL instruction is directly generated.
[0157] The device provided in this embodiment initializes the instruction set and the hash table in the NIR optimization stage through the NIR optimization stage processing module; initializes the function pointer, and obtains the current function according to the function pointer; initializes the basic block pointer according to the current function, and obtains the current basic block according to the basic block pointer; initializes the NIR instruction pointer according to the current basic block, and obtains the current instruction according to the NIR instruction pointer; adds the current instruction to the instruction set and the hash table according to whether the current instruction is a TEXKILL instruction; and optimizes the instructions according to the instruction set and the hash table in the emit assembly instruction stage through the emit assembly instruction stage processing module, thereby reducing the number of NIRs and the number of assembly instructions, thereby reducing the register pressure.
[0158] Based on the same inventive concept of the GPU instruction optimization method, this embodiment provides an electronic device, the electronic device such as Figure 6 As shown, it includes: a memory 601, a processor 602, and a computer program.
[0159] The computer program is stored in the memory 601 and is configured to be executed by the processor 602 to implement the above-mentioned GPU instruction optimization method.
[0160] Specifically,
[0161] In the NIR optimization phase, the instruction set and hash table are initialized. The function pointer is initialized and the current function is obtained according to the function pointer. The basic block pointer is initialized according to the current function and the current basic block is obtained according to the basic block pointer. The NIR instruction pointer is initialized according to the current basic block and the current instruction is obtained according to the NIR instruction pointer. Depending on whether the current instruction is a TEXKILL instruction, the current instruction is added to the instruction set and hash table.
[0162] In the emit assembly instruction stage, instruction optimization is performed based on the instruction set and hash table.
[0163] Optionally, depending on whether the current instruction is a TEXKILL instruction, the current instruction is added to the instruction set and the hash table, including:
[0164] If the current instruction is a TEXKILL instruction, obtain the source operand of the current instruction and determine whether the source operand is a static single assignment type. If it is a static single assignment type, add the current instruction to the instruction set and hash table based on the relationship between the parent instruction of the source operand and the current instruction. If it is not a static single assignment type, proceed to the next instruction.
[0165] If the current instruction is not a TEXKILL instruction, proceed to the next instruction.
[0166] Optionally, according to the relationship between the parent instruction of the source operand and the current instruction, the current instruction is added to the instruction set and the hash table, including:
[0167] Determine whether the parent instruction of the source operand is the previous instruction of the current instruction, and whether the parent instruction of the source operand is a conditional instruction.
[0168] If the parent instruction of the source operand is the previous instruction of the current instruction, and the parent instruction of the source operand is a conditional instruction, determine whether the destination operand of the parent instruction of the source operand is only used in the current instruction. If it is only used in the current instruction, determine that the current instruction is an optimized instruction, add the current instruction to the instruction set, and add the instruction pair consisting of the current instruction and the parent instruction of the source operand to the hash table. If it is not only used in the current instruction, proceed to the next instruction processing.
[0169] If the parent instruction of the source operand is not the previous instruction of the current instruction, or the parent instruction of the source operand is not a conditional instruction, the next instruction is processed.
[0170] Optionally, performing next instruction processing includes:
[0171] Determines whether the current instruction is the last instruction of the current basic block.
[0172] If it is not the last instruction, the NIR instruction pointer is pointed to the next instruction, and the step of obtaining the current instruction according to the NIR instruction pointer and subsequent steps are repeated.
[0173] If it is the last instruction, determine whether the current basic block is the last basic block of the current function. If it is not the last basic block, point the basic block pointer to the next basic block, and repeat the steps of obtaining the current basic block according to the basic block pointer and subsequent steps. If it is the last basic block, when the current function is the last function in the shader program, end the NIR optimization phase. When the current function is not the last function in the shader program, point the function pointer to the next function, and repeat the steps of obtaining the current function according to the function pointer and subsequent steps.
[0174] Optionally, the instruction set is used to store NIR conditional instructions, and the hash table is used to store <TEXKILL instruction, conditional instruction> pairs.
[0175] Perform instruction optimization according to the instruction set and the hash table, including:
[0176] When generating an alu instruction, if the alu instruction to be generated is in the instruction set, obtain the corresponding conditional instruction for the alu instruction to be generated according to the hash table.
[0177] If the corresponding conditional instruction is obtained successfully, perform instruction optimization according to the corresponding conditional instruction.
[0178] Optionally, performing instruction optimization according to the corresponding conditional instruction includes:
[0179] Set the corresponding condition code in the GPU instruction set architecture according to the corresponding conditional instruction.
[0180] If the condition code is NOT, generate the first source operand of the GPU instruction according to the first source operand in the corresponding conditional instruction, and modify the swizzle value of the corresponding first source operand according to the corresponding conditional instruction. If the condition code is not NOT, generate the two source operands of the GPU instruction according to the two source operands in the corresponding conditional instruction, and modify the swizzle values of the corresponding two source operands according to the corresponding conditional instruction.
[0181] Generate a combined TEXKILL instruction according to the corresponding source operand, target location, and condition code.
[0182] Optionally, after obtaining the corresponding conditional instruction for the alu instruction to be generated according to the hash table, it further includes:
[0183] If the corresponding conditional instruction fails to be obtained, directly generate a TEXKILL instruction.
[0184] In the electronic device provided in this embodiment, the computer program thereon is executed by the processor to perform instruction optimization according to the instruction set and the hash table during the emit assembly instruction phase, reducing the number of NIRs and the number of assembly instructions, thereby reducing the register pressure.
[0185] Based on the same inventive concept of the GPU instruction optimization method, this embodiment provides a computer-readable storage medium, and a computer program is stored thereon. The computer program is executed by a processor to implement the above-mentioned GPU instruction optimization method.
[0186] Specifically,
[0187] In the NIR optimization phase, the instruction set and hash table are initialized. The function pointer is initialized and the current function is obtained according to the function pointer. The basic block pointer is initialized according to the current function and the current basic block is obtained according to the basic block pointer. The NIR instruction pointer is initialized according to the current basic block and the current instruction is obtained according to the NIR instruction pointer. Depending on whether the current instruction is a TEXKILL instruction, the current instruction is added to the instruction set and hash table.
[0188] In the emit assembly instruction stage, instruction optimization is performed based on the instruction set and hash table.
[0189] Optionally, depending on whether the current instruction is a TEXKILL instruction, the current instruction is added to the instruction set and the hash table, including:
[0190] If the current instruction is a TEXKILL instruction, obtain the source operand of the current instruction and determine whether the source operand is a static single assignment type. If it is a static single assignment type, add the current instruction to the instruction set and hash table based on the relationship between the parent instruction of the source operand and the current instruction. If it is not a static single assignment type, proceed to the next instruction.
[0191] If the current instruction is not a TEXKILL instruction, proceed to the next instruction.
[0192] Optionally, according to the relationship between the parent instruction of the source operand and the current instruction, the current instruction is added to the instruction set and the hash table, including:
[0193] Determine whether the parent instruction of the source operand is the previous instruction of the current instruction, and whether the parent instruction of the source operand is a conditional instruction.
[0194] If the parent instruction of the source operand is the previous instruction of the current instruction, and the parent instruction of the source operand is a conditional instruction, determine whether the destination operand of the parent instruction of the source operand is only used in the current instruction. If it is only used in the current instruction, determine that the current instruction is an optimized instruction, add the current instruction to the instruction set, and add the instruction pair consisting of the current instruction and the parent instruction of the source operand to the hash table. If it is not only used in the current instruction, proceed to the next instruction processing.
[0195] If the parent instruction of the source operand is not the previous instruction of the current instruction, or the parent instruction of the source operand does not belong to a conditional instruction, then proceed to the next instruction processing.
[0196] Optionally, proceeding to the next instruction processing includes:
[0197] Determine whether the current instruction is the last instruction of the current basic block.
[0198] If it is not the last instruction, then point the NIR instruction pointer to the next instruction, and repeat the steps of obtaining the current instruction according to the NIR instruction pointer and subsequent steps.
[0199] If it is the last instruction, then determine whether the current basic block is the last basic block of the current function. If it is not the last basic block, then point the basic block pointer to the next basic block, and repeat the steps of obtaining the current basic block according to the basic block pointer and subsequent steps. If it is the last basic block, then when the current function is the last function in the shader program, end the NIR optimization phase. When the current function is not the last function in the shader program, point the function pointer to the next function, and repeat the steps of obtaining the current function according to the function pointer and subsequent steps.
[0200] Optionally, an instruction set is used to store NIR conditional instructions, and a hash table is used to store <TEXKILL instruction, conditional instruction> pairs.
[0201] Perform instruction optimization according to the instruction set and the hash table, including:
[0202] When generating an alu instruction, if the alu instruction to be generated is in the instruction set, then obtain the corresponding conditional instruction for the alu instruction to be generated according to the hash table.
[0203] If the corresponding conditional instruction is successfully obtained, then perform instruction optimization according to the corresponding conditional instruction.
[0204] Optionally, performing instruction optimization according to the corresponding conditional instruction includes:
[0205] Set the corresponding condition code in the GPU instruction set architecture according to the corresponding conditional instruction.
[0206] If the condition code is NOT, then generate the first source operand corresponding to the GPU instruction according to the first source operand in the corresponding conditional instruction, and modify the swizzle value of the corresponding first source operand according to the corresponding conditional instruction. If the condition code is not NOT, then generate the two source operands corresponding to the GPU instruction according to the two source operands in the corresponding conditional instruction, and modify the swizzle values of the corresponding two source operands according to the corresponding conditional instruction.
[0207] Generates the merged TEXKILL instruction based on the corresponding source operands, target locations, and condition codes.
[0208] Optionally, after obtaining the conditional instruction corresponding to the alu instruction to be generated according to the hash table, the method further includes:
[0209] If the corresponding conditional instruction fails to be obtained, the TEXKILL instruction is directly generated.
[0210] The computer-readable storage medium provided in this embodiment has a computer program thereon which is executed by a processor to optimize instructions according to an instruction set and a hash table at the emit assembly instruction stage, thereby reducing the number of NIRs and the number of assembly instructions, thereby reducing register pressure.
[0211] Those skilled in the art will appreciate that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, the present application can adopt the form of complete hardware embodiments, complete software embodiments, or embodiments in combination with software and hardware. Moreover, the present application can adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code. The scheme in the embodiments of the present application can be implemented in various computer languages, for example, object-oriented programming language Java and literal scripting language JavaScript, etc.
[0212] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0213] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0214] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0215] Although the preferred embodiments of the present application have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications falling within the scope of the present application.
[0216] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
Claims
1. A GPU instruction optimization method, characterized in that: The method comprises: In the NIR optimization stage, the instruction set and hash table are initialized; the function pointer is initialized, and the current function is obtained according to the function pointer; the basic block pointer is initialized according to the current function, and the current basic block is obtained according to the basic block pointer; the NIR instruction pointer is initialized according to the current basic block, and the current instruction is obtained according to the NIR instruction pointer; according to whether the current instruction is a TEXKILL instruction, the current instruction is added to the instruction set and hash table; In the emit assembly instruction stage, instruction optimization is performed based on the instruction set and hash table.
2. The method according to claim 1, characterized in that: The adding the current instruction to the instruction set and the hash table according to whether the current instruction is a TEXKILL instruction comprises: If the current instruction is a TEXKILL instruction, then obtain the source operand of the current instruction, and determine whether the source operand is a static single assignment type; if it is a static single assignment type, then add the current instruction to the instruction set and the hash table according to the relationship between the parent instruction of the source operand and the current instruction; if it is not a static single assignment type, proceed to the next instruction processing; If the current instruction is not a TEXKILL instruction, the next instruction processing is performed.
3. The method according to claim 2, characterized in that The adding the current instruction to the instruction set and the hash table according to the relationship between the parent instruction of the source operand and the current instruction comprises: Determine whether the parent instruction of the source operand is the previous instruction of the current instruction, and whether the parent instruction of the source operand is a conditional instruction; If the parent instruction of the source operand is the previous instruction of the current instruction, and the parent instruction of the source operand is a conditional instruction, determine whether the destination operand of the parent instruction of the source operand is only used in the current instruction; if it is only used in the current instruction, determine that the current instruction is an optimized instruction, add the current instruction to the instruction set, and add an instruction pair consisting of the current instruction and the parent instruction of the source operand to the hash table; if it is not only used in the current instruction, proceed to the next instruction processing; If the parent instruction of the source operand is not the previous instruction of the current instruction, or the parent instruction of the source operand is not a conditional instruction, the next instruction processing is performed.
4. The method according to claim 2 or 3, characterized in that: The performing next instruction processing comprises: Determine whether the current instruction is the last instruction of the current basic block; If it is not the last instruction, the NIR instruction pointer is pointed to the next instruction, and the step of obtaining the current instruction according to the NIR instruction pointer and subsequent steps are repeatedly executed; If it is the last instruction, determine whether the current basic block is the last basic block of the current function; if it is not the last basic block, point the basic block pointer to the next basic block, and repeat the step of obtaining the current basic block according to the basic block pointer and subsequent steps; if it is the last basic block, when the current function is the last function in the shader program, end the NIR optimization phase; when the current function is not the last function in the shader program, point the function pointer to the next function, and repeat the step of obtaining the current function according to the function pointer and subsequent steps.
5. The method according to claim 1, characterized in that The instruction set is used to store the conditional instructions of NIR, and the hash table is used to store <TEXKILL instruction, conditional instruction> pairs; The instruction optimization based on the instruction set and the hash table includes: When generating an alu instruction, if the alu instruction to be generated is in the instruction set, obtain the conditional instruction corresponding to the alu instruction to be generated according to the hash table; If the corresponding conditional instruction is obtained successfully, perform instruction optimization according to the corresponding conditional instruction.
6. The method according to claim 5, characterized in that The instruction optimization according to the corresponding conditional instruction includes: Set the corresponding condition code in the GPU instruction set architecture according to the corresponding conditional instruction; If the condition code is NOT, generate the first source operand corresponding to the GPU instruction according to the first source operand in the corresponding conditional instruction, and modify the swizzle value of the corresponding first source operand according to the corresponding conditional instruction; if the condition code is not NOT, generate the two source operands corresponding to the GPU instruction according to the two source operands in the corresponding conditional instruction, and modify the swizzle values of the corresponding two source operands according to the corresponding conditional instruction; Generate a combined TEXKILL instruction according to the corresponding source operands, target location, and condition code.
7. The method according to claim 5, characterized in that After obtaining the conditional instruction corresponding to the alu instruction to be generated according to the hash table, it further includes: If the corresponding conditional instruction fails to be obtained, directly generate a TEXKILL instruction.
8. A GPU instruction optimization device, characterized in that: The device includes: An NIR optimization phase processing module, configured to initialize an instruction set and a hash table during the NIR optimization phase; initialize a function pointer, obtain the current function according to the function pointer; initialize a basic block pointer according to the current function, obtain the current basic block according to the basic block pointer; initialize an NIR instruction pointer according to the current basic block, obtain the current instruction according to the NIR instruction pointer; and add the current instruction to the instruction set and the hash table according to whether the current instruction is a TEXKILL instruction. An emit assembly instruction phase processing module, configured to perform instruction optimization according to the instruction set and the hash table during the emit assembly instruction phase.
9. An electronic device, characterized in that: It includes: A memory; A processor; And A computer program; Wherein, the computer program is stored in the memory and is configured to be executed by the processor to implement the method according to any one of claims 1-7.
10. A computer-readable storage medium, characterized in that: A computer program is stored thereon; the computer program is executed by a processor to implement the method according to any one of claims 1 to 7.