- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
考虑这段代码:
// foo.cxx
int last;
int next() {
return ++last;
}
int index(int scale) {
return next() << scale;
}
使用 gcc 7.2 编译时:
$ g++ -std=c++11 -O3 -fPIC
这发出:
next():
movq last@GOTPCREL(%rip), %rdx
movl (%rdx), %eax
addl $1, %eax
movl %eax, (%rdx)
ret
index(int):
pushq %rbx
movl %edi, %ebx
call next()@PLT ## next() not inlined, call through PLT
movl %ebx, %ecx
sall %cl, %eax
popq %rbx
ret
但是,当使用 clang 3.9 编译具有相同标志的相同代码时:
next(): # @next()
movq last@GOTPCREL(%rip), %rcx
movl (%rcx), %eax
incl %eax
movl %eax, (%rcx)
retq
index(int): # @index(int)
movq last@GOTPCREL(%rip), %rcx
movl (%rcx), %eax
incl %eax ## next() was inlined!
movl %eax, (%rcx)
movl %edi, %ecx
shll %cl, %eax
retq
gcc 通过 PLT 调用 next()
,clang 将其内联。两者仍然从 GOT 中查找 last
。对于在 Linux 上编译,clang 进行优化是否正确并且 gcc 错过了简单的内联,或者 clang 进行优化是错误的,还是这纯粹是 QoI 问题?
最佳答案
我不认为该标准涉及那么多细节。它只是说,如果符号在不同的翻译单元中有外部链接,那么它就是同一个符号。这使得 clang 的版本正确。
从那时起,据我所知,我们超出了标准。编译器的选择因他们认为有用的 -fPIC
输出而异。
请注意 g++ -c -std=c++11 -O3 -fPIE
输出:
0000000000000000 <_Z4nextv>:
0: 8b 05 00 00 00 00 mov 0x0(%rip),%eax # 6 <_Z4nextv+0x6>
6: 83 c0 01 add $0x1,%eax
9: 89 05 00 00 00 00 mov %eax,0x0(%rip) # f <_Z4nextv+0xf>
f: c3 retq
0000000000000010 <_Z5indexi>:
10: 8b 05 00 00 00 00 mov 0x0(%rip),%eax # 16 <_Z5indexi+0x6>
16: 89 f9 mov %edi,%ecx
18: 83 c0 01 add $0x1,%eax
1b: 89 05 00 00 00 00 mov %eax,0x0(%rip) # 21 <_Z5indexi+0x11>
21: d3 e0 shl %cl,%eax
23: c3 retq
所以 GCC 确实知道如何优化它。它只是在使用 -fPIC
时选择不这样做。但为什么?我只能看到一种解释:使在动态链接期间覆盖符号成为可能,并始终如一地看到效果。该技术被称为 symbol interposition .
在共享库中,如果index
调用next
,因为next
是全局可见的,gcc必须考虑的可能性next
可以插入。所以它使用PLT。但是,当使用 -fPIE
时,不允许插入符号,因此 gcc 启用了优化。
那么clang错了吗?不,但是 gcc 似乎为符号插入提供了更好的支持,这对于检测代码很方便。但是,如果使用 -fPIC
而不是 -fPIE
来构建他的可执行文件,那么它会以一些开销为代价。
补充说明:
在this blog entry来自一位 gcc 开发人员,他在帖子末尾提到:
While comparing some benchmarks to clang, I noticed that clang actually ignore ELF interposition rules. While it is bug, I decided to add
-fno-semantic-interposition
flag to GCC to get similar behaviour. If interposition is not desirable, ELF's official answer is to use hidden visibility and if the symbol needs to be exported define an alias. This is not always practical thing to do by hand.
跟随那条线索,我登上了 x86-64 ABI spec .在第 3.5.5 节中,它确实强制要求所有调用全局可见符号的函数都必须通过 PLT(它根据内存模型定义要使用的确切指令序列)。
因此,尽管它没有违反 C++ 标准,但忽略语义插入似乎违反了 ABI。
最后一句话:不知道把它放在哪里,但你可能会感兴趣。我会为您省去转储,但我对 objdump 和编译器选项的测试表明:
在 gcc 方面:
gcc -fPIC
: 访问 last
通过 GOT,调用 next()
通过 PLT .gcc -fPIC -fno-semantic-interposition
:last
通过 GOT,next()
是内联。gcc -fPIE
: last
是 IP 相关的,next()
是内联的。-fPIE
表示 -fno-semantic-interposition
在 clang 方面:
clang -fPIC
: last
通过 GOT,next()
内联。clang -fPIE
: last
通过 GOT,next()
内联。以及一个编译为 IP 相关的修改版本,在两个编译器上内联:
// foo.cxx
int last_ __attribute__((visibility("hidden")));
extern int last __attribute__((alias("last_")));
int __attribute__((visibility("hidden"))) next_()
{
return ++last_;
}
// This one is ugly, because alias needs the mangled name. Could extern "C" next_ instead.
extern int next() __attribute__((alias("_Z5next_v")));
int index(int scale) {
return next_() << scale;
}
基本上,这明确表示尽管它们在全局范围内可用,但我们使用这些符号的隐藏版本,将忽略任何类型的插入。然后,无论传递的选项如何,两个编译器都会完全优化访问。
关于c++ - gcc 与 clang : inlining a function with -fPIC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45971091/
看看这个 fiddle http://jsfiddle.net/9S4zc/2/ 为什么这在 firefox 和 chrome 中看起来不同(文本对齐方式不同) 如何让 inner:before 元素
我从文档中了解到的是 %{ %} 之间的内容。被插入到包装器中,%inline %{ %} 呢? ? 是一样的吗?如果不是,有什么区别? 也许我们可以找到很多%inline %{ %}的出现。但仅出现
当我使用 显示:inline-flex; 或 显示:内联网格; 似乎有一些额外的“空间”或某种额外的重点计算发生。我不确定到底发生了什么。当使用箭头键在 contentediatble div 中导航
如果我想让一个 div 与容器 div 的其他内联元素内联,而我的目的仅此而已,我应该更喜欢使用 inline-block 或 display property 还是 inline-flex?不能使用
这个问题在这里已经有了答案: Why does an inline-block align to top if it has no content? (2 个答案) 关闭 8 年前。
Ada 信息交换所 states the following : The use of pragma Inline does have its disadvantages. It can create
Name
我问这个基本问题是为了让记录更正。已转介 this question和 its currently accepted answer ,这没有说服力。然而second most voted answer
你好, 在管理面板中,我创建了用于添加产品的表单。表单包括 2 个内联表单集,因为有一些与产品相关的模型。用户可以创建产品,然后定义该产品的不同属性的变体。我将举例说明这一点。用户拥有一个品牌的 3
有很多关于 inline 的使用以及如何正确执行此操作以达到所需目的的信息,例如此处(我目前将其用作引用)Inline Functions in C . 当我尝试实现页面中指定的内容时,出现编译器错误
我正在编写 gtk 代码。我经常有不需要闭包的简短回调,因为它们传递了它们需要的所有参数。例如,我在创建一些 gtk.TreeViewColumns 时将其置于循环中: def widthChange
这个问题在这里已经有了答案: What is the difference between display: inline and display: inline-block? (7 个答案) 关闭
我已经搜索了很长时间来找到答案,但是我没有找到解决方案... 我制作了一个无序列表的链接,并将它们放在标题下,就像导航栏一样。然而,在 IE 中(是的那个恶魔..)我的链接似乎没有对齐到中间。下面是我
我想将两张 table 并排放置。由于我不是 floating 或使用“css hacks”的忠实拥护者,您有什么建议?没有它是否可以解决,还是我运气不好? 最佳答案 使用 table-cell显示以
这个问题在这里已经有了答案: Why is this inline-block element pushed downward? (8 个答案) 关闭 6 年前。
CSS display 的 inline 和 inline-block 值到底有什么区别? 最佳答案 视觉答案 想象一个 中的元素.如果你给 例如,元素高度为 100px 和红色边框,它看起来像这
我想使用 /纯 CSS 弹出窗口的标签,但是 表现为内联 block ,我无法将其更改为内联。 有没有办法强制表现得像 display:inline 而不是 inline-block?
我的想法是这是不可能的,或者我缺少一个额外的步骤。无论哪种方式,我都被卡住了,无法弄清楚。 使用内联模板的原因是能够使用 Laravel Blade 语法并结合 Vue Js 的强大功能。似乎是两者中
http://christianselig.com/wp/ 对于主导航,如果我使用 display: inline,它们将显示为 block 。我心血来潮添加了 display: inline-blo
Firefox 的 -moz-inline-box 和 -moz-inline-stack 专有显示值有什么区别? 最佳答案 https://developer.mozilla.org/en/CSS/
我是一名优秀的程序员,十分优秀!