dimreduce.mli 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. (** Dimension reduction: transform multi-dimensional arrays to one-dimensional
  2. arrays, and pass original array dimensions in function calls. *)
  3. (**
  4. This phase lowers multi-dimensional ararys to one-dimensional arrays. This
  5. transformation is done in two steps.
  6. In the first step, function calls and function parameter lists are modified.
  7. For function calls, array dimensions are passed as explicit arguments before
  8. the array argument itself. Naturally, function declarations need to be
  9. modified accordingly, adding explicit parameters for array dimensions.
  10. Note that we need not create new variable declarations for array
  11. declarations here. This is already done during the desugaring phase
  12. ({!Desug} explains the reason for this).
  13. In the second step, statements and expressions involving multi-dimensional
  14. array referencing are transformed such that they reference a one-dimensional
  15. array. Variable declarations with type [ArrayDims] are transformed into
  16. variables of type [Array], thus removing the dimensionality information.
  17. Allocation statements become one-dimensional allocations with a length that
  18. is the product of all array dimensions. Indexing multi-dimensional arrays is
  19. changed such that arrays are accessed in row-major order.
  20. {v void foo(int[a, b, c] p) \{
  21. int n; int m; int k; int[n, m, k] q; // n, m, k are added during desugaring
  22. n = 5;
  23. m = 10;
  24. k = 3;
  25. q = __allocate(n, m, k);
  26. q[x, y, z] = p[x, y, z];
  27. foo(q);
  28. \} v}
  29. step 1:
  30. {v void foo(int a, int b, int c, int[] p) \{ // Array parameter
  31. int n; int m; int k; int[n, m, k] q;
  32. n = 5;
  33. m = 10;
  34. k = 3;
  35. q = __allocate(n, m, k);
  36. q[x, y, z] = p[x, y, z];
  37. foo(n, m, k, q); // Array argument
  38. \} v}
  39. step 2:
  40. {v void foo(int[a, b, c] p) \{
  41. int n; int m; int k; int[] q; // Removing dimension information
  42. n = 5;
  43. m = 10;
  44. k = 3;
  45. q = __allocate(n * m * k); // Allocation
  46. q[((x * m) + y) * k + z] = p[((x * b) + y) * c + z]; // Indexing
  47. foo(n, m, k, q);
  48. \} v}
  49. Note that when array dimensions are constant integer values, an array index
  50. may be simplified to a single constant value during contant propagation.
  51. *)
  52. (** Main phase function, called by {!Main}. *)
  53. val phase : Main.phase_func