[Git][ghc/ghc][master] compiler: properly handle non-word-sized CmmSwitch scrutinees in the wasm NCG
Marge Bot (@marge-bot)
gitlab at gitlab.haskell.org
Wed Feb 1 01:47:27 UTC 2023
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
f0eefa3c by Cheng Shao at 2023-01-31T20:47:03-05:00
compiler: properly handle non-word-sized CmmSwitch scrutinees in the wasm NCG
Currently, the wasm NCG has an implicit assumption: all CmmSwitch
scrutinees are 32-bit integers. This is not always true; #22864 is one
counter-example with a 64-bit scrutinee. This patch fixes the logic by
explicitly converting the scrutinee to a word that can be used as a
br_table operand. Fixes #22871. Also includes a regression test.
- - - - -
5 changed files:
- compiler/GHC/Wasm/ControlFlow/FromCmm.hs
- + testsuite/tests/cmm/should_run/T22871.hs
- + testsuite/tests/cmm/should_run/T22871.stdout
- + testsuite/tests/cmm/should_run/T22871_cmm.cmm
- testsuite/tests/cmm/should_run/all.T
Changes:
=====================================
compiler/GHC/Wasm/ControlFlow/FromCmm.hs
=====================================
@@ -75,7 +75,7 @@ flowLeaving platform b =
let (offset, target_labels) = switchTargetsToTable targets
(lo, hi) = switchTargetsRange targets
default_label = switchTargetsDefault targets
- scrutinee = smartPlus platform e offset
+ scrutinee = smartExtend platform $ smartPlus platform e offset
range = inclusiveInterval (lo+toInteger offset) (hi+toInteger offset)
in Switch scrutinee range target_labels default_label
CmmCall { cml_cont = Nothing, cml_target = e } -> TailCall e
@@ -314,6 +314,14 @@ structuredControl platform txExpr txBlock g =
nodeBody :: CmmBlock -> CmmActions
nodeBody (BlockCC _first middle _last) = middle
+-- | A CmmSwitch scrutinee may have any width, but a br_table operand
+-- must be exactly word sized, hence the extension here. (#22871)
+smartExtend :: Platform -> CmmExpr -> CmmExpr
+smartExtend p e | w0 == w1 = e
+ | otherwise = CmmMachOp (MO_UU_Conv w0 w1) [e]
+ where
+ w0 = cmmExprWidth p e
+ w1 = wordWidth p
smartPlus :: Platform -> CmmExpr -> Int -> CmmExpr
smartPlus _ e 0 = e
=====================================
testsuite/tests/cmm/should_run/T22871.hs
=====================================
@@ -0,0 +1,12 @@
+{-# LANGUAGE GHCForeignImportPrim #-}
+{-# LANGUAGE MagicHash #-}
+{-# LANGUAGE UnliftedFFITypes #-}
+
+import Data.Foldable
+import GHC.Exts
+import GHC.Int
+
+foreign import prim "foo" foo :: Int64# -> Int64#
+
+main :: IO ()
+main = for_ [0, 42, 114514] $ \(I64# x#) -> print $ I64# (foo x#)
=====================================
testsuite/tests/cmm/should_run/T22871.stdout
=====================================
@@ -0,0 +1,3 @@
+233
+84
+1919810
=====================================
testsuite/tests/cmm/should_run/T22871_cmm.cmm
=====================================
@@ -0,0 +1,16 @@
+#include "Cmm.h"
+
+foo (I64 x) {
+ switch [0 .. 114514] (x) {
+ case 0: { return (233 :: I64); }
+ case 1: { return (333 :: I64); }
+ case 2: { return (666 :: I64); }
+ case 3: { return (1989 :: I64); }
+ case 4: { return (1997 :: I64); }
+ case 5: { return (2012 :: I64); }
+ case 6: { return (2019 :: I64); }
+ case 7: { return (2022 :: I64); }
+ case 114514: { return (1919810 :: I64); }
+ default: { return (x * (2 :: I64)); }
+ }
+}
=====================================
testsuite/tests/cmm/should_run/all.T
=====================================
@@ -25,3 +25,12 @@ test('ByteSwitch',
],
multi_compile_and_run,
['ByteSwitch', [('ByteSwitch_cmm.cmm', '')], ''])
+
+test('T22871',
+ [ extra_run_opts('"' + config.libdir + '"')
+ , omit_ways(['ghci'])
+ , js_skip
+ , when(arch('i386'), skip) # x86 NCG panics with "iselExpr64(i386)"
+ ],
+ multi_compile_and_run,
+ ['T22871', [('T22871_cmm.cmm', '')], ''])
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f0eefa3cf058879246991747dcd18c811402f9e5
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f0eefa3cf058879246991747dcd18c811402f9e5
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/20230131/6d46efa4/attachment-0001.html>
More information about the ghc-commits
mailing list