Method and apparatus for improving compatibility and performance of c / c++ executable programs
Patent Information
- Application Number
- CN202311543066.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-20
- Publication Date
- 2026-09-15
- Estimated Expiration
- 2043-11-20
AI Technical Summary
[0005]因此,目前还缺少一种方法,可以在保证程序兼容性的同时拥有最大的性能
[0024] The above-mentioned English abbreviations have the following meanings:
Smart Images

Figure CN117687628B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention generally relate to the field of computer executable program technology, and more particularly to methods and apparatus for simultaneously improving the compatibility and performance of C / C++ executable programs. Background Technology
[0002] There are basically two common workflows: 1. Encode, compile on platform A, and run on platform A; 2. Encode, compile on platform A, and run on platform B.
[0003] For the first process, compilation and execution use the same CPU model with the same instruction set, so compatibility issues will not occur. For the second process, compilation and execution typically use different CPU models, and there is a certain probability that the two CPUs have different instruction sets. When A and B have different instruction sets, the following two scenarios may occur: 1. Platform B's instruction set includes platform A's instruction set, meaning B has a more advanced instruction set than A; 2. Platform B's instruction set does not include platform A's instruction set, meaning A has a more advanced instruction set than B.
[0004] In the first scenario, the program will not experience compatibility issues, but the more advanced instruction set available on platform B will become unavailable, preventing it from achieving its full performance potential. In the second scenario, the program will encounter compatibility problems, rendering it unable to run. The solution typically involves recompiling on platform B, or recompiling on platform A with specified compilation parameters that specify the instruction set for platform B.
[0005] Therefore, there is currently a lack of a method that can achieve maximum performance while ensuring program compatibility. Summary of the Invention
[0006] To address the above issues, this invention specifies a process for the coding, compilation, linking, and runtime stages, which greatly enhances the compatibility and performance of executable programs. Most projects only need to be fully compiled once to resolve compatibility and performance problems.
[0007] According to embodiments of the present invention, a method and apparatus are provided to simultaneously improve the compatibility and performance of C / C++ executable programs.
[0008] In a first aspect of the invention, a method is provided to simultaneously improve the compatibility and performance of C / C++ executable programs. The method includes:
[0009] S01: During the coding phase, determine whether the function is the entry point function. If it is not the entry point function, determine whether the function has compatibility or performance requirements. If the function has compatibility or performance requirements, add macro restrictions inside the function. The instruction set corresponding to each macro must correspond to the macro. Use the ## symbol to add a platform-specific suffix to the function name. The suffix is the same as the instruction set. Functions with compatibility and performance requirements are stored separately as .c / .cpp files.
[0010] S02: During the compilation and linking stage, it is determined whether the function is the entry point function. If it is not the entry point function, it is determined whether the function has compatibility or performance requirements. If the function has compatibility or performance requirements, it is compiled multiple times. The compilation parameters are distinguished according to the instruction set, and the .o file names are distinguished. All .o files are linked during linking.
[0011] S03: During the runtime phase, obtain the current CPU instruction set, construct the function name to be called using the function name instruction set containing the ## symbol, determine whether there is a function with the instruction set suffix, if there is a function with the instruction set suffix, call it; if there is no function with the instruction set suffix, call the function without any suffix.
[0012] Furthermore, if the function described in S01 is an entry function, then instruction set checking code is added and macro restrictions are not used.
[0013] Furthermore, the functions described in S01 do not use macro restrictions if there are no compatibility or performance requirements.
[0014] Furthermore, if the function described in S02 is the entry function, it will be compiled once using the instruction set parameters with the strongest compatibility, and the compiled function will have no suffix.
[0015] Furthermore, if the function described in S02 has no compatibility or performance requirements, it will be compiled once using the instruction set parameters with the strongest compatibility, and the compiled function will have no suffix.
[0016] In a second aspect of the invention, an apparatus is provided to simultaneously improve the compatibility and performance of C / C++ executable programs. The apparatus includes:
[0017] Encoding module: During the encoding phase, it determines whether a function is the entry point function. If it is not the entry point function, it determines whether the function has compatibility or performance requirements. If the function has compatibility or performance requirements, it adds macro restrictions inside the function. The instruction set corresponding to each macro must correspond to the macro. The ## symbol is used to add a platform-specific suffix to the function name. The suffix is the same as the instruction set. Functions with compatibility and performance requirements are stored separately as .c / .cpp files.
[0018] The compilation and linking module is used during the compilation and linking stage to determine whether a function is the entry point function. If it is not the entry point function, it determines whether the function has compatibility or performance requirements. If the function has compatibility or performance requirements, it performs multiple compilations, distinguishing the compilation parameters according to the instruction set and the .o file names of the compilations. During linking, all .o files are linked.
[0019] Runtime module: During the runtime phase, it obtains the current CPU instruction set, constructs the function name to be called using the function name instruction set containing the ## symbol, and determines whether there is a function with the instruction set suffix. If there is a function with the instruction set suffix, it is called; otherwise, the function without any suffix is called.
[0020] Furthermore, if the function described in the encoding module is an entry function, then instruction set checking code is added and macro restrictions are not used.
[0021] Furthermore, functions described in the encoding module do not use macro restrictions if they do not have compatibility or performance requirements.
[0022] Furthermore, if the function described in the compilation and linking module is the entry function, it will be compiled once using the instruction set parameters with the strongest compatibility, and the compiled function will have no suffix.
[0023] Furthermore, if the functions described in the compilation and linking module have no compatibility or performance requirements, they are compiled once using the instruction set parameters with the strongest compatibility, and the compiled functions have no suffixes.
[0024] The above-mentioned English abbreviations have the following meanings:
[0025] C / C++: A computer programming language
[0026] .o: target file
[0027] CPU: Central Processing Unit
[0028] .c / .cpp: C / C++ source files
[0029] This invention significantly enhances the compatibility and performance of executable programs by specifying a set of processes during the coding, compilation, linking, and runtime stages. Most projects only need to be fully compiled once to solve compatibility and performance issues.
[0030] It should be understood that the description in the Summary of the Invention is not intended to limit the key or essential features of the embodiments of the present invention, nor is it intended to restrict the scope of the invention. Other features of the invention will become readily apparent from the following description. Attached Figure Description
[0031] The above and other features, advantages, and aspects of the various embodiments of the present invention will become more apparent from the accompanying drawings and the following detailed description. Wherein:
[0032] Figure 1 A flowchart illustrating a method for simultaneously improving the compatibility and performance of C / C++ executables according to an embodiment of the present invention is shown.
[0033] Figure 2 A block diagram of an apparatus for simultaneously improving the compatibility and performance of C / C++ executables according to an embodiment of the present invention is shown. Detailed Implementation
[0034] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0035] According to embodiments of the present invention, a method and apparatus for simultaneously improving the compatibility and performance of C / C++ executable programs are proposed. By specifying a set of processes in the coding, compilation, linking, and runtime stages, the compatibility and performance of executable programs are greatly enhanced. Most projects only need to be fully compiled once, which can solve the compatibility and performance problems.
[0036] The principles and spirit of the present invention will be explained in detail below with reference to several representative embodiments.
[0037] Figure 1 This is a flowchart illustrating a method for simultaneously improving the compatibility and performance of C / C++ executable programs according to an embodiment of the present invention. The method includes:
[0038] S01: During the coding phase, determine whether the function is the entry point function. If it is not the entry point function, determine whether the function has compatibility or performance requirements. If the function has compatibility or performance requirements, add macro restrictions inside the function. The instruction set corresponding to each macro must correspond to the macro. Use the ## symbol to add a platform-specific suffix to the function name. The suffix is the same as the instruction set. Functions with compatibility and performance requirements are stored separately as .c / .cpp files.
[0039] S02: During the compilation and linking stage, it is determined whether the function is the entry point function. If it is not the entry point function, it is determined whether the function has compatibility or performance requirements. If the function has compatibility or performance requirements, it is compiled multiple times. The compilation parameters are distinguished according to the instruction set, and the .o file names are distinguished. All .o files are linked during linking.
[0040] S03: During the runtime phase, obtain the current CPU instruction set, construct the function name to be called using the function name instruction set containing the ## symbol, determine whether there is a function with the instruction set suffix, if there is a function with the instruction set suffix, call it; if there is no function with the instruction set suffix, call the function without any suffix.
[0041] It should be noted that although the operation of the method of the present invention has been described in a specific order in the above embodiments and figures, this does not require or imply that the operations must be performed in that specific order, or that all the operations shown must be performed to achieve the desired result. Additionally or alternatively, certain steps may be omitted, multiple steps may be combined into one step, and / or one step may be broken down into multiple steps.
[0042] To provide a clearer explanation of the above-mentioned method for simultaneously improving the compatibility and performance of C / C++ executable programs, a specific embodiment is described below. However, it is worth noting that this embodiment is only for better illustrating the present invention and does not constitute an improper limitation of the present invention.
[0043] The following concrete example will further illustrate in detail the methods for simultaneously improving the compatibility and performance of C / C++ executable programs:
[0044] There are three functions, namely:
[0045] Function A:
[0046]
[0047] Function B:
[0048]
[0049]
[0050] Function C:
[0051]
[0052] During the coding phase, it is determined whether functions A, B, and C are entry functions. Function A is the entry function, while functions B and C are not. It is then determined whether functions B and C have compatibility or performance requirements. Function B has compatibility or performance requirements, while function C does not. Therefore, no macro restrictions are added to functions A and C.
[0053] Add instruction set determination code inside function A, and construct different function names according to different instruction sets for calling.
[0054]
[0055]
[0056] Add macro restrictions inside function B. The instruction set corresponding to each macro must correspond to the macro. Use the ## symbol to add a platform-specific suffix to the function name. The suffix should be the same as the instruction set, as follows:
[0057]
[0058]
[0059] During the compilation and linking phase, it is determined whether functions A, B, and C are entry points. Function A is the entry point, while functions B and C are not. It is then determined whether functions B and C have compatibility or performance requirements. Function B has compatibility or performance requirements, while function C does not. Functions A and C are compiled only once, using the instruction set with the strongest compatibility, and the functions are not appended to their filenames.
[0060] Function B is compiled multiple times, with each compilation parameter distinguished according to the instruction set and the filename of the compiled .o file. All .o files are linked during linking.
[0061] The compilation script for function A is as follows:
[0062] gcc main.cc; / / Compile the entry function without compilation parameters. The .o file does not have platform-specific file extensions. The compilation script for function B is as follows:
[0063] 1. `gcc -c -DTEST_MARCH_VARIANT = hsw fun.co fun_hsw.o;` / / Compiles fun.c into a more compatible binary, with the .o file having the .hsw extension.
[0064] 2. `gcc -c -DTEST_MARCH_VARIANT = icl -march = icelake-client -mtune = icelake-client -mprefer-vector-width = 512fun.co fun_icl.o;` / / Compiles fun.c into a high-performance binary (compilation parameters specify microinstructions, using the AVX512F instruction set), and the .o file includes the icl filename.
[0065] The compilation script for function C is as follows:
[0066] gcc fun2.cc;
[0067] Finally, link all the .o files: gcc main.o fun_hsw.o fun_icl.o fun2.oo a.out / / Link all the .o files into one executable file.
[0068] During runtime, the assembly instruction `cpuid` is simply wrapped in C to obtain the current CPU instruction set. The function name is constructed using the instruction set containing the `##` symbol. It is then checked whether a function with that instruction set suffix exists. If so, the function is called; otherwise, the function without any suffix is called. Specifically:
[0069] Examining the exported function table of the generated executable program reveals that the same function has two implementations: _hsw and _icl.
[0070] show_my_info_fn_hsw
[0071] show_my_info_fn_icl
[0072] Final running result:
[0073] root@hecs-169728: / tyl / zhuanli / hou#. / a.out
[0074] in show_my_info_fn fun
[0075] Hello icl world
[0076] performance mode
[0077] in unPerffun
[0078] root@hecs-169728: / tyl / zhuanli / hou#. / a.out
[0079] in show_my_info_fn fun
[0080] Hello icl world
[0081] performance mode
[0082] in unPerffun
[0083] Based on the same inventive concept, this invention also proposes an apparatus for simultaneously improving the compatibility and performance of C / C++ executable programs. The implementation of this apparatus can be found in the implementation of the method described above, and repeated details will not be elaborated further. Figure 2As shown, the device 100 includes:
[0084] Encoding module 101: During the encoding phase, it determines whether a function is an entry point function. If it is not an entry point function, it determines whether the function has compatibility or performance requirements. If the function has compatibility or performance requirements, it adds macro restrictions inside the function. The instruction set corresponding to each macro must correspond to the macro. The ## symbol is used to add a platform-specific suffix to the function name. The suffix is the same as the instruction set. Functions with compatibility and performance requirements are stored separately as .c / .cpp files.
[0085] Compiler-linker module 102: During the compilation and linking stage, it is used to determine whether a function is the entry point function. If it is not the entry point function, it is used to determine whether the function has compatibility or performance requirements. If the function has compatibility or performance requirements, it is compiled multiple times, distinguishing the compilation parameters each time according to the instruction set and distinguishing the .o file names of the compilation. During linking, all .o files are linked.
[0086] Runtime module 103: During the runtime phase, it obtains the current CPU instruction set, constructs the function name to be called using the function name instruction set containing the ## symbol, and determines whether there is a function with the instruction set suffix. If there is a function with the instruction set suffix, it is called; otherwise, the function without any suffix is called.
[0087] The device proposed in this invention, which simultaneously improves the compatibility and performance of C / C++ executable programs, greatly enhances the compatibility and performance of executable programs by specifying a set of processes during the coding, compilation, linking, and runtime stages. Most projects only need to be fully compiled once to solve compatibility and performance issues.
[0088] While the spirit and principles of the invention have been described with reference to several specific embodiments, it should be understood that the invention is not limited to the disclosed specific embodiments, and the division of aspects does not imply that features in these aspects cannot be combined for benefit; such division is merely for ease of description. The invention is intended to cover various modifications and equivalent arrangements included within the spirit and scope of the appended claims.
[0089] Regarding the limitation of the scope of protection of this invention, those skilled in the art should understand that various modifications or variations that can be made by those skilled in the art without creative effort based on the technical solution of this invention are still within the scope of protection of this invention.
Claims
1. A method for simultaneously improving the compatibility and performance of C / C++ executable programs, characterized in that, The method includes: S01: During the coding phase, determine whether the function is the entry point function. If it is not the entry point function, determine whether the function has compatibility or performance requirements. If the function has compatibility or performance requirements, add macro restrictions inside the function. The instruction set corresponding to each macro must correspond to the macro. Use the ## symbol to add a platform-specific suffix to the function name. The suffix is the same as the instruction set. Functions with compatibility and performance requirements are stored separately as .c / .cpp files. S02: During the compilation and linking stage, it is determined whether the function is the entry point function. If it is not the entry point function, it is determined whether the function has compatibility or performance requirements. If the function has compatibility or performance requirements, it is compiled multiple times. The compilation parameters are distinguished according to the instruction set, and the .o file names are distinguished. All .o files are linked during linking. S03: During the runtime phase, obtain the current CPU instruction set, construct the function name to be called using the function name instruction set containing the ## symbol, determine whether there is a function with the instruction set suffix, if there is a function with the instruction set suffix, call it; if there is no function with the instruction set suffix, call the function without any suffix.
2. The method for simultaneously improving the compatibility and performance of C / C++ executable programs according to claim 1, characterized in that, If the function described in S01 is an entry function, then instruction set checking code is added and macro restrictions are not used.
3. The method for simultaneously improving the compatibility and performance of C / C++ executable programs according to claim 1, characterized in that, If the functions described in S01 do not have compatibility or performance requirements, macro restrictions are not used.
4. The method for simultaneously improving the compatibility and performance of C / C++ executable programs according to claim 1, characterized in that, If the function described in S02 is the entry function, it will be compiled once using the instruction set parameters with the strongest compatibility. The compiled function will have no suffix.
5. The method for simultaneously improving the compatibility and performance of C / C++ executable programs according to claim 1, characterized in that, If the function described in S02 has no compatibility or performance requirements, it will be compiled once using the instruction set parameters with the strongest compatibility. The compiled function will have no suffix.
6. An apparatus for simultaneously improving the compatibility and performance of C / C++ executable programs, characterized in that, The device includes: Encoding module: During the encoding phase, it determines whether a function is the entry point function. If it is not the entry point function, it determines whether the function has compatibility or performance requirements. If the function has compatibility or performance requirements, it adds macro restrictions inside the function. The instruction set corresponding to each macro must correspond to the macro. The ## symbol is used to add a platform-specific suffix to the function name. The suffix is the same as the instruction set. Functions with compatibility and performance requirements are stored separately as .c / .cpp files. The compilation and linking module is used during the compilation and linking stage to determine whether a function is the entry point function. If it is not the entry point function, it determines whether the function has compatibility or performance requirements. If the function has compatibility or performance requirements, it performs multiple compilations, distinguishing the compilation parameters according to the instruction set and the .o file names of the compilations. During linking, all .o files are linked. Runtime module: During the runtime phase, it obtains the current CPU instruction set, constructs the function name to be called using the function name instruction set containing the ## symbol, and determines whether there is a function with the instruction set suffix. If there is a function with the instruction set suffix, it is called; otherwise, the function without any suffix is called.
7. The apparatus for simultaneously improving the compatibility and performance of C / C++ executable programs according to claim 6, characterized in that, If the function described in the encoding module is an entry function, then instruction set checking code is added and macro restrictions are not used.
8. The apparatus for simultaneously improving the compatibility and performance of C / C++ executable programs according to claim 6, characterized in that, Functions described in the encoding module that do not have compatibility or performance requirements do not use macro restrictions.
9. The apparatus for simultaneously improving the compatibility and performance of C / C++ executable programs according to claim 6, characterized in that, If the function described in the compilation and linking module is the entry function, it will be compiled once using the instruction set parameters with the strongest compatibility. The compiled function will have no suffix.
10. The apparatus for simultaneously improving the compatibility and performance of C / C++ executable programs according to claim 6, characterized in that, If the functions described in the compilation and linking module have no compatibility or performance requirements, they will be compiled once using the instruction set parameters with the strongest compatibility. The compiled functions will have no suffixes.
Citation Information
Patent Citations
WINE (wine is not an emulator) compatibility automatic checking method on OMS (open mobile system) platform
CN102331968A
Method and system for translating a function into non-native instructions
EP2669797A2