Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Matlab & WebAssembly: A matrix-based library for the web.

David
October 15, 2018

Matlab & WebAssembly: A matrix-based library for the web.

Matlab is a matrix-based dynamic scientific language used by scientists and engineers worldwide. It provides easily expressible high-level matrix operations through a wide variety of builtin functions. As proprietary software, it is important to have alternative compilers and tools that will allow compilation and execution in other environments. JavaScript and its sophisticated virtual machines provides a way of easily distributing and executing code, albeit with some performance problems coming from the dynamic nature of the language. To bridge the gap in performance WebAssembly was introduced as a new binary encoding language for the Web. WebAssembly offers a typed, low-level ISA with an easily verifiable binary format and low-level operations that map easily and performantly to all major hardware architectures.

In this work, we will present a matrix library in WebAssembly, inspired by the Matlab builtins. First, we will explain our design and compiler strategies to represent Matlab constructs and builtins in WebAssembly. Secondly, we will show how our library can serve as a backend for two compilers: MatJuice, a static compiler from Matlab to JavaScript, and MatWably, a static compiler from Matlab directly to WebAssembly. In particular, we will discuss the key challenges in retargeting to a typed, low-level language such as WebAssembly. To evaluate the work so far we will present experimental results, comparing the original Matjuice (based entirely on JavaScript), with the new modified version using the WebAssembly built-ins, through a set of numerical benchmarks. Finally, we will discuss two aspects of continuing and future work. Firstly, we’d like to develop an approach to completely generalize the WebAssembly library to all number types supported by Matlab. Lastly, we would like to explore the relevant performance bottlenecks and possible optimizations that apply to this domain.

David

October 15, 2018
Tweet

More Decks by David

Other Decks in Research

Transcript

  1. WebAssembly and a matrix-based library for the web Sable Research

    Group McGill University David Herrera [email protected] October 15, 2018 David Herrera (McGill University) MatWably October 15, 2018 1 / 46
  2. Overview 1 Introduction Motivation Background Purpose 2 Memory Representation 3

    Built-in Library Operations Supported Templates Built-in Framework 4 Compiler Architecture 5 Julia 6 MatWably Background Making calls David Herrera (McGill University) MatWably October 15, 2018 2 / 46
  3. Motivation Bringing scientist the power of complex programming languages effortlessly

    and efficiently. Allowing scientist to deploy their Matlab code using web technologies Creating a performant matrix-based library for the web. David Herrera (McGill University) MatWably October 15, 2018 3 / 46
  4. Background: Matlab Highly dynamic language, functions take any number of

    arguments and types Many number types supported, default type is double All values are “treated as arrays“ and have the same array properties A plethora of Matlab builtins supporting many useful Matrix operations 1 f u n c t i o n [ b ] = matlab sample () 2 % Highly dynamic , m u l t i p l e ways to c a l l 3 % a given b u i l t i n 4 a = ones (2) ; % 2x2 a r r a y of ones 5 a = ones (2 ,2) ; 6 a = ones ( [ 2 ] ) ; 7 a = ones ( [ 2 , 2 ] ) ; 8 9 b = a % copy i s made on assignment 10 % 10 number types supported 11 b = ones (2 , ’ s i n g l e ’ ) ;%2x2 matrix of ones using f32 . 12 13 % Values are t r e a t e d as a r r a y s 14 c = 4 15 n s = s i z e ( c ) % Returns [ 1 , 1 ] 16 l s = numel ( c ) % Returns 1 17 18 19 % A v a r i e t y of u s e f u l o p e r a t i o n s 20 % to d e a l with m a t r i c e s 21 b ( : , 1 ) = 2; % F i r s t column i s s e t to 2 22 d = b+b ; % Matrix a d d i t i o n 23 e = b + 2 % Array b r o a d c a s t i n g of 2 24 e = b∗b % Matrix m u l t i p l i c a t i o n 25 end David Herrera (McGill University) MatWably October 15, 2018 4 / 46
  5. Background: WebAssembly New virtual ISA for the web Supported by

    all major browser vendors Purpose: to bring performance to the web Basic Types: i32, i64, f32, f64 Embeds into the JavaScript run-time Future features include SIMD and multi-threading To aid translation from higher-level constructs Linear memory of bytes Function pointers via tables David Herrera (McGill University) MatWably October 15, 2018 5 / 46
  6. Background: WebAssembly - How Fast? Figure: Geomeans of browser performance

    of WebAssembly against Native C in three workstations Experiment done using the Ostrich Numerical Benchmarks Suite Speedup over C varied from 0.58 to 0.88 Speedup over JavaScript varied from 1.35 to 2.80 David Herrera (McGill University) MatWably October 15, 2018 6 / 46
  7. Purpose Idea: Use WebAssembly to create an efficient matrix-based library

    for the web using the Matlab model Library will serve two purposes: An efficient matrix-based library with an expressive TypeScript/JavaScript interface. Back-end support for two static compilers - MatJuice - Matlab to JavaScript - MatWably - Matlab to WebAssembly David Herrera (McGill University) MatWably October 15, 2018 7 / 46
  8. Research Outcomes Insights about the interplay between JavaScript and WebAssembly

    for compiler writers. A reasonably performant matrix-based library for the web Efficient translation strategies from a high-level numerical language such as Matlab to the web languages David Herrera (McGill University) MatWably October 15, 2018 8 / 46
  9. Overview 1 Introduction Motivation Background Purpose 2 Memory Representation 3

    Built-in Library Operations Supported Templates Built-in Framework 4 Compiler Architecture 5 Julia 6 MatWably Background Making calls David Herrera (McGill University) MatWably October 15, 2018 9 / 46
  10. Matlab Representation in Memory All Matlab constructs have the same

    MxArray “structure“ WebAssembly offers a linear array of memory to represent it, indexed by i32 MxArray representation in WebAssembly: All arrays are 8 byte aligned 1 t y p e d e f s t r u c t { 2 TypeAttribute type ; // Contains type i n f o r m a t i o n 3 i n t numel ; // Length of a r r a y 4 i n t a r r 1 p t r ; // Data ptr , pr i f s p a r s e 5 i n t a r r 2 p t r ; // I f complex or c e l l −a r r a y f i e l d s 6 i n t j c ; // Optional f o r s p a r s e a r r a y s 7 i n t ndims ; // Number of dimensions 8 i n t dim ptr ; //Dim . a r r a y p t r 9 A t r i b u t e a t t r i b u t e s ; // Flags f o r other a t t r i b u t e s 10 } MxArray ; David Herrera (McGill University) MatWably October 15, 2018 10 / 46
  11. Matlab Representation in Memory All Matlab constructs have the same

    MxArray “structure“ WebAssembly offers a linear array of memory to represent it, indexed by i32 MxArray representation in WebAssembly: All arrays are 8 byte aligned David Herrera (McGill University) MatWably October 15, 2018 10 / 46
  12. Matlab Representation in Memory All Matlab constructs have the same

    MxArray “structure“ WebAssembly offers a linear array of memory to represent it, indexed by i32 MxArray representation in WebAssembly: All arrays are 8 byte aligned David Herrera (McGill University) MatWably October 15, 2018 10 / 46
  13. Overview 1 Introduction Motivation Background Purpose 2 Memory Representation 3

    Built-in Library Operations Supported Templates Built-in Framework 4 Compiler Architecture 5 Julia 6 MatWably Background Making calls David Herrera (McGill University) MatWably October 15, 2018 11 / 46
  14. Operations Supported Categories: Constructors: ones, randi, randn, colon, zeros,eye... Arithmetic:

    +,-,/,*, %, mtimes, mrdivide, mldivide... Relational: lt, gt, eq... Logical: true, false, and, not, xor... Transformations: transpose, horzcat, vertcat, concat... Numeric: sin, cos, tan, round, ceil... Others: mean, std, cumsum, cumprod... Most basic Matlab operations supported: https://www.mathworks.com/help/matlab/ operators-and-elementary-operations.html David Herrera (McGill University) MatWably October 15, 2018 12 / 46
  15. Built-in templates Example: element-wise-binary template Template takes a pointer to

    a function and two matrices as input. Broadcast of inputs appropriately. Returns an matrix of the same size as the largest input matrix. Other categories: Contructors: randn, rand, zeros,eye,ones Cumreduce: sum, prod, mean, std... Cumacc: cumsum, cumprod... Concat: Horzcat, Vertcat... David Herrera (McGill University) MatWably October 15, 2018 14 / 46
  16. Built-in templates Example: element-wise-binary template Template takes a pointer to

    a function and two matrices as input. Broadcast of inputs appropriately. Returns an matrix of the same size as the largest input matrix. Other categories: Contructors: randn, rand, zeros,eye,ones Cumreduce: sum, prod, mean, std... Cumacc: cumsum, cumprod... Concat: Horzcat, Vertcat... David Herrera (McGill University) MatWably October 15, 2018 14 / 46
  17. Overview 1 Introduction Motivation Background Purpose 2 Memory Representation 3

    Built-in Library Operations Supported Templates Built-in Framework 4 Compiler Architecture 5 Julia 6 MatWably Background Making calls David Herrera (McGill University) MatWably October 15, 2018 16 / 46
  18. Front-end Framework McLab as a front-end and analysis framework of

    Matlab programs Various useful analyses to generate efficient code: Value Analysis Determines the shapes of a Matlab programs at every program point given an entry function point and an initial parameter types and size. TamerIR Representation of Matlab programs. Three-address code. Resolution of ambiguities. David Herrera (McGill University) MatWably October 15, 2018 18 / 46
  19. Overview 1 Introduction Motivation Background Purpose 2 Memory Representation 3

    Built-in Library Operations Supported Templates Built-in Framework 4 Compiler Architecture 5 Julia 6 MatWably Background Making calls David Herrera (McGill University) MatWably October 15, 2018 19 / 46
  20. Detour: Julia Programming Language Idea: Create a type system that

    explicitly favors multiple-dispatch. No template meta-programming, instead uses MACROS. Performance is gained by specifying methods based on ALL argument types. In OOP, methods are based primarily on the this argument. At run-time the most specific method definition for a given set of concrete argument values is ran. If there is no method definition for that set of arguments, an error is thrown. Type inference is used to determine most specific function based on function arguments. No compile time vs. run-time types. David Herrera (McGill University) MatWably October 15, 2018 20 / 46
  21. Detour: Julia Programming Language - Example 1 j u l

    i a > f ( x : : Float64 , y : : Float64 ) = 2x + y # Function , d e f i n i t i o n f o r x , y f l o a t 6 4 . 2 #f ( g e n e r i c f u n c t i o n with 1 method ) 3 4 j u l i a > f (3 ,3) # Function c a l l with i n t 6 4 argument types 5 #ERROR: MethodError : no method matching f ( : : Int64 , : : Int64 ) 6 #S t a c k t r a c e : 7 # [ 1 ] top−l e v e l scope at none : 0 8 9 j u l i a > f ( x , y ) = 2x − y # D e f i n i t i o n of f u n c t i o n f o r g e n e r i c types . 10 #f ( g e n e r i c f u n c t i o n with 2 method ) 11 12 j u l i a > f (2 ,2) # 13 2 14 j u l i a > f ( 2 . 0 , 2 . 0 ) # Chosen method i s f ( x : : f64 , y : : f64 ) as i s most s p e c i f i c 15 6.0 16 j u l i a > f ( 2 , 2 . 0 ) # Since no d e f i n i t i o n f o r f ( x : : i64 , y : : f64 ) , most s p e c i f i c i s f ( x , y ) g e n e r i c method . 17 2.0 David Herrera (McGill University) MatWably October 15, 2018 21 / 46
  22. MatWably: How to build it... Lesson: The more we can

    specialize functions at compile time, the more performance we are going to get. David Herrera (McGill University) MatWably October 15, 2018 22 / 46
  23. Overview 1 Introduction Motivation Background Purpose 2 Memory Representation 3

    Built-in Library Operations Supported Templates Built-in Framework 4 Compiler Architecture 5 Julia 6 MatWably Background Making calls David Herrera (McGill University) MatWably October 15, 2018 23 / 46
  24. MatWably: Background MatWably: Static compiler from Matlab to WebAssembly Dependencies:

    ValueAnalysis, TamerIR, McSAF optimizations and simplifications, Matjuice Copy Analysis Support: Arrays of doubles of any number of dimensions with all the built-ins previously stated as back-bone. Operation Assumption: No overflows. Matlab has weird overflow rules. It basically sets an overflow to the largest mclass value, wasm simply overflows like a normal language. MxArray, everything is pass by reference. We use copy analysis to determine when to copy, or when to alias. Scalars are always pass by value. Every scalar variable is a local register in function. David Herrera (McGill University) MatWably October 15, 2018 24 / 46
  25. Boxing Placing of a primitive type into an object. In

    our case, we make a scalar, into an MxArray of length 1, dimensions 1x1. Example: 1 f u n c t i o n example () 2 % MxArray of doubles 3 c = ones (2 ,2) ; 4 % In memory i s an MxArray : [ TypeAttr , numel , data ptr , ndims , 5 % dim ptr , s t r i d e s , o t h e r a t t r ] 6 7 % S c a l a r 8 a = 2 ; % Stored v a l u e i n l o c a l r e g i s t e r and v a l u e i s not loaded i n / out 9 % of memory 10 11 a = 2 ; % I f boxing [ TypeAttr ,4 , − >[2] , 2 , − >[1 ,1] , − >[8 ,8] , o t h e r a t t r ] 12 % Total : 72 bytes and we have to unbox the v a r i a b l e to use i t . 13 end We hope to avoid this as it wastes space and brings performance down. David Herrera (McGill University) MatWably October 15, 2018 25 / 46
  26. MatWably: Ways to build it... Two alternatives: We consider everything

    an MxArray, even scalars (easy but very inefficient in terms of memory and run-time). We differentiate between scalars vs. mxarrays. Therefore scalars are always pass-by-value and treated as local registers. If we cannot know at compile time, we default to boxing. (or we restraint the compiler to ”well-behaved” programs). The 20 benchmarks that I use turn out to be well-behaved. This sort of distinction allows us to have ”specialization” in the Julia sense, but only when the programs are well-behaved. David Herrera (McGill University) MatWably October 15, 2018 26 / 46
  27. MatWably: Example 1... 1 f u n c t i

    o n example1 ( b ) 2 a = 2 3 d i s p ( a ) 4 a = [ 2 , 2 ] 5 d i s p ( a ) 6 end How do we generate this? Is there a value type conflict? Can we use a specialized version of the disp(a) function? David Herrera (McGill University) MatWably October 15, 2018 27 / 46
  28. MatWably: Example 1... 1 f u n c t i

    o n example1 ( b ) 2 a = 2 3 d i s p ( a ) 4 a = [ 2 , 2 ] 5 d i s p ( a ) 6 end How do we generate this? Is there a value type conflict? No conflict so far! Can we use a specialized version of the disp(a) function? Yes Generated: 1 (func $example1_S (param $b_f64 f64);; ValueType specified by value analysis 2 ;; Locals $a_f64 , $a_i32. 3 (local $a_f64 f64)(local $a_i32 i32)(local $mc_t3_f64 f64)(local $mc_t2_f64 f64)(local $mc_t6_i32 i32)(local $mc_t7_i32 i32)(local $mc_t5_i32 i32) 4 f64.const 2.0 5 set_local $a_f64 6 get_local $a_f64 7 call $disp_S ;; Function specialization to display scalars 8 ... ;; Code for creating literal [2 ,2] 9 call $horzcat 10 set_local $a_i32 11 get_local $a_i32 12 call $disp_M ;; Function specialization to display MxArray 13 ) David Herrera (McGill University) MatWably October 15, 2018 28 / 46
  29. MatWably: Example 1... Why does it work? Answer: We can

    determine at compile the right value types of definitions and uses for a given variable. Value Analysis in fact gives specific information about each local, (when statically possible) David Herrera (McGill University) MatWably October 15, 2018 29 / 46
  30. MatWably: Example 2... What if we can’t? Example: 1 f

    u n c t i o n example2 () 2 i f rand () > 0.5 3 h = 3 ; % S c a l a r here 4 e l s e 5 h = eye (2 ,2) ; % MxArray here 6 end 7 d i s p ( h ) ; % We only know c o r r e c t s p e c i a l i z a t i o n at run−time . 8 end How do we handle this? David Herrera (McGill University) MatWably October 15, 2018 30 / 46
  31. MatWably: Example 2... Must box h value in the case

    of scalar If there is any ambiguity we must maintain the values boxed. 1 (func $example2_S 2 (local $mt_c1_f64 f64)(local $h_i32 i32) 3 call $rand_S ;; Specialized rand_S function 4 set_local $mt_c1_f64 5 6 get_local $mt_c1_f64 7 f64.const 0.5 8 f64.gt 9 if 10 f64.const 3 11 call $box_scalar 12 set_local $h_i32 13 else 14 ... ;; Set up for eye 15 call $eye 16 set_local $h_i32 17 end 18 get_local $h_i32 19 call $disp_M ;; Must treat possible 20 ;; scalar as MxArray from now on. 21 ;; ( unless another definition of h happens ) 22 ) David Herrera (McGill University) MatWably October 15, 2018 31 / 46
  32. More on generating code... Some things that must be done

    correctly: Collect locals, (name + value type). (i.e. for scalar f64 (if known), for mxarray i32 pointers). Keeping track of labels for break and continue. Jumping out of places in WebAssembly requires knowledge of labels for loops and blocks. Determining value types to generate function signatures. Generating loops statically and dynamically. Making appropriate calls to library built-ins. (More on this later). David Herrera (McGill University) MatWably October 15, 2018 32 / 46
  33. Making calls [a, b, c] = call(arg1, ..., argn) David

    Herrera (McGill University) MatWably October 15, 2018 34 / 46
  34. How do we make calls to built-ins? Design Goals: Easy

    to add new built-ins later on... Cover a variety of possible scenarios. Maximize code re-use. Avoid ugly, hard-to-read if/else statement structure. David Herrera (McGill University) MatWably October 15, 2018 35 / 46
  35. How do we make calls to built-ins? Overall Procedure: Determine

    if built-in can be simplified. If so simplify. If not: Generate inputs Generate function call Set target variables. Result: For each call, we return a list of instructions and a set of new locals used during the call. 1 mc_t1 = 3; 2 mc_t2 = ones (3 ,3); 3 a = plus_SM(mc_t1 , mc_t2); 1 ;; Generating input 2 get_local $mc_t1 3 get_local $mc_t2 4 ;; Making call 5 call $plus_SM 6 ;; Setting Target 7 set_local $a_i32 David Herrera (McGill University) MatWably October 15, 2018 36 / 46
  36. Simplifying - Examples 1 f u n c t i

    o n s i m p l i f y () 2 mt 01 = 3 ; 3 mt 02 = 2 ; 4 a = mt 01+mt 02 ;% p l u s ( mt 01 , mt 02 ) ; Operator s i m p l i f i c a t i o n 5 % g e t l o c a l mt 01 6 % g e t l o c a l mt 02 7 % f64 . add 8 % s e t l o c a l $a f64 9 10 b = ones () % S i m p l i f y to f64 . const 1 11 % f64 . const 1 12 % s e t l o c a l $b f64 13 14 end David Herrera (McGill University) MatWably October 15, 2018 37 / 46
  37. Generating inputs : ... = ...(arg1, ..., argn) Matlab is

    a highly polymorphic language, a given function can take any number of parameters. Problem: In WebAssembly we must either generate built-ins using template meta-programming, or modify to fit a fixed number of parameters. Input Handlers Examples: 1 % Example 1 − Shape I n p u t s 2 a = ones (2 ,2) 3 a = ones ( [ 2 ] ) 4 a = ones ( [ 2 , 2 ] ) 5 a = ones (3 ,4 ,2 ,5) 6 % In wasm : ( func $ones ( param $ s i z e i32 ) ( r e s u l t i32 ) . . . Arguments added to 7 % a r r a y . 8 % Other f u n c t i o n s with s i m i l a r h a n dl i n g : eye , randn , rand , z e r o s etc . . . 9 10 % Example 2 − V a r i a b l e number of i n p u t s 11 b = horzcat ( mc t1 , . . . , mc tn ) 12 % Other f u n c t i o n s : v e r t c a t , g e t a r r a y , s e t a r r a y . 13 14 % Example 3 − As they come 15 % Defa u l t handler , pass arguments as they come . 16 c = plus MM ( mat1 , mat2 ) % Fixed , l i k e o p e r a t o r s . 17 c = u s e r f u n c t i o n ( arg1 , arg2 , arg3 ) % User Defined David Herrera (McGill University) MatWably October 15, 2018 38 / 46
  38. Making call : ... = call(...) Is it a built-in?

    If so, call built-in handler Is it user-defined. If so, return call with suffix Is it a specialized function? If so, return call with suffix Does the function have an alias. e.g. mrdivide MS & rdivide MS. David Herrera (McGill University) MatWably October 15, 2018 39 / 46
  39. Setting to Target: [a, b, c] = ...(...) [ a

    , b , c ] = c a l l ( . . . ) ; What could a,b,c be? Does the call return an array of scalars? Does the call return array of MxArrays? Could it be a combination of both? If so, box scalars, and unbox when setting a,b and c. David Herrera (McGill University) MatWably October 15, 2018 40 / 46
  40. Final notes BuiltinCallGenerator abstract class is the top of the

    class hierarchy. To avoid code re-use a class hierarchy based around input handlers is set. BuiltinCallGenerator creates abstract methods and properties for the other built-ins to implement based on what we saw. David Herrera (McGill University) MatWably October 15, 2018 41 / 46
  41. Performance - MatJuiceV2 drv_clos drv_collatz drv_crni drv_dich drv_fdtd drv_fft drv_fiff

    drv_lgdr drv_matmul_p drv_mcpi_p drv_prime drv_babai drv_bubble drv_capr geomean 0.0 0.5 1.0 1.5 2.0 2.5 3.0 Speedup of MatjuiceV2 (relative to MatJuice) Figure: MatJuiceV2 vs. MatJuice Problem with array access... most benchmarks with no array access perform well. In Wasm we call: get array index / set array index functions to get a given index, this function checks bounds, throws error if out-of-bounds. In JS: this is handled entirely by the JavaScript engine. David Herrera (McGill University) MatWably October 15, 2018 43 / 46
  42. Performance - MatWably drv_clos drv_collatz drv_crni drv_dich drv_fdtd drv_fft drv_fiff

    drv_lgdr drv_matmul_p drv_mcpi_p drv_prime drv_babai drv_bubble drv_capr geomean 0.0 0.5 1.0 1.5 2.0 2.5 3.0 Speedup of MatWably (relative to MatJuice) Figure: MatWably vs. MatJuice Again, problem with array access... Benchmarks with no array access perform well. David Herrera (McGill University) MatWably October 15, 2018 44 / 46
  43. TODO: Array access In JavaScript: easy, use: a = new

    Float64Array(memory.buffer, arr ptr , arr length ) In WebAssembly: In-line array access code, analysis on out-of-bounds to avoid writing this. David Herrera (McGill University) MatWably October 15, 2018 46 / 46
  44. TODO: Memory Clean Up WebAssembly does not support a garbage

    collector for the linear memory. In languages like C, a malloc implementation is used. So Emscripten relies on the input programs ability to free their own dynamic memory. MatWably built-in library supports a basic malloc implementation using a simple doubly linked list. Another Idea: Try to use Emscripten’s malloc. In MatWably, we must free memory ourselves whenever we can... For this, Value Analysis already has a use/def chain, I can check if I could re-use that to free memory. David Herrera (McGill University) MatWably October 15, 2018 47 / 46
  45. Possible Optimizations - Peephole Basic ones based on the stack

    instructions. TamerIR transforms a literal definition such as [2, 2; 2, 2] into individual three address c Original TamerIR 1 f u n c t i o n l i t e r a l s ( a ) 2 b = [ 1 , 2 ; 3 , 4 ] ; 3 d i s p ( b ) ; 4 end 1 f u n c t i o n [ ] = l i t e r a l s ( a ) 2 mc t1 = 1; 3 mc t2 = 2; 4 [ mc t0 ] = horzcat ( mc t1 , mc t2 ) ; 5 mc t4 = 3; 6 mc t5 = 4; 7 [ mc t3 ] = horzcat ( mc t4 , mc t5 ) ; 8 [ b ] = v e r t c a t ( mc t0 , mc t3 ) ; 9 d i s p ( b ) ; % [ ] = . . . 10 end David Herrera (McGill University) MatWably October 15, 2018 49 / 46
  46. Generated 1 (func $literals 2 ;;... Locals 3 f64.const 1.0

    4 set_local $mc_t1_f64 5 f64.const 2.0 6 set_local $mc_t2_f64 7 i32.const 2 8 ;;... 9 call $create_mxvector 10 set_local $mc_t7_i32 11 get_local $mc_t1_f64 12 call $convert_scalar_to_mxarray 13 set_local $mc_t8_i32 14 get_local $mc_t7_i32 15 i32.const 0 16 get_local $mc_t8_i32 17 call $set_array_index_i32_no_check 18 get_local $mc_t2_f64 19 call $convert_scalar_to_mxarray 20 set_local $mc_t9_i32 21 get_local $mc_t7_i32 22 i32.const 1 23 get_local $mc_t9_i32 24 call $set_array_index_i32_no_check 25 get_local $mc_t7_i32 26 call $horzcat 27 ;;... Other concat calls 28 call $disp_M 29 ) Solution: Recognize literal definition. Set in memory at compile time. David Herrera (McGill University) MatWably October 15, 2018 50 / 46
  47. Specializing further So far MatWably specializes based on scalar vs.

    non-scalar. Value analysis provides a little bit more information statically. e.g. vector, vs. 2D matrix. We can use that, for instance, to implement further functions such as a dot product instead of matrix multiplication. David Herrera (McGill University) MatWably October 15, 2018 51 / 46
  48. Allocating new arrays. CopyAnalysis assumes arrays are passed by reference

    and only adds copy statements when necessary. Idea: Extend Copy Analysis to other statements such as built-in calls. 1 f u n c t i o n [ b ] = a dd t h re e ( n ) 2 b = ones (3 ,3) 3 f o r v=1:n 4 b = p l u s (3 , b ) % No need to re−a l l o c a t e b 5 end 6 end David Herrera (McGill University) MatWably October 15, 2018 52 / 46