[Bioc-devel] package c++ optimization flag

Shian Su @u@@ @ending from wehi@edu@@u
Thu Sep 20 02:46:14 CEST 2018

As Kasper said, you shouldn’t try to override a user’s local settings. You are however free to change your own system level flags via ~/.R/Makevars, see https://cran.r-project.org/doc/manuals/r-release/R-admin.html#Customizing-package-compilation.

I’m not a C++ expert but I was under the impression that you’d want to do debugging under -g -O0 anyway and -O2 code is really no easier to decipher than -O3. So I recommend you put it on -O0 through ~/.R/Makevars when debugging and -O3 when benchmarking. Let users decide for themselves how slow they want their code to run.

It’s tedious but you’ll have to alternatively comment out regular and debug settings, for example I have the following in my ~/.R/Makevars

# Optimised flags
CXXFLAGS += -g -O3 -Wall -pipe -Wno-unused -pedantic
CXX11FLAGS += -g -O3 -Wall -pipe -Wno-unused -pedantic
CXX14FLAGS += -g -O3 -Wall -pipe -Wno-unused -pedantic
CXX17FLAGS += -g -O3 -Wall -pipe -Wno-unused -pedantic

# Debugging flags
# CXXFLAGS += -g -O0 -Wall -pipe -Wno-unused -pedantic
# CXX11FLAGS += -g -O0 -Wall -pipe -Wno-unused -pedantic
# CXX14FLAGS += -g -O0 -Wall -pipe -Wno-unused -pedantic
# CXX17FLAGS += -g -O0 -Wall -pipe -Wno-unused -pedantic

When you switch the flags you’ll also MUST clean out all the intermediate files from the previous build with R CMD BUILD —preclean.

One last note is that if you have an up-to-date version of clang or gcc then you shouldn’t need to worry about -03 breaking any code that works in -O2. Anything that does break is likely to be using some undefined behaviour, and really if we’re requiring packages work cross-platform we ought to require packages to work cross-optimisation-levels.

On 19 Sep 2018, at 22:58, Kasper Daniel Hansen <kasperdanielhansen using gmail.com<mailto:kasperdanielhansen using gmail.com>> wrote:

This should be done extremely sparingly, but it is sometimes necessary if
you know that higher optimization breaks the code. But that is the only use
case. Otherwise you should trust the setting of the user who compiles R,
ie. you should for example never force a higher optimization level IMO.

We do this override in affxparser.  The issue with the approach in
affxparser is that it is non-portable, since we just overwrite the users
-O* setting. However, a flag like -O* is complier dependent, so this is
actually not a great solution (although it has worked for 10 years).  The
"right" way to do this - which I think we should have example code for - is
to use autoconfigure to detect that the compiler is GCC or clang and _then_


the relevant part is essentially

MYCXXFLAGS = -Wno-sign-compare -O0

%.o: %.cpp

(remember you need TABS in Makefiles)

On Wed, Sep 19, 2018 at 5:12 AM Håkon Tjeldnes <hauken_heyken using hotmail.com<mailto:hauken_heyken using hotmail.com>>

When I put this line in makevars:


This is the output of the cpp precompiler:

-O3 -fpic  -g -O2

I read the cpp doc, and it states:

"If you use multiple -O options, with or without level numbers, the last
such option is the one that is effective."

Since, system wide R optimization -O2 always comes last, how can I make my
package overwrite the system wide optimization ?
And should I do this at all, since debugging could get more difficult ?

       [[alternative HTML version deleted]]

Bioc-devel using r-project.org<mailto:Bioc-devel using r-project.org> mailing list

   [[alternative HTML version deleted]]

Bioc-devel using r-project.org<mailto:Bioc-devel using r-project.org> mailing list


The information in this email is confidential and intended solely for the addressee.
You must not disclose, forward, print or use it without the permission of the sender.

The Walter and Eliza Hall Institute acknowledges the Wurundjeri people of the Kulin
Nation as the traditional owners of the land where our campuses are located and
the continuing connection to country and community.

	[[alternative HTML version deleted]]

More information about the Bioc-devel mailing list