Disabling vectorisation at '-O3'

Hi CFE-Devs,

While debugging a problem in our back-end, I want to temporarily remove JUST ‘-vectorize’ and ‘-vectorize-slp’ from the flags passed with ‘-cc1’, but I want to leave all the other command-line options the same. But when I use:

clang -S -O3 -fno-vectorize -fno-slp-vectorize foo.c

it is still inserting ‘-vectorize -vectorize-slp’. The code in ‘Clang.cpp’ for this is at line #4858 (on the v6.0 branch):

// Enable vectorization per default according to the optimization level

// selected. For optimization levels that want vectorization we use the alias

// option to simplify the hasFlag logic.

bool EnableVec = shouldEnableVectorizerAtOLevel(Args, false);

OptSpecifier VectorizeAliasOption =

EnableVec ? options::OPT_O_Group : options::OPT_fvectorize;

if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,

options::OPT_fno_vectorize, EnableVec))

CmdArgs.push_back("-vectorize-loops");

// -fslp-vectorize is enabled based on the optimization level selected.

bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, true);

OptSpecifier SLPVectAliasOption =

EnableSLPVec ? options::OPT_O_Group : options::OPT_fslp_vectorize;

if (Args.hasFlag(options::OPT_fslp_vectorize, SLPVectAliasOption,

options::OPT_fno_slp_vectorize, EnableSLPVec))

CmdArgs.push_back("-vectorize-slp");

but this is ignoring the ‘-fno-*’ flags and overriding because of the presence of ‘-O3’ and the defaults for ‘-O3’.

Am I just using ‘clang’ incorrectly, and if so, how can I achieve what I am trying to achieve? I don’t want to use ‘-O0’ or ‘-O1’ because I still want all of the other options passed as usual with ‘-cc1’ as would normally be passed with ‘-O3’. I am not just debugging one file, but would like to build hundreds of sources with the amended options by just changing some variables in the Makefiles to see what the impact is.

Thanks,

MartinO

This looks like a bug to me; I’d expect -fno-* to override the -O flag rather than meaning “use the default for this -O level”. I’d suggest you look up who added these flags and check with them to make sure this wasn’t the intent (it it was, we’ll need further discussion), then fix the driver to do the “obvious” thing.

Thanks Richard, I’ll do that - and I can add it as a bug. It’s almost certainly easy enough to fix, but I wanted to be sure that I wasn’t just making a naïve mistake.

MartinO

Yes, it looks like passing ‘EnableVec’ and ‘EnableSLPVec’ to ‘Args.hasFlag’ should be replaced with ‘false’ and then it has the expected behaviour.

MartinO

No, I’m wrong. I think that bug is actually in ‘hasFlag’ itself. In ‘llvm/lib/Option/ArgList.cpp’ line #70:

bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,

bool Default) const {

if (Arg *A = getLastArg(Pos, PosAlias, Neg))

return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);

return Default;

}

This doesn’t match when ‘Neg’ is matched, and should probably be:

bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,

bool Default) const {

if (Arg *A = getLastArg(Pos, PosAlias, Neg))

return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);

else

return !A->getOption().matches(Neg) && Default;

}

So the logic in ‘CLang.cpp’ looks valid. I’ve posted this to LLVM-Dev.

Thanks again,

MartinO

Please ignore this thread - I got myself confused, the code is fine - too many long days and nights staring at the screen.

MartinO

Please ignore this thread - I got myself confused, the code is fine - too many long days and nights staring at code.

There is an issue, but it is different to what I thought.

My command line is not:

clang -S -O3 -fno-vectorize -fno-slp-vectorize foo.c

but:

clang -S -fno-vectorize -fno-slp-vectorize -O3 foo.c

The difference was subtly hidden in a much longer argument list built by a Makefile.

So it seems that the position of ‘-O3’ is overriding the other flags. The code in both ‘Clang.cpp’ and ‘ArgList.cpp’ are fine, my mistake, and sorry for wasting people’s time.

MartinO