Ticket #5210: 0002-Port-Add-byte-array-copy-primops-to-the-new-code-gen.patch

File 0002-Port-Add-byte-array-copy-primops-to-the-new-code-gen.patch, 3.8 KB (added by tibbe, 2 years ago)
  • compiler/codeGen/StgCmmPrim.hs

    From 7fcfd41f28b26524a725dbe7a8be0a072c0291d8 Mon Sep 17 00:00:00 2001
    From: Johan Tibell <johan.tibell@gmail.com>
    Date: Thu, 16 Jun 2011 07:50:10 +0200
    Subject: [PATCH 2/2] Port "Add byte array copy primops" to the new code gen
    
    ---
     compiler/codeGen/StgCmmPrim.hs |   57 ++++++++++++++++++++++++++++++++++++++++
     1 files changed, 57 insertions(+), 0 deletions(-)
    
    diff --git a/compiler/codeGen/StgCmmPrim.hs b/compiler/codeGen/StgCmmPrim.hs
    index 2cf7227..66730af 100644
    a b  
    425425emitPrimOp res WriteByteArrayOp_Word32    args = doWriteByteArrayOp (Just mo_WordTo32) res args 
    426426emitPrimOp res WriteByteArrayOp_Word64    args = doWriteByteArrayOp Nothing res args 
    427427 
     428-- Copying byte arrays 
     429emitPrimOp [] CopyByteArrayOp [src,src_off,dst,dst_off,n] = 
     430    doCopyByteArrayOp src src_off dst dst_off n 
     431emitPrimOp [] CopyMutableByteArrayOp [src,src_off,dst,dst_off,n] = 
     432    doCopyMutableByteArrayOp src src_off dst dst_off n 
    428433 
    429434-- The rest just translate straightforwardly 
    430435emitPrimOp [res] op [arg] 
     
    704709setInfo closure_ptr info_ptr = mkStore closure_ptr info_ptr 
    705710 
    706711-- ---------------------------------------------------------------------------- 
     712-- Copying byte arrays 
     713 
     714-- | Takes a source 'ByteArray#', an offset in the source array, a 
     715-- destination 'MutableByteArray#', an offset into the destination 
     716-- array, and the number of bytes to copy.  Copies the given number of 
     717-- bytes from the source array to the destination array. 
     718doCopyByteArrayOp :: CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr 
     719                  -> FCode () 
     720doCopyByteArrayOp = emitCopyByteArray copy 
     721  where 
     722    -- Copy data (we assume the arrays aren't overlapping since 
     723    -- they're of different types) 
     724    copy _src _dst dst_p src_p bytes = 
     725        emitMemcpyCall dst_p src_p bytes (CmmLit (mkIntCLit 1)) 
     726 
     727-- | Takes a source 'MutableByteArray#', an offset in the source 
     728-- array, a destination 'MutableByteArray#', an offset into the 
     729-- destination array, and the number of bytes to copy.  Copies the 
     730-- given number of bytes from the source array to the destination 
     731-- array. 
     732doCopyMutableByteArrayOp :: CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr 
     733                         -> FCode () 
     734doCopyMutableByteArrayOp = emitCopyByteArray copy 
     735  where 
     736    -- The only time the memory might overlap is when the two arrays 
     737    -- we were provided are the same array! 
     738    -- TODO: Optimize branch for common case of no aliasing. 
     739    copy src dst dst_p src_p bytes = do 
     740        [moveCall, cpyCall] <- forkAlts [ 
     741            getCode $ emitMemmoveCall dst_p src_p bytes (CmmLit (mkIntCLit 1)), 
     742            getCode $ emitMemcpyCall  dst_p src_p bytes (CmmLit (mkIntCLit 1)) 
     743            ] 
     744        emit $ mkCmmIfThenElse (cmmEqWord src dst) moveCall cpyCall 
     745 
     746emitCopyByteArray :: (CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr 
     747                      -> FCode ()) 
     748                  -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr 
     749                  -> FCode () 
     750emitCopyByteArray copy src0 src_off0 dst0 dst_off0 n0 = do 
     751    -- Passed as arguments (be careful) 
     752    src     <- assignTempE src0 
     753    src_off <- assignTempE src_off0 
     754    dst     <- assignTempE dst0 
     755    dst_off <- assignTempE dst_off0 
     756    n       <- assignTempE n0 
     757 
     758    dst_p <- assignTempE $ cmmOffsetExpr (cmmOffsetB dst arrWordsHdrSize) dst_off 
     759    src_p <- assignTempE $ cmmOffsetExpr (cmmOffsetB src arrWordsHdrSize) src_off 
     760 
     761    copy src dst dst_p src_p n 
     762 
     763-- ---------------------------------------------------------------------------- 
    707764-- Copying pointer arrays 
    708765 
    709766-- EZY: This code has an unusually high amount of assignTemp calls, seen