-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade the Constant Folding pass #510
Comments
This will be very helpful! It is worth considering that we many many operations to constant fold:
Implementing each operation for each data type can be very time consuming and error-prone.
|
Possible solution, use the union member if (src_type == dst_type) {
new_constant.value_bits = input.value_bits;
success = true;
}
In constant_fold.cpp line 58 and 68: - new_constant.val_int32() = lhs->val[0].val_int32() * rhs->val[0].val_int32();
+ new_constant = lhs->val[0] * rhs->val[0]; Then write TypedConstant operator*(const TypedConstant &other)
{
TypeConstant result;
TI_ASSERT(other->dt == this->dt);
switch (this->dt) {
#define REGISTER_TYPE(t) case DataType::t: result->val_##t = this->val_##t * other->val_##t;
REGISTER_TYPE(i32)
REGISTER_TYPE(f32)
// ...
#undef REGISTER_TYPE
}
return result;
}
These works may also be reduced by using macros like Also try: #define REGISTER_TYPE(t, op) // ...
#define REGISTER_OP(op) \
TypedConstant operator##op(const TypedConstant &other) /* `##` here might be unnecessary */ \
{ \
/* ... */ \
} Hope this would be helpful to you. |
We can add bool is_expr_const(Expr &&expr) {
if (expr.is_pure_function()) // i.e. constexpr
if (expr.oprand.all(is_expr_const))
return true;
return false;
} |
Concisely describe the proposed feature
The constant folder works only for
DataType::i32
withBinaryOpType::mul
andBinaryOpType::sub
. We can systematically upgrade it.Describe the solution you'd like
We may write the constant folder for each type and each operation, but it takes too much code to write them one by one. It would be better to have something like
val(DataType dt)
to avoid type checkings likeif (data_type == DataType::i32) return val.val_int32();
.The text was updated successfully, but these errors were encountered: