On Thu, 24 Oct 2024 10:12:03 +0300 Ilias Tsitsimpis wrote:

Add replaceJump for a larger jump range.

On the Loong64 platform, there may be situations that are out of the
R_LARCH_B26 jump range. The previous commit added an additional
--ghc-options parameter, but the issue persists. The reason for this is
the lack of code for jump substitution, so we should add this part of
the code to make sure that the --ghc-options parameter works.

Please refer to the attachment for specific changes.

> Hi Dandan,
>
> On Thu, Oct 24, 2024 at 11:16AM, zhangdandan wrote:
> > Please give us some suggestions.
> > Let's discuss and solve the problem together under this bug.
> > Your opinions are welcome.
>
> Unfortunately I am not an expert on this and I don't really know why it
> fails. It looks like GHC is broken/incomplete on LoongArch64, so I would
> suggest you report this upstream[1]. I will happily backport any fix but
> we have to first discuss it with them.
>
> [1] https://gitlab.haskell.org/ghc/ghc/-/issues
>
> As a workaround, maybe we can try to build an unregisterised compiler on
> LoongArch64 and use that instead, until this bug is fixed. Can you try
> and see if an unregisterised GHC works and build pandoc successfully?
>
> Best,
>
> --
> Ilias
>
>

diff --git a/compiler/GHC/CmmToLlvm/Mangler.hs b/compiler/GHC/CmmToLlvm/Mangler.hs
index 749cede..6262519 100644
--- a/compiler/GHC/CmmToLlvm/Mangler.hs
+++ b/compiler/GHC/CmmToLlvm/Mangler.hs
@@ -38,7 +38,7 @@ llvmFixupAsm platform f1 f2 = {-# SCC "llvm_mangler" #-}
 
 -- | These are the rewrites that the mangler will perform
 rewrites :: [Rewrite]
-rewrites = [rewriteSymType, rewriteAVX, rewriteCall]
+rewrites = [rewriteSymType, rewriteAVX, rewriteCall, rewriteJump]
 
 type Rewrite = Platform -> B.ByteString -> Maybe B.ByteString
 
@@ -123,6 +123,29 @@ rewriteCall platform l
         removePlt = replaceOnce (B.pack "@plt") (B.pack "")
         appendInsn i = (`B.append` B.pack ("\n\t" ++ i))
 
+-- | This rewrites bl and b jump inst to avoid creating PLT entries for
+-- functions on loongarch64, because there is no separate call instruction
+-- for function calls in loongarch64. Also, this replacement will load
+-- the function address from the GOT, which is resolved to point to the
+-- real address of the function.
+rewriteJump :: Rewrite
+rewriteJump platform l
+  | not isLoongArch64 = Nothing
+  | isBL l            = Just $ replaceJump "bl" "$ra" "$ra" l
+  | isB l             = Just $ replaceJump "b" "$zero" "$t0" l
+  | otherwise         = Nothing
+  where
+    isLoongArch64 = platformArch platform == ArchLoongArch64
+    isBL = B.isPrefixOf (B.pack "bl\t")
+    isB = B.isPrefixOf (B.pack "b\t")
+
+    replaceJump jump rd rj l =
+        appendInsn ("jirl" ++ "\t" ++ rd ++ ", " ++ rj ++ ", 0") $ removeBracket $
+        replaceOnce (B.pack (jump ++ "\t%plt(")) (B.pack ("la\t" ++ rj ++ ", ")) l
+      where
+        removeBracket = replaceOnce (B.pack ")") (B.pack "")
+        appendInsn i = (`B.append` B.pack ("\n\t" ++ i))
+
 -- | @replaceOnce match replace bs@ replaces the first occurrence of the
 -- substring @match@ in @bs@ with @replace@.
 replaceOnce :: B.ByteString -> B.ByteString -> B.ByteString -> B.ByteString

Reply via email to