0

In a project that includes dozens of modules, we have O1 as the default debug optimization level.

Sometimes, I need a lesser optimization (i.e., no optimization at all). So I use the clang attribute optnone to exclude a single function from optimization.

But when the number of functions to exclude is large, this becomes cumbersome.

Is there a way to exclude a whole module from optimization? For example set a pragma at the top of the module.

ysap
  • 7,723
  • 7
  • 59
  • 122
  • 1
    “Needing” a lesser optimization is an indication of problems in the source code that usually should be fixed. Commonly, there is some code that has behavior not defined by the C standard that could be corrected by using an alternate method or, if necessary, by using some compiler feature. – Eric Postpischil Nov 12 '20 at 13:22
  • @EricPostpischil - or, it just makes it easier to step through the disassembly code in sync with the source. – ysap Nov 12 '20 at 13:29
  • 1
    @EricPostpischil I think the problem is worse than that - code that can be *observed* to fail when optimized isn't guaranteed to work when not optimized - it may fail in ways that aren't observed or it may fail intermittently. Turning off optimizations is merely **hoping to hide the real problem** and IMO a **really** suspect approach - at best. – Andrew Henle Nov 12 '20 at 13:31
  • @AndrewHenle - I think you two are making too many assumptions in this case, but as a general comment, you are right. – ysap Nov 12 '20 at 13:32
  • @ysap If you don't know why the code fails when it's optimized, you have no idea why it doesn't fail when not optimized. Which means you can not be sure it will work under all conditions. There are no assumptions there. – Andrew Henle Nov 12 '20 at 13:36
  • As I said, you are making too many assumptions in this particular case, even if you think you're not. But as a general statement you are correct. – ysap Nov 12 '20 at 13:54
  • Again: **no assumptions**. It all follows logically - if you don't know why it fails with optimizations enabled, **you don't know why it works without optimization**. Therefore, **if you don't know WHY it works, you CAN'T know IF it works in all cases**. There are no assumptions there. You are papering over a problem that you don't understand and hoping nothing bad happens. The code is **broken** in some manner and you don't know why. – Andrew Henle Nov 13 '20 at 12:32
  • @AndrewHenle - thanks for the insight (not cynically). – ysap Nov 13 '20 at 22:13

1 Answers1

2

Clang has a compiler-specific pragma yes, try:

#pragma clang optimize off
//region of code
#pragma clang optimize on
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122