From f192e623f579e09b7b5442cc707a40482b76e81e Mon Sep 17 00:00:00 2001 From: Sylvain Henry Date: Fri, 14 May 2021 17:31:38 +0200 Subject: Cmm: fix sinking after suspendThread Suppose a safe call: myCall(x,y,z) It is lowered into three unsafe calls in Cmm: r = suspendThread(...); myCall(x,y,z); resumeThread(r); Consider the following situation for myCall arguments: x = Sp[..] -- stack y = Hp[..] -- heap z = R1 -- global register r = suspendThread(...); myCall(x,y,z); resumeThread(r); The sink pass assumes that unsafe calls clobber memory (heap and stack), hence x and y assignments are not sunk after `suspendThread`. The sink pass also correctly handles global register clobbering for all unsafe calls, except `suspendThread`! `suspendThread` is special because it releases the capability the thread is running on. Hence the sink pass must also take into account global registers that are mapped into memory (in the capability). In the example above, we could get: r = suspendThread(...); z = R1 myCall(x,y,z); resumeThread(r); But this transformation isn't valid if R1 is (BaseReg->rR1) as BaseReg is invalid between suspendThread and resumeThread. This caused argument corruption at least with the C backend ("unregisterised") in #19237. Fix #19237 --- compiler/GHC/CmmToC.hs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'compiler/GHC/CmmToC.hs') diff --git a/compiler/GHC/CmmToC.hs b/compiler/GHC/CmmToC.hs index d5457d4fae..ae6f4b91b6 100644 --- a/compiler/GHC/CmmToC.hs +++ b/compiler/GHC/CmmToC.hs @@ -268,12 +268,18 @@ pprStmt platform stmt = hresults = zip results res_hints hargs = zip args arg_hints + need_cdecl + | Just _align <- machOpMemcpyishAlign op = True + | MO_ResumeThread <- op = True + | MO_SuspendThread <- op = True + | otherwise = False + fn_call -- The mem primops carry an extra alignment arg. -- We could maybe emit an alignment directive using this info. -- We also need to cast mem primops to prevent conflicts with GCC -- builtins (see bug #5967). - | Just _align <- machOpMemcpyishAlign op + | need_cdecl = (text ";EFF_(" <> fn <> char ')' <> semi) $$ pprForeignCall platform fn cconv hresults hargs | otherwise @@ -825,6 +831,9 @@ pprCallishMachOp_for_C mop MO_Memmove _ -> text "memmove" MO_Memcmp _ -> text "memcmp" + MO_SuspendThread -> text "suspendThread" + MO_ResumeThread -> text "resumeThread" + MO_BSwap w -> ftext (bSwapLabel w) MO_BRev w -> ftext (bRevLabel w) MO_PopCnt w -> ftext (popCntLabel w) -- cgit v1.2.1