haskell / haskell/cabal

Improvement: Improve GHC optimization flag management and interaction

Open
#11,958 0 comments 1 reaction 1 assignee Claimed by @zlonast View on GitHub
re: ghc-options type: enhancement
Dominant language
Haskell
Stars
1.7k
Forks
750
Avg merge
4d 3h
Merged PRs (30d)
28

Description

**Describe the feature request**
Improve how Cabal manages and propagates optimization flags (`-O`, `-g`) when invoking GHC. The goal is to ensure better consistency with GHC's actual flag semantics and to fix edge cases where incorrect or unsupported flags were being passed.

Specifically, the request includes:
1. **Adding explicit optimization flags:** Ensure Cabal passes `-optc-O2`, `-optcxx-O2`, and `-opta-O2` when building C/C++/ASM sources with optimization, provided the user hasn't already specified optimization flags (to avoid overriding or duplication).
2. **Aligning `GhcOptimisation` with GHC expectations:** Remove unsupported optimization levels (like `-Odph`) and normalize internal handling of optimization levels to match GHC’s documented behavior (e.g., handling `-On` where `n > 2`).
3. **Refining flag rendering:** Standardize the rendering of `-O` and `-g` flags to be more explicit (e.g., mapping defaults to `-O1` and `-g0` where appropriate) to ensure the compiler receives the expected optimization and debug-info levels.

**Additional context**
Currently, there is a discrepancy between Cabal's internal representation of optimization levels and GHC's actual capabilities.
* Some internal types (like `GhcSpecialOptimisation`) represent flags that don't map correctly to standard GHC behavior.
* GHC documentation states that any `-On` where `n > 2` is equivalent to `-O2`, and Cabal should reflect this more accurately.
* By making these flags explicit, we improve the robustness of the build process for native code and ensure that user-provided optimization settings are respected without conflict.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.