From 3b0d80ff6f80ca3e473b0c9a3db0c6bb7fa35bab Mon Sep 17 00:00:00 2001 From: Xin Wang Date: Fri, 13 Dec 2024 10:45:20 +0800 Subject: [PATCH] [PATCH] llvmGen: Pass mcmodel medium option to LLVM backend on LoongArch Gbp-Pq: Name 0004-llvmGen-Pass-mcmodel-medium-option-to-LLVM-backend-on-LoongArch.patch --- compiler/GHC/CmmToLlvm.hs | 36 ++++++++++++++++++++++++++++++++++- compiler/GHC/Llvm/MetaData.hs | 1 - configure.ac | 2 +- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/compiler/GHC/CmmToLlvm.hs b/compiler/GHC/CmmToLlvm.hs index ea588440..04a542c5 100644 --- a/compiler/GHC/CmmToLlvm.hs +++ b/compiler/GHC/CmmToLlvm.hs @@ -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 -- diff --git a/compiler/GHC/Llvm/MetaData.hs b/compiler/GHC/Llvm/MetaData.hs index 2b28be48..5d0a996c 100644 --- a/compiler/GHC/Llvm/MetaData.hs +++ b/compiler/GHC/Llvm/MetaData.hs @@ -2,7 +2,6 @@ module GHC.Llvm.MetaData ( MetaId(..) - , ppMetaId , MetaExpr(..) , MetaAnnot(..) , MetaDecl(..) diff --git a/configure.ac b/configure.ac index 02a1eeef..0665af54 100644 --- a/configure.ac +++ b/configure.ac @@ -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]) -- 2.30.2