Giter Club home page Giter Club logo

mlir-hs's Introduction

mlir-hs - Haskell bindings for MLIR

🚨 This is an early-stage project. All details are subject to arbitrary changes. 🚨

Note that the main branch tracks the current HEAD of LLVM and so it is likely to be incompatible with any past releases. We are planning to provide release-specifi branches in the future, but only once the API stabilizes. For now your best bet is to develop against MLIR built from source. See the Building MLIR from source section for guidance.

Building

The only prerequisite for building mlir-hs is that you have MLIR installed somewhere, and the llvm-config binary from that installation is available in your PATH (a good way to verify this is to run which llvm-config).

If that's looking reasonable, we recommend using Stack for development. To build the project simply run stack build, while the test suite can be executed using stack test.

Building MLIR from source

The instructions below assume that you have cmake and ninja installed. You should be able to get them from your favorite package manager.

  1. Clone the latest LLVM code (or use git pull if you cloned it before) into the root of this repository

    git clone https://github.com/llvm/llvm-project
  2. Create a temporary build directory

    mkdir llvm-project/build
  3. Configure the build using CMake. Remember to replace $PREFIX with the directory where you want MLIR to be installed. See LLVM documentation for extended explanation and other potentially interesting build flags.

    cmake -B llvm-project/build           \
      -G Ninja                            \ # Use the Ninja build system
      -DLLVM_ENABLE_PROJECTS=mlir         \ # Enable build MLIR
      -DCMAKE_INSTALL_PREFIX=$PREFIX      \ # Install prefix
      -DMLIR_BUILD_MLIR_C_DYLIB=ON        \ # Build shared libraries
      -DLLVM_BUILD_LLVM_DYLIB=ON          \
      llvm-project/llvm

    For development purposes we additionally recommend using -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLLVM_ENABLE_ASSERTIONS=ON to retain debug information and enable internal LLVM assertions. If one changes the install directory (CMAKE_INSTALL_PREFIX) then one needs to add this directory to PATH and LD_LIBRARY_PATH for the subsequent builds (e.g., stack) to find it.

  4. [Build and install MLIR]. Note that it uses the installation prefix specified in the previous step.

    cmake --build llvm-project/build -t install

Contributing

Contributions of all kinds are welcome! If you're planning to implement a larger feature, consider posting an issue so that we can discuss it before you put in the work.

License

See the LICENSE file.

mlir-hs is an early-stage project, not an official Google product.

mlir-hs's People

Contributors

apaszke avatar fuzzypixelz avatar jpienaar avatar mihaimaruseac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

mlir-hs's Issues

[Usage/Tutorial] Implement the Toy tutorial in Haskell

This seems like a fun thing to do -- especially given how convenient Haskell makes constructing front ends.

If you don't mind, I wouldn't mind working at this for a bit. I'll likely do it outside the repo, but then if it's useful -- it might be included as a small tutorial somewhere.

mlir-hs-tblgen broken

Expected Behavior

stack build terminate successfully.

Actual Behavior

The binary mlir-hs-tblgen failed to compiled. Commenting out the line #include "llvm/ADT/Optional.h" in hs-generators.cc allows the compilation to be successful, but now mlir-hs-tblgen failed with CommandLine Error: Option 'enable-fs-discriminator' registered more than once!

Steps to Reproduce the Problem

  1. Remove the line #include "llvm/ADT/Optional.h" from tblgen/hs-generators.cc
  2. Execute stack build

Specifications

  • Version: LLVM: 17.0.0git
  • Platform: archlinux

Invalid IR or function without blockargs

Self-contained'ish example showing issue with function without blockargs generating invalid IR:

{-# LANGUAGE BlockArguments #-}                                           
                                                                          
import qualified Data.Map.Strict as M                                   
import Control.Monad.Reader
import Control.Monad.Identity
import Data.Char (ord)
 
import Test.Hspec
 
import qualified MLIR.Native as MLIR
import MLIR.AST
import MLIR.AST.Builder
import MLIR.AST.Serialize
import qualified Data.ByteString as BS
import qualified MLIR.AST.Dialect.Arith as Arith
import qualified MLIR.AST.Dialect.Func as Func
 
pack :: String -> BS.ByteString
pack = BS.pack . fmap (fromIntegral . ord)
 
generate :: Operation
generate = runIdentity $ evalNameSupplyT $ buildModule $ do
  let w32 = IntegerType Signless 32
  val <- Arith.constant w32 (IntegerAttr w32 42)
  buildFunction (pack "function") [w32] NoAttrs $ do
    buildBlock $ do
      -- Correct value returned if blockArgument is used, else not
      -- a <- blockArgument w32
      c <- Arith.constant w32 (IntegerAttr w32 43)
      Func.return [c]
    endOfRegion
  val <- Arith.constant w32 (IntegerAttr w32 44)
  return ()
 
 
verifyAndDump :: Operation -> Expectation
verifyAndDump op = MLIR.withContext \ctx -> do
                     MLIR.registerAllDialects ctx
                     nativeOp <- fromAST ctx (mempty, mempty) op
                     MLIR.dump nativeOp
                     MLIR.verifyOperation nativeOp >>= (`shouldBe` True)
 
main = verifyAndDump generate

`stack build` issue -- possible local path specificity in `Setup.hs`

Expected Behavior

Having built LLVM + MLIR from source and added ..build/bin to my path, with access to llvm-config -- I should be able to run stack build and successfully build the project.

Actual Behavior

❯ stack build                                     Dev/mlir-hs main
mlir-hs> configure (lib)
tblgen/hs-generators.cc:36:10: fatal error: mlir/TableGen/Argument.h: No such file or directory
   36 | #include "mlir/TableGen/Argument.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

--  While building package mlir-hs-0.1.0.0 (scroll up to its section to see the error) using:
      /home/mccoy/Dev/mlir-hs/.stack-work/dist/x86_64-linux-tinfo6/Cabal-3.2.1.0/setup/setup --builddir=.stack-work/dist/x86_64-linux-tinfo6/Cabal-3.2.1.0 configure --user --package-db=clear --package-db=global --package-db=/home/mccoy/.stack/snapshots/x86_64-linux-tinfo6/425db5574a825305e370501fc2b13af8630b37876fd22c900ad31438145b6b0c/8.10.4/pkgdb --package-db=/home/mccoy/Dev/mlir-hs/.stack-work/install/x86_64-linux-tinfo6/425db5574a825305e370501fc2b13af8630b37876fd22c900ad31438145b6b0c/8.10.4/pkgdb --libdir=/home/mccoy/Dev/mlir-hs/.stack-work/install/x86_64-linux-tinfo6/425db5574a825305e370501fc2b13af8630b37876fd22c900ad31438145b6b0c/8.10.4/lib --bindir=/home/mccoy/Dev/mlir-hs/.stack-work/install/x86_64-linux-tinfo6/425db5574a825305e370501fc2b13af8630b37876fd22c900ad31438145b6b0c/8.10.4/bin --datadir=/home/mccoy/Dev/mlir-hs/.stack-work/install/x86_64-linux-tinfo6/425db5574a825305e370501fc2b13af8630b37876fd22c900ad31438145b6b0c/8.10.4/share --libexecdir=/home/mccoy/Dev/mlir-hs/.stack-work/install/x86_64-linux-tinfo6/425db5574a825305e370501fc2b13af8630b37876fd22c900ad31438145b6b0c/8.10.4/libexec --sysconfdir=/home/mccoy/Dev/mlir-hs/.stack-work/install/x86_64-linux-tinfo6/425db5574a825305e370501fc2b13af8630b37876fd22c900ad31438145b6b0c/8.10.4/etc --docdir=/home/mccoy/Dev/mlir-hs/.stack-work/install/x86_64-linux-tinfo6/425db5574a825305e370501fc2b13af8630b37876fd22c900ad31438145b6b0c/8.10.4/doc/mlir-hs-0.1.0.0 --htmldir=/home/mccoy/Dev/mlir-hs/.stack-work/install/x86_64-linux-tinfo6/425db5574a825305e370501fc2b13af8630b37876fd22c900ad31438145b6b0c/8.10.4/doc/mlir-hs-0.1.0.0 --haddockdir=/home/mccoy/Dev/mlir-hs/.stack-work/install/x86_64-linux-tinfo6/425db5574a825305e370501fc2b13af8630b37876fd22c900ad31438145b6b0c/8.10.4/doc/mlir-hs-0.1.0.0 --dependency=Cabal=Cabal-3.2.1.0 --dependency=array=array-0.5.4.0 --dependency=base=base-4.14.1.0 --dependency=bytestring=bytestring-0.10.12.0 --dependency=containers=containers-0.6.2.1 --dependency=directory=directory-1.3.6.0 --dependency=filepath=filepath-1.4.2.1 --dependency=inline-c=inline-c-0.9.1.4-2xfcSMu20nLGOzvS94xHVu --dependency=lens=lens-4.19.2-7Q00MKR54DVFFru2tZkQiv --dependency=mtl=mtl-2.2.2 --dependency=raw-strings-qq=raw-strings-qq-1.1-CnHDj5KA9CM15ZkVQP2bmF --dependency=transformers=transformers-0.5.6.2 --exact-configuration --ghc-option=-fhide-source-paths
    Process exited with code: ExitFailure 1

Steps to Reproduce the Problem

  1. Build LLVM + MLIR on HEAD -- using
git clone https://github.com/llvm/llvm-project
cd llvm-project
mkdir build
cmake -G Ninja ../llvm \
   -DLLVM_ENABLE_PROJECTS=mlir \
   -DLLVM_BUILD_EXAMPLES=ON \
   -DLLVM_TARGETS_TO_BUILD="X86;NVPTX;AMDGPU" \
   -DCMAKE_BUILD_TYPE=Release \
   -DLLVM_ENABLE_ASSERTIONS=ON \
   -DCMAKE_C_COMPILER=clang \
   -DCMAKE_CXX_COMPILER=clang++ \
   -DLLVM_ENABLE_LLD=ON
cmake --build .
export PATH="$(pwd)/bin:$PATH"
  1. stack build

Specifications

  • Version: HEAD
  • Platform:
mccoy in pop-os in toy-hs on ξ‚  main via β–³ v3.18.4
❯ neofetch                                         Dev/toy-hs main
             /////////////                mccoy@pop-os
         /////////////////////            ------------
      ///////*767////////////////         OS: Pop!_OS 21.04 x86_64
    //////7676767676*//////////////       Kernel: 5.11.0-7620-gene
   /////76767//7676767//////////////      Uptime: 28 mins
  /////767676///*76767///////////////     Packages: 2349 (dpkg)
 ///////767676///76767.///7676*///////    Shell: zsh 5.8
/////////767676//76767///767676////////   Resolution: 1920x1080
//////////76767676767////76767/////////   Terminal: /dev/pts/4
///////////76767676//////7676//////////   CPU: AMD Ryzen 9 3900XT
////////////,7676,///////767///////////   GPU: NVIDIA GeForce RTX
/////////////*7676///////76////////////   Memory: 1465MiB / 32077M
///////////////7676////////////////////
 ///////////////7676///767////////////
  //////////////////////'////////////
   //////.7676767676767676767,//////
    /////767676767676767676767/////
      ///////////////////////////
         /////////////////////
             /////////////

Missing getelementptr operation in the LLVM dialect

Expected Behavior

The operation in question would be generated (alongside a pattern).

Actual Behavior

Only the documentation is present.

Steps to Reproduce the Problem

  1. Check https://google.github.io/mlir-hs/mlir-hs-0.1.0.0/MLIR-AST-Dialect-LLVM.html#g:58

Comments

For a couple missing operations in mlir-hs, the solution was adding missing Attributes in the Haskell side, but in this case I'm not very sure why hs-generators.cc refuses to generate it.

Building On Windows?

Hi, Ive been trying to build the project on Windows and made a lot of headway learning what would be necessary to build mlir-hs with minimal pain; I was curious if anyone had any insights or suggestions to limit any issues (like the fact building LLVM dylibs are currently unsupported on Windows.)

Autogenerated tests do not handle ElementsAttr

Results in error like

test/MLIR/AST/Dialect/Generated/LLVMSpec.hs:779:62: error:                                          
    β€’ Couldn't match type β€˜a’
                     with β€˜MLIR.AST.IStorableArray.IStorableArray a2 GHC.Int.Int32’
        because type variable β€˜a2’ would escape its scope
      This (rigid, skolem) type variable is bound by
        a pattern with pattern synonym:
          InternalGEPOpAttributes :: () =>
                                     forall a0.
                                     (GHC.Ix.Ix a0, Prelude.Show a0) =>
                                     MLIR.AST.IStorableArray.IStorableArray a0 GHC.Int.Int32
                                     -> MLIR.AST.NamedAttributes,
        in a case alternative
        at test/MLIR/AST/Dialect/Generated/LLVMSpec.hs:779:15-57
      Expected type: Maybe a
        Actual type: Maybe
                       (MLIR.AST.IStorableArray.IStorableArray a2 GHC.Int.Int32)
    β€’ In the expression: Just (structIndices_match)
      In a case alternative:
          InternalGEPOpAttributes structIndices_match
            -> Just (structIndices_match)
      In the expression:
        case (InternalGEPOpAttributes structIndices) <> Prelude.mempty of
          InternalGEPOpAttributes structIndices_match
            -> Just (structIndices_match)
          _ -> Nothing

Builder names are not sanitized.

Expected Behavior

If an operation name is a Haskell keyword, the hs-generator program should sanitize it.

Actual Behavior

Keywords in builder names are not sanitized.

Steps to Reproduce the Problem

  1. Enable the SCF dialect in the Setup.hs script, the generated module will fail to build because of the scf.if operation.

Specifications

This can be fixed by simply calling santizeName (cf. the corresponding PR)

Platform: Linux x86_64

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.