[PATCH] llvmGen: Pass mcmodel medium option to LLVM backend on LoongArch
authorXin Wang <wangxin03@loongson.cn>
Fri, 13 Dec 2024 02:45:20 +0000 (10:45 +0800)
committerGianfranco Costamagna <locutusofborg@debian.org>
Mon, 17 Feb 2025 08:44:23 +0000 (09:44 +0100)
Gbp-Pq: Name 0004-llvmGen-Pass-mcmodel-medium-option-to-LLVM-backend-on-LoongArch.patch

compiler/GHC/CmmToLlvm.hs
compiler/GHC/Llvm/MetaData.hs
configure.ac

index ea5884405e8e3e52f920a5814fb3d2a91cb1b613..04a542c552431e2d42483d1ccec4aa68bf375bb5 100644 (file)
@@ -190,7 +190,7 @@ cmmLlvmGen _ = return ()
 
 cmmMetaLlvmPrelude :: LlvmM ()
 cmmMetaLlvmPrelude = do
-  metas <- flip mapM stgTBAA $ \(uniq, name, parent) -> do
+  tbaa_metas <- flip mapM stgTBAA $ \(uniq, name, parent) -> do
     -- Generate / lookup meta data IDs
     tbaaId <- getMetaUniqueId
     setUniqMeta uniq tbaaId
@@ -203,9 +203,43 @@ cmmMetaLlvmPrelude = do
               -- just a name on its own. Previously `null` was accepted as the
               -- name.
               Nothing -> [ MetaStr name ]
+
+  platform <- getPlatform
+  cfg <- getConfig
+  let code_model_metas =
+          case platformArch platform of
+            -- FIXME: We should not rely on LLVM
+            ArchLoongArch64 -> [mkCodeModelMeta CMMedium]
+            _                                 -> []
+  module_flags_metas <- mkModuleFlagsMeta code_model_metas
+  let metas = tbaa_metas ++ module_flags_metas
   cfg <- getConfig
   renderLlvm $ ppLlvmMetas cfg metas
 
+mkNamedMeta :: LMString -> [MetaExpr] -> LlvmM [MetaDecl]
+mkNamedMeta name exprs = do
+    (ids, decls) <- unzip <$> mapM f exprs
+    return $ decls ++ [MetaNamed name ids]
+  where
+    f expr = do
+      i <- getMetaUniqueId
+      return (i, MetaUnnamed i expr)
+
+mkModuleFlagsMeta :: [ModuleFlag] -> LlvmM [MetaDecl]
+mkModuleFlagsMeta =
+    mkNamedMeta "llvm.module.flags" . map moduleFlagToMetaExpr
+
+-- LLVM's @LLVM::CodeModel::Model@ enumeration
+data CodeModel = CMMedium
+
+-- Pass -mcmodel=medium option to LLVM on LoongArch64
+mkCodeModelMeta :: CodeModel -> ModuleFlag
+mkCodeModelMeta codemodel =
+    ModuleFlag MFBError "Code Model" (MetaLit $ LMIntLit n i32)
+  where
+    n = case codemodel of CMMedium -> 3 -- as of LLVM 8
+
+
 -- -----------------------------------------------------------------------------
 -- | Marks variables as used where necessary
 --
index 2b28be4829f996f3c90f2ed6a6b8fb8dde576f41..5d0a996c5a36091f451b25159636a86aef2bcc6e 100644 (file)
@@ -2,7 +2,6 @@
 
 module GHC.Llvm.MetaData
   ( MetaId(..)
-  , ppMetaId
   , MetaExpr(..)
   , MetaAnnot(..)
   , MetaDecl(..)
index 02a1eeeff15b3fd974a59eb7ce9392397f3f9122..0665af54254a1dbfb14226710ec89e79810c5bd4 100644 (file)
@@ -555,7 +555,7 @@ AC_SUBST(InstallNameToolCmd)
 # tools we are looking for. In the past, GHC supported a number of
 # versions of LLVM simultaneously, but that stopped working around
 # 3.5/3.6 release of LLVM.
-LlvmMinVersion=11  # inclusive
+LlvmMinVersion=13  # inclusive
 LlvmMaxVersion=19 # not inclusive
 AC_SUBST([LlvmMinVersion])
 AC_SUBST([LlvmMaxVersion])