Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid MemoryManager allocations for constant strings #648

Closed
rahulmutt opened this issue Feb 13, 2018 · 4 comments
Closed

Avoid MemoryManager allocations for constant strings #648

rahulmutt opened this issue Feb 13, 2018 · 4 comments

Comments

@rahulmutt
Copy link
Member

The code:

stringConstant = "hello"

generates

stringConstant = unpackCString# "hello"#

We should pattern match on the express above at the STG (code generator) level and perform a direct conversion from java.lang.String to Eta's list type [].

[1] = 1 : [] = (:) 1 [] is the same as new ZC(new Izh(1), DZMZN()). where ZC is defined in ghc_prim.ghc.types.datacons.ZC and DZMZN() is defined in ghc_prim.ghc.Types. Thus, one can define a method on the Java side that performs the conversion. The code generator should call this method instead.

The main thing to look for is unpackCString# with a literal string argument. If it has a variable argument, we cannot make this optimization. This most likely requires a special case in cgIdApp in the codegen.

@pranjaltale16
Copy link

Hello, I would like to work on this issue. Could you please help me in getting started with this?

@rahulmutt
Copy link
Member Author

First step is to get a feel for how the code generator works. All code generator related code is in compiler/ETA/CodeGen/*. You can trace the codegen by making a test program and adding the following field to your .cabal file for any Eta test project you setup:

    eta-options: -ddump-cg-trace -ddump-stg -ddump-to-file

You can find the generated *.dump-stg and *.dump-cg-trace files in dist/build/**/*.

These debugging options will be useful when debugging code generator changes as well.

When making changes to the code generator, make sure you run:

./cleaninstall.sh # to build your changes
cd tests/verify
./verify.sh # to verify that you changes generate valid bytecode

Feel free to ask more questions if you get stuck!

@betterclever
Copy link
Contributor

@rahulmutt I am trying to get my head around solving this issue.

I have currently generated the dump code and realized the task.

[1] = 1 : [] = (:) 1 [] is the same as new ZC(new Izh(1), DZMZN()). where ZC is defined in ghc_prim.ghc.types.datacons.ZC and DZMZN() is defined in ghc_prim.ghc.Types. Thus, one can define a method on the Java side that performs the conversion. The code generator should call this method instead.

I am unable to get where to find the above packages in the codebase though. Can you give some suggestions?

@rahulmutt
Copy link
Member Author

@betterclever Look inside libraries/ghc-prim/dist/build/HSghc-prim-*.jar and you should find a class ghc_prim/ghc/Types.class. This is the output of compiling GHC.Types module in ghc-prim where . It is helpful to have a java decompiler at hand to quickly look through to find what you need. the ZC stuff is the z-encoded form of : and ZMZN is []. You can see a reference on z-encoding here, although the Eta version has slight differences and the source code can be consulted.

So the first step toward achieving this might be to write the method inside of java-utils/Utils.java file in the base package and write the conversion function there. Then in the codegen, call the method you've defined in base to perform the conversion from java.lang.String to ghc_prim.ghc.types.tycons.ZMZN.

The key thing to note here is that when you compile java files with the Eta compiler (by specifying a java-sources: field) all the dependencies you've specified in build-depends: will be available for import in the file you mention. So in java-utils/Utils.java you are free to import any generated class from ghc-prim.

Note that in this case [] refers to the type constructor which has two data constructors : and [].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants