[Git][ghc/ghc][wip/angerman/32bit-cmm] CgUtils.fixStgRegStmt respect register width

Moritz Angermann (@angerman) gitlab at gitlab.haskell.org
Thu Oct 12 07:35:16 UTC 2023



Moritz Angermann pushed to branch wip/angerman/32bit-cmm at Glasgow Haskell Compiler / GHC


Commits:
b13c48d3 by Moritz Angermann at 2023-10-12T15:34:46+08:00
CgUtils.fixStgRegStmt respect register width

This change ensure that the reg + offset computation is always of the
same size.  Before this we could end up with a 64bit register, and then
add a 32bit offset (on 32bit platforms).  This not only would fail type
sanity checking, but also incorrectly truncate 64bit values into 32bit
values silently on 32bit architectures.

- - - - -


1 changed file:

- compiler/GHC/StgToCmm/CgUtils.hs


Changes:

=====================================
compiler/GHC/StgToCmm/CgUtils.hs
=====================================
@@ -177,16 +177,18 @@ fixStgRegStmt platform stmt = fixAssign $ mapExpDeep fixExpr stmt
                                      (globalRegSpillType platform reg)
                                      NaturallyAligned
 
-        CmmRegOff (CmmGlobal reg_use) offset ->
+        CmmRegOff greg@(CmmGlobal reg) offset ->
             -- RegOf leaves are just a shorthand form. If the reg maps
             -- to a real reg, we keep the shorthand, otherwise, we just
             -- expand it and defer to the above code.
-            let reg = globalRegUseGlobalReg reg_use in
-            case reg `elem` activeStgRegs platform of
+            -- NB: to ensure type correctness we need to ensure the Add
+            --     as well as the Int need to be of the same size as the
+            --     register.
+            case globalRegUseGlobalReg reg `elem` activeStgRegs platform of
                 True  -> expr
-                False -> CmmMachOp (MO_Add (wordWidth platform)) [
-                                    fixExpr (CmmReg (CmmGlobal reg_use)),
+                False -> CmmMachOp (MO_Add (cmmRegWidth greg)) [
+                                    fixExpr (CmmReg greg),
                                     CmmLit (CmmInt (fromIntegral offset)
-                                                   (wordWidth platform))]
+                                                   (cmmRegWidth greg))]
 
         other_expr -> other_expr



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/b13c48d3b6e44c24ea7a1ffdd636977c1ce47d88

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/b13c48d3b6e44c24ea7a1ffdd636977c1ce47d88
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20231012/c1f1fc4b/attachment-0001.html>


More information about the ghc-commits mailing list