|
2 | 2 | from pydsl.macro import CallMacro, Compiled |
3 | 3 | from pydsl.protocols import lower_single, SubtreeOut, ToMLIRBase |
4 | 4 | from pydsl.type import Int, Float, Sign |
| 5 | +from pydsl.vector import Vector |
5 | 6 |
|
6 | 7 | import mlir.dialects.arith as arith |
7 | 8 |
|
@@ -59,3 +60,69 @@ def min(visitor: ToMLIRBase, a: Compiled, b: Compiled) -> SubtreeOut: |
59 | 60 | return rett(arith.MinimumFOp(av, bv)) |
60 | 61 | else: |
61 | 62 | raise TypeError(f"cannot take min of {rett.__qualname__}") |
| 63 | + |
| 64 | + |
| 65 | +@CallMacro.generate() |
| 66 | +def trunc( |
| 67 | + visitor: ToMLIRBase, |
| 68 | + a: Compiled, |
| 69 | + truncated_type: Compiled, |
| 70 | + *, |
| 71 | + round_mode: Compiled = None, |
| 72 | +) -> SubtreeOut: |
| 73 | + a_type = type(a) |
| 74 | + out_type = truncated_type |
| 75 | + if isinstance(a, Vector): |
| 76 | + out_type = Vector.get(a.shape, truncated_type) |
| 77 | + a_type = a.element_type |
| 78 | + |
| 79 | + if truncated_type.width >= a_type.width: |
| 80 | + raise TypeError("truncated type must be smaller than called type.") |
| 81 | + |
| 82 | + if issubclass(a_type, Int): |
| 83 | + out = arith.TruncIOp(lower_single(out_type), lower_single(a)) |
| 84 | + elif issubclass(a_type, Float): |
| 85 | + out = arith.TruncFOp(lower_single(out_type), lower_single(a)) |
| 86 | + else: |
| 87 | + raise TypeError(f"cannot take trunc of {a_type.__qualname__}") |
| 88 | + if round_mode is not None: |
| 89 | + out.attributes["round_mode"] = lower_single(round_mode) |
| 90 | + return (out_type)(out) |
| 91 | + |
| 92 | + |
| 93 | +@CallMacro.generate() |
| 94 | +def vadd(visitor: ToMLIRBase, a: Compiled, b: Compiled) -> SubtreeOut: |
| 95 | + rett = type(a) |
| 96 | + |
| 97 | + if not isinstance(a, Vector): |
| 98 | + raise TypeError(f"NOT a vector addition operation") |
| 99 | + if type(a) != type(b): |
| 100 | + raise TypeError(f"VADD type {type(a)} does not match {type(b)}") |
| 101 | + |
| 102 | + a_type = a.element_type |
| 103 | + if issubclass(a_type, Int): |
| 104 | + op = arith.addi(lower_single(a), lower_single(b)) |
| 105 | + elif issubclass(a_type, Float): |
| 106 | + op = arith.addf(lower_single(a), lower_single(b)) |
| 107 | + else: |
| 108 | + raise TypeError(f"unsupported vector addition type: {a_type}") |
| 109 | + return rett(op) |
| 110 | + |
| 111 | + |
| 112 | +@CallMacro.generate() |
| 113 | +def vmul(visitor: ToMLIRBase, a: Compiled, b: Compiled) -> SubtreeOut: |
| 114 | + rett = type(a) |
| 115 | + |
| 116 | + if not isinstance(a, Vector): |
| 117 | + raise TypeError(f"NOT a vector multiplication operation") |
| 118 | + if type(a) != type(b): |
| 119 | + raise TypeError(f"VMUL type {type(a)} does not match {type(b)}") |
| 120 | + |
| 121 | + a_type = a.element_type |
| 122 | + if issubclass(a_type, Int): |
| 123 | + op = arith.muli(lower_single(a), lower_single(b)) |
| 124 | + elif issubclass(a_type, Float): |
| 125 | + op = arith.mulf(lower_single(a), lower_single(b)) |
| 126 | + else: |
| 127 | + raise TypeError(f"unsupported vector multiplication type: {a_type}") |
| 128 | + return rett(op) |
0 commit comments