开源 企业版 高校版 私有云 模力方舟 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
/
InjectHostDevBufferCopies.cpp
Halide
/
src
/
InjectHostDevBufferCopies.cpp
InjectHostDevBufferCopies.cpp 29.08 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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
#include "InjectHostDevBufferCopies.h"
#include "CodeGen_GPU_Dev.h"
#include "Debug.h"
#include "ExternFuncArgument.h"
#include "IRMutator.h"
#include "IROperator.h"
#include "IRPrinter.h"
#include "Substitute.h"
#include <map>
#include <utility>
namespace Halide {
namespace Internal {
using std::set;
using std::string;
using std::vector;
Stmt call_extern_and_assert(const string &name, const vector<Expr> &args) {
Expr call = Call::make(Int(32), name, args, Call::Extern);
string call_result_name = unique_name(name + "_result");
Expr call_result_var = Variable::make(Int(32), call_result_name);
return LetStmt::make(call_result_name, call,
AssertStmt::make(EQ::make(call_result_var, 0), call_result_var));
}
namespace {
class FindBufferUsage : public IRVisitor {
using IRVisitor::visit;
void visit(const Load *op) override {
IRVisitor::visit(op);
if (op->name == buffer) {
devices_touched.insert(current_device_api);
}
}
void visit(const Store *op) override {
IRVisitor::visit(op);
if (op->name == buffer) {
devices_touched.insert(current_device_api);
devices_writing.insert(current_device_api);
}
}
bool is_buffer_var(const Expr &e) {
const Variable *var = e.as<Variable>();
return var && (var->name == buffer + ".buffer");
}
void visit(const Call *op) override {
if (op->is_intrinsic(Call::image_load)) {
internal_assert(!op->args.empty());
if (is_buffer_var(op->args[1])) {
devices_touched.insert(current_device_api);
}
for (size_t i = 0; i < op->args.size(); i++) {
if (i == 1) {
continue;
}
op->args[i].accept(this);
}
} else if (op->is_intrinsic(Call::image_store)) {
internal_assert(!op->args.empty());
if (is_buffer_var(op->args[1])) {
devices_touched.insert(current_device_api);
devices_writing.insert(current_device_api);
}
for (size_t i = 0; i < op->args.size(); i++) {
if (i == 1) {
continue;
}
op->args[i].accept(this);
}
} else if (op->is_intrinsic(Call::debug_to_file)) {
internal_assert(op->args.size() == 3);
if (is_buffer_var(op->args[2])) {
devices_touched.insert(current_device_api);
devices_writing.insert(current_device_api);
}
} else if (op->is_extern() && op->func.defined()) {
// This is a call to an extern stage
Function f(op->func);
internal_assert((f.extern_arguments().size() + f.outputs()) == op->args.size())
<< "Mismatch between args size and extern_arguments size in call to "
<< op->name << "\n";
// Check each buffer arg
for (size_t i = 0; i < op->args.size(); i++) {
if (is_buffer_var(op->args[i])) {
DeviceAPI extern_device_api = f.extern_function_device_api();
devices_touched_by_extern.insert(extern_device_api);
if (i >= f.extern_arguments().size()) {
// An output. The extern stage is responsible
// for dealing with any device transitions for
// inputs.
devices_touched.insert(extern_device_api);
devices_writing.insert(extern_device_api);
}
} else {
op->args[i].accept(this);
}
}
} else {
IRVisitor::visit(op);
}
}
void visit(const For *op) override {
internal_assert(op->device_api != DeviceAPI::Default_GPU)
<< "A GPU API should have been selected by this stage in lowering\n";
DeviceAPI old = current_device_api;
if (op->device_api != DeviceAPI::None) {
current_device_api = op->device_api;
}
IRVisitor::visit(op);
current_device_api = old;
}
string buffer;
DeviceAPI current_device_api;
public:
std::set<DeviceAPI> devices_writing, devices_touched;
// Any buffer passed to an extern stage may have had its dirty
// bits and device allocation messed with.
std::set<DeviceAPI> devices_touched_by_extern;
FindBufferUsage(const std::string &buf, DeviceAPI d)
: buffer(buf), current_device_api(d) {
}
};
// Inject the device copies, mallocs, and dirty flag setting for a
// single allocation. Sticks to the same loop level as the original
// allocation and treats the stmt as a serial sequence of leaf
// stmts. We walk this sequence of leaves, tracking what we know about
// the buffer as we go, sniffing usage within each leaf using
// FindBufferUsage, and injecting device buffer logic as needed.
class InjectBufferCopiesForSingleBuffer : public IRMutator {
using IRMutator::visit;
// The buffer being managed
const string buffer;
const bool is_external;
MemoryType memory_type;
enum FlagState {
Unknown,
False,
True
};
struct State {
// What do we know about the dirty flags and the existence of a device allocation?
FlagState device_dirty = Unknown, host_dirty = Unknown, device_allocation_exists = Unknown;
// If it exists on a known device API, which device does it exist
// on? Meaningless if device_allocation_exists is not True.
DeviceAPI current_device = DeviceAPI::None;
void union_with(const State &other) {
if (device_dirty != other.device_dirty) {
device_dirty = Unknown;
}
if (host_dirty != other.host_dirty) {
host_dirty = Unknown;
}
if (device_allocation_exists != other.device_allocation_exists ||
other.current_device != current_device) {
device_allocation_exists = Unknown;
current_device = DeviceAPI::None;
}
}
} state;
Expr buffer_var() {
return Variable::make(type_of<struct halide_buffer_t *>(), buffer + ".buffer");
}
Stmt make_device_malloc(DeviceAPI target_device_api) {
Expr device_interface = make_device_interface_call(target_device_api, memory_type);
Stmt device_malloc = call_extern_and_assert("halide_device_malloc",
{buffer_var(), device_interface});
return device_malloc;
}
Stmt make_copy_to_host() {
return call_extern_and_assert("halide_copy_to_host", {buffer_var()});
}
Stmt make_copy_to_device(DeviceAPI target_device_api) {
Expr device_interface = make_device_interface_call(target_device_api, memory_type);
return call_extern_and_assert("halide_copy_to_device", {buffer_var(), device_interface});
}
Stmt make_host_dirty() {
return Evaluate::make(Call::make(Int(32), Call::buffer_set_host_dirty,
{buffer_var(), const_true()}, Call::Extern));
}
Stmt make_device_dirty() {
return Evaluate::make(Call::make(Int(32), Call::buffer_set_device_dirty,
{buffer_var(), const_true()}, Call::Extern));
}
Stmt make_device_free() {
return call_extern_and_assert("halide_device_free", {buffer_var()});
}
Stmt do_copies(Stmt s) {
// Sniff what happens to the buffer inside the stmt
FindBufferUsage finder(buffer, DeviceAPI::Host);
s.accept(&finder);
// Insert any appropriate copies/allocations before, and set
// dirty flags after. Do not recurse into the stmt.
// First figure out what happened
bool touched_on_host = finder.devices_touched.count(DeviceAPI::Host);
bool touched_on_device = finder.devices_touched.size() > (touched_on_host ? 1 : 0);
bool written_on_host = finder.devices_writing.count(DeviceAPI::Host);
bool written_on_device = finder.devices_writing.size() > (touched_on_host ? 1 : 0);
DeviceAPI touching_device = DeviceAPI::None;
for (DeviceAPI d : finder.devices_touched) {
// TODO: looks dubious, but removing causes crashes in correctness_debug_to_file
// with target=host-metal.
if (d == DeviceAPI::Host) {
continue;
}
internal_assert(touching_device == DeviceAPI::None)
<< "Buffer " << buffer << " was touched on multiple devices within a single leaf Stmt!\n";
touching_device = d;
}
// Then figure out what to do
bool needs_device_malloc = (touched_on_device &&
(state.device_allocation_exists != True));
bool needs_device_flip = (state.device_allocation_exists != False &&
state.current_device != touching_device &&
state.current_device != DeviceAPI::None &&
touching_device != DeviceAPI::None &&
!is_external);
// TODO: If only written on device, and entirely clobbered on
// device, a copy-to-device is not actually necessary.
bool needs_copy_to_device = (touched_on_device &&
((state.host_dirty != False) ||
needs_device_flip));
if (needs_copy_to_device) {
// halide_copy_to_device already does a halide_device_malloc if necessary.
needs_device_malloc = false;
}
// Device flips go via host memory
bool needs_copy_to_host = ((touched_on_host || needs_device_flip) &&
(state.device_dirty != False));
bool needs_host_dirty = (written_on_host &&
(state.host_dirty != True));
bool needs_device_dirty = (written_on_device &&
(state.device_dirty != True));
vector<Stmt> stmts;
// Then do it, updating what we know about the buffer
if (needs_copy_to_host) {
stmts.push_back(make_copy_to_host());
state.device_dirty = False;
}
// When flipping a buffer between devices, we need to free the
// old device memory before allocating the new one.
if (needs_device_flip) {
stmts.push_back(make_host_dirty());
stmts.push_back(make_device_free());
state.device_allocation_exists = False;
state.device_dirty = False;
}
if (needs_copy_to_device) {
stmts.push_back(make_copy_to_device(touching_device));
state.host_dirty = False;
state.device_allocation_exists = True;
state.current_device = touching_device;
}
if (needs_device_malloc) {
stmts.push_back(make_device_malloc(touching_device));
state.device_allocation_exists = True;
state.current_device = touching_device;
}
stmts.push_back(s);
if (needs_host_dirty) {
stmts.push_back(make_host_dirty());
state.host_dirty = True;
}
if (needs_device_dirty) {
stmts.push_back(make_device_dirty());
state.device_dirty = True;
}
s = Block::make(stmts);
if (!finder.devices_touched_by_extern.empty()) {
// This buffer was passed to an extern stage. Unless we
// explicitly marked it after the stmt ran, we no longer
// know the state of the dirty bits.
if (!needs_host_dirty) {
state.host_dirty = Unknown;
}
if (!needs_device_dirty) {
state.device_dirty = Unknown;
}
// Also, the extern stage may have gifted a host
// allocation, or flipped the buffer to another device.
state.device_allocation_exists = Unknown;
state.current_device = DeviceAPI::None;
}
return s;
}
// We want to break things down into a serial sequence of leaf
// stmts, and possibly do copies and update state around each
// leaf.
Stmt visit(const For *op) override {
FindBufferUsage finder(buffer, DeviceAPI::Host);
op->accept(&finder);
if (finder.devices_touched.size() > 1) {
// The state of the buffer going into the loop is the
// union of the state before the loop starts and the state
// after one iteration. Just forget everything we know.
state = State{};
Stmt s = IRMutator::visit(op);
// The state after analyzing the loop body might not be the
// true state if the loop ran for zero iterations. So
// forget everything again.
state = State{};
return s;
} else {
return do_copies(op);
}
}
Stmt visit(const Fork *op) override {
return do_copies(op);
}
Stmt visit(const Evaluate *op) override {
return do_copies(op);
}
Stmt visit(const LetStmt *op) override {
// If op->value uses the buffer, we need to treat this as a
// single leaf. Otherwise we can recurse.
FindBufferUsage finder(buffer, DeviceAPI::Host);
op->value.accept(&finder);
if (finder.devices_touched.empty() &&
finder.devices_touched_by_extern.empty()) {
return IRMutator::visit(op);
} else {
return do_copies(op);
}
}
Stmt visit(const AssertStmt *op) override {
return do_copies(op);
}
// Check if a stmt has any for loops (and hence possible device
// transitions).
class HasLoops : public IRVisitor {
using IRVisitor::visit;
void visit(const For *op) override {
result = true;
}
public:
bool result = false;
};
Stmt visit(const Block *op) override {
// If both sides of the block have no loops (and hence no
// device transitions), treat it as a single leaf. This stops
// host dirties from getting in between blocks of store stmts
// that could be interleaved.
HasLoops loops;
op->accept(&loops);
if (loops.result) {
return IRMutator::visit(op);
} else {
return do_copies(op);
}
}
Stmt visit(const Store *op) override {
return do_copies(op);
}
Stmt visit(const IfThenElse *op) override {
State old = state;
Stmt then_case = mutate(op->then_case);
State then_state = state;
state = old;
Stmt else_case = mutate(op->else_case);
state.union_with(then_state);
return IfThenElse::make(op->condition, then_case, else_case);
}
public:
InjectBufferCopiesForSingleBuffer(const std::string &b, bool e, MemoryType m)
: buffer(b), is_external(e), memory_type(m) {
if (is_external) {
// The state of the buffer is totally unknown, which is
// the default constructor for this->state
} else {
// This is a fresh allocation
state.device_allocation_exists = False;
state.device_dirty = False;
state.host_dirty = False;
state.current_device = DeviceAPI::None;
}
}
};
// Find the last use of a given buffer, which will used later for injecting
// device free calls.
class FindLastUse : public IRVisitor {
public:
Stmt last_use;
FindLastUse(const string &b)
: buffer(b) {
}
private:
string buffer;
using IRVisitor::visit;
void check_and_record_last_use(const Stmt &s) {
// Sniff what happens to the buffer inside the stmt
FindBufferUsage finder(buffer, DeviceAPI::Host);
s.accept(&finder);
if (!finder.devices_touched.empty() ||
!finder.devices_touched_by_extern.empty()) {
last_use = s;
}
}
// We break things down into a serial sequence of leaf
// stmts similar to InjectBufferCopiesForSingleBuffer.
void visit(const For *op) override {
check_and_record_last_use(op);
}
void visit(const Fork *op) override {
check_and_record_last_use(op);
}
void visit(const Evaluate *op) override {
check_and_record_last_use(op);
}
void visit(const LetStmt *op) override {
// If op->value uses the buffer, we need to treat this as a
// single leaf. Otherwise we can recurse.
FindBufferUsage finder(buffer, DeviceAPI::Host);
op->value.accept(&finder);
if (finder.devices_touched.empty() &&
finder.devices_touched_by_extern.empty()) {
IRVisitor::visit(op);
} else {
check_and_record_last_use(op);
}
}
void visit(const AssertStmt *op) override {
check_and_record_last_use(op);
}
void visit(const Store *op) override {
check_and_record_last_use(op);
}
void visit(const IfThenElse *op) override {
check_and_record_last_use(op);
}
};
// Inject the buffer-handling logic for all internal
// allocations. Inputs and outputs are handled below.
class InjectBufferCopies : public IRMutator {
using IRMutator::visit;
// Inject the registration of a device destructor just after the
// .buffer symbol is defined (which is safely before the first
// device_malloc).
class InjectDeviceDestructor : public IRMutator {
using IRMutator::visit;
Stmt visit(const LetStmt *op) override {
if (op->name == buffer) {
Expr buf = Variable::make(type_of<struct halide_buffer_t *>(), buffer);
Stmt destructor =
Evaluate::make(Call::make(Handle(), Call::register_destructor,
{Expr("halide_device_free_as_destructor"), buf}, Call::Intrinsic));
Stmt body = Block::make(destructor, op->body);
return LetStmt::make(op->name, op->value, body);
} else {
return IRMutator::visit(op);
}
}
string buffer;
public:
InjectDeviceDestructor(string b)
: buffer(std::move(b)) {
}
};
// Find the let stmt that defines the .buffer and insert inside of
// it a combined host/dev allocation, a destructor registration,
// and an Allocate node that takes its host field from the
// .buffer.
class InjectCombinedAllocation : public IRMutator {
using IRMutator::visit;
Stmt visit(const LetStmt *op) override {
if (op->name == buffer + ".buffer") {
Expr buf = Variable::make(type_of<struct halide_buffer_t *>(), buffer + ".buffer");
Stmt body = op->body;
// The allocate node is innermost
Expr host = Call::make(Handle(), Call::buffer_get_host, {buf}, Call::Extern);
body = Allocate::make(buffer, type, MemoryType::Heap, extents, condition, body,
host, "halide_device_host_nop_free");
// Then the destructor
Stmt destructor =
Evaluate::make(Call::make(Handle(), Call::register_destructor,
{Expr("halide_device_and_host_free_as_destructor"), buf},
Call::Intrinsic));
body = Block::make(destructor, body);
// Then the device_and_host malloc
Stmt device_malloc = call_extern_and_assert("halide_device_and_host_malloc",
{buf, device_interface});
if (!is_const_one(condition)) {
device_malloc = IfThenElse::make(condition, device_malloc);
}
body = Block::make(device_malloc, body);
// In the value, we want to use null for the initial value of the host field.
Expr value = substitute(buffer, reinterpret(Handle(), make_zero(UInt(64))), op->value);
// Rewrap the letstmt
return LetStmt::make(op->name, value, body);
} else {
return IRMutator::visit(op);
}
}
string buffer;
Type type;
vector<Expr> extents;
Expr condition;
Expr device_interface;
public:
InjectCombinedAllocation(string b, Type t, vector<Expr> e, Expr c, Expr d)
: buffer(std::move(b)), type(t), extents(std::move(e)),
condition(std::move(c)), device_interface(std::move(d)) {
}
};
class FreeAfterLastUse : public IRMutator {
Stmt last_use;
Stmt free_stmt;
public:
bool success = false;
using IRMutator::mutate;
Stmt mutate(const Stmt &s) override {
if (s.same_as(last_use)) {
internal_assert(!success);
success = true;
return Block::make(last_use, free_stmt);
} else {
return IRMutator::mutate(s);
}
}
FreeAfterLastUse(Stmt s, Stmt f)
: last_use(std::move(s)), free_stmt(std::move(f)) {
}
};
Stmt visit(const Allocate *op) override {
FindBufferUsage finder(op->name, DeviceAPI::Host);
op->body.accept(&finder);
bool touched_on_host = finder.devices_touched.count(DeviceAPI::Host);
bool touched_on_device = finder.devices_touched.size() > (touched_on_host ? 1 : 0);
if (!touched_on_device && finder.devices_touched_by_extern.empty()) {
// Boring.
return IRMutator::visit(op);
}
Stmt body = mutate(op->body);
InjectBufferCopiesForSingleBuffer injector(op->name, false, op->memory_type);
body = injector.mutate(body);
string buffer_name = op->name + ".buffer";
Expr buffer = Variable::make(Handle(), buffer_name);
// Device what type of allocation to make.
if (touched_on_host && finder.devices_touched.size() == 2) {
// Touched on a single device and the host. Use a combined allocation.
DeviceAPI touching_device = DeviceAPI::None;
for (DeviceAPI d : finder.devices_touched) {
if (d != DeviceAPI::Host) {
touching_device = d;
}
}
// Make a device_and_host_free stmt
FindLastUse last_use(op->name);
body.accept(&last_use);
if (last_use.last_use.defined()) {
Stmt device_free = call_extern_and_assert("halide_device_and_host_free", {buffer});
FreeAfterLastUse free_injecter(last_use.last_use, device_free);
body = free_injecter.mutate(body);
internal_assert(free_injecter.success);
}
Expr device_interface = make_device_interface_call(touching_device, op->memory_type);
return InjectCombinedAllocation(op->name, op->type, op->extents,
op->condition, device_interface)
.mutate(body);
} else {
// Only touched on host but passed to an extern stage, or
// only touched on device, or touched on multiple
// devices. Do separate device and host allocations.
// Add a device destructor
body = InjectDeviceDestructor(buffer_name).mutate(body);
// Make a device_free stmt
FindLastUse last_use(op->name);
body.accept(&last_use);
if (last_use.last_use.defined()) {
Stmt device_free = call_extern_and_assert("halide_device_free", {buffer});
FreeAfterLastUse free_injecter(last_use.last_use, device_free);
body = free_injecter.mutate(body);
internal_assert(free_injecter.success);
}
Expr condition = op->condition;
bool touched_on_one_device = !touched_on_host && finder.devices_touched.size() == 1 &&
(finder.devices_touched_by_extern.empty() ||
(finder.devices_touched_by_extern.size() == 1 &&
*(finder.devices_touched.begin()) == *(finder.devices_touched_by_extern.begin())));
if (touched_on_one_device) {
condition = const_false();
// There's no host allocation, so substitute any
// references to it (e.g. the one in the make_buffer
// call) with NULL.
body = substitute(op->name, reinterpret(Handle(), make_zero(UInt(64))), body);
}
return Allocate::make(op->name, op->type, op->memory_type, op->extents,
condition, body, op->new_expr, op->free_function, op->padding);
}
}
Stmt visit(const For *op) override {
if (op->device_api != DeviceAPI::Host &&
op->device_api != DeviceAPI::None) {
// Don't enter device loops
return op;
} else {
return IRMutator::visit(op);
}
}
};
// Find the site in the IR where we want to inject the copies/dirty
// flags for the inputs and outputs. It's the innermost IR node that
// contains all ProducerConsumer nodes. Often this is the outermost
// ProducerConsumer node. Sometimes it's a Block containing a pair of
// them.
class FindOutermostProduce : public IRVisitor {
using IRVisitor::visit;
void visit(const Block *op) override {
op->first.accept(this);
if (result.defined()) {
result = op;
} else {
op->rest.accept(this);
}
}
void visit(const ProducerConsumer *op) override {
result = op;
}
public:
Stmt result;
};
// Inject the buffer handling code for the inputs and outputs at the
// appropriate site.
class InjectBufferCopiesForInputsAndOutputs : public IRMutator {
Stmt site;
// Find all references to external buffers.
class FindInputsAndOutputs : public IRVisitor {
using IRVisitor::visit;
void include(const Parameter &p) {
if (p.defined()) {
result.insert(p.name());
result_storage[p.name()] = p.memory_type();
}
}
void include(const Buffer<> &b) {
if (b.defined()) {
result.insert(b.name());
result_storage[b.name()] = MemoryType::Auto;
}
}
void visit(const Variable *op) override {
include(op->param);
include(op->image);
}
void visit(const Load *op) override {
include(op->param);
include(op->image);
IRVisitor::visit(op);
}
void visit(const Store *op) override {
include(op->param);
IRVisitor::visit(op);
}
void visit(const Call *op) override {
// We shouldn't need to look for Buffers here,
// since we expect this to be run after StorageFlattening.
// Add an assertion check just in case a change to lowering ever
// subverts this ordering expectation.
internal_assert(op->call_type != Call::Halide &&
op->call_type != Call::Image);
IRVisitor::visit(op);
}
public:
set<string> result;
std::map<string, MemoryType> result_storage;
};
public:
using IRMutator::mutate;
Stmt mutate(const Stmt &s) override {
if (s.same_as(site)) {
FindInputsAndOutputs finder;
s.accept(&finder);
Stmt new_stmt = s;
for (const string &buf : finder.result) {
new_stmt = InjectBufferCopiesForSingleBuffer(buf, true, finder.result_storage.at(buf)).mutate(new_stmt);
}
return new_stmt;
} else {
return IRMutator::mutate(s);
}
}
InjectBufferCopiesForInputsAndOutputs(Stmt s)
: site(std::move(s)) {
}
};
} // namespace
Stmt inject_host_dev_buffer_copies(Stmt s, const Target &t) {
// Hexagon code assumes that the host-based wrapper code
// handles all copies to/from device, so this isn't necessary;
// furthermore, we would actually generate wrong code by proceeding
// here, as this implementation assumes we start from the host (which
// isn't true for Hexagon), and that it's safe to inject calls to copy
// and/or mark things dirty (which also isn't true for Hexagon).
if (t.arch == Target::Hexagon) {
return s;
}
// Handle internal allocations
s = InjectBufferCopies().mutate(s);
// Handle inputs and outputs
FindOutermostProduce outermost;
s.accept(&outermost);
if (outermost.result.defined()) {
// If the entire pipeline simplified away, or just dispatches
// to another pipeline, there may be no outermost produce.
s = InjectBufferCopiesForInputsAndOutputs(outermost.result).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 によって変換されたページ (->オリジナル) /