Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
1b1ab96
Initial implementation of Common Subexpression Elimination for TIR (#…
FranckQC Oct 29, 2021
b8f63b4
Merge branch 'apache:main' into FranckQC-CSE
FranckQC Nov 10, 2021
08f4ae2
Merge branch 'main' of github.com:apache/tvm into FranckQC-CSE
FranckQC Nov 15, 2021
0869a42
Added empty newline at the end of every new file
FranckQC Nov 15, 2021
c38de61
Rolled-back the pointer to the submodule vta-hw
FranckQC Nov 15, 2021
114740a
Merge branch 'main' of github.com:apache/tvm into FranckQC-CSE
FranckQC Nov 15, 2021
5de7f7d
Merge branch 'main' into FranckQC-CSE
FranckQC Dec 13, 2021
08c3cb1
Merged upstream/main into branch
FranckQC Jan 20, 2022
7afac3f
Improved the CSE by not commoning at the toplevel redundant computati…
FranckQC Jan 13, 2022
2aa7ef9
Spelling and comment
FranckQC Jan 18, 2022
c4138d9
Improved the CSE by not commoning at the toplevel redundant computati…
FranckQC Jan 13, 2022
b5f4c97
Revert "Improved the CSE by not commoning at the toplevel redundant c…
FranckQC Jan 20, 2022
2ae01d3
Fixed reference used for no reason instead of normal variable.
FranckQC Jan 21, 2022
06f2303
Added comment explaning why we do not need the union/intersection ove…
FranckQC Jan 21, 2022
58fb734
Merge branch 'main' of github.com:apache/tvm into FranckQC-CSE
FranckQC Feb 2, 2022
0d28cc4
Did most of the changes suggested by upstream
FranckQC Feb 1, 2022
a2364bd
Continued to work on the remarks given on the public repo.
FranckQC Feb 2, 2022
bfe8d4b
Final remarks addressed, small formatting things, and fixing things r…
FranckQC Feb 3, 2022
86c6020
Last linter fix.
FranckQC Feb 3, 2022
bb7d56c
Fixing newline
FranckQC Feb 3, 2022
35100f1
Adding newline missing.
FranckQC Feb 3, 2022
ea7a042
Minor commit for style fo conform with clang-format
FranckQC Feb 3, 2022
0073e8e
Removed trailing space at end of line
FranckQC Feb 3, 2022
406868d
And more minor style changes
FranckQC Feb 3, 2022
6f5fc37
Fixing style of the python test files
FranckQC Feb 3, 2022
cbb8d94
And one more for style in python tests!
FranckQC Feb 3, 2022
0294739
This linter is very annoying to force the style of indentation in a c…
FranckQC Feb 3, 2022
24a1f9c
Deactivate the CSE pass for the lowering tests as it would otherwise …
FranckQC Feb 7, 2022
8e6b4ef
Fixing new lint offenses
FranckQC Feb 7, 2022
34c13f2
Removing debug statement
FranckQC Feb 7, 2022
eac78dc
Restore other test file to its previous state
FranckQC Feb 7, 2022
79451e2
One more for the linter...
FranckQC Feb 7, 2022
7803bbb
Linter again, this time for the new test...
FranckQC Feb 7, 2022
24ee891
again
FranckQC Feb 7, 2022
542f963
again...
FranckQC Feb 7, 2022
c824f33
Deactivating the CSE pass for another lowering test as it does some c…
FranckQC Feb 7, 2022
d8a2c4c
Disabling the CSE for the a test for GPU too
FranckQC Feb 7, 2022
3b6f725
Trying to fix a VTA test by disabling the CSE pass for it, as it prob…
FranckQC Feb 7, 2022
ab41b40
Complying with the linter
FranckQC Feb 7, 2022
a6409f9
Restarting the CI 1/2
FranckQC Feb 7, 2022
8d62f32
Restarting the CI 2/2
FranckQC Feb 7, 2022
b8b33d2
Restarting CI 1/2
FranckQC Feb 8, 2022
4b8c0dd
Restarting CI 2/2
FranckQC Feb 8, 2022
2ea3215
Slightly reduce size of large pretty printer test, copied from https:…
FranckQC Feb 8, 2022
780a00f
Merge branch 'main' of github.com:apache/tvm into FranckQC-CSE
FranckQC Feb 8, 2022
51fff03
Trying to resolve the problems on the weird tests
FranckQC Feb 8, 2022
29e83e4
Linter.
FranckQC Feb 8, 2022
1dc15dc
Restarting CI which has skipped the MacOS build for no reason 1/2
FranckQC Feb 8, 2022
220aa87
Restarting CI which has skipped the MacOS build for no reason 2/2
FranckQC Feb 8, 2022
e4c9832
Merge branch 'main' of github.com:apache/tvm into FranckQC-CSE
FranckQC Feb 8, 2022
accb0ce
Commented buggy tests
FranckQC Feb 9, 2022
50f1707
Linter...
FranckQC Feb 9, 2022
45fd7da
Restore the VTA tests, and use trick kindly given by Masa to disable…
FranckQC Feb 9, 2022
8230999
New fix, which this time does not break the doc (VTA uses a set with …
FranckQC Feb 9, 2022
4885ad4
More VTA fixes
FranckQC Feb 9, 2022
c8b2ff1
vta tutorial fix
masahi Feb 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/tvm/tir/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,14 @@ TVM_DLL Pass FlattenBuffer();
*/
TVM_DLL Pass TextureFlatten();

/*!
* \brief Implements a Common Subexpression Elimination (CSE) for TIR
* which introduces let-in bindings for duplicated sub-expressions.
* \param enable_cse_tir Whether common subexpression elimination is enabled.
* \return The pass.
*/
TVM_DLL Pass CommonSubexprElimTIR(bool enable_cse_tir = true);

/*!
* \brief Unify all the thread bindings for "blockIdx.x/y/z", "threadIdx.x/y/z", and
* "vthread.x/y/z". Before the unification, two vars that are bound to a thread axis (e.g.,
Expand Down
11 changes: 11 additions & 0 deletions python/tvm/tir/transform/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,17 @@ def BF16TypeLowering():
return _ffi_api.BF16TypeLowering() # type: ignore


def CommonSubexprElimTIR(enable_cse_tir: bool = True):
"""Replace redundant computations by new variables.

Returns
-------
fpass : tvm.transform.Pass
The result pass
"""
return _ffi_api.CommonSubexprElimTIR(enable_cse_tir) # type: ignore


def RewriteUnsafeSelect():
"""Detect and rewrite unsafe select that contains memory access.

Expand Down
5 changes: 5 additions & 0 deletions src/driver/driver_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ TVM_REGISTER_PASS_CONFIG_OPTION("tir.detect_global_barrier", Bool);
TVM_REGISTER_PASS_CONFIG_OPTION("tir.instrument_bound_checkers", Bool);
TVM_REGISTER_PASS_CONFIG_OPTION("tir.disable_assert", Bool);
TVM_REGISTER_PASS_CONFIG_OPTION("tir.disable_vectorize", Bool);
TVM_REGISTER_PASS_CONFIG_OPTION("tir.disable_cse_tir", Bool);
TVM_REGISTER_PASS_CONFIG_OPTION("tir.disable_storage_rewrite", Bool);
TVM_REGISTER_PASS_CONFIG_OPTION("tir.is_entry_func", Bool);
TVM_REGISTER_PASS_CONFIG_OPTION("tir.add_lower_pass", Array<Array<ObjectRef>>);
Expand Down Expand Up @@ -196,6 +197,7 @@ Array<tvm::transform::Pass> CreatePassList(bool disable_loop_partition) {
pass_ctx->GetConfig<Bool>("tir.disable_storage_rewrite", Bool(false)).value();
bool instrument_bound_checkers =
pass_ctx->GetConfig<Bool>("tir.instrument_bound_checkers", Bool(false)).value();
bool disable_cse_tir = pass_ctx->GetConfig<Bool>("tir.disable_cse_tir", Bool(false)).value();

// Get any user-added passes
Array<Array<ObjectRef>> add_lower_pass =
Expand Down Expand Up @@ -283,6 +285,9 @@ Array<tvm::transform::Pass> CreatePassList(bool disable_loop_partition) {
if (instrument_bound_checkers) {
pass_list.push_back(tir::transform::InstrumentBoundCheckers());
}

pass_list.push_back(tir::transform::CommonSubexprElimTIR(!disable_cse_tir));

return pass_list;
}

Expand Down
98 changes: 98 additions & 0 deletions src/tir/analysis/check_contains.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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 check_contains.cc
* \brief Implementation of the analysis that tells if an expression contains
a node that satisfies a given predicate.
*/

#include "check_contains.h"

#include <tvm/tir/expr.h>

#include <vector>

namespace tvm {
namespace tir {

/*!
* \brief Toplevel (static) function that tells if an expression contains a subexpression that
satisfies a given predicate.
* \param expr The expression to check
* \param predicate The predicate that must be satisfied
* \return Whether `expr` contains a subexpression that satisfies `predicate`
*/
bool CheckContains::ExprContains(const PrimExpr& expr,
std::function<bool(const PrimExpr&)> predicate) {
CheckContains check_contains(predicate);
check_contains.VisitExpr(expr);
return check_contains.contains_it_;
}

/*!
* \brief Toplevel (static) function that tells if a statement contains a subexpression that
satisfies a given predicate.
* \param stmt The statement to check
* \param predicate The predicate that must be satisfied
* \return Whether `stmt` contains a subexpression that satisfies `predicate`
*/
bool CheckContains::StmtContains(const Stmt& stmt, std::function<bool(const PrimExpr&)> predicate) {
CheckContains check_contains(predicate);
check_contains.VisitStmt(stmt);
return check_contains.contains_it_;
}

/*!
* \brief Protected constructor of CheckContains.
* \param predicate The predicate that must be satisfied
*/
CheckContains::CheckContains(std::function<bool(const PrimExpr&)> predicate)
: predicate_(predicate) {}

/*!
* \brief The method which overrides the generic dispatcher of StmtExprVisitor for expressions.
* \param expr The expression to visit
*/
void CheckContains::VisitExpr(const PrimExpr& expr) {
// If the predicate holds on `expr`, we know `expr` contains something which makes
// the predicate hold
if (predicate_(expr)) {
contains_it_ = true;
} else {
// Otherwise we continue to look for it recursively by calling the dispatcher
StmtExprVisitor::VisitExpr(expr);
}
}

/*!
* \brief The method which overrides the generic dispatcher of StmtExprVisitor for statements.
* \param stmt The statement to visit
*/
void CheckContains::VisitStmt(const Stmt& stmt) {
// We keep exploring only if `contains_it_` is false
if (!contains_it_) {
// and in order to do that we call the general dispatcher
StmtExprVisitor::VisitStmt(stmt);
}
// As otherwise we already have our answer
}

} // namespace tir
} // namespace tvm
60 changes: 60 additions & 0 deletions src/tir/analysis/check_contains.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 check_contains.h
* \brief Interface of the analysis that tells if an expression contains
a node that satisfies a given predicate.
*/

#ifndef TVM_TIR_ANALYSIS_CHECK_CONTAINS_H_
#define TVM_TIR_ANALYSIS_CHECK_CONTAINS_H_

#include <tvm/tir/expr.h>
#include <tvm/tir/stmt_functor.h> // For the class StmtExprVisitor

namespace tvm {
namespace tir {

/*!
* \brief Visitor which tells if a given expression or statement contains a subexpression
that satisfies a given predicate
*/
class CheckContains : public StmtExprVisitor {
public:
// Toplevel (static) functions
static bool ExprContains(const PrimExpr& expr, std::function<bool(const PrimExpr&)> predicate);
static bool StmtContains(const Stmt& stmt, std::function<bool(const PrimExpr&)> predicate);

protected:
// Constructor
explicit CheckContains(std::function<bool(const PrimExpr&)> predicate);

void VisitExpr(const PrimExpr& expr) override;
void VisitStmt(const Stmt& stmt) override;

private:
std::function<bool(const PrimExpr&)> predicate_;
bool contains_it_ = false;
};

} // namespace tir
} // namespace tvm

#endif // TVM_TIR_ANALYSIS_CHECK_CONTAINS_H_
Loading