Add SymbolicExpr::IsBinaryOp() method

This CL introduces a new helper method SymbolicExpr::IsBinaryOp() to quickly determine if a SymbolicExpr is a binary operation (i.e., not a constant or a variable). This is used in indexing_map.cc in several places for AffineMap and it will simplify the refactor.

PiperOrigin-RevId: 826468454
This commit is contained in:
A. Unique TensorFlower 2025-10-31 06:44:31 -07:00 committed by TensorFlower Gardener
parent 718fe5695e
commit f73a954906
2 changed files with 11 additions and 6 deletions

View File

@ -557,6 +557,12 @@ SymbolicExprContext* SymbolicExpr::GetContext() const { return impl_->ctx_; }
SymbolicExprType SymbolicExpr::GetType() const { return impl_->type_; }
bool SymbolicExpr::IsBinaryOp() const {
auto type = GetType();
return type != SymbolicExprType::kConstant &&
type != SymbolicExprType::kVariable;
}
SymbolicExpr SymbolicExpr::GetLHS() const { return impl_->lhs_; }
SymbolicExpr SymbolicExpr::GetRHS() const { return impl_->rhs_; }
@ -732,12 +738,11 @@ SymbolicExpr SymbolicExpr::Replace(
return it->second;
}
SymbolicExprType type = GetType();
if (type == SymbolicExprType::kConstant ||
type == SymbolicExprType::kVariable) {
if (!IsBinaryOp()) {
return *this;
}
SymbolicExprType type = GetType();
SymbolicExpr lhs = GetLHS();
SymbolicExpr rhs = GetRHS();
SymbolicExpr new_lhs = lhs.Replace(replacements);
@ -779,12 +784,11 @@ SymbolicExpr SymbolicExpr::Canonicalize() const {
return *this;
}
SymbolicExprType type = GetType();
if (type == SymbolicExprType::kConstant ||
type == SymbolicExprType::kVariable) {
if (!IsBinaryOp()) {
return *this;
}
SymbolicExprType type = GetType();
SymbolicExpr lhs = this->GetLHS().Canonicalize();
SymbolicExpr rhs = this->GetRHS().Canonicalize();

View File

@ -65,6 +65,7 @@ class SymbolicExpr {
SymbolicExprContext* GetContext() const;
SymbolicExprType GetType() const;
bool IsBinaryOp() const;
SymbolicExpr GetLHS() const;
SymbolicExpr GetRHS() const;
int64_t GetValue() const;