R: C++ Optimization Flag When Using the Inline Package

R: C++ Optimization flag when using the inline package

There are a couple of options:

  1. The best solution is to modify this for all usage by R so create e.g. a file ~/.R/Makevars and set CFLAGS, CXXFLAGS, ... there. This will affect all use by R CMD INSTALL ..., R CMD SHLIB ... etc pp and as cxxfunction() from inline uses it, it works here too.

  2. Specific to inline and Rcpp: modify the plugin, that's why it is a plugin system. See Rcpp:::Rcpp.plugin.maker().

  3. Switch back from cxxfunction() to cfunction(), hence do not use a plugin and set all arguments manually.

Needless to say, I like option 1 and use it myself.

Edit: A fourth (and crude !!) method I used to use in the past is to edit $R_HOME/Makeconf and/or Makeconf.site.

How to set g++ compiler flags using Rcpp and inline?

After some source code study, and a hint from Dirk Eddelbuettel, I've worked this out:

settings$env$PKG_CXXFLAGS='-std=c++0x'

You can set PKG_CPPFLAGS the same way.

Here is a complete and more robust example:

library(inline)

src='
using namespace Rcpp;
std::vector<const char*> test={"Hello","World","!!!"};
return wrap(test);
'

settings=getPlugin("Rcpp")
settings$env$PKG_CXXFLAGS=paste('-std=c++0x',settings$env$PKG_CXXFLAGS,sep=' ')
fun=cxxfunction(signature(),src,plugin="Rcpp",settings=settings)

Sys.unsetenv('PKG_CXXFLAGS')

print(fun())

The paste() makes sure if there were any settings already in the plugin then they are preserved.

The unsetenv() is something cxxfunction should already be doing (IMHO). Currently it will add variables to the environment, but not remove them after. So, without the unsetenv() call, if you later ran cxxfunction again, but with all defaults, any CXXFLAGS you had earlier set would get used. This might not matter, or it might give surprising results. (Imagine if your were using PKG_CXXFLAGS to set -Wall -Werror for your own code, but later code links to a 3rd party library and refuses to compile with those options.)

gcc inline-assembly compilation error when optimization flags is not enabled?

If you remove some of the operands, like for just a 256-bit add, you'll notice that with optimization disabled, GCC wants to put a pointer directly to each memory operand in a separate register, instead of inventing addressing modes for each of them relative to the same base. So it runs out of registers. (See the middle part of Strange 'asm' operand has impossible constraints error for compiler output that demos this.)

You might want __attribute__((optimize("-O3"))) on this function or something so it doesn't stop the rest of your program from compiling.


Also, this doesn't need a "memory" clobber; you don't write any memory, and you only read via "m" operands. It also doesn't need to be volatile: it has no side effects besides writing the "+r" in/output regs. Except for sum7, technically those should be "+&r" early-clobber operands, since you write them before reading all of the input and in-out operands, but there's basically no plausible way for GCC to overlap registers between pointers and integer in/out here.

You could also let the compiler choose "mre" instead of forcing memory source operands even if the source operand was a compile-time constant or in a register. But if that makes it generate worse asm for your actual use-case (e.g. separate mov load into regs instead of memory source for adc), then maybe just "me". (The "e" constraint is like "i" but only allowing constants that fit in a 32-bit signed integer, i.e. safe for use as an immediate with 64-bit operand-size for instructions other than mov.)


BTW, with clang (but not GCC) you don't need inline asm at all: use typedef unsigned _ExtInt(512) uint512; - see 256-bit arithmetic in Clang (extended integers)

Rcpp sourceCpp compiler settings

No, you can't (easily), and in general not from a function.

These settings are "fixed" from when R itself is built. You can edit the file -- but you will have to so each time R is rebuilt / reinstalled.

On my box the file is $(R RHOME)/etc/Makeconf.

What is the specific GCC flag that turns on immediate value propagation for inline assembly parameters?

Looks like -ftree-ter (Replace temporary expressions in the SSA->normal pass - whatever that is) does the trick:

gcc -ftree-ter test.c   # no errors

Here's how I determined that:

  1. gcc -Q --help=optimizers tells you what optimizations are enabled/disabled by default (some are enabled)
  2. gcc -O -Q --help=optimizers tells you what optimizations are enabled/disabled for -O
  3. redirect the output of those commands to files and diff them.
  4. try the optimizations that are enabled only when -O is specified until one works.

No support to -finline-functions in clang 3.5?

See this commit: http://llvm.org/klaus/clang/commit/6590426aeb5275ec33dac2877f9349bbbb2d4b2e/#0-L-571

Previously, that flag was ignored and the user was not notified. Now the user is notified that it is ignored. You shouldn't have seen any difference in the code generation with or without that flag.

It should only be a warning, but you've upgraded it to an error with -Werror.



Related Topics



Leave a reply



Submit