1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*!
21 * \file tvm/arith/constraint_extract.cc
22 */
23
24#include "constraint_extract.h"
25
26#include <tvm/arith/analyzer.h>
27#include <tvm/tir/expr.h>
28
29#include "pattern_match.h"
30
31namespace tvm {
32namespace arith {
33
34template <typename F>
35void CollectConstraints(PrimExpr expr, F callback, bool keep_composite_constraints) {
36 if (keep_composite_constraints) {
37 callback(expr);
38 }
39
40 PVar<PrimExpr> x, y;
41 if ((x && y).Match(expr)) {
42 CollectConstraints(x.Eval(), callback, keep_composite_constraints);
43 CollectConstraints(y.Eval(), callback, keep_composite_constraints);
44 } else if (!keep_composite_constraints) {
45 callback(expr);
46 }
47}
48
49std::vector<PrimExpr> ExtractConstraints(const PrimExpr& expr, bool keep_composite_constraints) {
50 std::vector<PrimExpr> out;
51 CollectConstraints(
52 expr, [&](const PrimExpr& part) { out.push_back(part); }, keep_composite_constraints);
53 return out;
54}
55
56template <typename F>
57void CollectComponents(PrimExpr expr, F callback) {
58 PVar<PrimExpr> x, y;
59 if ((x || y).Match(expr)) {
60 CollectComponents(x.Eval(), callback);
61 CollectComponents(y.Eval(), callback);
62 } else {
63 callback(expr);
64 }
65}
66
67std::vector<PrimExpr> ExtractComponents(const PrimExpr& expr) {
68 std::vector<PrimExpr> out;
69 CollectComponents(expr, [&](const PrimExpr& part) { out.push_back(part); });
70 return out;
71}
72
73} // namespace arith
74} // namespace tvm
75