|
8 | 8 |
|
9 | 9 | #include <tvm/ir.h>
|
10 | 10 | #include <tvm/runtime/device_api.h>
|
| 11 | +#include <tvm/ir_mutator.h> |
11 | 12 | #include <vector>
|
12 | 13 |
|
13 | 14 | namespace TVM {
|
@@ -154,6 +155,124 @@ inline int GetTempAllocaAlignment(Type type, int32_t const_size) {
|
154 | 155 | }
|
155 | 156 | return align;
|
156 | 157 | }
|
| 158 | + |
| 159 | +// Remove cast in binary expressions |
| 160 | +// Example: (cast(x) + cast(y)) -> (x + y) |
| 161 | +// Usage: CastRemover castRemover; |
| 162 | +// Expr expr = castRemover.Mutate(expr); |
| 163 | +class CastRemover final : public IRMutator { |
| 164 | + public: |
| 165 | + CastRemover() {} |
| 166 | + |
| 167 | + Expr Mutate_(const Cast* op, const Expr& e) { |
| 168 | + return op->value; |
| 169 | + } |
| 170 | + |
| 171 | + Expr Mutate_(const Add* op, const Expr& e) { |
| 172 | + Expr a = this->Mutate(op->a); |
| 173 | + Expr b = this->Mutate(op->b); |
| 174 | + if (const Cast* ca = a.as<Cast>()) { |
| 175 | + a = ca->value; |
| 176 | + } |
| 177 | + if (const Cast* cb = b.as<Cast>()) { |
| 178 | + b = cb->value; |
| 179 | + } |
| 180 | + if (a.type() != b.type()) |
| 181 | + LOG(FATAL) << "CastRemover: type mismatch " |
| 182 | + << a.type() << " vs " << b.type(); |
| 183 | + return Add::make(a, b); |
| 184 | + } |
| 185 | + |
| 186 | + Expr Mutate_(const Sub* op, const Expr& e) { |
| 187 | + Expr a = this->Mutate(op->a); |
| 188 | + Expr b = this->Mutate(op->b); |
| 189 | + if (const Cast* ca = a.as<Cast>()) { |
| 190 | + a = ca->value; |
| 191 | + } |
| 192 | + if (const Cast* cb = b.as<Cast>()) { |
| 193 | + b = cb->value; |
| 194 | + } |
| 195 | + if (a.type() != b.type()) |
| 196 | + LOG(FATAL) << "CastRemover: type mismatch " |
| 197 | + << a.type() << " vs " << b.type(); |
| 198 | + return Sub::make(a, b); |
| 199 | + } |
| 200 | + |
| 201 | + Expr Mutate_(const Mul* op, const Expr& e) { |
| 202 | + Expr a = this->Mutate(op->a); |
| 203 | + Expr b = this->Mutate(op->b); |
| 204 | + if (const Cast* ca = a.as<Cast>()) { |
| 205 | + a = ca->value; |
| 206 | + } |
| 207 | + if (const Cast* cb = b.as<Cast>()) { |
| 208 | + b = cb->value; |
| 209 | + } |
| 210 | + if (a.type() != b.type()) |
| 211 | + LOG(FATAL) << "CastRemover: type mismatch " |
| 212 | + << a.type() << " vs " << b.type(); |
| 213 | + return Mul::make(a, b); |
| 214 | + } |
| 215 | + |
| 216 | + Expr Mutate_(const Div* op, const Expr& e) { |
| 217 | + Expr a = this->Mutate(op->a); |
| 218 | + Expr b = this->Mutate(op->b); |
| 219 | + if (const Cast* ca = a.as<Cast>()) { |
| 220 | + a = ca->value; |
| 221 | + } |
| 222 | + if (const Cast* cb = b.as<Cast>()) { |
| 223 | + b = cb->value; |
| 224 | + } |
| 225 | + if (a.type() != b.type()) |
| 226 | + LOG(FATAL) << "CastRemover: type mismatch " |
| 227 | + << a.type() << " vs " << b.type(); |
| 228 | + return Div::make(a, b); |
| 229 | + } |
| 230 | + |
| 231 | + Expr Mutate_(const Mod* op, const Expr& e) { |
| 232 | + Expr a = this->Mutate(op->a); |
| 233 | + Expr b = this->Mutate(op->b); |
| 234 | + if (const Cast* ca = a.as<Cast>()) { |
| 235 | + a = ca->value; |
| 236 | + } |
| 237 | + if (const Cast* cb = b.as<Cast>()) { |
| 238 | + b = cb->value; |
| 239 | + } |
| 240 | + if (a.type() != b.type()) |
| 241 | + LOG(FATAL) << "CastRemover: type mismatch " |
| 242 | + << a.type() << " vs " << b.type(); |
| 243 | + return Mod::make(a, b); |
| 244 | + } |
| 245 | + |
| 246 | + Expr Mutate_(const Min* op, const Expr& e) { |
| 247 | + Expr a = this->Mutate(op->a); |
| 248 | + Expr b = this->Mutate(op->b); |
| 249 | + if (const Cast* ca = a.as<Cast>()) { |
| 250 | + a = ca->value; |
| 251 | + } |
| 252 | + if (const Cast* cb = b.as<Cast>()) { |
| 253 | + b = cb->value; |
| 254 | + } |
| 255 | + if (a.type() != b.type()) |
| 256 | + LOG(FATAL) << "CastRemover: type mismatch " |
| 257 | + << a.type() << " vs " << b.type(); |
| 258 | + return Min::make(a, b); |
| 259 | + } |
| 260 | + |
| 261 | + Expr Mutate_(const Max* op, const Expr& e) { |
| 262 | + Expr a = this->Mutate(op->a); |
| 263 | + Expr b = this->Mutate(op->b); |
| 264 | + if (const Cast* ca = a.as<Cast>()) { |
| 265 | + a = ca->value; |
| 266 | + } |
| 267 | + if (const Cast* cb = b.as<Cast>()) { |
| 268 | + b = cb->value; |
| 269 | + } |
| 270 | + if (a.type() != b.type()) |
| 271 | + LOG(FATAL) << "CastRemover: type mismatch " |
| 272 | + << a.type() << " vs " << b.type(); |
| 273 | + return Max::make(a, b); |
| 274 | + } |
| 275 | +}; |
157 | 276 | } // namespace ir
|
158 | 277 | } // namespace TVM
|
159 | 278 | #endif // PASS_IR_UTIL_H_
|
0 commit comments