pragma optimize vs pragma target 有什么区别
pragma optimize vs pragma target what is difference
#pragma GCC optimize()
和#pragma GCC target()
有什么区别,什么时候选哪个,其他选项是什么?
在高层次上,optimize()
用于控制在编译代码时是否使用某些优化技术:大局的想法是编译器可以花更多的时间来生成执行速度更快的代码或- 偶尔 - 更紧凑的代码需要更少的内存 运行。优化的代码有时更难分析或调试,因此您可能希望大部分程序未优化或未优化,但对性能至关重要的某些特定功能进行高度优化。 pragma 使您可以自由地在逐个函数的基础上改变优化。
在 https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options
列出并解释了个别优化
优化属性符号和用途的一般概述from here:
optimize (level, …)
optimize (string, …)
The optimize attribute is used to specify that a function is to be compiled with different optimization options than specified on the command line. Valid arguments are constant non-negative integers and strings. Each numeric argument specifies an optimization level. Each string argument consists of one or more comma-separated substrings. Each substring that begins with the letter O refers to an optimization option such as -O0
or -Os
. Other substrings are taken as suffixes to the -f prefix jointly forming the name of an optimization option. See Optimize Options.
‘#pragma GCC optimize’
can be used to set optimization options for more than one function. See Function Specific Option Pragmas, for details about the pragma.
Providing multiple strings as arguments separated by commas to specify multiple options is equivalent to separating the option suffixes with a comma (‘,’) within a single string. Spaces are not permitted within the strings.
Not every optimization option that starts with the -f
prefix specified by the attribute necessarily has an effect on the function. The optimize attribute should be used for debugging purposes only. It is not suitable in production code.
target()
表示编译器可以对特定的CPU使用机器码指令。通常,如果您知道人们 运行 宁您的代码可能使用不同代的特定 CPU,并且每个代都是向后兼容的,那么您会为这些代中最早的代进行编译,这样每个人都可以 运行 你的程序。使用 target()
,您可以编译不同的函数,利用后代 CPU 的更高级功能(机器代码指令,或针对特定缓存大小或流水线功能等进行优化),然后您的代码可以在 CPU 上有选择地调用可以支持它的更快版本。
更多详情from here
target (string, …)
Multiple target back ends implement the target attribute to specify that a function is to be compiled with different target options than specified on the command line. One or more strings can be provided as arguments. Each string consists of one or more comma-separated suffixes to the -m prefix jointly forming the name of a machine-dependent option. See Machine-Dependent Options.
The target attribute can be used for instance to have a function compiled with a different ISA (instruction set architecture) than the default. ‘#pragma GCC target’ can be used to specify target-specific options for more than one function. See Function Specific Option Pragmas, for details about the pragma.
For instance, on an x86, you could declare one function with the target("sse4.1,arch=core2") attribute and another with target("sse4a,arch=amdfam10"). This is equivalent to compiling the first function with -msse4.1 and -march=core2 options, and the second function with -msse4a and -march=amdfam10 options. It is up to you to make sure that a function is only invoked on a machine that supports the particular ISA it is compiled for (for example by using cpuid on x86 to determine what feature bits and architecture family are used).
int core2_func (void) __attribute__ ((__target__ ("arch=core2")));
int sse3_func (void) __attribute__ ((__target__ ("sse3")));
Providing multiple strings as arguments separated by commas to specify multiple options is equivalent to separating the option suffixes with a comma (‘,’) within a single string. Spaces are not permitted within the strings.
The options supported are specific to each target; refer to x86 Function Attributes, PowerPC Function Attributes, ARM Function Attributes, AArch64 Function Attributes, Nios II Function Attributes, and S/390 Function Attributes for details.
#pragma GCC optimize()
和#pragma GCC target()
有什么区别,什么时候选哪个,其他选项是什么?
在高层次上,optimize()
用于控制在编译代码时是否使用某些优化技术:大局的想法是编译器可以花更多的时间来生成执行速度更快的代码或- 偶尔 - 更紧凑的代码需要更少的内存 运行。优化的代码有时更难分析或调试,因此您可能希望大部分程序未优化或未优化,但对性能至关重要的某些特定功能进行高度优化。 pragma 使您可以自由地在逐个函数的基础上改变优化。
在 https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options
列出并解释了个别优化优化属性符号和用途的一般概述from here:
optimize (level, …)
optimize (string, …)
The optimize attribute is used to specify that a function is to be compiled with different optimization options than specified on the command line. Valid arguments are constant non-negative integers and strings. Each numeric argument specifies an optimization level. Each string argument consists of one or more comma-separated substrings. Each substring that begins with the letter O refers to an optimization option such as
-O0
or-Os
. Other substrings are taken as suffixes to the -f prefix jointly forming the name of an optimization option. See Optimize Options.
‘#pragma GCC optimize’
can be used to set optimization options for more than one function. See Function Specific Option Pragmas, for details about the pragma.Providing multiple strings as arguments separated by commas to specify multiple options is equivalent to separating the option suffixes with a comma (‘,’) within a single string. Spaces are not permitted within the strings.
Not every optimization option that starts with the
-f
prefix specified by the attribute necessarily has an effect on the function. The optimize attribute should be used for debugging purposes only. It is not suitable in production code.
target()
表示编译器可以对特定的CPU使用机器码指令。通常,如果您知道人们 运行 宁您的代码可能使用不同代的特定 CPU,并且每个代都是向后兼容的,那么您会为这些代中最早的代进行编译,这样每个人都可以 运行 你的程序。使用 target()
,您可以编译不同的函数,利用后代 CPU 的更高级功能(机器代码指令,或针对特定缓存大小或流水线功能等进行优化),然后您的代码可以在 CPU 上有选择地调用可以支持它的更快版本。
更多详情from here
target (string, …)
Multiple target back ends implement the target attribute to specify that a function is to be compiled with different target options than specified on the command line. One or more strings can be provided as arguments. Each string consists of one or more comma-separated suffixes to the -m prefix jointly forming the name of a machine-dependent option. See Machine-Dependent Options.
The target attribute can be used for instance to have a function compiled with a different ISA (instruction set architecture) than the default. ‘#pragma GCC target’ can be used to specify target-specific options for more than one function. See Function Specific Option Pragmas, for details about the pragma.
For instance, on an x86, you could declare one function with the target("sse4.1,arch=core2") attribute and another with target("sse4a,arch=amdfam10"). This is equivalent to compiling the first function with -msse4.1 and -march=core2 options, and the second function with -msse4a and -march=amdfam10 options. It is up to you to make sure that a function is only invoked on a machine that supports the particular ISA it is compiled for (for example by using cpuid on x86 to determine what feature bits and architecture family are used).
int core2_func (void) __attribute__ ((__target__ ("arch=core2")));
int sse3_func (void) __attribute__ ((__target__ ("sse3")));
Providing multiple strings as arguments separated by commas to specify multiple options is equivalent to separating the option suffixes with a comma (‘,’) within a single string. Spaces are not permitted within the strings.
The options supported are specific to each target; refer to x86 Function Attributes, PowerPC Function Attributes, ARM Function Attributes, AArch64 Function Attributes, Nios II Function Attributes, and S/390 Function Attributes for details.