google/mlir-hs

Invalid IR or function without blockargs

jpienaar opened this issue · 0 comments

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