ghc: internal error: Tag inference failed on:TagCheck failed on entry in Data.Sequence.Internal - value:$dQuote_s1X95 _s1X95::P64 (GHC version 9.4.8 for x86_64_unknown_linux) Please report this as a GHC bug: https://www.haskell.org/ghc/reportabug
Since PUSH_G doesn't guarantee we get a tagged pointer things to go wrong. Fwiw the code we produce is the same in 9.6+, but it seems we do apply the tags for $QuoteQ in newer versions.
We must grab the properly tagged pointer now instead of a untagged one, but it's very unclear to me at this point what made the difference.
Still not sure why this works on newer versions. Enabling debug info for the interpreter I see the final relevant assembly for 9.4
bcoSize = 24Sp = 0x4200dd0a60 pc = 0 PUSH_UBX 0x1 Sp = 0x4200dd0a58 pc = 3 PACK 1 words with itbl 0x7fe9eaa18b08 Built Object 0x4200dd0b99 = 0x4200dd0b99: ghc-prim:GHC.Types.I#(0x1#)Sp = 0x4200dd0a58 pc = 6 PUSH_L 0Sp = 0x4200dd0a50 pc = 8 PACK 1 words with itbl 0x7fe9f433fd48 Built Object 0x4200dd0baa = 0x4200dd0baa: main:SDef.Single(0x4200dd0b99)Sp = 0x4200dd0a50 pc = 11 PUSH_L 0Sp = 0x4200dd0a48 pc = 13 PUSH_G 0x7fe9ebddbb80 <- the argument we care about - no tag hereSp = 0x4200dd0a40 pc = 15 PUSH_G 0x7fe9ebddba28 Sp = 0x4200dd0a38 pc = 17 PUSH_APPLY_PPPSp = 0x4200dd0a30 pc = 18 PUSH_G 0x7fe9f43421c8Sp = 0x4200dd0a28 pc = 20 SLIDE 5 down by 2Sp = 0x4200dd0a38 pc = 23 ENTER
And for ghc_head:
bcoSize = 24Sp = 0x420027f450 pc = 0 PUSH_UBX 0x1 Sp = 0x420027f448 pc = 3 PACK 1 words with itbl 0x7f762ad27ab8 Built Object 0x420027f589 = 0x420027f589: ghc-prim:GHC.Types.I#(0x1#)Sp = 0x420027f448 pc = 6 PUSH_L 0Sp = 0x420027f440 pc = 8 PACK 1 words with itbl 0x7f7634002080 Built Object 0x420027f59a = 0x420027f59a: main:SDef.Single(0x420027f589)Sp = 0x420027f440 pc = 11 PUSH_L 0Sp = 0x420027f438 pc = 13 PUSH_G 0x7f762bfc9530 <- the argument we care about - no tag hereSp = 0x420027f430 pc = 15 PUSH_G 0x7f762bfc9400Sp = 0x420027f428 pc = 17 PUSH_APPLY_PPPSp = 0x420027f420 pc = 18 PUSH_G 0x7f7634004198Sp = 0x420027f418 pc = 20 SLIDE 5 down by 2Sp = 0x420027f428 pc = 23 ENTER
So we seemingly push a untagged address to the stack either way. But somehow this works out fine on master.
Alright seems the actual bug is still present in master. It just happens to not cause segfaults on master.
The difference is on master we always end up using the expected-tagged argument as argument to other functions, which are not strict workers themselves.
That is on 9.4 we get something like:
case $dQuote_s3aE<TagProper> of wild2_s3b4 [Occ=Once1] { Language.Haskell.TH.Syntax.C:Quote ww4_s3b5 [Occ=Once1!] _ [Occ=Dead] ->
And we blow up on the case as the argument isn't actually tagged.
In contrast on ghc-head we get the same $dQuote argument but all the uses look like this:
case Language.Haskell.TH.Lib.Syntax.$p1Quote $dQuote_s2Sw of $dMonad_s2SA [Dmd=SP(MP(A,1C(1,L),A,A,A,A),1C(1,C(1,L)),A,LC(S,L))] { __DEFAULT -> let { sat_s2SE [Occ=Once1] :: Language.Haskell.TH.Lib.Syntax.Exp -> m_s2I5 (Language.Haskell.TH.Lib.Syntax.TExp (SDef.FingerTree a_s2I6)) [LclId] = {$dMonad_s2SA} \r [e_s2SD] GHC.Internal.Base.return $dMonad_s2SA e_s2SD; } in
Where $p1Quote itself re-evaluates the argument so nothing goes wrong.
In the interpreter in the case for PUSH_G we could look at the object we are pushing, follow indirections and then tag the closure the indirection ends up pointing to before we actually push to the stack.
In the tag analysis we could be more pessimistic when analyzing code for the interpreter, resulting in the rewrite pass inserting an additional eval that would then apply the proper tag once executed.
I think I will give the former a try as it seems like it should improve bytecode performance while also fixing the bug.