开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 3

xiongying/Halide

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
main
分支 (1093)
标签 (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
分支 (1093)
标签 (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
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
main
分支 (1093)
标签 (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
/
AddAtomicMutex.cpp
Halide
/
src
/
AddAtomicMutex.cpp
AddAtomicMutex.cpp 15.40 KB
一键复制 编辑 原始数据 按行查看 历史
Andrew Adams 提交于 2022年12月17日 01:56 +08:00 . Explicitly stage strided loads (#7230)
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
#include "AddAtomicMutex.h"
#include "ExprUsesVar.h"
#include "Func.h"
#include "IREquality.h"
#include "IRMutator.h"
#include "IROperator.h"
#include "OutputImageParam.h"
#include <utility>
namespace Halide {
namespace Internal {
using std::map;
using std::set;
using std::string;
namespace {
/** Collect names of all stores matching the producer name inside a statement. */
class CollectProducerStoreNames : public IRGraphVisitor {
public:
CollectProducerStoreNames(const std::string &producer_name)
: producer_name(producer_name) {
}
Scope<void> store_names;
protected:
using IRGraphVisitor::visit;
void visit(const Store *op) override {
IRGraphVisitor::visit(op);
if (op->name == producer_name || starts_with(op->name, producer_name + ".")) {
// This is a Store for the desginated Producer.
store_names.push(op->name);
}
}
const std::string &producer_name;
};
/** Find Store inside of an Atomic node for the designated producer
* and return their indices. */
class FindProducerStoreIndex : public IRGraphVisitor {
public:
FindProducerStoreIndex(const std::string &producer_name)
: producer_name(producer_name) {
}
Expr index; // The returned index.
protected:
using IRGraphVisitor::visit;
// Need to also extract the let bindings of a Store index.
void visit(const Let *op) override {
IRGraphVisitor::visit(op); // Make sure we visit the Store first.
if (index.defined()) {
if (expr_uses_var(index, op->name)) {
index = Let::make(op->name, op->value, index);
}
}
}
void visit(const LetStmt *op) override {
IRGraphVisitor::visit(op); // Make sure we visit the Store first.
if (index.defined()) {
if (expr_uses_var(index, op->name)) {
index = Let::make(op->name, op->value, index);
}
}
}
void visit(const Store *op) override {
IRGraphVisitor::visit(op);
if (op->name == producer_name || starts_with(op->name, producer_name + ".")) {
// This is a Store for the designated producer.
// Ideally we want to insert equal() checks here for different stores,
// but the indices of them actually are different in the case of tuples,
// since they usually refer to the strides/min/extents of their own tuple
// buffers. However, different elements in a tuple would have the same
// strides/min/extents so we are fine.
if (index.defined()) {
return;
}
index = op->index;
}
}
const std::string &producer_name;
};
/** Throws an assertion for cases where the indexing on left-hand-side of
* an atomic update references to itself.
* e.g. f(clamp(f(r), 0, 100)) = f(r) + 1 should be rejected. */
class CheckAtomicValidity : public IRGraphVisitor {
protected:
using IRGraphVisitor::visit;
void visit(const Atomic *op) override {
// Collect the names of all Store nodes inside.
CollectProducerStoreNames collector(op->producer_name);
op->body.accept(&collector);
// Find the indices from the Store nodes inside the body.
FindProducerStoreIndex find(op->producer_name);
op->body.accept(&find);
Expr index = find.index;
if (index.defined()) {
user_assert(!expr_uses_vars(index, collector.store_names))
<< "Can't use atomic() on an update where the index written "
<< "to depends on the current value of the Func\n";
}
op->body.accept(this);
}
};
/** Search if the value of a Store node has a variable pointing to a let binding,
* where the let binding contains the Store location. Use for checking whether
* we need a mutex lock for Atomic since some lowering pass before lifted a let
* binding from the Store node (currently only SplitTuple would do this). */
class FindAtomicLetBindings : public IRGraphVisitor {
public:
FindAtomicLetBindings(const Scope<void> &store_names)
: store_names(store_names) {
}
bool found = false;
protected:
using IRVisitor::visit;
void visit(const Let *op) override {
include(op->value);
{
ScopedBinding<Expr> bind(let_bindings, op->name, op->value);
include(op->body);
}
}
void visit(const LetStmt *op) override {
include(op->value);
{
ScopedBinding<Expr> bind(let_bindings, op->name, op->value);
include(op->body);
}
}
void visit(const Variable *op) override {
if (!inside_store.empty()) {
// If this Variable inside the store value is an expression
// that depends on one of the store_names, we found a lifted let.
if (expr_uses_vars(op, store_names, let_bindings)) {
found = true;
}
}
}
void visit(const Store *op) override {
include(op->predicate);
if (store_names.contains(op->name)) {
// If we are in a designated store and op->value has a let binding
// that uses one of the store_names, we found a lifted let.
ScopedValue<string> old_inside_store(inside_store, op->name);
include(op->value);
} else {
include(op->value);
}
include(op->index);
}
string inside_store;
const Scope<void> &store_names;
Scope<Expr> let_bindings;
};
/** Clear out the Atomic node's mutex usages if it doesn't need one. */
class RemoveUnnecessaryMutexUse : public IRMutator {
public:
set<string> remove_mutex_lock_names;
protected:
using IRMutator::visit;
Stmt visit(const Atomic *op) override {
// Collect the names of all Store nodes inside.
CollectProducerStoreNames collector(op->producer_name);
op->body.accept(&collector);
// Search for let bindings that access the producers.
FindAtomicLetBindings finder(collector.store_names);
op->body.accept(&finder);
// Each individual Store that remains can be done as a CAS
// loop or an actual atomic RMW of some form.
if (finder.found) {
// Can't remove mutex lock. Leave the Stmt as is.
return IRMutator::visit(op);
} else {
remove_mutex_lock_names.insert(op->mutex_name);
Stmt body = mutate(op->body);
return Atomic::make(op->producer_name,
string(),
std::move(body));
}
}
};
/** Find Store inside an Atomic that matches the provided store_names. */
class FindStoreInAtomicMutex : public IRGraphVisitor {
public:
using IRGraphVisitor::visit;
FindStoreInAtomicMutex(const std::set<std::string> &store_names)
: store_names(store_names) {
}
bool found = false;
string producer_name;
string mutex_name;
protected:
void visit(const Atomic *op) override {
if (!found && !op->mutex_name.empty()) {
ScopedValue<bool> old_in_atomic_mutex(in_atomic_mutex, true);
include(op->body);
if (found) {
// We found a Store inside Atomic with matching name,
// record the mutex information.
producer_name = op->producer_name;
mutex_name = op->mutex_name;
}
} else {
include(op->body);
}
}
void visit(const Store *op) override {
if (in_atomic_mutex) {
if (store_names.find(op->name) != store_names.end()) {
found = true;
}
}
IRGraphVisitor::visit(op);
}
bool in_atomic_mutex = false;
const set<string> &store_names;
};
/** Replace the indices in the Store nodes with the specified variable. */
class ReplaceStoreIndexWithVar : public IRMutator {
public:
ReplaceStoreIndexWithVar(const std::string &producer_name, Expr var)
: producer_name(producer_name), var(std::move(var)) {
}
protected:
using IRMutator::visit;
Stmt visit(const Store *op) override {
Expr predicate = mutate(op->predicate);
Expr value = mutate(op->value);
return Store::make(op->name,
std::move(value),
var,
op->param,
std::move(predicate),
op->alignment);
}
const std::string &producer_name;
Expr var;
};
/** Add mutex allocation & lock & unlock if required. */
class AddAtomicMutex : public IRMutator {
public:
AddAtomicMutex(const map<string, Function> &env)
: env(env) {
}
protected:
using IRMutator::visit;
const map<string, Function> &env;
// The set of producers that have allocated a mutex buffer
set<string> allocated_mutexes;
Stmt allocate_mutex(const string &mutex_name, Expr extent, Stmt body) {
Expr mutex_array = Call::make(type_of<halide_mutex_array *>(),
"halide_mutex_array_create",
{std::move(extent)},
Call::Extern);
// Allocate a scalar of halide_mutex_array.
// This generates halide_mutex_array mutex[1];
body = Allocate::make(mutex_name,
Handle(),
MemoryType::Stack,
{},
const_true(),
body,
mutex_array,
"halide_mutex_array_destroy");
return body;
}
Stmt visit(const Allocate *op) override {
// If this Allocate node is allocating a buffer for a producer,
// and there is a Store node inside of an Atomic node requiring mutex lock
// matching the name of the Allocate, allocate a mutex lock.
set<string> store_names{op->name};
FindStoreInAtomicMutex finder(store_names);
op->body.accept(&finder);
if (!finder.found) {
// No Atomic node that requires mutex lock from this node inside.
return IRMutator::visit(op);
}
if (allocated_mutexes.find(finder.mutex_name) != allocated_mutexes.end()) {
// We've already allocated a mutex.
return IRMutator::visit(op);
}
allocated_mutexes.insert(finder.mutex_name);
const string &mutex_name = finder.mutex_name;
Stmt body = mutate(op->body);
Expr extent = Expr(1);
for (const Expr &e : op->extents) {
extent = extent * e;
}
body = allocate_mutex(mutex_name, extent, body);
return Allocate::make(op->name,
op->type,
op->memory_type,
op->extents,
op->condition,
std::move(body),
op->new_expr,
op->free_function,
op->padding);
}
Stmt visit(const ProducerConsumer *op) override {
// Usually we allocate the mutex buffer at the Allocate node,
// but outputs don't have Allocate. For those we allocate the mutex
// buffer at the producer node.
if (!op->is_producer) {
// This is a consumer.
return IRMutator::visit(op);
}
// Find the corresponding output.
auto func_it = env.find(op->name);
if (func_it == env.end()) {
// Not an output.
return IRMutator::visit(op);
}
Func f = Func(func_it->second);
if (f.output_buffers().empty()) {
// Not an output.
return IRMutator::visit(op);
}
set<string> store_names;
for (const auto &buffer : f.output_buffers()) {
store_names.insert(buffer.name());
}
FindStoreInAtomicMutex finder(store_names);
op->body.accept(&finder);
if (!finder.found) {
// No Atomic node that requires mutex lock from this node inside.
return IRMutator::visit(op);
}
if (allocated_mutexes.find(finder.mutex_name) != allocated_mutexes.end()) {
// We've already allocated a mutex.
return IRMutator::visit(op);
}
allocated_mutexes.insert(finder.mutex_name);
// We assume all output buffers in a Tuple have the same extent.
OutputImageParam output_buffer = f.output_buffers()[0];
Expr extent = Expr(1);
for (int i = 0; i < output_buffer.dimensions(); i++) {
extent = extent * output_buffer.dim(i).extent();
}
Stmt body = mutate(op->body);
body = allocate_mutex(finder.mutex_name, extent, body);
return ProducerConsumer::make(op->name, op->is_producer, std::move(body));
}
Stmt visit(const Atomic *op) override {
if (op->mutex_name.empty()) {
return IRMutator::visit(op);
}
// Lock the mutexes using the indices from the Store nodes inside the body.
FindProducerStoreIndex find(op->producer_name);
op->body.accept(&find);
Stmt body = op->body;
Expr index = find.index;
Expr index_let; // If defined, represents the value of the lifted let binding.
if (!index.defined()) {
// Scalar output.
index = Expr(0);
} else {
// Lift the index outside of the atomic node.
// This is for avoiding side-effects inside those expressions
// being evaluated twice.
string name = unique_name('t');
index_let = index;
index = Variable::make(index.type(), name);
body = ReplaceStoreIndexWithVar(op->producer_name, index).mutate(body);
}
// This generates a pointer to the mutex array
Expr mutex_array = Variable::make(
type_of<halide_mutex_array *>(), op->mutex_name);
// Add mutex locks & unlocks
// If a thread locks the mutex and throws an exception,
// halide_mutex_array_destroy will be called and cleanup the mutex locks.
body = Block::make(
Evaluate::make(Call::make(type_of<int>(),
"halide_mutex_array_lock",
{mutex_array, index},
Call::CallType::Extern)),
Block::make(std::move(body),
Evaluate::make(Call::make(type_of<int>(),
"halide_mutex_array_unlock",
{mutex_array, index},
Call::CallType::Extern))));
Stmt ret = Atomic::make(op->producer_name,
op->mutex_name,
std::move(body));
if (index_let.defined()) {
// Attach the let binding outside of the atomic node.
internal_assert(index.as<Variable>() != nullptr);
ret = LetStmt::make(index.as<Variable>()->name, index_let, ret);
}
return ret;
}
};
} // namespace
Stmt add_atomic_mutex(Stmt s, const map<string, Function> &env) {
CheckAtomicValidity check;
s.accept(&check);
s = RemoveUnnecessaryMutexUse().mutate(s);
s = AddAtomicMutex(env).mutate(s);
return s;
}
} // namespace Internal
} // namespace Halide
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

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

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

取消
提交

简介

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

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/VisionDeveloper/Halide.git
git@gitee.com:VisionDeveloper/Halide.git
VisionDeveloper
Halide
Halide
main
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

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