为什么 constexpr 上下文会使编译器失败,而没有完美优化?

本文介绍了为什么 constexpr 上下文会使编译器失败,而没有完美优化?的处理方法,对大家解决问题具有一定的参考价值

问题描述

我玩弄了 constexpr 并发现了一些有趣的行为:

I played around with constexpr and realized some interesting behavior:

  • 在某些情况下,在函数前添加 constexpr 可以使 GCC 尝试更加努力地优化,从而完全优化函数并仅提供计算值.
  • 但是,从 constexpr 上下文调用这样一个完全优化的函数会导致错误,因为它在内部使用(编译器内置的)函数/内在函数,这些函数/内在函数未标记为 constexpr(尤其是 memcpy).
  • (Clang 在将 constexpr 应用于此类函数时直接失败,即使没有 constexpr 上下文.)
  • In some situations adding constexpr in front of a function enables GCC to try optimizing harder which results in fully optimizing the function away and just providing the calculated value.
  • However, calling such a fully optimized function from a constexpr context results in errors because it internally uses (compiler-built-in) functions/intrinsics which are not marked constexpr (in particularly memcpy).
  • (Clang fails directly when applying constexpr to such a function, even without a constexpr context.)

为什么会这样?

  • 即使在 constexpr 上下文中,编译器 (GCC) 是否仍能优化?
  • C++ 提案 P0202(将其变为 C++20)想要制作像 这样的函数memcpy constexpr(参见 original 中的 III.B 部分修订版),但被拒绝并更改了,因为此类函数的编译器内置版本将实现相同的效果(请参阅<a href="https://中的III.A部分wg21.link/p0202" rel="nofollow noreferrer" target="_blank">最新版本).
  • 那么,GCCClangconstexpr 函数/上下文中不允许 memcpy 是错误的吗?(注意:memcpy__builtin_memcpy 是等价的.)
  • Shouldn't the compiler (GCC) be able to still optimize, even in constexpr context?
  • C++ proposal P0202 (which made it into C++20) wanted to make functions like memcpy constexpr (see section III.B in original revision), but that was rejected and changed because compiler-built-in versions of such functions would achieve the same (see section III.A in latest revision).
  • So, are GCC and Clang wrong in not allowing memcpy in constexpr functions/context? (Note: memcpy and __builtin_memcpy are equivalent.)

因为例子比较容易理解,这里有这样一个例子.
(您甚至可以在 Compiler Explorer here 中更轻松地看到它的结果.)

As examples are easier to understand, here is such an example.
(You can even see it more comfortably with its results in Compiler Explorer here.)

注意:我无法想出一个简单的例子,在函数中简单地添加 constexpr 有助于 GCC 优化器完全优化,否则它不会.但请相信我,我有这样的例子,它们更复杂(而且很遗憾是封闭源代码).

Note: I was unable to come up with a simple example where simply adding constexpr to the function helped the GCC optimizer to fully optimize, which it otherwise would not. But believe me, that I have such examples, which are more complicated (and sadly closed-source).

#include <array>
#include <cstdint>
#include <cstring>

constexpr std::uint32_t extract(const std::uint8_t* data) noexcept
{
    std::uint32_t num;
    memcpy(&num, data, sizeof(std::uint32_t));
    return num;
}

int main()
{
    constexpr std::array<std::uint8_t, 4> a1 {{ 0xff, 0xff, 0xff, 0xff }};
    /*constexpr*/ auto val = extract(a1.data());  // <--- Using constexpr here makes compiler fail.
    return val;
}

GCC 能够将其优化为:

main:     # @main
    mov   eax, -1
    ret

Clang 也可以优化它,如果去掉函数定义前面的 constexpr.

Clang can optimize it too, if removing the constexpr in front of the function definition.

但是,如果在函数调用前面的 constexpr 中进行注释(从而从 constexpr 上下文调用函数),编译器将失败并显示如下内容:

However, if commenting in the constexpr in front of the function call (and thereby calling the function from constexpr context) the compiler fails with something like this:

海合会:

<source>: In function 'int main()':
<source>:15:33:   in 'constexpr' expansion of 'extract(a1.std::array<unsigned char, 4>::data())'
<source>:8:11: error: 'memcpy(((void*)(& num)), ((const void*)(& a1.std::array<unsigned char, 4>::_M_elems)), 4)' is not a constant expression
    8 |     memcpy(&num, data, sizeof(std::uint32_t));
      |     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compiler returned: 1

叮当声:

<source>:5:25: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
constexpr std::uint32_t extract(const std::uint8_t* data) noexcept
                        ^
<source>:8:5: note: non-constexpr function 'memcpy' cannot be used in a constant expression
    memcpy(&num, data, sizeof(std::uint32_t));
    ^
<source>:15:20: error: constexpr variable 'val' must be initialized by a constant expression
    constexpr auto val = extract(a1.data());  // <--- Error!
                   ^     ~~~~~~~~~~~~~~~~~~
<source>:8:5: note: non-constexpr function 'memcpy' cannot be used in a constant expression
    memcpy(&num, data, sizeof(std::uint32_t));
    ^
<source>:15:26: note: in call to 'extract(&a1._M_elems[0])'
    constexpr auto val = extract(a1.data());  // <--- Error!

                         ^
2 errors generated.
Compiler returned: 1

推荐答案

根据dcl.constexpr

对于既不是默认值也不是模板的 constexpr 函数或 constexpr 构造函数,如果不存在参数值,则函数或构造函数的调用可以是核心常量表达式的求值子表达式,或者,对于构造函数,某些常量初始化对象 ([basic.start.static]) 的初始化完整表达式的求值子表达式,程序格式错误,不需要诊断.

For a constexpr function or constexpr constructor that is neither defaulted nor a template, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression, or, for a constructor, an evaluated subexpression of the initialization full-expression of some constant-initialized object ([basic.start.static]), the program is ill-formed, no diagnostic required.

因为 memcpy 不是 constexpr,你的程序是不正确的 NDR.

As memcpy is not constexpr, your program is ill formed NDR.

contsexpr 上下文中使用该函数将允许进行诊断.

Using the function in contsexpr context would allow to have diagnostic.

在某些情况下,在函数前面添加 constexpr 可以让 GCC 尝试更努力地优化,从而完全优化函数并只提供计算值.

In some situations adding constexpr in front of a function enables GCC to try optimizing harder which results in fully optimizing the function away and just providing the calculated value.

这是一个很好的提示(就像之前的inline).

It is a good hint (as inline before).

constexpr 函数可能被误用":

constexpr std::size_t factorial(std::size_t n) {/*..*/}

int main()
{
    std::cout << factorial(5); // computed at runtime (but probably optimized)
}

正确的方法是

int main()
{
    constexpr auto fact5 = factorial(5); // computed at compile time
    std::cout << fact5; 
}

这篇关于为什么 constexpr 上下文会使编译器失败,而没有完美优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1167

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1069

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:806

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:898

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:930

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:876

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:863

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:833

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1718

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1308