How to override pgcc's optimization flag?

When compiling a C source file with pgcc, the command below works, of course:

pgcc -O1 -c hello.c -o hello.o



/* hello.c */
int main()
{
    return 123;
}

This also works:

pgcc -O1 -O0 -c hello.c -o hello.o

But this doesn’t work as I expected (code eventually gets compiled with -O2):

pgcc -O2 -O0 -c hello.c -o hello.o
pgcc-Info-Switch -Mvect forces -O2

This doesn’t work either:

pgcc -O2 -Mnovect -O0 -c hello.c -o hello.o
pgcc-Info-Switch -Mvect forces -O2

Are these expected behaviors? At least the last one looks surprising.

Thanks,
Victor

There is a “Command-line Conflicting Options” section in the reference manual, which reads:

Some options have an opposite or negated counterpart. For example, both -⁠Mvect and -⁠Mnovect are available. -⁠Mvect enables vectorization and -⁠Mnovect disables it. If you used both of these commands on a command line, they would conflict.

Note: Rule: When you use conflicting options on a command line, the last encountered option takes precedence over any previous one.

Link: PGI Compiler User's Guide Version 19.7 for x86 and NVIDIA Processors

So I think what I described above is actually a bug, isn’t it? Any ideas?

Hi Victor,

We haven’t found a solution for you yet. Do you get the proper optimization when you do
% pgcc -O2 -Mnovect hello.c?

Hi Victor,

Brent’s been looking into what can be done. We think we can modify our configuration (rc) files to get this to work the way you’d like, but we’ll know for sure once an engineer is able to dig into it further. I’ve added problem report TPR #27806 to track.

In the meantime, there’s not much you can do except not include “-O2”, but Brent let me know that it gets implicitly added by your build system so may not be easy to remove.

Thanks,
Mat

Hi Brent, hi Mat,

Thanks a lot for looking into this. Let me put it into context. There is one source file in one external library our group is using that always segfaults when compiled with “-O2” or above. The user usually sets a universal optimization flag in CFLAGS, e.g. “-fast”. This flag works fine for all but that one file. Therefore, in the Makefile, we append “-O1” to CFLAGS for that specific file, hoping that “-O1” would override “-O2” “-O3” “-O4” or “-fast”.

Unfortunately, the code also segfaults with “-O2 -Mnovect”. Since the code in question was not written by us, nobody wants to touch it. What I could do is using something like “filter-out” to remove “-O2” and above from CFLAGS.

Thanks,
Victor

Is the problematic library publicly available so I could download it and take a look?

-Mat