Code translation method and device, electronic equipment and readable storage medium
By determining the translation rules based on the instruction information of the target instruction during the source code migration process, and automatically calling the target translation function for instruction translation, the problems of low translation efficiency and insufficient accuracy in the existing technology are solved, and efficient and accurate instruction translation is achieved.
Patent Information
- Application Number
- CN202510293041.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-12
- Publication Date
- 2025-06-27
AI Technical Summary
Prior art requires manual writing of rules to translate SIMD functions when migrating source code from one operating system or hardware platform to another, resulting in inefficient translation and inability to ensure the accuracy of translation.
When the target instruction is included in the code to be translated on the source platform, the target translation rule is determined based on the instruction information of the target instruction, and when the target translation rule contains the complex type of instruction conversion rules, the target translation function corresponding to the target instruction name is called for instruction translation.
It improves the translation efficiency of the instruction translation process and ensures the accuracy of instruction translation through systematic instruction translation methods.
Smart Images

Figure CN120216020A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular, to a code translation method, apparatus, electronic device, and readable storage medium. Background Art
[0002] SIMD instructions, i.e., Single Instruction Multiple Data instructions, can synchronously execute the same instruction at the same time to perform parallel processing on multiple data elements.
[0003] Software needs to run on different operating systems or hardware platforms. Therefore, the source code needs to be migrated to the target platform for compilation and execution. During the process of source code migration, it often involves the conversion of SIMD functions from the source architecture, such as SIMD functions of the x86 architecture, to the target architecture, such as SIMD functions of the LoongArch architecture. The function can be an intrinsic function related to SIMD. In related technologies, the method of manually writing rules is usually used, where the semantic features of the SIMD instructions of the source architecture are analyzed manually, and corresponding translation rules are written for each SIMD function to translate the SIMD functions one by one. However, this translation method requires manual analysis and writing of translation rules for each SIMD function. Due to the high complexity of the writing process, the translation efficiency of the instruction translation process is low and the accuracy of the instruction translation cannot be ensured. Summary of the Invention
[0004] To overcome the problems existing in the related technologies, the present invention provides a code translation method, apparatus, electronic device, and readable storage medium.
[0005] In a first aspect, the present invention provides a code translation method, the method comprising:
[0006] When the code to be translated on the source platform contains a target instruction, for any one of the target instructions, based on the instruction information corresponding to the target instruction, determine the target translation rule corresponding to the target instruction; the instruction information includes the target instruction name and target instruction parameters corresponding to the target instruction;
[0007] When the target translation rule includes an instruction conversion rule of a complex type, call the target translation function corresponding to the target instruction name, and perform instruction translation on the target instruction name and the target instruction parameters to obtain a target translation instruction.
[0008] In a second aspect, the present invention provides a code translation apparatus, the apparatus comprising:
[0009] A first determination module, configured to, when a target instruction is included in the code to be translated on a source platform, determine, for any one of the target instructions, a target translation rule corresponding to the target instruction based on instruction information corresponding to the target instruction; the instruction information includes a target instruction name corresponding to the target instruction and target instruction parameters.
[0010] A first translation module, configured to, when the target translation rule includes an instruction conversion rule of a complex type, call a target translation function corresponding to the target instruction name to perform instruction translation on the target instruction name and the target instruction parameters to obtain a target translation instruction.
[0011] In a third aspect, the present invention provides an electronic device, including: a processor, a memory, and a computer program stored on the memory and executable on the processor, wherein when the processor executes the program, the instruction translation method according to any one of the above first aspects is implemented.
[0012] In a fourth aspect, the present invention provides a readable storage medium, when instructions in the storage medium are executed by a processor of an electronic device, enabling the electronic device to execute the steps in the instruction translation method according to any one of the above first aspect embodiments.
[0013] In an embodiment of the present invention, when a target instruction is included in the code to be translated on a source platform, for any target instruction, a target translation rule corresponding to the target instruction is determined based on instruction information corresponding to the target instruction; the instruction information includes a target instruction name corresponding to the target instruction and target instruction parameters; when the target translation rule includes an instruction conversion rule of a complex type, a target translation function corresponding to the target instruction name is called to perform instruction translation on the target instruction name and the target instruction parameters to obtain a target translation instruction. In this way, by determining the target translation rule corresponding to the target instruction based on the instruction information corresponding to the target instruction, for an instruction translation method with a complex instruction conversion rule, a target translation function corresponding to the target instruction name is automatically called to perform instruction translation, without manually writing translation rules for instructions one by one, improving the translation efficiency in the instruction translation process, and at the same time ensuring the accuracy of instruction translation through a systematic instruction translation method. Description of the Drawings
[0014] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the drawings in the following description are some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.
[0015] Figure 1It is a flowchart of the steps of an instruction translation method provided by an embodiment of the present invention;
[0016] Figure 2 It is a schematic diagram of the specific steps of an instruction translation method provided by an embodiment of the present invention;
[0017] Figure 3 It is a structural diagram of an instruction translation device provided by an embodiment of the present invention;
[0018] Figure 4 It is a structural diagram of an electronic device provided by an embodiment of the present invention. Detailed implementation manners
[0019] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.
[0020] Source code migration refers to the process of transferring software source code from one environment or system to another environment or system to ensure its normal operation in the new environment. Specifically, source code migration technology can convert source code suitable for one instruction set architecture into source code suitable for another instruction set architecture. In other words, source code migration technology can enable the source code of one architecture to run normally in another architecture to expand the ecosystem. For example, the source code in the x86 instruction set architecture can be migrated to the arm instruction set architecture (such as arm64), and vice versa.
[0021] In the embodiments of the present invention, the source program (source code) can be any type of application program. The source platform refers to the machine platform on which the source program can run. The processor of the source platform can be a processor based on the first instruction set architecture. The first instruction set architecture can be, for example, the x86 instruction set architecture or the arm instruction set architecture (such as arm64). The source program is a program developed based on the first instruction set architecture. Therefore, the source program can run normally on the source platform. The target platform is the machine platform on which the source program is desired to be ported. In some embodiments, the target platform can also be referred to as the host platform (HOST platform) or the host computer. The processor of the target platform can be a processor based on the second instruction set architecture. The second instruction set architecture is different from the first instruction set architecture. For example, the first instruction set architecture is the x86 instruction set architecture and the second instruction set architecture is the arm instruction set architecture. Another example is that the first instruction set architecture is the arm instruction set architecture and the second instruction set architecture is the x86 instruction set architecture. Another example is that the first instruction set architecture is the x86 instruction set architecture and the second instruction set architecture is LoongArch (Loong Architecture).
[0022] Source code migration by translation or code conversion includes the migration of platform - related functions in the source code. Such functions include, for example, built - in functions and intrinsic functions. In this application, they are referred to as "built - in functions", which refer to functions directly mapped to CPU instructions by the compiler and called by the compiler. For example, compilers suitable for the X86 platform provide a series of SSE / AVX - related built - in functions, which allow developers to directly use SIMD extensions such as SSE / AVX during compilation. Taking AVX as an example, some built - in functions may correspond to one AVX instruction, some may correspond to a combination of multiple AVX instructions; or a more advanced operation, which may be implemented by multiple AVX instructions at the underlying level. The SSE / AVX - related built - in functions of the X86 platform are hereinafter referred to as SSE / AVX functions. Compilers for other platforms, such as the LA platform, provide different built - in functions corresponding to the SIMD instructions lax and lasx of the LA architecture, hereinafter referred to as lax / lasx functions.
[0023] Although generally, SSE / AVX functions refer to built - in functions in the source code and SSE / AVX instructions refer to machine instructions in the instruction set, for the sake of convenient description, the functions may be referred to by instructions hereinafter. For example, in the example conversion process from SSE / AVX functions to lax / lasx functions, the "target instruction" may be used to refer to the SSE / AVX function to be translated in the X86 source platform, and the "macro instruction" may be used to refer to the combination of multiple lax / lasx functions corresponding to one SSE / AVX function. The rules for function migration and conversion are also called "instruction translation rules". The converted lsx / lasx functions may be called "target translation instructions".
[0024] Figure 1 It is a step - flow chart of a code translation method provided by an embodiment of the present invention, applied to a target translation tool. The target translation tool can be composed of an instruction database file, a macro instruction header file, an instruction translation module, and a translation script corresponding to the instruction conversion rule of a complex type, such as Complex.py.
[0025] Among them, the instruction database file contains a preset rule set. The preset rule set contains multiple instruction translation rules. The instruction translation rules define the correspondence between the instruction function names in the source platform and the instruction function names or macro names in the target platform. The macro instruction header file contains macro instructions that define combinations of multiple instruction contents in the target platform corresponding to the target instructions in the source platform. For example, when a certain SSE instruction or AVX instruction (SSE / AVX function) in the source platform corresponds to a combination of multiple instruction contents in the target platform, such as the combination of lsx instructions or lasx instructions (lsx / lasx functions), the combination of lsx instructions or lasx instructions will be defined as a macro instruction in the macro instruction header file. Then, based on the macro name corresponding to the macro instruction, the instruction translation rule corresponding to the SSE instruction or AVX instruction is determined, and the instruction translation rule is added to the instruction database file. The instruction translation module contains an instruction translation script. The instruction translation script is used to read the source code file containing the target instruction in the source platform, determine the instruction translation rule matching the target instruction based on the instruction translation rules in the instruction database file, and perform translation conversion of the instruction name and instruction parameters according to the matching instruction translation rule to obtain the translated instruction in the target platform. When the instruction translation rule corresponding to the target instruction contains a translation rule for complex types of instructions, the instruction translation module will call the translation script corresponding to the translation rule for complex types of instructions to translate the target instruction.
[0026] As Figure 1 shown, the method may include:
[0027] Step 101, when the target instruction is included in the code to be translated in the source platform, for any one of the target instructions, based on the instruction information corresponding to the target instruction, determine the target translation rule corresponding to the target instruction; the instruction information includes the target instruction name and target instruction parameters corresponding to the target instruction.
[0028] In the embodiments of the present invention, if it is necessary to migrate the code to be translated in the source platform, such as the x86 platform, to obtain the code that can run normally in the target platform, such as the LoongArch platform, it is necessary to first input the code to be translated in the source platform. Traverse the code to be translated to determine whether the target instruction is included in the code to be translated. Among them, the target instruction refers to a single instruction multiple data (SIMD) instruction, such as SSE instructions and AVX instructions. Different target instructions may correspond to different translation rules. In this embodiment, by pre-defining the corresponding instruction translation rules for different SIMD instructions, the instruction translation rule matching the SIMD instruction can be automatically determined during the actual translation process, and the SIMD instruction can be translated according to the instruction translation rule.
[0029] When the target instruction is included in the code to be translated, the target instructions are translated one by one. For any target instruction, the target instruction is parsed to obtain the instruction information corresponding to the target instruction. The instruction information corresponding to the target instruction may include instruction semantic information, specifically including the target instruction name corresponding to the target instruction, each target instruction parameter, the instruction return value, the data type of the instruction return value, and the data layout, etc.
[0030] Instruction translation rules corresponding to each SIMD instruction in the source platform are defined in advance and stored in the instruction database file. The instruction translation rules are used to indicate how to translate the SIMD instructions of the source platform. The instruction translation rules may include the SIMD instruction name of the source platform, the number of instruction parameters of the SIMD instruction of the source platform, the SIMD instruction name of the target platform, and the instruction conversion rule (which may be empty). Each part can be separated by a hash sign.
[0031] Exemplarily, the rule format of the instruction translation rules included in the instruction database file can be:
[0032] raw inst#op number#new inst#rules
[0033] Among them, raw inst is used to represent the instruction name of the SIMD instruction of the source platform; op number is used to represent the number of instruction parameters corresponding to the SIMD instruction of the source platform; new inst is used to represent the instruction name or macro name of the replacement instruction equivalent to the SIMD instruction of the source platform in the target platform; rules is used to represent the instruction conversion rule, which is used to define how to convert and translate the parameters of the SIMD instruction of the source platform specifically.
[0034] For SIMD instructions of different types of source platforms, there will be corresponding different instruction translation rules. Translating SIMD instructions of the source platform into instructions of the target platform can be specifically divided into 3 types of instruction translation methods. The first type is that the equivalent SIMD instruction of the target platform can be directly used for replacement (that is, only the instruction name needs to be replaced, and the number of instruction parameters and the order of instruction parameters are the same). The second type is that not only the instruction name needs to be replaced, but also the order of instruction parameters needs to be adjusted and / or parameters need to be added or deleted, etc. For example, if multiple instructions of the target platform need to be combined for translation, then both the instruction name and the instruction parameters need to be adjusted. The third type is that multiple equivalent SIMD instructions of the target platform need to be used for replacement translation, which is called the complex type, that is, a translation function needs to be called to translate the SIMD instructions of the source platform. The target instructions applicable to the third type of instruction translation rule can be specifically: a certain parameter is very flexible and can accept various types of inputs, such as immediate numbers, register values, or more complex expressions (these expressions may need to be calculated through branch logic). Exemplarily, an expression can either be optimized as an immediate number load, or be an operation between registers, or may need to be calculated through a series of complex instructions, then the third type of instruction translation rule needs to be adopted to handle these different situations to ensure that the generated code is both efficient and correct.
[0035] Exemplarily, the following are specific examples of instruction translation rules:
[0036] Instruction translation rule 1: _mm256_packs_epi16#2#lasx_packs_h
[0037] Instruction translation rule 1 can represent that the instruction name of the SIMD instruction of the source platform is _mm256_packs_epi16, the number of instruction parameters corresponding to the SIMD instruction of the source platform is 2, and the instruction name or macro name of the instruction equivalent to the SIMD instruction of the source platform in the target platform is lasx_packs_h. Instruction translation rule 1 does not include instruction conversion rules. Then instruction translation rule 1 can be used to indicate replacing the instruction name of the SIMD instruction of the source platform with lasx_packs_h, that is, completing the instruction translation of the SIMD instruction of the source platform.
[0038] Instruction translation rule 2: _mm256_unpackhi_epi16#2#__lasx_xvilvh_h#swap2
[0039] Instruction translation rule 2 can characterize that the instruction name of the SIMD instruction of the source platform is _mm256_unpackhi_epi16, the number of instruction parameters corresponding to the SIMD instruction of the source platform is 2, the instruction name or macro name of the instruction equivalent to the SIMD instruction of the source platform in the target platform is __lasx_xvilvh_h, and the instruction conversion rule is swap2 (used to indicate swapping the parameter positions of the two parameters in the SIMD instruction of the source platform). Then instruction translation rule 2 can be used to indicate replacing the instruction name of the SIMD instruction of the source platform with __lasx_xvilvh_h and swapping the parameter positions of the two parameters in the SIMD instruction of the source platform to obtain the __lasx_xvilvh_h instruction corresponding to the target platform, that is, completing the instruction translation of the SIMD instruction of the source platform.
[0040] Instruction translation rule 3: _mm256_cmp_ps#3#_mm256_cmp_ps#complex
[0041] Instruction translation rule 3 can characterize that the instruction name of the SIMD instruction of the source platform is _mm256_cmp_ps, the number of instruction parameters corresponding to the SIMD instruction of the source platform is 3, the instruction name or macro name of the instruction equivalent to the SIMD instruction of the source platform in the target platform is _mm256_cmp_ps, and the instruction conversion rule is complex, that is, the instruction conversion rule for complex types, and a translation function needs to be called to perform translation processing on the SIMD instruction of the source platform. Then instruction translation rule 3 can be used to indicate calling a translation function to perform translation processing on the SIMD instruction of the source platform to complete the instruction translation of the SIMD instruction of the source platform.
[0042] The instruction name or macro name of the replacement instruction in the instruction translation rule can be determined according to the equivalent instruction of the SIMD instruction of the source platform in the target platform. When the SIMD instruction of the source platform corresponds to an equivalent instruction as the replacement instruction in the target platform, the instruction name of the replacement instruction can be directly used as the instruction name of the replacement instruction in the instruction translation rule corresponding to the SIMD instruction. When the SIMD instruction of the source platform corresponds to at least two equivalent instructions in the target platform for equivalent replacement, a macro instruction can be defined in a specified file, such as lamicro.h, to represent the functions implemented by at least two equivalent instructions. When defining the macro instruction, the "header", "body", and "operation bit width" in the instruction name of the SIMD instruction of the source platform are translated respectively to obtain the "header", "body", and "operation bit width" of the corresponding macro instruction in the target platform to determine the macro name.
[0043] Exemplarily, taking the source platform as the x86 platform and the target platform as the LoongArch platform as an example, adding macro instructions in lamicro.h can be defined based on the following rules: 1. For 128-bit macro instructions, use "lsx_" as the head of the instruction name, and for 256-bit macro instructions, use "lasx_" as the head of the instruction name to facilitate differentiation from non-macro instructions. 2. When writing the macro instructions corresponding to the SIMD instructions of the x86 platform on the LoongArch platform, the main body of the SIMD instructions of the x86 platform needs to be retained, and the data bit width corresponding to the instruction operation should be translated into the data bit width of the LoongArch platform, and the operation bit width of the macro name is represented based on b, h, w, d, s (single-precision floating-point), d (double-precision floating-point). 3. If the SIMD instruction of the x86 platform is a floating-point instruction, add "f" at the very front of the main body as the start of the main body.
[0044] Among them, the instruction architectures of the SIMD instructions of the x86 platform and the corresponding macro instructions of the LoongArch platform can include "head", "main body", and "operation bit width". Taking _mm256_hadd_epi32 as an example, "_mm256_" is the "head", "hadd" is the "main body", and "_epi32" is the "operation bit width".
[0045] The following are specific examples of macro instructions:
[0046] _mm256_set_epi32 #8#lasx_set_w
[0047] _mm256_set1_epi8 #1#lasx_set1_b
[0048] _mm256_permutevar_ps #2#lasx_xvpermutevar_s
[0049] The instruction conversion rule can be the parameter transformation rule when performing instruction translation on the target instruction, and can be specifically determined based on the instruction set architecture of the source platform and the instruction set architecture of the target platform. Different instruction conversion rules are used to indicate different parameter transformation operations. Based on the instruction set architectures of the source platform and the target platform, multiple instruction conversion rules can be specified in advance. Exemplarily, the instruction conversion rule can include various types of rules, specifically including but not limited to:
[0050] cast(idx,type), which is used to represent converting the parameter at the idx index to the type type. When idx is 0, it means converting the return value of op to the type type;
[0051] is_memory, which is used to indicate that this instruction is a memory access type. The script will append an immediate offset of 0 to the instruction parameters of the target translation instruction on the target platform after conversion;
[0052] swap2, which is used to indicate swapping the positions of two ops with two parameters;
[0053] swap(idx1,idx2), which is used to indicate swapping the parameters at idx1 and idx2 of the op;
[0054] append(arg), which is used to indicate appending arg to the end of the parameter list (or other types of collections). append supports multiple types of arguments. arg can be a number or a keyword, such as vzero. When append has two arguments, the first argument indicates the type of append, and the second argument indicates the content of append. For example, append(arg,1) means appending the first argument of the instruction to the end of the parameter list (or other types of collections).
[0055] complex, indicating that the instruction conversion rule is of a complex type. It is used to indicate that when the instruction conversion step for SIMD instructions on the source platform is complex, a translation function needs to be called for processing. Among them, when the instruction conversion rule is complex, the content filled in the new inst position in the instruction translation rule can be the same as that of the raw inst. In the case of a complex type of instruction conversion rule, a specific translation function can be defined in the instruction conversion rule, and the translation function can be placed in the corresponding translation script of the instruction conversion rule, such as Complex.py. The translation function can use fmt+rawinst, that is, fmt+the instruction name of the SIMD instruction on the source platform, as the function name. It can be understood that the identification field of the complex type of instruction conversion rule in the instruction translation rule can be set according to actual needs, not limited to "complex". Exemplarily, the identification field of the complex type of instruction conversion rule in the instruction translation rule can be set to "aaa". That is, when the instruction translation rule contains the "aaa" field, it is determined that the instruction translation rule contains a complex type of instruction conversion rule, and then the target translation function in the corresponding translation script is called.
[0056] The instruction translation rule may not contain an instruction conversion rule or may include at least one instruction conversion rule. Correspondingly, the above three types of instruction translation methods can correspond to three ways of setting instruction conversion rules in the instruction translation rule, namely not containing an instruction conversion rule, containing at least one instruction conversion rule other than the complex type of instruction conversion rule, and containing a complex type of instruction conversion rule. For example, the instruction translation rule contains the complex identifier.
[0057] Based on the instruction information corresponding to the target instruction, obtain a preset rule set in the instruction database file, and determine a target translation rule corresponding to the target instruction based on multiple instruction translation rules in the preset rule set. Exemplarily, since the instruction translation rules include the instruction name, the number of instruction parameters, and the instruction conversion rule, therefore, based on the target instruction name in the instruction information and the target number of parameters of the target instruction, an instruction translation rule with the same instruction name as the target instruction and the same number of parameters as the target number of parameters can be determined as the target translation rule.
[0058] Step 102, in the case where the target translation rule includes a complex type of instruction conversion rule, call the target translation function corresponding to the target instruction name to perform instruction translation on the target instruction name and the target instruction parameters to obtain a target translation instruction.
[0059] In an embodiment of the present invention, after determining the target translation rule, parameter transformation processing is performed on the target instruction according to the instruction conversion rule included in the target translation rule. Determine the rule type of the instruction conversion rule. If the instruction conversion rule is of a complex type, it indicates that complex parameter transformation needs to be performed on the target instruction to achieve instruction translation, and then call the target translation function corresponding to the target instruction name. Among them, the instruction conversion rule being of a complex type refers to a complex parameter transformation rule, which can be marked with a preset identifier, such as "complex", in the instruction translation rule. Exemplarily, when the rules field in the target translation rule is the preset identifier "complex", it indicates that the instruction conversion rule is of a complex type; correspondingly, when it is not the preset identifier "complex", such as "swap2", it indicates that the instruction conversion rule is not of a complex type. Different SIMD instructions can correspond to different translation functions, and the translation functions can be predefined in the translation script in advance. The function name of the target translation function can be determined based on the target instruction name, such as fmt + target instruction name. The parameter of the target translation function can be the instruction parameter corresponding to the target instruction. For example, when the target instruction name is _mm256_cmp_ps, the function name of the target translation function can be fmt_mm256_cmp_ps; when the target instruction name is _mm_shuffle_epi32, the function name of the target translation function can be fmt_mm_shuffle_epi32. Further, the target translation function can be fmt_mm_shuffle_epi32(params), where params is the target instruction parameter obtained by parsing the target instruction.
[0060] Exemplarily, when the instruction conversion rule included in the target translation rule is of a complex type, such as "complex", the instruction translation module will automatically call the translation script corresponding to the instruction conversion rule of the complex type. Search for the target translation function that matches the domain target instruction name among the multiple translation functions predefined in the translation script based on the target instruction name, and translate the target instruction name and the target instruction parameters based on the target translation function.
[0061] Since the target translation function is a translation function predefined in the translation script, after calling the target translation function and running the target translation function, the processing logic of the target translation function will be executed on the target instruction to obtain the target translation instruction. Among them, the target translation instruction is the target translation instruction obtained in the target platform after translating the target instruction of the source platform. The semantics of the target translation instruction are the same as those of the target instruction.
[0062] In summary, in the embodiments of the present invention, when the target instruction is included in the code to be translated on the source platform, for any target instruction, based on the instruction information corresponding to the target instruction, determine the target translation rule corresponding to the target instruction; the instruction information includes the target instruction name and the target instruction parameters corresponding to the target instruction; when the target translation rule includes an instruction conversion rule of a complex type, call the target translation function corresponding to the target instruction name to perform instruction translation on the target instruction name and the target instruction parameters to obtain the target translation instruction. In this way, by determining the target translation rule corresponding to the target instruction based on the instruction information corresponding to the target instruction, for the instruction translation method with a complex instruction conversion rule, automatically call the target translation function corresponding to the target instruction name to perform instruction translation, without manually writing translation rules for each instruction one by one, improving the translation efficiency of the instruction translation process, and at the same time ensuring the accuracy of the instruction translation through a systematic instruction translation method.
[0063] Optionally, step 101 may include the following steps:
[0064] Step 201: Parse the target instruction to obtain the target instruction name and the target instruction parameters corresponding to the target instruction.
[0065] In the embodiments of the present invention, the target instruction is parsed to obtain the instruction information corresponding to the target instruction. Among them, the instruction information may include the target instruction name corresponding to the target instruction, each target instruction parameter, the instruction return value, the data type of the instruction return value, and the data layout, etc.
[0066] Exemplarily, the target instruction is parsed to receive the target instruction, and the opcode part in the target instruction is parsed to identify the target instruction name. The parameter part in the target instruction is parsed to extract each target instruction parameter, and according to the instruction definition, the data type and bit width of each target instruction parameter are determined. At the same time, according to the instruction definition, the return value of the target instruction and its data type (such as integer type, floating-point type, vector type, etc.) are determined, and the relevant part in the target instruction is parsed to determine the return value bit width information of the return value and the arrangement order of vector elements (such as row-major or column-major). According to the instruction definition and data type, the data layout information is parsed, including the storage order of vector elements, the memory alignment requirements of the vector, etc. The parsed instruction information is output in a structured format, which may include the instruction name string, parameter list (each parameter includes data type and value), return value data type, return value bit width, and data layout description, etc.
[0067] Step 202, based on the preset rule set, determine the instruction translation rule that matches the target instruction name and the number of parameters of the target instruction parameters as the target translation rule; the preset rule set contains multiple instruction translation rules for characterizing the correspondence between the instruction name, the number of parameters of the instruction parameters, and the instruction conversion rule.
[0068] In the embodiment of the present invention, multiple instruction translation rules are defined in the preset rule set. Each instruction translation rule is used to characterize the correspondence between different instruction names, the number of parameters of the instruction parameters, and the instruction conversion rule. Correspondingly, the instruction translation rule may include the instruction name, the number of parameters of the instruction parameters, and the instruction conversion rule. Based on the target instruction name and the number of parameters of the target instruction parameters, it is possible to match the instruction name and the number of parameters of the instruction parameters in the instruction translation rules in the preset rule set. When the instruction name in the instruction translation rule is the same as the target instruction name, and the number of parameters of the instruction parameters is the same as the number of parameters of the target instruction parameters, it is determined that the instruction translation rule matches the target instruction name and the number of parameters of the target instruction parameters, and then this instruction translation rule is determined as the target translation rule.
[0069] In the embodiment of the present invention, by setting the preset rule set, based on the target instruction name and the target instruction parameters obtained by parsing the target instruction, the target translation rule that matches them is determined in the preset rule set, which can quickly locate the target translation rule that best matches the target instruction, enhancing the flexibility and scalability of the translation process.
[0070] Optionally, step 102 may include the following steps:
[0071] Step 301, based on the target instruction name, determine the target translation function corresponding to the target instruction name.
[0072] In an embodiment of the present invention, a plurality of translation functions are predefined in a translation script corresponding to the instruction conversion rules of complex types. According to the target instruction name of the target instruction, the function name of the target translation function is determined, and then based on the function name, the target translation function is determined in the translation script.
[0073] Exemplarily, assuming that the target instruction name is _mm256_cmp_ps, the function name of the target translation function can be determined as fmt_mm256_cmp_ps. Correspondingly, a translation function with the same function name is searched for in the translation script as the target translation function.
[0074] Step 302: Pass the target instruction parameters to the target translation function.
[0075] In an embodiment of the present invention, each target instruction parameter corresponding to the target instruction is passed to the target translation function. Specifically, each target instruction parameter corresponding to the target instruction can be used as an input parameter to input into the target translation function.
[0076] Step 303: Run the target translation function to obtain the instruction translation name and instruction translation parameters output by the target translation function.
[0077] In an embodiment of the present invention, when the target translation function is run, the target instruction parameters will be necessarily processed or converted inside the target translation function according to the preset rules or logic of the function to generate corresponding translation results. Among them, the translation results may include the instruction translation name and instruction translation parameters. Exemplarily, the translation results of the target translation function can be output in the form of a list. The first item in the list is the instruction translation name, and the subsequent items are the instruction translation parameters in sequence.
[0078] When executing the target translation function, for the target instruction parameters that require complex translation processing, the target translation function will first determine whether the instruction parameters can be fused based on the target instruction parameters. When the target instruction parameters contain an instruction and instruction fusion can be performed, the translated instruction name after fusion is determined, and according to the parameter type of the target instruction parameters and the register mapping relationship between the source platform and the target platform, the target instruction parameters are translated into corresponding instruction translation parameters that conform to the target architecture of the target platform. Exemplarily, when the parameter type of the target instruction parameters is a direct operand, the instruction translation parameters corresponding to the target instruction parameters are determined according to the register mapping relationship between the source platform and the target platform; when the parameter type of the target instruction parameters is a control parameter, the target instruction parameters are adjusted according to the instruction format corresponding to the translated instruction name after fusion. For example: Some operation codes may no longer be applicable after instruction fusion and need to be replaced with the operation codes corresponding to the fused instructions of the target platform. If the target instruction parameters contain an instruction but instruction fusion cannot be performed, the target instruction parameters are decomposed to obtain independent instructions (i.e., the instructions contained in the target instruction parameters) and other instruction parameters. The independent instructions are translated separately, including instruction set mapping, register renaming, etc., to generate equivalent instructions in the target platform. Based on the translated equivalent instructions, the other instruction parameters of the target instruction are correspondingly adjusted, and based on the adjusted other instruction parameters and the equivalent instructions in the target platform, the instruction translation parameters are determined. Based on the instruction set of the target architecture of the target platform, the translated instruction name corresponding to the target instruction is determined.
[0079] Optionally, step 303 may include the following steps:
[0080] Step 401, when any instruction is included in the target instruction parameters, determine the target fusion instruction name that matches the first instruction name corresponding to the target instruction name and the instruction included in the target instruction parameters as the translated instruction name.
[0081] In the embodiments of the present invention, according to the instruction set architecture of the target platform in advance, multiple groups of instruction names of at least two instructions that can be fused in the target platform and the fused instruction names after instruction fusion are determined as the translated instruction names. The target instruction parameters are traversed to determine whether an instruction is included in the target instruction parameters. When an instruction is included in the target instruction parameters, the target fusion instruction name that matches the first instruction name corresponding to the target instruction name and the instruction included in the target instruction parameters is determined.
[0082] Construct an instruction fusion list with the instruction names of at least two instructions for pre-determined multi-group instruction fusion and the fused instruction name after instruction fusion. According to the instruction characteristics, determine whether an instruction is included in the target instruction parameter. For example, SIMD instructions on the x86 platform often start with _mm or _mm256, so it can be detected whether the first 3 characters of each target instruction parameter are _mm to determine whether an instruction is included in the target instruction parameter. When the list content identical to the target instruction name and the first instruction name is matched in the instruction fusion list, determine its corresponding fused instruction name as the target fused instruction name. For example: the content included in the instruction fusion list is: "mm_mul_ps", "mm_add_ps", "__lsx_vfmadd_s". Then, when the target instruction name is mm_mul_ps and the first instruction name is mm_add_ps, determine __lsx_vfmadd_s as the target fused instruction name. Exemplarily, assume the target instruction is __m128 vr = _mm_add_ps(_mm_mul_ps(va, vb), vc), and the content included in the instruction fusion list includes: "mm_mul_ps", "mm_add_ps", "__lsx_vfmadd_s". After parsing the target instruction, the target instruction name is _mm_add_ps, and the target instruction parameters are _mm_mul_ps(va, vb) and vc respectively. Then, according to the instruction characteristics, it is determined that the target instruction parameter includes an instruction. Based on the target instruction name _mm_add_ps and the first instruction name _mm_mul_ps, a search will be conducted in the instruction fusion list. The search rule is that there are only two fused instructions, the first item is mm_mul_ps, and the second item is mm_add_ps, then the match is successful, and the target fused instruction name is lsx_vfmadd_s.
[0083] Step 402: Map the target instruction parameter based on the parameter type of the target instruction parameter to obtain the instruction translation parameter.
[0084] In the embodiments of the present invention, according to the parameter type of the target instruction parameter and the register mapping relationship between the source platform and the target platform, the target instruction parameter is translated into a corresponding instruction translation parameter that conforms to the target architecture of the target platform. Among them, the parameter types of the target instruction parameter include direct operands (such as registers, memory addresses, etc.) and control parameters (such as operation codes, masks, etc.). Exemplarily, when the parameter type of the target instruction parameter is a direct operand, the instruction translation parameter corresponding to the target instruction parameter is determined according to the register mapping relationship between the source platform and the target platform; when the parameter type of the target instruction parameter is a control parameter, the target instruction parameter is adjusted according to the instruction format corresponding to the fused instruction translation name. For example: some operation codes may no longer be applicable after instruction fusion and need to be replaced with the operation codes corresponding to the fused instructions of the target platform.
[0085] In the embodiments of the present invention, when any instruction is included in the target instruction parameter, by determining instruction fusion, multiple operations can be combined into one instruction, reducing the overhead of instruction fetching, decoding, and distribution, etc. of the processor on the target platform, thereby improving the utilization rate of resources.
[0086] Step 304: Concatenate the instruction translation name and the instruction translation parameter to obtain the target translation instruction.
[0087] In the embodiments of the present invention, the instruction translation name and the instruction translation parameter are concatenated to obtain the target translation instruction. Exemplarily, assuming that the instruction translation name output by the target translation function is aa, and the instruction translation parameters output by the target translation function are arg1, arg2, arg3 in the output order, the target translation instruction can be aa(arg1, arg2, arg3).
[0088] In the embodiments of the present invention, the instruction translation name and the instruction translation parameter are output based on the target instruction parameter through the target translation function to determine the target translation instruction, which can accurately translate the target instruction name and the target instruction parameter, ensuring the accuracy and consistency of the target instruction in a cross-platform environment, and contributing to maintaining the stability and reliability of instruction translation.
[0089] Optionally, the embodiments of the present invention may further include the following steps:
[0090] Step 501: When the target translation rule includes an instruction conversion rule other than the instruction conversion rule for complex types, determine the output instruction name based on the first instruction name in the target translation rule.
[0091] In an embodiment of the present invention, when at least one instruction conversion rule other than the instruction conversion rule for complex types is included in the target translation rule, for example, instruction conversion rules such as "is_memory" and "swap2", it indicates that during the translation of the target instruction, both the target instruction name and the target instruction parameters of the target instruction need to be adjusted. Since the first instruction name and specific instruction conversion rules are defined in the target translation rule, the output instruction name can be determined based on the first instruction name defined in the target translation rule. Among them, the first instruction name can be determined in advance based on the instruction name equivalent to the target instruction in the target platform. Specifically, the first instruction name can be directly used as the output instruction name.
[0092] Step 502: Process the target instruction parameters based on the parameter processing operation indicated by the instruction conversion rule to obtain the first instruction parameters.
[0093] In an embodiment of the present invention, the target instruction parameters are processed based on the parameter processing operation indicated in the instruction conversion rule to obtain the first instruction parameters. Different instruction conversion rules indicate different parameter processing operations. For example, the instruction conversion rule "swap(idx1,idx2)" is used to indicate swapping the parameters at idx1 and idx2 of op. When the number of instruction conversion rules is at least two, when processing the parameters of the target instruction, in the order of arrangement of the instruction conversion rules in the target translation rule, for example, in the order from left to right in the target translation rule, the parameter processing operations defined by each instruction conversion rule are sequentially performed on the target instruction parameters, and the execution of the subsequent instruction conversion rule is based on the execution result of the previous instruction conversion rule to obtain the first instruction parameters. Exemplarily, assume the instruction translation rule is:
[0094] _mm256_broadcast_ss#1#__lasx_xvldrepl_w#is_memory#cast(0,__m256)
[0095] When the target instruction name of the target instruction is _mm256_broadcast_ss and the number of target instruction parameters is 1, determine that the translation rule for this instruction is the target translation rule. Then convert the target instruction name _mm256_broadcast_ss to __lasx_xvldrepl_w, is_memory indicates that this is a memory access instruction, and an immediate offset 0 will be appended to the converted la instruction parameters. cast(0, __m256) means converting the return value type to __m256+. Then the target instruction _mm256_broadcast_ss(addr) will be converted to (__m256)__lasx_xvldrepl_w(addr,0), which is the target translation instruction.
[0096] Step 503, determine the target translation instruction based on the output instruction name and the first instruction parameter.
[0097] In the embodiments of the present invention, the output instruction name and the first instruction parameter are combined to obtain the target translation instruction. Exemplarily, assume that the instruction translation name output by the target translation function is aa, and the instruction translation parameters output by the target translation function are arg1, arg2, arg3 in the output order. Then the target translation instruction can be aa(arg1,arg2,arg3).
[0098] In the embodiments of the present invention, when the target translation rule includes an instruction conversion rule other than the instruction conversion rule for complex types, based on the first instruction name in the target translation rule and the parameter processing operation defined by the instruction conversion rule, the target instruction is translated to obtain the target translation instruction, which can quickly and accurately translate the target instruction according to the translation method indicated in the target translation rule, improving the translation efficiency of the instruction translation process.
[0099] Optionally, the embodiments of the present invention may further include the following steps:
[0100] Step 601, when the target translation rule does not include an instruction conversion rule, replace the target instruction name based on the second instruction name of the target translation rule to obtain the target translation instruction.
[0101] In the embodiments of the present invention, when the target translation rule does not include an instruction conversion rule, it indicates that there is an equivalent instruction corresponding to the target instruction in the target platform. It is necessary to adjust the target instruction name, but there is no need to convert the target instruction parameters. Therefore, based on the second instruction name in the target translation rule, on the basis of the target instruction, the target instruction name can be replaced while keeping the target instruction parameters unchanged to obtain the target translation instruction.
[0102] Exemplarily, assuming that the target translation rule is _mm256_div_ps#2#__lasx_xvfdiv_s, when the target instruction name of the target instruction is _mm256_div_ps and the number of parameters of the target instruction parameters is two, _mm256_div_ps of the target instruction can be replaced with __lasx_xvfdiv_s while keeping the target instruction parameters unchanged to obtain the target translation instruction.
[0103] Exemplarily, Figure 2 A specific step flowchart of an instruction translation method is shown. As Figure 2 shown, load multiple instruction translation rules in the instruction database file. Read the code to be translated on the source platform. When the code to be translated contains SIMD instructions, parse the SIMD instructions to obtain instruction information. Based on the instruction name and instruction parameters in the instruction information, determine the instruction translation rule that matches the SIMD instruction among the multiple instruction translation rules. Based on the instruction conversion rule in the instruction translation rule and the instruction name of the replacement instruction, process the target instruction to obtain the target translation instruction. Specifically, when the instruction translation rule includes a complex type of instruction conversion rule, based on calling the target translation function corresponding to the SIMD instruction, perform translation processing on the instruction name and instruction parameters of the SIMD instruction to obtain the target translation instruction. For example, a complex SIMD instruction can be mapped to a set of SIMD instructions on the target platform. After all SIMD instructions in the code to be translated are translated and all other instructions are translated, generate the target code corresponding to the target platform. This target code can run normally on the target platform and can be compiled and run normally on the target platform.
[0104] Figure 3 It is a schematic structural diagram of a code translation device provided by an embodiment of the present invention. As Figure 3 shown, the device may specifically include:
[0105] The first determination module 701 is configured to, when the code to be translated on the source platform contains a target instruction, for any one of the target instructions, determine the target translation rule corresponding to the target instruction based on the instruction information corresponding to the target instruction; the instruction information includes the target instruction name and target instruction parameters corresponding to the target instruction;
[0106] The first translation module 702 is configured to, when the target translation rule includes a complex type of instruction conversion rule, call the target translation function corresponding to the target instruction name, and perform instruction translation on the target instruction name and the target instruction parameters to obtain the target translation instruction.
[0107] An embodiment of the present invention provides an instruction translation device. When the code to be translated on the source platform contains a target instruction, for any target instruction, based on the instruction information corresponding to the target instruction, determine the target translation rule corresponding to the target instruction; the instruction information includes the target instruction name corresponding to the target instruction and the target instruction parameters; when the target translation rule includes an instruction conversion rule of a complex type, call the target translation function corresponding to the target instruction name to perform instruction translation on the target instruction name and the target instruction parameters to obtain the target translation instruction. In this way, by determining the target translation rule corresponding to the target instruction based on the instruction information corresponding to the target instruction, for the instruction translation method with complex instruction conversion rules, automatically call the target translation function corresponding to the target instruction name to perform instruction translation, without manually writing translation rules for each instruction one by one, improving the translation efficiency of the instruction translation process, and at the same time ensuring the accuracy of the instruction translation through a systematic instruction translation method.
[0108] Optionally, the first determination module 701 includes:
[0109] A first parsing module, configured to parse the target instruction to obtain the target instruction name and the target instruction parameters corresponding to the target instruction;
[0110] A first determination sub-module, configured to determine, based on a preset rule set, an instruction translation rule that matches the number of parameters of the target instruction name and the target instruction parameters as the target translation rule; the preset rule set contains multiple instruction translation rules used to represent the correspondence between the instruction name, the number of parameters of the instruction parameters, and the instruction conversion rule.
[0111] Optionally, the first translation module 702 includes:
[0112] A second determination sub-module, configured to determine, based on the target instruction name, a target translation function corresponding to the target instruction name;
[0113] A first transfer module, configured to transfer the target instruction parameters to the target translation function;
[0114] A first running module, configured to run the target translation function to obtain the instruction translation name and the instruction translation parameters output by the target translation function;
[0115] A first combination module, configured to combine the instruction translation name and the instruction translation parameters to obtain the target translation instruction.
[0116] Optionally, the first running module includes:
[0117] A third determination sub-module, configured to, when any instruction is included in the target instruction parameter, determine a target fusion instruction name that matches the first instruction name corresponding to the target instruction name and the instruction included in the target instruction parameter as the instruction translation name;
[0118] A first mapping module, configured to map the target instruction parameter based on the parameter type of the target instruction parameter to obtain the instruction translation parameter.
[0119] Optionally, the apparatus further includes:
[0120] A second determination module, configured to, when the target translation rule includes an instruction conversion rule other than the instruction conversion rule of the complex type, determine the output instruction name based on the first instruction name in the target translation rule;
[0121] A first processing module, configured to process the target instruction parameter based on the parameter processing operation indicated by the instruction conversion rule to obtain a first instruction parameter;
[0122] A third determination module, configured to determine the target translation instruction based on the output instruction name and the first instruction parameter.
[0123] Optionally, the apparatus further includes:
[0124] A first replacement module, configured to, when the target translation rule does not include an instruction conversion rule, replace the target instruction name based on the second instruction name in the target translation rule to obtain the target translation instruction.
[0125] The present invention also provides an electronic device. Refer to Figure 4 , Figure 4 which is a schematic structural diagram of the electronic device provided in the embodiment of the present invention. The electronic device includes: a processor, a memory, a communication interface, and a communication bus. The processor, the memory, and the communication interface complete communication with each other through the communication bus; the memory is used to store at least one executable instruction, and the executable instruction enables the processor to execute the steps of the instruction translation method in the foregoing embodiment.
[0126] The present invention also provides a readable storage medium. When the instructions in the storage medium are executed by the processor of the electronic device, the electronic device can execute the instruction translation method in the foregoing embodiment.
[0127] For the apparatus embodiment, since it is basically similar to the method embodiment, the description is relatively simple. For the relevant parts, refer to the partial description of the method embodiment.
[0128] The algorithms and displays provided herein are not inherently related to any particular computer, virtual system, or other apparatus. Various general-purpose systems may also be used in conjunction with the teachings based hereon. The structure required to construct such systems will be apparent from the above description. In addition, the present invention is not directed to any particular programming language. It should be appreciated that the present invention as described herein may be implemented using various programming languages, and the description of specific languages above is for the purpose of disclosing the best mode of the present invention.
[0129] In the specification provided herein, numerous specific details are set forth. 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 description.
[0130] Similarly, it should be understood that in order to streamline the present invention and assist in understanding one or more of the various inventive aspects, in the foregoing description of exemplary embodiments of the present invention, the various features of the present invention are sometimes grouped together in a single embodiment, figure, or description thereof. However, the disclosed method should not be construed as reflecting an intention that the claimed invention requires more features than are expressly recited in each claim. Rather, as the following claims reflect, the inventive aspects lie in less than all of the features of the preceding disclosed single embodiment. Thus, the claims following the detailed description are hereby expressly incorporated into this detailed description, with each claim standing on its own as a separate embodiment of the present invention.
[0131] Those skilled in the art will appreciate that the modules in the devices in the embodiments can be adaptively changed and disposed in one or more devices different from the embodiments. The modules or units or components in the embodiments can be combined into one module or unit or component, and in addition, they can be divided into multiple sub-modules or sub-units or sub-components. Except that at least some of such features and / or processes or units are mutually exclusive, any combination can be used to combine all the features disclosed in this specification (including the accompanying claims, abstract and drawings) and all the processes or units of any method or device so disclosed. Unless otherwise expressly stated, each feature disclosed in this specification (including the accompanying claims, abstract and drawings) can be replaced by an alternative feature providing the same, equivalent or similar purpose.
[0132] Each component embodiment of the present invention can be implemented in hardware, or in software modules running on one or more processors, or in a combination thereof. Those skilled in the art should understand that a microprocessor or a digital signal processor (DSP) 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 for 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, or provided on a carrier signal, or provided in any other form.
[0133] It should be noted that the above embodiments are illustrative of the present invention rather than restrictive of the present invention, and those skilled in the art can design alternative embodiments without departing from the scope of the appended claims. In the claims, any reference signs placed between parentheses shall not be construed as limiting the claim. The word "comprising" does not exclude the presence of elements or steps not listed in the claim. The word "a" or "an" preceding an element does not exclude the presence of a plurality of such elements. The present invention can be implemented by means of hardware including several different elements and by means of a suitably programmed computer. In a unit claim listing several devices, several of these devices can be embodied by the same item of hardware. The use of the words first, second, and third, etc. does not denote any order. These words can be interpreted as names.
[0134] Those skilled in the art can clearly understand that for the sake of convenience and brevity of description, the specific working processes of the systems, devices, and units described above can refer to the corresponding processes in the foregoing method embodiments and will not be repeated here.
[0135] It should be pointed out that all actions of obtaining signals, information, or data in this application are carried out on the premise of complying with the corresponding data protection regulations and policies of the country where the location is located and obtaining authorization from the owner of the corresponding device.
[0136] The above are only the preferred embodiments of the present invention and are not intended to limit the present invention. Any modifications, equivalent replacements, and improvements made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
[0137] The above is only the specific implementation manner of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art can easily think of changes or replacements within the technical scope disclosed by the present invention, and all of them should be covered by the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.
Claims
1. A code translation method, characterized in that: The method comprises: In the case where the source platform's to-be-translated code includes a target instruction, for any of the target instructions, determining a target translation rule corresponding to the target instruction based on instruction information corresponding to the target instruction; the instruction information includes a target instruction name and target instruction parameters corresponding to the target instruction; In the case where the target translation rule includes a complex type of instruction conversion rule, the target translation function corresponding to the target instruction name is called, and instruction translation is performed on the target instruction name and the target instruction parameters to obtain a target translation instruction.
2. The method according to claim 1, characterized in that: The determining, based on the instruction information corresponding to the target instruction, a target translation rule corresponding to the target instruction comprises: Parsing the target instruction to obtain a target instruction name and target instruction parameters corresponding to the target instruction; Based on a preset rule set, an instruction translation rule that matches the target instruction name and the number of parameters of the target instruction parameters is determined as the target translation rule; the preset rule set includes multiple instruction translation rules for characterizing the correspondence between the instruction name, the number of parameters of the instruction parameters and the instruction conversion rules.
3. The method according to claim 1, characterized in that: The calling of the target translation function corresponding to the target instruction name, performing instruction translation on the target instruction name and the target instruction parameter, and obtaining the target translation instruction includes: Based on the target instruction name, determining a target translation function corresponding to the target instruction name; Passing the target instruction parameter to the target translation function; Running the target translation function to obtain the instruction translation name and instruction translation parameters output by the target translation function; The instruction translation name and the instruction translation parameter are combined to obtain the target translation instruction.
4. The method according to claim 3, characterized in that The step of running the target translation function to obtain the instruction translation name and instruction translation parameters output by the target translation function includes: In the case where the target instruction parameter includes any instruction, determining a target fusion instruction name that matches the target instruction name and a first instruction name corresponding to the instruction included in the target instruction parameter as the instruction translation name; Based on the parameter type of the target instruction parameter, the target instruction parameter is mapped to obtain the instruction translation parameter.
5. The method according to claim 1, characterized in that The method further comprises: In the case where the target translation rule includes instruction conversion rules other than complex type instruction conversion rules, determining the output instruction name based on the first instruction name in the target translation rule; Based on the parameter processing operation indicated by the instruction conversion rule, the target instruction parameter is processed to obtain a first instruction parameter; The target translation instruction is determined based on the output instruction name and the first instruction parameter.
6. The method according to claim 1, characterized in that The method further comprises: In the case that the target translation rule does not include an instruction conversion rule, the target instruction name is replaced based on the second instruction name in the target translation rule to obtain the target translation instruction.
7. The method according to claim 1, characterized in that The target instructions of the code to be translated include intrinsic functions or built-in functions related to SIMD instructions of the source platform, and the target translation function translates the intrinsic functions or built-in functions of the source platform into intrinsic functions or built-in functions of the target platform to be transplanted.
8. The method according to claim 7, characterized in that In a case where at least one of the parameters of the intrinsic function or built-in function is another intrinsic function or built-in function of the source platform, the instruction conversion rule of the complex type is used.
9. The method according to claim 8, characterized in that The target platform intrinsic function corresponding to the intrinsic function and another intrinsic function as a parameter is stored in the instruction fusion list; the target translation function translates the target platform intrinsic function by searching the instruction fusion list.
10. A code translation device, characterized in that: The device comprises: A first determination module is used to determine, for any target instruction, a target translation rule corresponding to the target instruction based on instruction information corresponding to the target instruction when the source platform's to-be-translated code includes the target instruction; the instruction information includes a target instruction name and target instruction parameters corresponding to the target instruction; The first translation module is used to call the target translation function corresponding to the target instruction name when the target translation rule contains a complex type of instruction conversion rule, perform instruction translation on the target instruction name and the target instruction parameters, and obtain a target translation instruction.
11. An electronic device, characterized in that: include: A processor, a memory, and a computer program stored in the memory and executable on the processor, wherein the processor implements the code translation method according to any one of claims 1 to 9 when executing the program.
12. A readable storage medium, characterized in that: When the instructions in the storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the code translation method described in any one of claims 1 to 9.