简体   繁体   English

在gcc编译器中禁用特定的优化(消除死代码)

[英]Disabling specific optimization(Dead code elimination) in gcc compiler

I would like to disable dead code elimination optimization in c++ compilation. 我想在C ++编译中禁用死代码消除优化。 Is there a way to disable this particular optimization by keeping all other -O optimization. 有没有一种方法可以通过保留所有其他-O优化来禁用此特定优化。 I tried with -fnodce but its not working. 我尝试使用-fnodce,但无法正常工作。

Update (copied from a comment): I have something like 更新 (从评论中复制):我有类似

timer t;
t.start();
for(int i=1;i<=1000;++i)
    object t;
t.stop();

I want to measure object t construction time and do nothing with it. 我想测量对象t构建时间,而对其不执行任何操作。 I dont want to do this by creating an array of 1000 objects. 我不想通过创建1000个对象的数组来做到这一点。 Is there a way to solve this? 有办法解决吗?

Add "volatile" qualifier on constructed objects, this tells the compiler to assume that there are side-effects to construction thus preventing optimizing it away. 在构造的对象上添加“易失”限定符,这告诉编译器假定构造存在副作用,因此阻止了对其进行优化。 That is: 那是:

timer t; 
t.start(); 
for(int i=1;i<=1000;++i) 
  volatile object t; 
t.stop(); 

Well if you just want to measure the initialization time of your objects why try to coerce the compiler to avoid DCE and whatnot and not just write it in such a way as to avoid the problem in the first place? 好吧,如果您只是想测量对象的初始化时间,为什么要强迫编译器避免DCE和诸如此类的事情,而不是仅仅以避免问题的方式编写它?

object *arr = new object[100];   // allocate that outside the function and pass it into it
for (int i = 0; i < 100; i++) {
    arr[i] = new object;
}

If the function is large enough to avoid inlining that should do the trick just fine - otherwise you can export the function and call it from another compilation module to avoid unwanted optimizations. 如果函数足够大,可以避免内联,那么就可以解决问题-否则,您可以导出函数并从另一个编译模块调用它,以避免不必要的优化。 Simple, no tricks with some compiler flags that may have unintended consequences and the only overhead is an array store - if THAT measurably influences your timing you're measuring the wrong things anyhow. 简单,没有带有某些编译器标志的技巧,可能会带来意想不到的后果,并且唯一的开销就是数组存储-如果可测量地影响您的时间,那么无论如何您都在测量错误的东西。

Or if you really want some compiler specific flags - gcc has a noinline attribute.. 或者,如果您真的想要一些编译器特定的标志-gcc具有noinline属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM