gcc:inline 文章目录参考说明如果非要做inline内核里就有这样的宏定义来实现强制的inline 函数gcc 相关的警告gcc inline相关源码inline 转非inlie的例子目标是降低二进制代码量非inline 转inline的一个例子性能上有所提高相关编译选项与选项 -O0的关系-O2systemtapgcc c 扩展 vague linkage非inlinec17 中的新用法参考https://gcc.gnu.org/bugzilla/show_bug.cgi?id66122说明inline 关键字只是提示编译器当前函数需要inline。但是编译器是否真正实现inline需要根据具体的优化场景决定。如果非要做inline需要使用attributealways_inlineGenerally, function are not inlined unless optimization is specified. For functions declared inline, this attribute inlines the function independent of any restrictions that otherwise apply to inlining. Failure to inline such a function is diagnosed as an error. Note that if such a function is called indirectly the compiler may or may not inline it depending on optimization level and a failure to inline an indirect call may or may not be diagnosed.内核里就有这样的宏定义来实现强制的inline 函数linux/compiler-gcc.h#define __always_inline inlineattribute((always_inline))/include/uapi/linux/stddef.h#ifndef__always_inline 如果之前没有定义这个宏就定义#define__always_inlineinline#endifgcc 相关的警告-Winline Warn if a function that is declared as inline cannot be inlined.如果函数被标记为inline但是实际没有inline。 但是即使有这个选项如果inline函数是存在系统头文件中的话编译器可能也不会报警告。编译器使用了很多种启发策略来决定是否要对函数做inline处理。例如For example, the compiler takes into account the size of the function being inlined and the amount of inlining that has already been done in the current function. Therefore, seemingly insignificant changes in the source program can cause the warnings produced by ‘-Winline’ to appear or disappear.gcc inline相关源码gcc/ipa-inline-analysis.cAnalysis used by inlining decision heuristics.inline 转非inlie的例子目标是降低二进制代码量commit8990571eb573032c1192440febb17132074c5575 Author:Pavel Emelyanovxemulopenvz.orgDate:Thu Oct1823:40:192007-0700Uninline find_pid etc set of functions The find_pid/_vpid/_pid_ns functions are used to find thestructpidby its id,depending on whic id-globalorvirtual-is used.Thefind_vpid()is a macro that pushes the current-nsproxy-pid_ns on the stack to call another function-find_pid_ns().It turned out,thatthisdereference together with the push itself cause the kernel text size to grow too much.Move all these out-of-line.Together with the previous patchthissaves a bit less that400bytes from.text section.非inline 转inline的一个例子性能上有所提高commit0bd3a173d711857fc9f583eb5825386cc08f3948 Author:Peter Zijlstrapeterzinfradead.orgDate:Tue Nov1916:13:3820130100sched/preempt,locking:Rework local_bh_{dis,en}able()Currentlylocal_bh_disable()is out-of-lineforno apparent reason.、、没有明显原因将函数置为非inline函数 Soinlineit to save a few cycles on call/returnnonsense,the。。inline可用节省一些函数调用返回的非必要开销 function body is a single add onx86(a few loads and store extra on函数体只是一个加操作 load/store archs).Also expose two new local_bh functions:__local_bh_{dis,en}able_ip(unsignedlongip,unsignedintcnt);Which implement the actual local_bh_{dis,en}able()behaviour.The next patch uses the exposed cnt argument to optimize bh lock functions.With build fixes from Jacob Pan.相关编译选项和inline相关的编译选项还是很多可以做“inline”函数的定制化。需要详细查看gcc手册。-fno-inline不做inline处理-finline-small-functions Enabled at level ‘-O2’.-findirect-inlining-finline-functions Enabled at level ‘-O3’.-finline-functions-called-once Enabled at levels ‘-O1’, ‘-O2’, ‘-O3’ and ‘-Os’.-fearly-inlining-finline-limitn-fno-keep-inline-dllexport-fkeep-inline-functions与选项 -O0的关系的时候也不会做inline处理。-O2https://blog.csdn.net/qq_36428903/article/details/111357810#_44systemtaphttps://blog.csdn.net/qq_36428903/article/details/132201040脚本里不能使用inline函数。gcc c 扩展 vague linkageInline functions are typically defined in a header file which can be included in many different compilations. Hopefully they can usually be inlined, but sometimes an out-of-line copy is necessary, if the address of the function is taken or if inlining fails. In general, we emit an out-of-line copy in all translation units where one is needed. As an exception, we only emit inline virtual functions with the vtable, since it always requires a copy.Local static variables and string constants used in an inline function are also considered to have vague linkage, since they must be shared between all inlined and out-of-line instances of the function.当程序里需要这个函数的指针的时候inline会失败。非inline__attribute__((noinline))voidabc(M_MSG*msg)c17 中的新用法C17扩展支持inline变量。 头文件中定义全局const常量如果不加inline头文件被多个cpp include会出现多重定义链接错误加inline变量允许多个TU定义同一份变量链接器合并成唯一实例。// header.hinline const std::string Greet “hello”;