A compiler backend code automatic construction method and system
By automatically building the compiler back-end code, it solves the problems of low compiler development efficiency and difficulty, enables rapid adaptation to different target platforms, reduces development costs and time, and is suitable for agile compiler development.
Patent Information
- Application Number
- CN202111563671.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-20
- Publication Date
- 2024-09-17
- Estimated Expiration
- 2041-12-20
AI Technical Summary
The existing compiler development process suffers from low R&D efficiency and high difficulty. Especially when faced with the rapid design of new chips, the compiler back-end development speed cannot match, resulting in 'agile compiler development challenges', and manual writing of target description files is prone to errors. And it’s time-consuming.
By obtaining the back-end description file of the sample platform, constructing an identifier reference graph, classifying identifiers and converting them into agile identifiers, generating code layout templates, matching target platform instructions and filling in feature information to automatically generate compiler back-end code, Use Word2vec to calculate feature vectors for instruction matching to achieve automated compiler back-end code construction.
It greatly reduces the labor and time costs of compiler back-end development, improves research and development efficiency, adapts to different target platforms, and is versatile and scalable, allowing chip developers to quickly support the compiler of new chips without having to understand the compiler in depth.
Smart Images

Figure CN114237573B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of compiler technology, and more specifically, to compiler backend code construction technology, and more specifically, to a compiler backend code automatic construction method and system. Background Art
[0002] Compiler technology is the link between new processors and field applications, and is usually designed at the same time as new processors. The quality of compiler technology largely determines whether the processor's personalized design and architecture advantages can be fully utilized. However, the compiler R&D cycle is relatively long and the R&D process is difficult. This is the so-called "slow and difficult" problem in compiler R&D efficiency.
[0003] The problem of compiler R&D efficiency is actually caused by the role of the compiler itself. The functional characteristics of the compiler itself require that compiler R&D personnel need to reserve a large amount of cross-domain knowledge. The compiler development process requires not only in-depth understanding of the compiler infrastructure and mature algorithms, but also a certain understanding of the target system architecture, which brings great difficulties to R&D personnel: first, the most urgently needed hardware platform-oriented porting of chip processors requires a long period of exploration, and the labor and time costs are very high; second, the compiler is complex and difficult to learn, and the core functions of the compiler - performance analysis and optimization are recognized as the two most difficult stages, which require a long period of experience accumulation.
[0004] In particular, the emergence of agile chip design methods has greatly accelerated the development of new architectures, which means that a large number of new chips will be released in a short period of time. It is impossible to wait for a long time to develop compilers corresponding to the chips, which makes the compiler back-end development speed lag far behind the chip design speed. Therefore, how to efficiently develop compilers to cope with the large number of new chips that appear in a short period of time has become a key challenge, which people call the "agile compiler development challenge."
[0005] Existing compilers, such as GCC and LLVM. GCC (GNU Compiler Collection) is a compiler developed by GNU (an open source software organization). GCC was originally a compiler written specifically for the GNU operating system. It has now been adopted as a standard compiler by most Unix-like operating systems (such as Linux, BSD, MacOS X, etc.), and can even be used on Microsoft Windows. GCC supports a variety of computer architecture chips, such as x86, ARM, MIPS, etc., and has been ported to a variety of other hardware platforms. LLVM is a framework system for building compilers, written in C++. LLVM provides compiler-related support and can be used as the background of compilers for multiple languages. It can be regarded as a collection of modular and reusable compiler and tool technologies.
[0006] The working process of the compiler is to first convert the code written by the programmer into its internal representation, and then convert the internal representation into code (machine code) that can be run on the computer. The machine code of different computer architectures (which can also be understood as different types of computers) is different. In the prior art, a target-independent code generation mechanism has been used to support different microprocessors. The target-independent code generation mechanism means that the compiler provides a set of reusable components for converting the compiler's internal representation into a specified type of machine code. It is not limited to a certain architecture, so it is called target-independent. The target refers to the type of architecture. In particular, the compiler developer writes a set of target description (.td) files (for LLVM) or machine description (.md) files (for GCC) by specifying the properties of the target architecture (mainly the instruction system architecture ISA). Then, the target-independent code generator will automatically generate the corresponding backend code to support the new microprocessor. Compared with redeveloping the compiler backend for each architecture, the target-independent code generation mechanism greatly reduces the development work. However, when faced with new compiler development challenges, manually writing target description files is still error-prone and places a heavy burden on developers. The backend porting and development of compilers is still an error-prone and labor-intensive task. Take LLVM as an example. LLVM provides a declarative TableGen language for developers to write a set of .td files for the target ISA. In these .td files, developers can specify target information, including instruction generation, register definition, calling convention, target machine description, supported instructions, and target-specific extensions. These files are used for code generation and optimization. However, LLVM 9.0 alone contains backend code for 18 different architectures. If the MIPS architecture is used in LLVM9.0, 32 target description files are required, which contain nearly 30,000 lines of code. The MIPS architecture is a processor architecture that adopts a reduced instruction set (RISC). It appeared in 1981 and was developed and authorized by MIPS Technologies. It is widely used in many electronic products, network equipment, personal entertainment devices, and commercial devices. If the X86 architecture is used in LLVM9.0, developers need to write and provide 40 files, including nearly 54,000 lines of code. What's worse, due to the flexibility of the TableGen language, the .td file shows extremely diverse personalized styles between different target platforms.The TableGen codes of different platforms have the following differences: ① The target description code is distributed in multiple different *.td files, and developers will organize these files in their own way; ② In the process of supporting different target platforms, the compiler backend will inevitably involve some information that reflects the characteristics of the target platform; ③ Different developers have inconsistent code styles when developing backends corresponding to different target platforms. These differences have brought difficulties and obstacles to the development and porting of the compiler backend, and cannot well cope with the development challenges of agile compilers. Summary of the invention
[0007] Therefore, the purpose of the present invention is to overcome the defects of the above-mentioned prior art and provide a new compiler back-end code automatic construction method and system that can meet the challenges of compiler development.
[0008] According to a first aspect of the present invention, a method for automatically constructing compiler backend code is provided, which is used to automatically generate compiler backend code for a target platform under a specified framework system, and the method comprises: S1, obtaining a backend description file of an existing sample platform under the same framework as the target platform, and constructing an identifier reference relationship graph based on a definition-reference relationship for each instruction in the backend description file of the sample platform, in which each identifier corresponds to a node; S2, based on the identifier reference relationship graph in step S1, classifying identifiers, wherein the identifiers are divided into language-specific types, platform feature information types, and developer-defined types; S3, converting different types of identifiers in each instruction into corresponding agile Identifier, and based on the identifier reference relationship diagram, generate a code layout template represented by an agile identifier corresponding to each instruction, wherein the code layout template contains the identifier location of the platform characteristic information; S4, obtain the target platform coding instruction, match the target platform coding instruction with the instruction of the sample platform to obtain the code layout template corresponding to the sample platform instruction, fill in the attribute values of the language specific type and the developer defined type in the sample platform corresponding to the agile identifier based on the code layout template to generate the instruction module of the target platform, fill in the platform characteristic information attribute value in the target platform coding instruction input by the user according to its corresponding location into the instruction template to generate the compiler backend code corresponding to the current target platform coding instruction.
[0009] Among them, the language-specific type identifier refers to an identifier that is commonly present in a programming language, and the node corresponding to the language-specific type identifier in the identifier reference relationship diagram has neither input nor output; the platform characteristic information type identifier refers to an identifier that depends on the instruction set architecture, and the node corresponding to the platform characteristic information type identifier in the identifier reference relationship diagram has only output but no input; the developer-defined type identifier refers to an identifier whose name is defined by the developer himself, and the node corresponding to the developer-defined type identifier in the identifier reference relationship diagram has both input and output.
[0010] Preferably, the identifier location of the platform characteristic information type indicates the position of the attribute value of the platform characteristic information type in the code layout template.
[0011] In some embodiments of the present invention, step S4 includes executing the following steps for each target platform coding instruction: S41, obtaining the platform characteristic information attribute value in the current target platform coding instruction input by the user, wherein different platform characteristic information attribute values correspond to different agile identifier attribute indexes, and the agile identifier index is a unique index pre-set for each type of agile identifier attribute; S42, calculating the feature vector of the current target platform coding instruction based on the platform characteristic information attribute value obtained in step S41; S43, calculating the Euclidean distance between the target platform coding instruction and each instruction of the sample platform based on the feature vector calculated in step S42 and the feature vector of each instruction of the sample platform, and obtaining the distance between the target platform coding instruction and each instruction of the sample platform. The code instruction Euclidean distance is less than or equal to the preset threshold to match the sample platform instruction successfully; S44, based on the code layout module corresponding to the obtained sample platform instruction, fill in the attribute values of the language-specific type and developer-defined type in the sample platform corresponding to the agile identifier based on the code layout template to generate the instruction module of the target platform, the attribute of the identifier of the platform characteristic information type in the instruction template is empty and is represented by the agile identifier with attribute index information according to the identifier location of the platform characteristic information type; S45, fill the platform characteristic information attribute value in the target platform coding instruction input by the user into the instruction template according to its corresponding agile identifier attribute index to generate the compiler backend code corresponding to the current target platform coding instruction. Preferably, the preset threshold is 0.2.
[0012] Preferably, the present invention uses Word2vec to generate a feature vector of an instruction in the following manner:
[0013]
[0014] Among them, FV i is the feature vector of the ith instruction, L is the length of the agile identifier of the platform feature information type of instruction i, is the tth attribute in the agile identifier list of the platform feature information type corresponding to the ith instruction, pw t is the weight corresponding to the tth attribute in the agile identifier list of the platform feature information type. The feature vector of each instruction is a 10-dimensional vector.
[0015] Preferably, the present invention calculates the Euclidean distance between two instructions in the following manner:
[0016]
[0017] Among them, SD(i,j) represents the Euclidean distance between the i-th instruction and the j-th instruction, and FV i k is the k-th dimension value in the feature vector of the ith instruction.
[0018] In some embodiments of the present invention, the weight corresponding to each attribute in the agile identifier list of the platform characteristic information type is pre-obtained in the following manner: P1. Obtain backend description files of multiple sample platforms, and perform identifier splitting and identifier classification on each instruction in the backend description file of each sample platform, convert identifiers of all categories into corresponding agile identifiers, and count the agile identifiers of all attributes corresponding to the annotations of the platform characteristic information type to obtain an agile identifier list of the platform characteristic information type; P2. Assign a weight in the value space of [0,1] to each attribute in the agile identifier list of the platform characteristic information type to obtain a weight combination, so that the cumulative sum of all weights in the weight combination is 1, calculate the Euclidean distance of instructions between different sample platforms based on the current weight to perform instruction matching, and two instructions whose Euclidean distance between instructions is less than or equal to a preset threshold are matched successfully, and count the number of entries with successful instruction matching between different sample platforms; P3. Repeat step P2 until the weight combination corresponding to the maximum number of entries with successful instruction matching between different sample platforms is selected.
[0019] According to a second aspect of the present invention, a system for implementing the method as described in the first aspect of the present invention is provided, the system comprising: a sample platform code processing module, used to obtain a backend description file of an existing sample platform under the same framework as a target platform, and construct an identifier reference relationship graph based on a definition-reference relationship for each instruction in the backend description file of the sample platform, in which each identifier corresponds to a node; a code layout template generation module, used to convert different types of identifiers in each instruction of the sample platform into corresponding agile identifiers, and generate a code layout template represented by an agile identifier corresponding to each instruction based on the identifier reference relationship graph, wherein the code layout template contains identifier positioning of platform characteristic information; an instruction template generation module, used to match the target platform coding instruction with the instruction of the sample platform to obtain a code layout template corresponding to the sample platform instruction, and fill in the attribute values of the language-specific type and the developer-defined type in the sample platform corresponding to the agile identifier based on the code layout template to generate an instruction module for the target platform; and a code generator, used to fill in the attribute values of the platform characteristic information in the target platform coding instruction input by the user into the instruction template according to their corresponding positioning to generate a compiler backend code corresponding to the target platform coding instruction.
[0020] Compared with the prior art, the advantages of the present invention are: the present invention not only solves the problem of slow and difficult compiler backend development, but also lowers the threshold for chip development, saves time and labor costs, and greatly improves the efficiency of chip development work. The agile target platform code generator that implements the method of the present invention is a black box for chip developers, which enables chip developers to quickly implement compiler support for new chips without having to have an in-depth understanding of the compiler. The labor and time costs of compiler backend development are greatly reduced, and R&D efficiency is improved. In addition, the method can adapt to different target platforms and has good versatility and extensibility. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] The embodiments of the present invention are further described below with reference to the accompanying drawings, in which:
[0022] Figure 1 A schematic diagram of a method for automatically constructing encoder backend code according to an embodiment of the present invention;
[0023] Figure 2 A schematic diagram of a partial backend description file code example of a sample platform according to an embodiment of the present invention;
[0024] Figure 3 According to an embodiment of the present invention, Figure 2 The identifier reference relationship diagram constructed by part of the backend description file code of the sample platform shown;
[0025] Figure 4According to an embodiment of the present invention, Figure 2 The identifier reference relationship diagram constructed by part of the backend description file code of the sample platform shown;
[0026] Figure 5 According to an embodiment of the present invention, Figure 2 Schematic diagram of instruction template generated by sample platform code layout template in;
[0027] Figure 6 According to an embodiment of the present invention, Figure 2 Schematic diagram of instruction template generated by sample platform code layout template in;
[0028] Figure 7 According to an embodiment of the present invention, Figure 5 Schematic diagram of the backend code generated by the instruction template in;
[0029] Figure 8 The figure is a schematic diagram of the structure of a compiler backend code generation system according to an embodiment of the present invention. DETAILED DESCRIPTION
[0030] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail by specific embodiments below. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0031] As described in the background technology, compiler development under the existing technology is difficult. The compiler is complex and difficult to learn. The compiler backend development speed lags behind the chip design speed. The labor cost and time cost are high. There is an urgent need for a backend code generation method that can meet the development challenges of an agile compiler.
[0032] Through research, the inventors found that one of the difficulties in automatic code generation of compilers under the existing technology lies in the differences in back-end codes for different target platforms, including the organization of files and the coding style of developers. The most important problem that cannot cope with the development challenges of agile compilers is that there is no universal target description file to realize the automatic generation of back-end code for different target platforms. Therefore, from the perspective of seeking a new target description file generation method, the inventors proposed a standardized intermediate representation method with the purpose of analyzing certain target samples, automatically generating target description files for new target platforms, and ultimately realizing the automatic development of the compiler back end. The compiler back-end description file is standardized in this representation, and finally the instruction architecture (ISA) in the sample is represented in a standardized form. Combined with the characteristic information of the new instruction architecture (ISA), the intermediate representation is automatically converted into a new compiler back-end description file.
[0033] In addition, it should be noted that another difficulty of automatic code generation is that there are differences in the characteristic information corresponding to different instruction architectures (ISAs), and it is difficult to effectively and accurately extract these characteristic information from the back-end code. To this end, the present invention uses artificial design experience to guide the automatic code generation of this part, and proposes an identifier reference graph (TRG), which is similar to the definition-use relationship in the compiler. With the help of artificial design experience and the identifier reference graph, the present invention classifies the identifiers in the compiler back-end description file, and filters out the characteristic information, and represents this information by a standardized intermediate representation. In this way, the compiler back-end description file is standardized as an intermediate representation. For a new instruction architecture (ISA), once the user provides enough characteristic information, the automatic code generation method can automatically generate its corresponding compiler back-end description file from the standardized intermediate representation, and finally complete the support for the new instruction architecture (ISA). In the embodiment of the present invention, the LLVM compiler architecture will be used as an example to carry out an expanded description.
[0034] According to an embodiment of the present invention, a method for automatically constructing compiler backend code is provided. Figure 1 As shown, it includes steps S1, S2, S3, and S4, and each step is described in detail below.
[0035] In step S1, the backend description file of an existing sample platform under the same framework as the target platform is obtained, and each instruction in the backend description file of the sample platform is identifier-split, and an identifier reference relationship graph based on the definition-reference relationship of each instruction in the backend description file of the sample platform is constructed. In the identifier reference relationship graph, each identifier corresponds to a node.
[0036] One of the major difficulties in realizing automatic compiler code construction is that the compiler codes of mature target platforms written by different developers have different code styles and organizational methods, which are difficult to be directly represented in a unified standardized form. Therefore, based on the concept of definition-reference relationship in the compiler, the present invention proposes an identifier reference graph (abbreviated as TRG), which divides all possible definition-reference relationships in the source file into different categories, then locates each identifier, and determines the classification of each identifier based on experience, paving the way for building a standardized intermediate representation. All identifiers (Tokens) in the source file can be located through the identifier reference graph (TRG).
[0037] According to an example of the present invention, Figure 2Taking the backend description file shown in the figure as an example, an identifier reference relationship graph of each instruction is constructed based on the definition-reference relationship. Among them, the backend description file code of the jr16 instruction has 20 identifiers (Token), and the lw64 instruction has 21 identifiers. In the identifier reference relationship graph, each identifier is represented by t plus a superscript. Figure 3 Shows the Figure 2 (a) shows the identifier reference relationship diagram constructed by the jr16 instruction. Figure 4 Shows the Figure 2 (b) shows the identifier reference relationship diagram constructed by the lw64 instruction. Figure 3 and Figure 4 As can be seen from the figure, there are three types of identifiers, namely, language-specific identifiers, platform-specific information identifiers, and developer-defined identifiers. The language-specific identifiers are identifiers that are commonly found in programming languages. In the identifier reference graph, the nodes corresponding to language-specific identifiers have no input or output, for example Figure 2 The let in R2 (Record 2) in (a) corresponds to Figure 2 Line 5 of (a) Figure 3 In the TRG graph shown, the node corresponding to let has no edge pointing to let, and it also does not point to other nodes. The identifier of the platform characteristic information type refers to an identifier that depends on the instruction set architecture. In the identifier reference relationship graph, the node corresponding to the identifier of the platform characteristic information type has only output and no input, for example Figure 2 (a) In R1, 0x0c, Figure 3 In the TRG graph shown, there is no edge pointing to the node corresponding to 0x0c, but its corresponding node points to op in R2. The developer-defined identifier refers to an identifier whose name is defined by the developer himself. In the identifier reference relationship graph, the node corresponding to the developer-defined identifier has both input and output, for example Figure 2 (a) op in R2, 0x0c in R1 points to op in R2, and op in R2 points to Inst in R2, which describes Figure 2 (a) The reference relationship is defined from 0x0c in the first line of code to op on the right side of the equal sign in the fifth line of code, and finally to Inst on the left side of the equal sign. Figure 3 In the TRG graph shown, the op node has both input and output; for example, Figure 2 (b) 0x23 in R3 points to op in R4, and op in R4 points to Inst in R4, which describes Figure 2 (b) The reference relationship between 0x23 in the first line of code, op on the right side of the equal sign in the fourth line of code, and Inst on the left side of the equal sign is defined. Figure 4In the TRG graph shown, the node op has both input and output. It can be seen that the TRG graph can be used to locate different identifiers.
[0038] In step S2, the identifiers are classified according to the TRG graph in step S1. As described above, the identifiers are classified into language specific types, platform characteristic information types, and developer defined types.
[0039] According to an example of the present invention, the present invention is Figure 2 The following is an example of the partial coding of the sample platform shown in Figure 2 (a) is the backend description file code corresponding to the jr16 instruction in the MIPS platform. Figure 2 (b) is the backend description file code corresponding to the lw64 instruction in the MIPS platform. Figure 2 In the jr16 instruction shown in (a), the identifiers "let", "bits", "def", "class", ";", "}", etc. are language-specific identifiers, the identifiers "0x0c", "5", "16", and "9" are platform-specific information identifiers, and the identifiers "op", "Inst", "JALR_FM_16", and "JR_16MM" are developer-defined identifiers. Figure 2 In the lw64 instruction shown in (b), identifiers "let", "bits", "class", "}", etc. are identifiers of language-specific types, identifiers "0x23", "26", "31", "32", "1" are identifiers of platform characteristic information types, and identifiers "op", "Inst", "LW_FM", "LW_64", and "canFoldAsLoad" are identifiers of developer-defined types.
[0040] In step S3, after different types of identifiers in each instruction are converted into corresponding agile identifiers, a code layout template represented by agile identifiers corresponding to each instruction is generated based on the identifier reference relationship graph, and the code layout template contains identifier positioning of platform characteristic information.
[0041] Another difficulty in realizing automatic compiler code construction is the understanding of the grammatical rules of the compiler backend development language and the processing of the target platform feature information. Different developers have different understandings of the development language, and different target platform feature information is also different. How to achieve automated construction depends on how to solve these inconsistencies. In response to this problem, the present invention proposes a standardized intermediate representation method-agile identifier. Agile identifiers are automatically constructed based on the traditional manual classification method of identifiers. Taking the TableGen language in LLVM as an example, its code is divided into three categories: language-specific types, developer-defined types, and platform feature information types. Agile identifiers are the core intermediate representation of the entire agile code generation technology. During the processing, the identifiers in the compiler backend description file are standardized as agile identifiers. In the process of constructing agile identifiers, no manual intervention is required, and the system will automatically process each identifier and convert it into a corresponding agile identifier. The present invention finally converts the agile identifier into an identifier in the compiler backend description file to complete the backend support for the new target platform.
[0042] According to an embodiment of the present invention, if a node corresponding to an identifier in a TRG graph has neither input nor output, it is a language-specific type identifier, represented by an agile identifier LS, such as Figure 3 If the node corresponding to the identifier in a TRG graph has only output but no input, it is the platform feature information type, represented by the agile identifier TS, such as Figure 3 The "0x0c", "5", "16", "9" nodes in Figure 4 0x23", "26", "31", "32", "1" in the TRG graph; if the node corresponding to the identifier in a TRG graph has both input and output, the developer defines the type (PD), such as Figure 3 The "op", "Inst" and other nodes in Figure 4 Then, based on the TRG graph, a code layout template represented by agile identifiers can be generated. The form of the code layout template is consistent with the code form of the original compiler backend description file of the sample platform, except that all identifiers are represented by agile identifiers. The code layout template will not be described in detail here.
[0043] In step S4, the target platform coding instructions are obtained, and the target platform coding instructions are matched with the instructions of the sample platform to obtain a code layout template corresponding to the sample platform instructions. Based on the code layout template, the attribute values of the language-specific type and the developer-defined type in the sample platform corresponding to the agile identifier are filled in to generate an instruction module for the target platform. The platform characteristic information attribute values in the target platform coding instructions input by the user are filled in the instruction template according to their corresponding positions to generate the compiler back-end code corresponding to the current target platform coding instructions.
[0044] As mentioned above, the present invention proposes agile identifiers as a standardized intermediate representation. When facing a target platform, the present invention automatically standardizes the identifiers in the *.td file of the sample platform into agile identifiers, and then combines the input of the chip developer to convert the agile identifiers into identifiers in the *.td file of the new target platform. As mentioned above, the agile identifiers proposed by the present invention are divided into three categories: language specific types (LS): identifiers that are commonly found in programming languages, such as keywords, operators, and punctuation marks; developer defined types (PD): identifiers whose names are defined by the developer himself, and the naming method of these identifiers will not affect the correctness of the compiler; platform feature information types (TS): identifiers that depend on the instruction set architecture (ISA), which are essential for the correct support of the target platform in the backend and are highly dependent on the target platform. Based on this, the identifiers of the language-specific type and developer-defined type of the sample platform in the code layout template can be directly reused, and the platform characteristic information attribute identifier needs to extract the platform characteristic information attribute value in the current target platform coding instruction, and fill the platform characteristic information attribute value in the target platform coding instruction into the instruction template according to the positioning of the type identifier in the code layout template. Therefore, the present invention uses the identifier of the language-specific type and the identifier of the developer-defined type corresponding to the code layout template that are the same as the sample platform to generate the instruction template of the target platform. Figure 2 The instruction template generated by successfully matching the jr16 instruction in (a) is as follows Figure 5 As shown, the attribute values of the language-specific type identifier and the developer-defined type identifier in the instruction template are the same as Figure 2 (a) is consistent with the attribute value of the corresponding identifier in the jr16 instruction of the sample platform. You only need to fill in the identifier attribute value of the platform feature information corresponding to the target platform to generate the backend description file code of the target platform. Figure 2 The instruction template generated by taking the lw64 instruction in (b) as an example is as follows: Figure 6 shown.
[0045] The process of generating instruction templates is described in detail below with reference to the accompanying drawings.
[0046] Among them, Figure 5 As can be seen in the instruction template, the agile identifiers of different platform feature information types are represented by their corresponding agile identifiers plus different attribute indexes. For example, the agile identifiers plus attribute indexes corresponding to "0x0c", "5", "16", and "9" in the original sample platform are TS 14 TS 15 TS 7 TS 16 , the superscript of the agile identifier represents the index of the attribute value. The agile identifier attribute index corresponding to different platforms may be the same, but the attribute value is different. The TS index is obtained by counting all TS identifiers in the sample platform backend description file to obtain the TS list and assign an index to each attribute. Taking the MIPS sample platform as an example, assuming that the identifiers in its compiler backend description file contain a total of n TS identifiers of platform feature information after standardization, each TS identifier is assigned an index in [1,n]. The index table is the same as Figure 2 The relevant contents of the instructions in (a) are shown in Table 1.
[0047] Table 1
[0048]
[0049] Index table with Figure 2 Part of the contents related to the instructions in (b) are shown in Table 2. ,It can be seen that different instructions have different values for the attribute with the same index.
[0050] Table 2
[0051]
[0052] According to one embodiment of the present invention, the present invention performs matching by calculating the Euclidean distance between the feature vector of the target platform coding instruction and the feature vector of the sample instruction to select the corresponding code layout template. If the Euclidean distance is less than or equal to the preset threshold, the match is successful. According to one embodiment of the present invention, the preset threshold is 0.2. According to one embodiment of the present invention, the present invention uses the word vector model Word2vec to calculate the feature vector of the instruction. Word2vec is a group of related models used to generate word vectors, and generates a feature vector containing 10-dimensional features of the instruction in the following way:
[0053]
[0054] Among them, FV i is the feature vector of the ith instruction, L is the length of the agile identifier of the platform feature information type of instruction i, is the tth attribute in the agile identifier list of the platform feature information type corresponding to the ith instruction, pw t is the weight corresponding to the tth attribute in the agile identifier list of the platform feature information type.
[0055] According to an embodiment of the present invention, the weight corresponding to each attribute in the agile identifier list of the platform characteristic information type is pre-obtained in the following manner: P1. Acquire the backend description files of multiple sample platforms, and perform identifier splitting and identifier classification on each instruction in the backend description file of each sample platform, convert all categories of identifiers into corresponding agile identifiers, and count the agile identifiers of all attributes corresponding to the identifiers of the platform characteristic information type to obtain the agile identifier list of the platform characteristic information type; P2. Assign a weight in the value space of [0,1] to each attribute in the agile identifier list of the platform characteristic information type to obtain a weight combination, so that the cumulative sum of all weights in the weight combination is 1, calculate the Euclidean distance of instructions between different sample platforms based on the current weight to perform instruction matching, and two instructions whose Euclidean distance between instructions is less than or equal to a preset threshold are matched successfully, and count the number of entries with successful instruction matching between different sample platforms; P3. Repeat step P2 until the weight combination corresponding to the maximum number of entries with successful instruction matching between different sample platforms is selected.
[0056] It should be noted that each TS attribute value can obtain a vector containing 10-dimensional decimals after passing through the Word2vec model. The vectors corresponding to all TS attribute values in each instruction are mapped one by one according to the dimensions, and the values of the same dimension of the vectors corresponding to all TS attribute values are accumulated to finally obtain the 10-dimensional feature vector corresponding to the instruction.
[0057] In order to better understand the implementation process, Figure 2 Taking the instruction encoding in as an example, it is explained how to generate the corresponding feature vectors.
[0058] First, count the TS list corresponding to each instruction, where: Figure 2 The TS list corresponding to the jr16 instruction in (a) is shown in Table 3:
[0059] Table 3
[0060]
[0061] Figure 2 The TS list corresponding to the lw64 instruction in (b) is shown in Table 4:
[0062] Table 4
[0063]
[0064] By using the Word2vec model and taking the attribute values corresponding to the TS lists corresponding to the two instructions as input, the feature vectors corresponding to the two instructions can be obtained respectively, as shown in Table 5:
[0065] Table 5
[0066]
[0067] Taking the target platform RISC-V as an example, assuming that the TS attribute value corresponding to the c.jr instruction encoding in RISC-V input by the user is shown in Table 6, find the template corresponding to the instruction:
[0068] Table 6
[0069]
[0070] Preferably, after obtaining its feature vector through the Word2vec model, the Euclidean distance between it and the feature vector of each instruction in the sample platform is calculated as follows:
[0071]
[0072] Among them, SD(i,j) represents the Euclidean distance between the i-th instruction and the j-th instruction, and FV i k is the kth dimension value in the feature vector of the ith instruction. After calculation, it is found that it successfully matches the jr16 instruction of the sample platform. The code layout template based on the jr16 instruction is generated as follows Figure 5 The instruction template shown in the figure is then filled with the TS attribute value corresponding to the c.jr instruction encoding in RISC-V into the instruction template to obtain the following Figure 7 The backend description file code shown.
[0073] From the description of the above embodiments, it can be seen that the present invention can automatically analyze the backend code of other platforms of the mature target platform, divide it into standardized intermediate representations according to the identifier reference graph (TRG), and finally combine the characteristic information of the new target platform input by the user to automatically generate the backend code for the new target platform, thereby completing the rapid support for the new target platform.
[0074] The present invention also provides a system for realizing the automatic construction of the compiler backend code of the present invention, such as Figure 8As shown, the system includes: a sample platform code processing module, which is used to obtain the backend description file of the existing sample platform under the same framework as the target platform, and construct an identifier reference relationship graph based on the definition-reference relationship for each instruction in the backend description file of the sample platform, in which each identifier corresponds to a node; a code layout template generation module, which is used to convert different types of identifiers in each instruction of the sample platform into corresponding agile identifiers, and generate a code layout template represented by agile identifiers corresponding to each instruction based on the identifier reference relationship graph, wherein the code layout template contains identifier positioning of platform characteristic information; an instruction template generation module, which is used to match the target platform coding instruction with the instruction of the sample platform to obtain a code layout template corresponding to the sample platform instruction, and fill in the attribute values of the language-specific type and the developer-defined type in the sample platform corresponding to the agile identifier based on the code layout template to generate an instruction module for the target platform; a code generator, which is used to fill in the platform characteristic information attribute values in the target platform coding instruction input by the user into the instruction template according to their corresponding positioning to generate the compiler backend code corresponding to the target platform coding instruction.
[0075] The present invention not only solves the problem of slow and difficult compiler backend development, but also lowers the threshold for chip development, saves time and labor costs, and greatly improves the efficiency of chip development work. The agile target platform code generator (ATG for short) that implements the method of the present invention is a black box for chip developers, which enables chip developers to quickly implement compiler support for new chips without having to have an in-depth understanding of the compiler. The labor and time costs of compiler backend development are greatly reduced, and research and development efficiency is improved. In addition, the method can adapt to different target platforms and has good versatility and extensibility.
[0076] It should be noted that although the above describes the various steps in a specific order, it does not mean that the various steps must be executed in the above specific order. In fact, some of these steps can be executed concurrently or even in a different order as long as the required functions can be achieved.
[0077] The present invention may be a system, a method and / or a computer program product. The computer program product may include a computer-readable storage medium carrying computer-readable program instructions for causing a processor to implement various aspects of the present invention.
[0078] Computer readable storage medium can be a tangible device that holds and stores instructions used by an instruction execution device. Computer readable storage medium can include, for example, but is not limited to, an electrical storage device, a magnetic storage device, an optical storage device, an electromagnetic storage device, a semiconductor storage device, or any suitable combination thereof. More specific examples (non-exhaustive list) of computer readable storage medium include: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a static random access memory (SRAM), a portable compact disk read-only memory (CD-ROM), a digital versatile disk (DVD), a memory stick, a floppy disk, a mechanical encoding device, for example, a punch card or a protruding structure in a groove on which instructions are stored, and any suitable combination thereof.
[0079] The embodiments of the present invention have been described above, and the above description is exemplary, not exhaustive, and is not limited to the disclosed embodiments. Many modifications and variations will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the described embodiments. The selection of terms used herein is intended to best explain the principles of the embodiments, practical applications, or technical improvements in the market, or to enable other persons of ordinary skill in the art to understand the embodiments disclosed herein.
Claims
1. A compiler backend code automatic construction method, used to automatically generate compiler backend code for a target platform under a specified framework system, the method comprising: S1. Obtain a backend description file of an existing sample platform under the same framework as the target platform, and construct an identifier reference relationship graph based on a definition-reference relationship for each instruction in the backend description file of the sample platform. In the identifier reference relationship graph, each identifier corresponds to a node; S2. Classify the identifiers based on the identifier reference relationship diagram in step S1, wherein the identifiers are classified into language specific types, platform characteristic information types, and developer defined types; S3. Convert different types of identifiers in each instruction into corresponding agile identifiers, and generate a code layout template corresponding to each instruction represented by the agile identifier based on the identifier reference relationship graph, wherein the code layout template contains identifier positioning of platform characteristic information; S4. Obtain target platform coding instructions, match the target platform coding instructions with the instructions of the sample platform to obtain a code layout template corresponding to the sample platform instructions, fill in the attribute values of the language-specific type and the developer-defined type in the sample platform corresponding to the agile identifier based on the code layout template to generate an instruction module for the target platform, and fill in the platform characteristic information attribute values in the target platform coding instructions input by the user into the instruction template according to their corresponding positions to generate a compiler backend code corresponding to the current target platform coding instructions; Wherein, step S4 includes executing the following steps for each target platform coding instruction: S41, obtaining a platform characteristic information attribute value in the current target platform coding instruction input by the user, wherein different platform characteristic information attribute values correspond to different agile identifier attribute indexes, and the agile identifier index is a unique index pre-set and assigned to each type of agile identifier attribute; S42, calculating a feature vector of the current target platform coding instruction based on the platform characteristic information attribute value obtained in step S41; S43, based on the feature vector calculated in step S42 and the feature vector of each instruction of the sample platform, the Euclidean distance between the target platform coding instruction and each instruction of the sample platform is calculated, and the sample platform instruction whose Euclidean distance with the target platform coding instruction is less than or equal to a preset threshold value and successfully matches is obtained; S44. Based on the obtained code layout module corresponding to the sample platform instruction, fill in the attribute values of the language specific type and the developer defined type in the sample platform corresponding to the agile identifier based on the code layout template to generate an instruction module for the target platform, wherein the attribute of the identifier of the platform characteristic information type in the instruction template is empty and is represented by the agile identifier with attribute index information according to the identifier location of the platform characteristic information type; S45. Fill the platform characteristic information attribute value of the current target platform coding instruction into the instruction template according to its corresponding agile identifier attribute index to generate the compiler backend code corresponding to the current target platform coding instruction.
2. The method according to claim 1, characterized in that: The language-specific identifier refers to an identifier commonly found in a programming language, and the node corresponding to the language-specific identifier in the identifier reference relationship graph has no input or output; The identifier of the platform characteristic information type refers to an identifier that depends on the instruction set architecture, and the node corresponding to the identifier of the platform characteristic information type in the identifier reference relationship diagram has only output and no input; The developer-defined type identifier refers to an identifier whose name is defined by the developer himself. In the identifier reference relationship graph, the node corresponding to the developer-defined type identifier has both input and output.
3. The method according to claim 1, characterized in that The identifier location of the platform characteristic information type indicates the location of the attribute value of the platform characteristic information type in the code layout template.
4. The method according to claim 1, characterized in that: The preset threshold is 0.
2.
5. The method according to claim 4, characterized in that Word2vec is used to generate the feature vector of the instruction in the following way: Among them, FV i is the feature vector of the ith instruction, L is the length of the agile identifier of the platform feature information type of instruction i, is the tth attribute in the agile identifier list of the platform feature information type corresponding to the ith instruction, pw t is the weight corresponding to the tth attribute in the agile identifier list of the platform feature information type.
6. The method according to claim 5, characterized in that The feature vector of each instruction is a 10-dimensional vector.
7. The method according to claim 6, characterized in that The Euclidean distance between two instructions is calculated as follows: Among them, SD(i,j) represents the Euclidean distance between the i-th instruction and the j-th instruction, and FV i k is the k-th dimension value in the feature vector of the ith instruction.
8. The method according to claim 7, characterized in that The weight corresponding to each attribute in the agile identifier list of the platform characteristic information type is obtained in advance in the following manner: P1. Obtain the backend description files of multiple sample platforms, and perform identifier splitting and identifier classification on each instruction in the backend description file of each sample platform, convert all categories of identifiers into corresponding agile identifiers, and count the agile identifiers of all attributes corresponding to the annotations of the platform characteristic information type to obtain an agile identifier list of the platform characteristic information type; P2. Assign a weight in the value space [0,1] to each attribute in the agile identifier list of the platform feature information type to obtain a weight combination, so that the cumulative sum of all weights in the weight combination is 1, calculate the Euclidean distance of instructions between different sample platforms based on the current weight to perform instruction matching, and successfully match two instructions whose Euclidean distance between instructions is less than or equal to the preset threshold, and count the number of successful instruction matching entries between different sample platforms; P3. Repeat step P2 until a weight combination corresponding to the maximum number of entries with successful instruction matching between different sample platforms is selected.
9. A system for implementing the method according to any one of claims 1 to 8, characterized in that: The system comprises: The sample platform code processing module is used to obtain the backend description file of the existing sample platform under the same framework as the target platform, and construct an identifier reference relationship graph based on the definition-reference relationship of each instruction in the backend description file of the sample platform. In the identifier reference relationship graph, each identifier corresponds to a node; A code layout template generation module, used to convert different types of identifiers in each instruction of the sample platform into corresponding agile identifiers, and generate a code layout template represented by agile identifiers corresponding to each instruction based on the identifier reference relationship graph, wherein the code layout template contains identifier positioning of platform characteristic information; An instruction template generation module is used to match the target platform coding instructions with the instructions of the sample platform to obtain a code layout template corresponding to the sample platform instructions, and fill in the attribute values of the language-specific type and the developer-defined type in the sample platform corresponding to the agile identifier based on the code layout template to generate an instruction module for the target platform; The code generator is used to fill in the platform characteristic information attribute value in the target platform coding instruction input by the user according to its corresponding position into the instruction template to generate the compiler backend code corresponding to the target platform coding instruction.
10. A computer-readable storage medium, characterized in that: A computer program is stored thereon, and the computer program can be executed by a processor to implement the steps of any method described in claims 1 to 8.
11. An electronic device, characterized in that: include: one or more processors; A storage device for storing one or more programs, which, when executed by the one or more processors, enables the electronic device to implement the steps of the method as claimed in any one of claims 1 to 8.
