Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 5,246
    • Issues 5,246
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 577
    • Merge requests 577
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell CompilerGlasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #20174
Closed
Open
Issue created Jul 28, 2021 by Sylvain Henry@hsyl20Developer

Constant folding unpackAppendCString

Consider:

{-# LANGUAGE MagicHash #-}
{-# OPTIONS_GHC -ddump-simpl -dsuppress-all -dno-typeable-binds -O2 #-}

module Test (foobar, foobar2, foobar3) where

import GHC.Exts

bar :: String
bar = unpackCString# "bar"#

foobar :: String
foobar = unpackAppendCString# "foo"# bar

foobar2 :: String
foobar2 = unpackAppendCString# "foo"# (unpackCString# "bar"#)

foobar3 :: String
foobar3 = unpackAppendCString# "foo"# (unpackAppendCString# "bar"# [])

We would expect appending of string literals to happen at compile time but currently we get:

==================== Tidy Core ====================
Result size of Tidy Core
  = {terms: 21, types: 10, coercions: 0, joins: 0/0}

-- RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}
foobar6 = "foo"#

-- RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}
foobar5 = "bar"#

-- RHS size: {terms: 2, types: 0, coercions: 0, joins: 0/0}
foobar1 = unpackCString# foobar5

-- RHS size: {terms: 3, types: 0, coercions: 0, joins: 0/0}
foobar = unpackAppendCString# foobar6 foobar1

-- RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}
foobar2 = foobar

-- RHS size: {terms: 3, types: 1, coercions: 0, joins: 0/0}
foobar7 = unpackAppendCString# foobar5 []

-- RHS size: {terms: 3, types: 0, coercions: 0, joins: 0/0}
foobar3 = unpackAppendCString# foobar6 foobar7

The example is contrived but the real case happened in the GHC's LLVM codegen where we fail to build shared FastStrings because of this (e.g. fsLit $ "xyz" ++ suf).

Patch coming.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking