Opened 2 years ago

Closed 12 months ago

#14452 closed bug (fixed)

`-optc-O3` getting shadowed by automatically injected -O flags

Reported by: hvr Owned by: RolandSenn
Priority: normal Milestone: 8.8.1
Component: Compiler Version: 8.2.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case: make test TEST=T14452
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D5318 Phab:D5398
Wiki Page:

Description

Consider the following example:

{-# LANGUAGE CApiFFI #-}

{-# OPTIONS_GHC -optc-O3 #-}

module M where

foreign import capi unsafe "stdlib.h exit" c_exit :: Int -> IO ()

However, the -optc-O3 flag has no effect, unless ghc is called without any -O flags, or with -O0.

Here's the resulting C compiler invocations for different -O flags:

$ ghc -fforce-recomp -v -c  m.hs |& grep O3
gcc -fno-stack-protector -DTABLES_NEXT_TO_CODE -O3 -x c /tmp/ghc14023_0/ghc_2.c -o /tmp/ghc14023_0/ghc_3.s -Wimplicit -S -include /opt/ghc/8.2.1/lib/ghc-8.2.1/include/ghcversion.h -I. -I/opt/ghc/8.2.1/lib/ghc-8.2.1/base-4.10.0.0/include -I/opt/ghc/8.2.1/lib/ghc-8.2.1/integer-gmp-1.0.1.0/include -I/opt/ghc/8.2.1/lib/ghc-8.2.1/include
$ ghc -fforce-recomp -O0 -v -c  m.hs |& grep O3
gcc -fno-stack-protector -DTABLES_NEXT_TO_CODE -O3 -x c /tmp/ghc14045_0/ghc_2.c -o /tmp/ghc14045_0/ghc_3.s -Wimplicit -S -include /opt/ghc/8.2.1/lib/ghc-8.2.1/include/ghcversion.h -I. -I/opt/ghc/8.2.1/lib/ghc-8.2.1/base-4.10.0.0/include -I/opt/ghc/8.2.1/lib/ghc-8.2.1/integer-gmp-1.0.1.0/include -I/opt/ghc/8.2.1/lib/ghc-8.2.1/include
$ ghc -fforce-recomp -O1 -v -c  m.hs |& grep O3
gcc -fno-stack-protector -DTABLES_NEXT_TO_CODE -O3 -x c /tmp/ghc14073_0/ghc_2.c -o /tmp/ghc14073_0/ghc_3.s -Wimplicit -S -O -include /opt/ghc/8.2.1/lib/ghc-8.2.1/include/ghcversion.h -I. -I/opt/ghc/8.2.1/lib/ghc-8.2.1/base-4.10.0.0/include -I/opt/ghc/8.2.1/lib/ghc-8.2.1/integer-gmp-1.0.1.0/include -I/opt/ghc/8.2.1/lib/ghc-8.2.1/include
$ ghc -fforce-recomp -O2 -v -c  m.hs |& grep O3
gcc -fno-stack-protector -DTABLES_NEXT_TO_CODE -O3 -x c /tmp/ghc14093_0/ghc_2.c -o /tmp/ghc14093_0/ghc_3.s -Wimplicit -S -O2 -include /opt/ghc/8.2.1/lib/ghc-8.2.1/include/ghcversion.h -I. -I/opt/ghc/8.2.1/lib/ghc-8.2.1/base-4.10.0.0/include -I/opt/ghc/8.2.1/lib/ghc-8.2.1/integer-gmp-1.0.1.0/include -I/opt/ghc/8.2.1/lib/ghc-8.2.1/include
$ ghc -fforce-recomp -O3 -v -c  m.hs |& grep O3
gcc -fno-stack-protector -DTABLES_NEXT_TO_CODE -O3 -x c /tmp/ghc14119_0/ghc_2.c -o /tmp/ghc14119_0/ghc_3.s -Wimplicit -S -O2 -include /opt/ghc/8.2.1/lib/ghc-8.2.1/include/ghcversion.h -I. -I/opt/ghc/8.2.1/lib/ghc-8.2.1/base-4.10.0.0/include -I/opt/ghc/8.2.1/lib/ghc-8.2.1/integer-gmp-1.0.1.0/include -I/opt/ghc/8.2.1/lib/ghc-8.2.1/include

To summarise, here's the resulting effective -O-level passed to gcc:

GHC invocation GCC invocation Effective C -O-level
ghc gcc -O3 -O3
ghc -O0 gcc -O3 -O3
ghc -O1 gcc -O3 -O1 -O1
ghc -O2 gcc -O3 -O2 -O2
ghc -O3 gcc -O3 -O2 -O2

Consequently, the only way to have C code compiled with -O3 is to force GHC into -O0 mode; this is obviously not ideal, as it easily kills any benefit you'd gain from passing -O3 to the C compiler... ;-)

I see a few alternatives on how to resolve this:

  1. Have GHC recognise -optc-O[0-9] and suppress automatically injecting any -O<n> of its own
  2. Have GHC inject its automatic -O-flags before user-provided -optc flags
  3. Have GHC inject -optc flags as late as possible on the C compiler command-line
  4. Implement a new variant of the -optc flag which injects its flags as late as possible on the C compiler command-line

Change History (10)

comment:1 Changed 13 months ago by RolandSenn

Milestone: 8.8.1
Owner: set to RolandSenn

comment:2 Changed 13 months ago by RolandSenn

Differential Rev(s): Phab:D5318
Status: newpatch
Test Case: make test TEST=T14452

comment:3 Changed 13 months ago by osa1

Phab:D5318 implements (2) by passing user-provided flags last, which I think is a good way to fix this.

comment:4 Changed 13 months ago by Ben Gamari <ben@…>

In f2d9fb0c/ghc:

Calling gcc: Pass optc flags as last options (#14452)

Test Plan: make test TEST=T14452

Reviewers: hvr, bgamari, monoidal, thomie, osa1

Reviewed By: osa1

Subscribers: rwbarton, carter

GHC Trac Issues: #14452

Differential Revision: https://phabricator.haskell.org/D5318

comment:5 Changed 13 months ago by bgamari

Resolution: fixed
Status: patchclosed

comment:6 Changed 13 months ago by RolandSenn

Owner: RolandSenn deleted
Resolution: fixed
Status: closednew

The test T14452 is broken under Windows. Under Windows all the parameters of gcc including the last (-O3) are quoted:

"C:\msys64\home\Roli\ghc\inplace\lib\../mingw/bin/gcc.exe" "-fno-stack-protector" "-DTABLES_NEXT_TO_CODE" "-x" "c" "C:\Users\Roli\AppData\Local\Temp\ghc7308_0\ghc_2.c" "-o" "C:\Users\Roli\AppData\Local\Temp\ghc7308_0\ghc_3.s" "-no-pie" "-Wimplicit" "-S" "-O2" "-include" "C:/msys64/home/Roli/ghc/includes\ghcversion.h" "-iquote." "-IC:\msys64\home\Roli\ghc\libraries\base\include" "-IC:\msys64\home\Roli\ghc\libraries\base\dist-install\build\include" "-IC:\msys64\home\Roli\ghc\libraries\integer-gmp\include" "-IC:\msys64\home\Roli\ghc\libraries\integer-gmp\dist-install\build\include" "-IC:/msys64/home/Roli/ghc/rts/dist/build" "-IC:/msys64/home/Roli/ghc/includes" "-IC:/msys64/home/Roli/ghc/includes/dist-derivedconstants/header" "-O3"

I'll create a better test.

comment:7 Changed 13 months ago by RolandSenn

Owner: set to RolandSenn

comment:8 Changed 13 months ago by RolandSenn

Differential Rev(s): Phab:D5318Phab:D5318 Phab:D5398
Status: newpatch

comment:9 Changed 13 months ago by Tamar Christina <tamar@…>

In 6090002e/ghc:

Improve test T14452 for Windows

Summary: Under Windows all parameters to gcc are enclosed in quotes, opposite to
Linux, where the quotes are missing. Therefore in the test, we remove all
quotes in the stdout file with sed.

Test Plan: make test TEST=T14452

Reviewers: osa1, hvr, bgamari, monoidal, Phyx, simonpj

Reviewed By: Phyx

Subscribers: rwbarton, carter

GHC Trac Issues: #14452

Differential Revision: https://phabricator.haskell.org/D5398

comment:10 Changed 12 months ago by Phyx-

Resolution: fixed
Status: patchclosed
Note: See TracTickets for help on using tickets.