Sunday, September 13, 2020

MLIR Toy Tutorial - How to simplify the AST so it is easier to read.

 https://mlir.llvm.org/docs/Tutorials/Toy/Ch-1/

# User defined generic function that operates on unknown shaped arguments.
def multiply_transpose(a, b) {
  return transpose(a) * transpose(b);
}

def main() {
  # Define a variable `a` with shape <2, 3>, initialized with the literal value.
  var a = [[1, 2, 3], [4, 5, 6]];
  var b<2, 3> = [1, 2, 3, 4, 5, 6];

  # This call will specialize `multiply_transpose` with <2, 3> for both
  # arguments and deduce a return type of <3, 2> in initialization of `c`.
  var c = multiply_transpose(a, b);

  # A second call to `multiply_transpose` with <2, 3> for both arguments will
  # reuse the previously specialized and inferred version and return <3, 2>.
  var d = multiply_transpose(b, a);

  # A new call with <3, 2> (instead of <2, 3>) for both dimensions will
  # trigger another specialization of `multiply_transpose`.
  var e = multiply_transpose(c, d);

  # Finally, calling into `multiply_transpose` with incompatible shape will
  # trigger a shape inference error.
  var f = multiply_transpose(transpose(a), c);
}

 ~/llvm-project/build/bin/toyc-ch1 test/Examples/Toy/Ch1/ast.toy -emit=ast > ast.ir 2>&1 

# Strip the comments
sed 's/\@test.*//g' ast.ir

# Lets get rid of the fractional floating point to reduce the "niose"
 sed 's/\@test.*//g' ast.ir | sed 's/.000000e+00//g'

  Module:
    Function 
      Proto 'multiply_transpose' 
      Params: [a, b]
      Block {
        Return
          BinOp: * 
            Call 'transpose' [ 
              var: a 
            ]
            Call 'transpose' [ 
              var: b 
            ]
      } // Block
    Function 
      Proto 'main' 
      Params: []
      Block {
        VarDecl a<> 
          Literal: <2, 3>[ <3>[ 1, 2, 3], <3>[ 4, 5, 6]] 
        VarDecl b<2, 3> 
          Literal: <6>[ 1, 2, 3, 4, 5, 6] 
        VarDecl c<> 
          Call 'multiply_transpose' [ 
            var: a 
            var: b 
          ]
        VarDecl d<> 
          Call 'multiply_transpose' [ 
            var: b 
            var: a 
          ]
        VarDecl e<> 
          Call 'multiply_transpose' [ 
            var: b 
            var: c 
          ]
        VarDecl f<> 
          Call 'multiply_transpose' [ 
            Call 'transpose' [ 
              var: a 
            ]
            var: c 
          ]
      } // Block

-00 option turns paragraph slurp mode on, your regex now can work on multi line.

$ sed 's/\@test.*//g' ast.ir | sed 's/.000000e+00//g' | perl -00pe 's/\n[ \t]*var://gi' | perl -00pe 's/\n[ \t]*]/]/gi'
  Module:
    Function 
      Proto 'multiply_transpose' 
      Params: [a, b]
      Block {
        Return
          BinOp: * 
            Call 'transpose' [  a ]
            Call 'transpose' [  b ]
      } // Block
    Function 
      Proto 'main' 
      Params: []
      Block {
        VarDecl a<> 
          Literal: <2, 3>[ <3>[ 1, 2, 3], <3>[ 4, 5, 6]] 
        VarDecl b<2, 3> 
          Literal: <6>[ 1, 2, 3, 4, 5, 6] 
        VarDecl c<> 
          Call 'multiply_transpose' [  a  b ]
        VarDecl d<> 
          Call 'multiply_transpose' [  b  a ]
        VarDecl e<> 
          Call 'multiply_transpose' [  b  c ]
        VarDecl f<> 
          Call 'multiply_transpose' [ 
            Call 'transpose' [  a ] c ]
      } // Block





No comments:

Post a Comment