Specialising through specialised method calls (#19644)
In #19644, we discovered that the ClassOp/DFun rules from Note [ClassOp/DFun selection] inhibit transitive specialisation in a scenario like ``` class C a where m :: Show b => a -> b -> ...; n :: ... instance C Int where m = ... -- $cm :: Show b => Int -> b -> ... f :: forall a b. (C a, Show b) => ... f $dC $dShow = ... m @a $dC @b $dShow ... main = ... f @Int @Bool ... ``` After we specialise `f` for `Int`, we'll see `m @a $dC @b $dShow` in the body of `$sf`. But before this patch, Specialise doesn't apply the ClassOp/DFun rule to rewrite to a call of the instance method for `C Int`, e.g., `$cm @Bool $dShow`. As a result, Specialise couldn't further specialise `$cm` for `Bool`. There's a better example in `Note [Specialisation modulo dictionary selectors]`. This patch enables proper Specialisation, as follows: 1. In the App case of `specExpr`, try to apply the CalssOp/DictSel rule on the head of the application 2. Attach an unfolding to freshly-bound dictionary ids such as `$dC` and `$dShow` in `bindAuxiliaryDict` NB: Without (2), (1) would be pointless, because `lookupRule` wouldn't be able to look into the RHS of `$dC` to see the DFun. (2) triggered #21332, because the Specialiser floats around dictionaries without accounting for them in the `SpecEnv`'s `InScopeSet`, triggering a panic when rewriting dictionary unfoldings. Fixes #19644 and #21332.
Showing
- compiler/GHC/Core/Opt/Arity.hs 1 addition, 1 deletioncompiler/GHC/Core/Opt/Arity.hs
- compiler/GHC/Core/Opt/Specialise.hs 171 additions, 63 deletionscompiler/GHC/Core/Opt/Specialise.hs
- compiler/GHC/Core/Rules.hs 6 additions, 3 deletionscompiler/GHC/Core/Rules.hs
- compiler/GHC/Core/SimpleOpt.hs 1 addition, 1 deletioncompiler/GHC/Core/SimpleOpt.hs
- compiler/GHC/Core/Subst.hs 14 additions, 5 deletionscompiler/GHC/Core/Subst.hs
- compiler/GHC/Plugins.hs 1 addition, 1 deletioncompiler/GHC/Plugins.hs
- testsuite/tests/perf/compiler/T4007.stdout 0 additions, 1 deletiontestsuite/tests/perf/compiler/T4007.stdout
- testsuite/tests/perf/compiler/all.T 1 addition, 0 deletionstestsuite/tests/perf/compiler/all.T
- testsuite/tests/simplCore/should_compile/Makefile 0 additions, 5 deletionstestsuite/tests/simplCore/should_compile/Makefile
- testsuite/tests/simplCore/should_compile/T17966.stderr 310 additions, 0 deletionstestsuite/tests/simplCore/should_compile/T17966.stderr
- testsuite/tests/simplCore/should_compile/T19644.hs 20 additions, 0 deletionstestsuite/tests/simplCore/should_compile/T19644.hs
- testsuite/tests/simplCore/should_compile/T19644.stderr 246 additions, 0 deletionstestsuite/tests/simplCore/should_compile/T19644.stderr
- testsuite/tests/simplCore/should_compile/T6056.stderr 0 additions, 1 deletiontestsuite/tests/simplCore/should_compile/T6056.stderr
- testsuite/tests/simplCore/should_compile/T7785.stderr 405 additions, 5 deletionstestsuite/tests/simplCore/should_compile/T7785.stderr
- testsuite/tests/simplCore/should_compile/all.T 8 additions, 4 deletionstestsuite/tests/simplCore/should_compile/all.T
Loading
Please register or sign in to comment