ekalda commented on code in PR #16523: URL: https://github.com/apache/tvm/pull/16523#discussion_r1481340370
########## src/arith/scalable_expression.cc: ########## @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file tvm/arith/scalable_expression.cc + * \brief Analyze scalable expressions. + */ + +#include "scalable_expression.h" + +#include <tvm/tir/expr.h> +#include <tvm/tir/op.h> + +#include "../tir/transforms/replace_selected_expr.h" +#include "./pattern_match.h" + +namespace tvm { +namespace arith { + +bool IsVScaleCall(const PrimExpr& expr) { + if (auto call = expr.as<tir::CallNode>()) { + return call->op.same_as(tir::builtin::vscale()); + } + return false; +} + +PrimExpr CanonicalizeScalableLanes(const PrimExpr& lanes) { + PVar<IntImm> multiplier; + PCallExpr<PVscaleOp> vscale; + + PrimExpr new_lanes; + + if ((multiplier * vscale).Match(lanes)) { + new_lanes = lanes; + } else if ((vscale * multiplier).Match(lanes)) { + new_lanes = Review Comment: > Is this canonicalization necessary? It seems like it would occur by default when applying the simplification passes. If it is necessary, we should match the behavior of the simplifier, with constants collected to the RHS of expressions. I suppose this canonicalization is not strictly necessary, but it has proven useful in things like testing the scalable Ramps in isolation. It shouldn't be a lot of computational overhead, so I think it wouldn't hurt to force the order. But yes, let's go for `vscale * 4` to match the behaviour of the rest of the stack. > I'd recommend instead having a std::optional<int> ExtractScalableLanes(const PrimExpr& lanes) method. That way, the calling scope could use if(auto scale = ExtractScalableLanes(lanes)) { ... }, to immediately have access to the extracted value. As it is, the two uses of this function need to first check if the expression is scalable, then manually extract the scale factor. ``` if (PMatchesOneOf{ multiplier * vscale, vscale * multiplier, }.Match(lanes)) { return vscale.Eval()->value; } else { return std::nullopt; } ``` Thanks, yes that sounds much better :) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@tvm.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org