Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 3

xiongying/Halide

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
main
Branches (1093)
Tags (17)
main
xtensa-codegen
vksnk/dma-limit-channels
rootjalex/trs-codegen-cross
abadams/fix_7374
abadams/remove_hack_from_gpu_only_aottest
srj/gpu-cache
srj/generator_aot_gpu_multi_context_threaded
srj/xtensa-merge
abadams/vector_scan
abadams/fix_7365
darya-ver/ir-viz
vulkan-phase2-runtime
srj/param-map-deprecation
srj/rt-return-types
srj/main-vs2022
release/15.x
srj/param-map
abadams/ir_builder_unique_ptr
vksnk/restrict
v14.0.0
v13.0.4
v13.0.3
v13.0.2
v13.0.1
v13.0.0
v12.0.1
v12.0.0
v11.0.1
v11.0.0
v10.0.1
v10.0.0
release_2019_08_27
release_8.0.0
v8.0.0
release_2018_02_15
release_2013_11_11
main
Branches (1093)
Tags (17)
main
xtensa-codegen
vksnk/dma-limit-channels
rootjalex/trs-codegen-cross
abadams/fix_7374
abadams/remove_hack_from_gpu_only_aottest
srj/gpu-cache
srj/generator_aot_gpu_multi_context_threaded
srj/xtensa-merge
abadams/vector_scan
abadams/fix_7365
darya-ver/ir-viz
vulkan-phase2-runtime
srj/param-map-deprecation
srj/rt-return-types
srj/main-vs2022
release/15.x
srj/param-map
abadams/ir_builder_unique_ptr
vksnk/restrict
v14.0.0
v13.0.4
v13.0.3
v13.0.2
v13.0.1
v13.0.0
v12.0.1
v12.0.0
v11.0.1
v11.0.0
v10.0.1
v10.0.0
release_2019_08_27
release_8.0.0
v8.0.0
release_2018_02_15
release_2013_11_11
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
main
Branches (1093)
Tags (17)
main
xtensa-codegen
vksnk/dma-limit-channels
rootjalex/trs-codegen-cross
abadams/fix_7374
abadams/remove_hack_from_gpu_only_aottest
srj/gpu-cache
srj/generator_aot_gpu_multi_context_threaded
srj/xtensa-merge
abadams/vector_scan
abadams/fix_7365
darya-ver/ir-viz
vulkan-phase2-runtime
srj/param-map-deprecation
srj/rt-return-types
srj/main-vs2022
release/15.x
srj/param-map
abadams/ir_builder_unique_ptr
vksnk/restrict
v14.0.0
v13.0.4
v13.0.3
v13.0.2
v13.0.1
v13.0.0
v12.0.1
v12.0.0
v11.0.1
v11.0.0
v10.0.1
v10.0.0
release_2019_08_27
release_8.0.0
v8.0.0
release_2018_02_15
release_2013_11_11
Halide
/
src
/
SimplifyCorrelatedDifferences.cpp
Halide
/
src
/
SimplifyCorrelatedDifferences.cpp
SimplifyCorrelatedDifferences.cpp 10.26 KB
Copy Edit Raw Blame History
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
#include "SimplifyCorrelatedDifferences.h"
#include "CSE.h"
#include "CompilerLogger.h"
#include "ExprUsesVar.h"
#include "IRMatch.h"
#include "IRMutator.h"
#include "IROperator.h"
#include "Monotonic.h"
#include "Scope.h"
#include "Simplify.h"
#include "Solve.h"
#include "Substitute.h"
namespace Halide {
namespace Internal {
namespace {
using std::string;
using std::vector;
class PartiallyCancelDifferences : public IRMutator {
using IRMutator::visit;
// Symbols used by rewrite rules
IRMatcher::Wild<0> x;
IRMatcher::Wild<1> y;
IRMatcher::Wild<2> z;
IRMatcher::WildConst<0> c0;
IRMatcher::WildConst<1> c1;
Expr visit(const Sub *op) override {
Expr a = mutate(op->a), b = mutate(op->b);
// Partially cancel terms in correlated differences of
// various kinds to get tighter bounds. We assume any
// correlated term has already been pulled leftmost by
// solve_expression.
if (op->type == Int(32)) {
auto rewrite = IRMatcher::rewriter(IRMatcher::sub(a, b), op->type);
if (
// Differences of quasi-affine functions
rewrite((x + y) / c0 - (x + z) / c0, ((x % c0) + y) / c0 - ((x % c0) + z) / c0) ||
rewrite(x / c0 - (x + z) / c0, 0 - ((x % c0) + z) / c0) ||
rewrite((x + y) / c0 - x / c0, ((x % c0) + y) / c0) ||
// truncated cones have a constant upper or lower
// bound that isn't apparent when expressed in the
// form in the LHS below
rewrite(min(x, c0) - max(x, c1), min(min(c0 - x, x - c1), fold(min(0, c0 - c1)))) ||
rewrite(max(x, c0) - min(x, c1), max(max(c0 - x, x - c1), fold(max(0, c0 - c1)))) ||
rewrite(min(x, y) - max(x, z), min(min(x, y) - max(x, z), 0)) ||
rewrite(max(x, y) - min(x, z), max(max(x, y) - min(x, z), 0)) ||
rewrite(min(x + c0, y) - select(z, min(x, y) + c1, x), select(z, (max(min(y - x, c0), 0) - c1), min(y - x, c0)), c0 > 0) ||
rewrite(min(y, x + c0) - select(z, min(y, x) + c1, x), select(z, (max(min(y - x, c0), 0) - c1), min(y - x, c0)), c0 > 0) ||
false) {
return rewrite.result;
}
}
return a - b;
}
};
class SimplifyCorrelatedDifferences : public IRMutator {
using IRMutator::visit;
string loop_var;
Scope<ConstantInterval> monotonic;
struct OuterLet {
string name;
Expr value;
bool may_substitute;
};
vector<OuterLet> lets;
template<typename LetStmtOrLet, typename StmtOrExpr>
StmtOrExpr visit_let(const LetStmtOrLet *op) {
// Visit an entire chain of lets in a single method to conserve stack space.
struct Frame {
const LetStmtOrLet *op;
ScopedBinding<ConstantInterval> binding;
Expr new_value;
Frame(const LetStmtOrLet *op, const string &loop_var, Scope<ConstantInterval> &scope)
: op(op),
binding(scope, op->name, derivative_bounds(op->value, loop_var, scope)) {
}
Frame(const LetStmtOrLet *op)
: op(op) {
}
};
std::vector<Frame> frames;
StmtOrExpr result;
// Note that we must add *everything* that depends on the loop
// var to the monotonic scope and the list of lets, even
// things which we can never substitute in (e.g. impure
// things). This is for two reasons. First this pass could be
// used at a time when we still have nested lets under the
// same name. If we decide not to add an inner let, but do add
// the outer one, then later references to it will be
// incorrect. Second, if we don't add something that happens
// to be non-monotonic, then derivative_bounds finds a variable
// that references it in a later let, it will think it's a
// constant, not an unknown.
do {
result = op->body;
if (loop_var.empty()) {
frames.emplace_back(op);
continue;
}
bool pure = is_pure(op->value);
if (!pure || expr_uses_vars(op->value, monotonic) || monotonic.contains(op->name)) {
frames.emplace_back(op, loop_var, monotonic);
Expr new_value = mutate(op->value);
bool may_substitute_in = new_value.type() == Int(32) && pure;
lets.emplace_back(OuterLet{op->name, new_value, may_substitute_in});
frames.back().new_value = std::move(new_value);
} else {
// Pure and constant w.r.t the loop var. Doesn't
// shadow any outer thing already in the monotonic
// scope.
frames.emplace_back(op);
}
} while ((op = result.template as<LetStmtOrLet>()));
result = mutate(result);
for (auto it = frames.rbegin(); it != frames.rend(); it++) {
if (it->new_value.defined()) {
result = LetStmtOrLet::make(it->op->name, it->new_value, result);
} else {
result = LetStmtOrLet::make(it->op->name, it->op->value, result);
}
if (it->binding.bound()) {
lets.pop_back();
}
}
return result;
}
Expr visit(const Let *op) override {
return visit_let<Let, Expr>(op);
}
Stmt visit(const LetStmt *op) override {
return visit_let<LetStmt, Stmt>(op);
}
Stmt visit(const For *op) override {
Stmt s = op;
// This is unfortunately quadratic in maximum loop nesting depth
if (loop_var.empty()) {
decltype(monotonic) tmp_monotonic;
decltype(lets) tmp_lets;
tmp_monotonic.swap(monotonic);
tmp_lets.swap(lets);
loop_var = op->name;
{
ScopedBinding<ConstantInterval> bind(monotonic, loop_var, ConstantInterval::single_point(1));
s = IRMutator::visit(op);
}
loop_var.clear();
tmp_monotonic.swap(monotonic);
tmp_lets.swap(lets);
}
s = IRMutator::visit(s.as<For>());
return s;
}
Expr cancel_correlated_subexpression(Expr e, const Expr &a, const Expr &b, bool correlated) {
auto ma = is_monotonic(a, loop_var, monotonic);
auto mb = is_monotonic(b, loop_var, monotonic);
if ((ma == Monotonic::Increasing && mb == Monotonic::Increasing && correlated) ||
(ma == Monotonic::Decreasing && mb == Monotonic::Decreasing && correlated) ||
(ma == Monotonic::Increasing && mb == Monotonic::Decreasing && !correlated) ||
(ma == Monotonic::Decreasing && mb == Monotonic::Increasing && !correlated)) {
for (auto it = lets.rbegin(); it != lets.rend(); it++) {
if (expr_uses_var(e, it->name)) {
if (!it->may_substitute) {
// We have to stop here. Can't continue
// because there might be an outer let with
// the same name that we *can* substitute in,
// and then inner uses will get the wrong
// value.
break;
}
}
e = Let::make(it->name, it->value, e);
}
e = common_subexpression_elimination(e);
e = solve_expression(e, loop_var).result;
e = PartiallyCancelDifferences().mutate(e);
e = simplify(e);
const bool check_non_monotonic = debug::debug_level() > 0 || get_compiler_logger() != nullptr;
if (check_non_monotonic &&
is_monotonic(e, loop_var) == Monotonic::Unknown) {
// Might be a missed simplification opportunity. Log to help improve the simplifier.
if (get_compiler_logger()) {
get_compiler_logger()->record_non_monotonic_loop_var(loop_var, e);
}
debug(1) << "Warning: expression is non-monotonic in loop variable "
<< loop_var << ": " << e << "\n";
}
}
return e;
}
template<typename T>
Expr visit_binop(const T *op, bool correlated) {
Expr e = IRMutator::visit(op);
op = e.as<T>();
if (op == nullptr ||
op->a.type() != Int(32) ||
loop_var.empty()) {
return e;
} else {
// Bury the logic that doesn't depend on the template
// parameter in a separate function to save code size and
// reduce stack frame size in the recursive path.
return cancel_correlated_subexpression(e, op->a, op->b, correlated);
}
}
// Binary ops where it pays to cancel a correlated term on both
// sides. E.g. consider the x in:
//
// (x*3 + y)*2 - max(x*6, 0)))
//
// Both sides increase monotonically with x so interval arithmetic
// will overestimate the bounds. If we subtract x*6 from both
// sides we get:
//
// y*2 - max(0, x*-6)
//
// Now only one side depends on x and interval arithmetic becomes
// exact.
Expr visit(const Sub *op) override {
return visit_binop(op, true);
}
Expr visit(const LT *op) override {
return visit_binop(op, true);
}
Expr visit(const LE *op) override {
return visit_binop(op, true);
}
Expr visit(const GT *op) override {
return visit_binop(op, true);
}
Expr visit(const GE *op) override {
return visit_binop(op, true);
}
Expr visit(const EQ *op) override {
return visit_binop(op, true);
}
Expr visit(const NE *op) override {
return visit_binop(op, true);
}
// For add you actually want to cancel any anti-correlated term
// (e.g. x in (x*3 + y)*2 + max(x*-6, 0))
Expr visit(const Add *op) override {
return visit_binop(op, false);
}
};
} // namespace
Stmt simplify_correlated_differences(const Stmt &stmt) {
return SimplifyCorrelatedDifferences().mutate(stmt);
}
Expr bound_correlated_differences(const Expr &expr) {
return PartiallyCancelDifferences().mutate(expr);
}
} // namespace Internal
} // namespace Halide
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

MIT计算机科学和人工智能实验室的研究人员创造出一种专门设计简化图像处理的程序语言Halide,源代码托管在GitHub上,目前二进制程序只支持Mac OS X和Ubuntu 12
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/VisionDeveloper/Halide.git
git@gitee.com:VisionDeveloper/Halide.git
VisionDeveloper
Halide
Halide
main
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /