开源 企业版 高校版 私有云 模力方舟 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
/
CodeGen_PTX_Dev.cpp
Halide
/
src
/
CodeGen_PTX_Dev.cpp
CodeGen_PTX_Dev.cpp 30.35 KB
一键复制 编辑 原始数据 按行查看 历史
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
#include "CodeGen_PTX_Dev.h"
#include "CSE.h"
#include "CodeGen_GPU_Dev.h"
#include "CodeGen_Internal.h"
#include "CodeGen_LLVM.h"
#include "ConciseCasts.h"
#include "Debug.h"
#include "ExprUsesVar.h"
#include "IREquality.h"
#include "IRMatch.h"
#include "IRMutator.h"
#include "IROperator.h"
#include "IRPrinter.h"
#include "LLVM_Headers.h"
#include "LLVM_Runtime_Linker.h"
#include "Simplify.h"
#include "Solve.h"
#include "Target.h"
#include <fstream>
// This is declared in NVPTX.h, which is not exported. Ugly, but seems better than
// hardcoding a path to the .h file.
#ifdef WITH_NVPTX
namespace llvm {
FunctionPass *createNVVMReflectPass(const StringMap<int> &Mapping);
}
#endif
namespace Halide {
namespace Internal {
using std::string;
using std::vector;
using namespace Halide::ConciseCasts;
using namespace llvm;
#ifdef WITH_NVPTX
namespace {
/** A code generator that emits GPU code from a given Halide stmt. */
class CodeGen_PTX_Dev : public CodeGen_LLVM, public CodeGen_GPU_Dev {
public:
/** Create a PTX device code generator. */
CodeGen_PTX_Dev(const Target &host);
~CodeGen_PTX_Dev() override;
void add_kernel(Stmt stmt,
const std::string &name,
const std::vector<DeviceArgument> &args) override;
static void test();
std::vector<char> compile_to_src() override;
std::string get_current_kernel_name() override;
void dump() override;
std::string print_gpu_name(const std::string &name) override;
std::string api_unique_name() override {
return "cuda";
}
protected:
using CodeGen_LLVM::visit;
/** (Re)initialize the PTX module. This is separate from compile, since
* a PTX device module will often have many kernels compiled into it for
* a single pipeline. */
/* override */ void init_module() override;
/** We hold onto the basic block at the start of the device
* function in order to inject allocas */
llvm::BasicBlock *entry_block;
/** Nodes for which we need to override default behavior for the GPU runtime */
// @{
void visit(const Call *) override;
void visit(const For *) override;
void visit(const Allocate *) override;
void visit(const Free *) override;
void visit(const AssertStmt *) override;
void visit(const Load *) override;
void visit(const Store *) override;
void visit(const Atomic *) override;
void codegen_vector_reduce(const VectorReduce *op, const Expr &init) override;
// @}
std::string march() const;
std::string mcpu_target() const override;
std::string mcpu_tune() const override;
std::string mattrs() const override;
bool use_soft_float_abi() const override;
int native_vector_bits() const override;
bool promote_indices() const override {
return false;
}
Type upgrade_type_for_arithmetic(const Type &t) const override {
return t;
}
Type upgrade_type_for_storage(const Type &t) const override;
/** Map from simt variable names (e.g. foo.__block_id_x) to the llvm
* ptx intrinsic functions to call to get them. */
std::string simt_intrinsic(const std::string &name);
bool supports_atomic_add(const Type &t) const override;
};
CodeGen_PTX_Dev::CodeGen_PTX_Dev(const Target &host)
: CodeGen_LLVM(host) {
context = new llvm::LLVMContext();
}
CodeGen_PTX_Dev::~CodeGen_PTX_Dev() {
// This is required as destroying the context before the module
// results in a crash. Really, responsibility for destruction
// should be entirely in the parent class.
// TODO: Figure out how to better manage the context -- e.g. allow using
// same one as the host.
module.reset();
delete context;
}
Type CodeGen_PTX_Dev::upgrade_type_for_storage(const Type &t) const {
if (t.element_of() == Float(16)) {
return t;
}
return CodeGen_LLVM::upgrade_type_for_storage(t);
}
void CodeGen_PTX_Dev::add_kernel(Stmt stmt,
const std::string &name,
const std::vector<DeviceArgument> &args) {
internal_assert(module != nullptr);
debug(2) << "In CodeGen_PTX_Dev::add_kernel\n";
// Now deduce the types of the arguments to our function
vector<llvm::Type *> arg_types(args.size());
for (size_t i = 0; i < args.size(); i++) {
if (args[i].is_buffer) {
arg_types[i] = llvm_type_of(UInt(8))->getPointerTo();
} else {
arg_types[i] = llvm_type_of(args[i].type);
}
}
// Make our function
FunctionType *func_t = FunctionType::get(void_t, arg_types, false);
function = llvm::Function::Create(func_t, llvm::Function::ExternalLinkage, name, module.get());
set_function_attributes_from_halide_target_options(*function);
// Mark the buffer args as no alias
for (size_t i = 0; i < args.size(); i++) {
if (args[i].is_buffer) {
function->addParamAttr(i, Attribute::NoAlias);
}
}
// Make the initial basic block
entry_block = BasicBlock::Create(*context, "entry", function);
builder->SetInsertPoint(entry_block);
// Put the arguments in the symbol table
vector<string> arg_sym_names;
{
size_t i = 0;
for (auto &fn_arg : function->args()) {
string arg_sym_name = args[i].name;
sym_push(arg_sym_name, &fn_arg);
fn_arg.setName(arg_sym_name);
arg_sym_names.push_back(arg_sym_name);
i++;
}
}
// We won't end the entry block yet, because we'll want to add
// some allocas to it later if there are local allocations. Start
// a new block to put all the code.
BasicBlock *body_block = BasicBlock::Create(*context, "body", function);
builder->SetInsertPoint(body_block);
debug(1) << "Generating llvm bitcode for kernel...\n";
// Ok, we have a module, function, context, and a builder
// pointing at a brand new basic block. We're good to go.
stmt.accept(this);
// Now we need to end the function
builder->CreateRetVoid();
// Make the entry block point to the body block
builder->SetInsertPoint(entry_block);
builder->CreateBr(body_block);
// Add the nvvm annotation that it is a kernel function.
llvm::Metadata *md_args[] = {
llvm::ValueAsMetadata::get(function),
MDString::get(*context, "kernel"),
llvm::ValueAsMetadata::get(ConstantInt::get(i32_t, 1))};
MDNode *md_node = MDNode::get(*context, md_args);
module->getOrInsertNamedMetadata("nvvm.annotations")->addOperand(md_node);
// Now verify the function is ok
verifyFunction(*function);
// Finally, verify the module is ok
verifyModule(*module);
debug(2) << "Done generating llvm bitcode for PTX\n";
// Clear the symbol table
for (const auto &arg_sym_name : arg_sym_names) {
sym_pop(arg_sym_name);
}
}
void CodeGen_PTX_Dev::init_module() {
init_context();
module = get_initial_module_for_ptx_device(target, context);
struct Intrinsic {
const char *name;
Type ret_type;
const char *intrin_name;
vector<Type> arg_types;
};
Intrinsic ptx_intrins[] = {
{"dp4a", Int(32), "dp4a_s32_s32", {Int(8, 4), Int(8, 4), Int(32)}},
{"dp4a", Int(32), "dp4a_s32_u32", {Int(8, 4), UInt(8, 4), Int(32)}},
{"dp4a", Int(32), "dp4a_u32_s32", {UInt(8, 4), Int(8, 4), Int(32)}},
{"dp4a", UInt(32), "dp4a_u32_u32", {UInt(8, 4), UInt(8, 4), UInt(32)}},
{"dp2a", Int(32), "dp2a_s32_s32", {Int(16, 4), Int(8, 4), Int(32)}},
{"dp2a", Int(32), "dp2a_s32_u32", {Int(16, 4), UInt(8, 4), Int(32)}},
{"dp2a", Int(32), "dp2a_u32_s32", {UInt(16, 4), Int(8, 4), Int(32)}},
{"dp2a", UInt(32), "dp2a_u32_u32", {UInt(16, 4), UInt(8, 4), UInt(32)}},
{"round", Float(32), "llvm.rint.f32", {Float(32)}},
{"round", Float(64), "llvm.rint.f64", {Float(64)}},
};
for (auto &&i : ptx_intrins) {
auto *fn = declare_intrin_overload(i.name, i.ret_type, i.intrin_name, std::move(i.arg_types));
function_does_not_access_memory(fn);
fn->addFnAttr(llvm::Attribute::NoUnwind);
}
}
void CodeGen_PTX_Dev::visit(const Call *op) {
if (op->is_intrinsic(Call::gpu_thread_barrier)) {
// Even though we always insert a __syncthreads equivalent
// (which has both a device and shared memory fence)
// check to make sure the intrinsic has the right number of
// arguments
internal_assert(op->args.size() == 1) << "gpu_thread_barrier() intrinsic must specify memory fence type.\n";
const auto *fence_type_ptr = as_const_int(op->args[0]);
internal_assert(fence_type_ptr) << "gpu_thread_barrier() parameter is not a constant integer.\n";
llvm::Function *barrier0 = module->getFunction("llvm.nvvm.barrier0");
internal_assert(barrier0) << "Could not find PTX barrier intrinsic (llvm.nvvm.barrier0)\n";
builder->CreateCall(barrier0);
value = ConstantInt::get(i32_t, 0);
return;
}
// TODO: It would be better if CodeGen_LLVM could handle overloaded intrin calls by default.
value = call_overloaded_intrin(op->type, op->name, op->args);
if (!value) {
CodeGen_LLVM::visit(op);
}
}
string CodeGen_PTX_Dev::simt_intrinsic(const string &name) {
if (ends_with(name, ".__thread_id_x")) {
return "llvm.nvvm.read.ptx.sreg.tid.x";
} else if (ends_with(name, ".__thread_id_y")) {
return "llvm.nvvm.read.ptx.sreg.tid.y";
} else if (ends_with(name, ".__thread_id_z")) {
return "llvm.nvvm.read.ptx.sreg.tid.z";
} else if (ends_with(name, ".__thread_id_w")) {
return "llvm.nvvm.read.ptx.sreg.tid.w";
} else if (ends_with(name, ".__block_id_x")) {
return "llvm.nvvm.read.ptx.sreg.ctaid.x";
} else if (ends_with(name, ".__block_id_y")) {
return "llvm.nvvm.read.ptx.sreg.ctaid.y";
} else if (ends_with(name, ".__block_id_z")) {
return "llvm.nvvm.read.ptx.sreg.ctaid.z";
} else if (ends_with(name, ".__block_id_w")) {
return "llvm.nvvm.read.ptx.sreg.ctaid.w";
}
internal_error << "simt_intrinsic called on bad variable name\n";
return "";
}
void CodeGen_PTX_Dev::visit(const For *loop) {
if (is_gpu_var(loop->name)) {
Expr simt_idx = Call::make(Int(32), simt_intrinsic(loop->name), std::vector<Expr>(), Call::Extern);
internal_assert(is_const_zero(loop->min));
sym_push(loop->name, codegen(simt_idx));
codegen(loop->body);
sym_pop(loop->name);
} else {
CodeGen_LLVM::visit(loop);
}
}
void CodeGen_PTX_Dev::visit(const Allocate *alloc) {
user_assert(!alloc->new_expr.defined()) << "Allocate node inside PTX kernel has custom new expression.\n"
<< "(Memoization is not supported inside GPU kernels at present.)\n";
if (alloc->memory_type == MemoryType::GPUShared) {
// PTX uses zero in address space 3 as the base address for shared memory
Value *shared_base = Constant::getNullValue(PointerType::get(i8_t, 3));
sym_push(alloc->name, shared_base);
} else {
debug(2) << "Allocate " << alloc->name << " on device\n";
string allocation_name = alloc->name;
debug(3) << "Pushing allocation called " << allocation_name << " onto the symbol table\n";
// Jump back to the entry and generate an alloca. Note that by
// jumping back we're rendering any expression we carry back
// meaningless, so we had better only be dealing with
// constants here.
int32_t size = alloc->constant_allocation_size();
internal_assert(size > 0)
<< "Allocation " << alloc->name << " has a dynamic size. "
<< "This should have been moved to the heap by the "
<< "fuse_gpu_thread_loops lowering pass.\n";
BasicBlock *here = builder->GetInsertBlock();
builder->SetInsertPoint(entry_block);
Value *ptr = builder->CreateAlloca(llvm_type_of(alloc->type), ConstantInt::get(i32_t, size));
builder->SetInsertPoint(here);
sym_push(allocation_name, ptr);
}
codegen(alloc->body);
}
void CodeGen_PTX_Dev::visit(const Free *f) {
sym_pop(f->name);
}
void CodeGen_PTX_Dev::visit(const AssertStmt *op) {
// Discard the error message for now.
Expr trap = Call::make(Int(32), "halide_ptx_trap", {}, Call::Extern);
codegen(IfThenElse::make(!op->condition, Evaluate::make(trap)));
}
void CodeGen_PTX_Dev::visit(const Load *op) {
// Do aligned 4-wide 32-bit loads as a single i128 load.
const Ramp *r = op->index.as<Ramp>();
// TODO: lanes >= 4, not lanes == 4
if (is_const_one(op->predicate) && r && is_const_one(r->stride) && r->lanes == 4 && op->type.bits() == 32) {
ModulusRemainder align = op->alignment;
if (align.modulus % 4 == 0 && align.remainder % 4 == 0) {
Expr index = simplify(r->base / 4);
Expr equiv = Load::make(UInt(128), op->name, index,
op->image, op->param, const_true(), align / 4);
equiv = reinterpret(op->type, equiv);
codegen(equiv);
return;
}
}
CodeGen_LLVM::visit(op);
}
void CodeGen_PTX_Dev::visit(const Store *op) {
// Issue atomic store if we are inside an Atomic node.
if (emit_atomic_stores) {
user_assert(is_const_one(op->predicate)) << "Atomic update does not support predicated store.\n";
user_assert(op->value.type().bits() >= 32) << "CUDA: 8-bit or 16-bit atomics are not supported.\n";
}
// Do aligned 4-wide 32-bit stores as a single i128 store.
const Ramp *r = op->index.as<Ramp>();
// TODO: lanes >= 4, not lanes == 4
if (is_const_one(op->predicate) && r && is_const_one(r->stride) && r->lanes == 4 && op->value.type().bits() == 32) {
ModulusRemainder align = op->alignment;
if (align.modulus % 4 == 0 && align.remainder % 4 == 0) {
Expr index = simplify(r->base / 4);
Expr value = reinterpret(UInt(128), op->value);
Stmt equiv = Store::make(op->name, value, index, op->param, const_true(), align / 4);
codegen(equiv);
return;
}
}
CodeGen_LLVM::visit(op);
}
void CodeGen_PTX_Dev::visit(const Atomic *op) {
// CUDA requires all the threads in a warp to perform the same operations,
// which means our mutex will lead to deadlock.
user_assert(op->mutex_name.empty())
<< "The atomic update requires a mutex lock, which is not supported in CUDA.\n";
// Issue atomic stores.
ScopedValue<bool> old_emit_atomic_stores(emit_atomic_stores, true);
CodeGen_LLVM::visit(op);
}
// The NVPTX backend generates really terrible code if loads aren't 32-bit. This
// mutator replaces 8- or 16-bit loads aligned to 32-bits with 32-bit loads of fewer
// lanes instead.
class RewriteLoadsAs32Bit : public IRMutator {
using IRMutator::visit;
Expr visit(const Load *op) override {
if (op->type.is_scalar() || op->type.bits() * op->type.lanes() < 32) {
return IRMutator::visit(op);
}
Expr index = mutate(op->index);
int sub_lanes = 32 / op->type.bits();
const Ramp *idx = index.as<Ramp>();
if (idx &&
is_const_one(op->predicate) &&
is_const_one(idx->stride) &&
op->alignment.modulus % sub_lanes == 0 &&
op->alignment.remainder % sub_lanes == 0) {
Expr new_idx = simplify(idx->base / sub_lanes);
int load_lanes = op->type.lanes() / sub_lanes;
if (op->type.lanes() > sub_lanes) {
new_idx = Ramp::make(new_idx, 1, load_lanes);
}
Expr new_load = Load::make(Int(32, load_lanes), op->name, new_idx, op->image, op->param, const_true(load_lanes), op->alignment / sub_lanes);
return reinterpret(op->type, new_load);
} else if (index.same_as(op->index)) {
return op;
} else {
return Load::make(op->type, op->name, op->index, op->image, op->param, op->predicate, op->alignment);
}
}
};
void CodeGen_PTX_Dev::codegen_vector_reduce(const VectorReduce *op, const Expr &init) {
// Pattern match 8/16-bit dot products
struct Pattern {
VectorReduce::Operator op;
int factor;
Expr pattern;
const char *name;
int flags;
enum {
SwapOps = 1 << 0, // This happens before narrowing op 1 below.
NarrowOp1 = 1 << 1,
};
};
static Expr wild_i8x = Variable::make(Int(8, 0), "*");
static Expr wild_u8x = Variable::make(UInt(8, 0), "*");
static Expr wild_i16x = Variable::make(Int(16, 0), "*");
static Expr wild_u16x = Variable::make(UInt(16, 0), "*");
// TODO: Support rewriting to arbitrary calls in IRMatch and use that instead
// of expr_match here. That would probably allow avoiding the redundant swapping
// operands logic.
// clang-format off
static const Pattern patterns[] = {
{VectorReduce::Add, 4, i32(widening_mul(wild_i8x, wild_i8x)), "dp4a"},
{VectorReduce::Add, 4, i32(widening_mul(wild_i8x, wild_u8x)), "dp4a"},
{VectorReduce::Add, 4, i32(widening_mul(wild_u8x, wild_i8x)), "dp4a"},
{VectorReduce::Add, 4, u32(widening_mul(wild_u8x, wild_u8x)), "dp4a"},
{VectorReduce::Add, 4, widening_mul(wild_i16x, wild_i16x), "dp2a", Pattern::NarrowOp1},
{VectorReduce::Add, 4, widening_mul(wild_i16x, wild_u16x), "dp2a", Pattern::NarrowOp1},
{VectorReduce::Add, 4, widening_mul(wild_u16x, wild_i16x), "dp2a", Pattern::NarrowOp1},
{VectorReduce::Add, 4, widening_mul(wild_u16x, wild_u16x), "dp2a", Pattern::NarrowOp1},
{VectorReduce::Add, 4, widening_mul(wild_i16x, wild_i16x), "dp2a", Pattern::SwapOps | Pattern::NarrowOp1},
{VectorReduce::Add, 4, widening_mul(wild_u16x, wild_i16x), "dp2a", Pattern::SwapOps | Pattern::NarrowOp1},
{VectorReduce::Add, 4, widening_mul(wild_i16x, wild_u16x), "dp2a", Pattern::SwapOps | Pattern::NarrowOp1},
{VectorReduce::Add, 4, widening_mul(wild_u16x, wild_u16x), "dp2a", Pattern::SwapOps | Pattern::NarrowOp1},
};
// clang-format on
const int input_lanes = op->value.type().lanes();
const int factor = input_lanes / op->type.lanes();
std::vector<Expr> matches;
for (const Pattern &p : patterns) {
if (p.op != op->op || factor % p.factor != 0) {
continue;
}
if (!expr_match(p.pattern, op->value, matches)) {
continue;
}
Expr a = matches[0];
Expr b = matches[1];
if (p.flags & Pattern::SwapOps) {
std::swap(a, b);
}
if (p.flags & Pattern::NarrowOp1) {
// This pattern needs the second operand to be narrowed further.
Expr b_narrow = lossless_cast(b.type().narrow(), b);
if (!b_narrow.defined()) {
b_narrow = lossless_cast(b.type().narrow().with_code(halide_type_uint), b);
if (!b_narrow.defined()) {
continue;
}
}
b = b_narrow;
}
Expr i = init;
if (!i.defined()) {
i = cast(op->value.type(), 0);
}
vector<Expr> result;
for (int l = 0; l < op->type.lanes(); l++) {
// To compute a single lane of the output, we'll
// extract the appropriate slice of the args, which
// have been reinterpreted as 32-bit vectors, then
// call either dp4a or dp2a the appropriate number of
// times, and finally sum the result.
Expr i_slice = Shuffle::make_extract_element(i, l);
for (int i = 0; i < factor; i += p.factor) {
Expr a_slice = Shuffle::make_slice(a, i + l * factor, 1, p.factor);
Expr b_slice = Shuffle::make_slice(b, i + l * factor, 1, p.factor);
i_slice = Call::make(i_slice.type(), p.name, {a_slice, b_slice, i_slice}, Call::PureExtern);
}
i_slice = RewriteLoadsAs32Bit().mutate(i_slice);
i_slice = simplify(i_slice);
i_slice = common_subexpression_elimination(i_slice);
result.push_back(i_slice);
}
// Concatenate the per-lane results to get the full vector result
Expr equiv = Shuffle::make_concat(result);
equiv.accept(this);
return;
}
CodeGen_LLVM::codegen_vector_reduce(op, init);
}
string CodeGen_PTX_Dev::march() const {
return "nvptx64";
}
string CodeGen_PTX_Dev::mcpu_target() const {
if (target.has_feature(Target::CUDACapability86)) {
return "sm_86";
} else if (target.has_feature(Target::CUDACapability80)) {
return "sm_80";
} else if (target.has_feature(Target::CUDACapability75)) {
return "sm_75";
} else if (target.has_feature(Target::CUDACapability70)) {
return "sm_70";
} else if (target.has_feature(Target::CUDACapability61)) {
return "sm_61";
} else if (target.has_feature(Target::CUDACapability50)) {
return "sm_50";
} else if (target.has_feature(Target::CUDACapability35)) {
return "sm_35";
} else if (target.has_feature(Target::CUDACapability32)) {
return "sm_32";
} else if (target.has_feature(Target::CUDACapability30)) {
return "sm_30";
} else {
return "sm_20";
}
}
string CodeGen_PTX_Dev::mcpu_tune() const {
return mcpu_target();
}
string CodeGen_PTX_Dev::mattrs() const {
if (target.has_feature(Target::CUDACapability86)) {
return "+ptx71";
} else if (target.has_feature(Target::CUDACapability80)) {
return "+ptx70";
} else if (target.has_feature(Target::CUDACapability70) ||
target.has_feature(Target::CUDACapability75)) {
return "+ptx60";
} else if (target.has_feature(Target::CUDACapability61)) {
return "+ptx50";
} else if (target.features_any_of({Target::CUDACapability32,
Target::CUDACapability50})) {
// Need ptx isa 4.0.
return "+ptx40";
} else {
// Use the default. For llvm 3.5 it's ptx 3.2.
return "";
}
}
bool CodeGen_PTX_Dev::use_soft_float_abi() const {
return false;
}
vector<char> CodeGen_PTX_Dev::compile_to_src() {
debug(2) << "In CodeGen_PTX_Dev::compile_to_src";
// DISABLED - hooked in here to force PrintBeforeAll option - seems to be the only way?
/*char* argv[] = { "llc", "-print-before-all" };*/
/*int argc = sizeof(argv)/sizeof(char*);*/
/*cl::ParseCommandLineOptions(argc, argv, "Halide PTX internal compiler\n");*/
llvm::Triple triple(module->getTargetTriple());
// Allocate target machine
std::string err_str;
const llvm::Target *llvm_target = TargetRegistry::lookupTarget(triple.str(), err_str);
internal_assert(llvm_target) << err_str << "\n";
TargetOptions options;
options.AllowFPOpFusion = FPOpFusion::Fast;
options.UnsafeFPMath = true;
options.NoInfsFPMath = true;
options.NoNaNsFPMath = true;
options.HonorSignDependentRoundingFPMathOption = false;
options.NoZerosInBSS = false;
options.GuaranteedTailCallOpt = false;
std::unique_ptr<TargetMachine>
target_machine(llvm_target->createTargetMachine(triple.str(),
mcpu_target(), mattrs(), options,
llvm::Reloc::PIC_,
llvm::CodeModel::Small,
CodeGenOpt::Aggressive));
internal_assert(target_machine.get()) << "Could not allocate target machine!";
module->setDataLayout(target_machine->createDataLayout());
// Set up passes
llvm::SmallString<8> outstr;
raw_svector_ostream ostream(outstr);
ostream.SetUnbuffered();
// NVidia's libdevice library uses a __nvvm_reflect to choose
// how to handle denormalized numbers. (The pass replaces calls
// to __nvvm_reflect with a constant via a map lookup. The inliner
// pass then resolves these situations to fast code, often a single
// instruction per decision point.)
//
// The default is (more) IEEE like handling. FTZ mode flushes them
// to zero. (This may only apply to single-precision.)
//
// The libdevice documentation covers other options for math accuracy
// such as replacing division with multiply by the reciprocal and
// use of fused-multiply-add, but they do not seem to be controlled
// by this __nvvvm_reflect mechanism and may be flags to earlier compiler
// passes.
const int kFTZDenorms = 1;
// Insert a module flag for the FTZ handling.
module->addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz",
kFTZDenorms);
if (kFTZDenorms) {
for (llvm::Function &fn : *module) {
fn.addFnAttr("nvptx-f32ftz", "true");
}
}
const bool do_loop_opt = get_target().has_feature(Target::EnableLLVMLoopOpt);
// Define and run optimization pipeline with new pass manager
PipelineTuningOptions pto;
pto.LoopInterleaving = do_loop_opt;
pto.LoopVectorization = do_loop_opt;
pto.SLPVectorization = true; // Note: SLP vectorization has no analogue in the Halide scheduling model
pto.LoopUnrolling = do_loop_opt;
pto.ForgetAllSCEVInLoopUnroll = true;
llvm::PassBuilder pb(target_machine.get(), pto);
bool debug_pass_manager = false;
// These analysis managers have to be declared in this order.
llvm::LoopAnalysisManager lam;
llvm::FunctionAnalysisManager fam;
llvm::CGSCCAnalysisManager cgam;
llvm::ModuleAnalysisManager mam;
// Register all the basic analyses with the managers.
pb.registerModuleAnalyses(mam);
pb.registerCGSCCAnalyses(cgam);
pb.registerFunctionAnalyses(fam);
pb.registerLoopAnalyses(lam);
pb.crossRegisterProxies(lam, fam, cgam, mam);
ModulePassManager mpm;
using OptimizationLevel = llvm::OptimizationLevel;
OptimizationLevel level = OptimizationLevel::O3;
target_machine->registerPassBuilderCallbacks(pb);
mpm = pb.buildPerModuleDefaultPipeline(level, debug_pass_manager);
mpm.run(*module, mam);
if (llvm::verifyModule(*module, &errs())) {
report_fatal_error("Transformation resulted in an invalid module\n");
}
// Optimization pipeline completed; run codegen pipeline
// NOTE: use of the "legacy" PassManager here is still required; it is deprecated
// for optimization, but is still the only complete API for codegen as of work-in-progress
// LLVM14. At the time of this comment (Dec 2021), there is no firm plan as to when codegen will
// be fully available in the new PassManager, so don't worry about this 'legacy'
// tag until there's any indication that the old APIs start breaking.
//
// See:
// https://lists.llvm.org/pipermail/llvm-dev/2021-April/150100.html
// https://releases.llvm.org/13.0.0/docs/ReleaseNotes.html#changes-to-the-llvm-ir
// https://groups.google.com/g/llvm-dev/c/HoS07gXx0p8
legacy::PassManager module_pass_manager;
module_pass_manager.add(createTargetTransformInfoWrapperPass(target_machine->getTargetIRAnalysis()));
// Override default to generate verbose assembly.
target_machine->Options.MCOptions.AsmVerbose = true;
// Output string stream
// Ask the target to add backend passes as necessary.
bool fail = target_machine->addPassesToEmitFile(module_pass_manager, ostream, nullptr,
::llvm::CGFT_AssemblyFile,
true);
internal_assert(!fail) << "Failed to set up passes to emit PTX source\n";
module_pass_manager.run(*module);
// Codegen pipeline completed.
if (debug::debug_level() >= 2) {
dump();
}
debug(2) << "Done with CodeGen_PTX_Dev::compile_to_src";
debug(1) << "PTX kernel:\n"
<< outstr.c_str() << "\n";
vector<char> buffer(outstr.begin(), outstr.end());
// Dump the SASS too if the cuda SDK is in the path
if (debug::debug_level() >= 2) {
debug(2) << "Compiling PTX to SASS. Will fail if CUDA SDK is not installed (and in the path).\n";
TemporaryFile ptx(get_current_kernel_name(), ".ptx");
TemporaryFile sass(get_current_kernel_name(), ".sass");
std::ofstream f(ptx.pathname());
f.write(buffer.data(), buffer.size());
f.close();
string cmd = "ptxas --gpu-name " + mcpu_target() + " " + ptx.pathname() + " -o " + sass.pathname();
if (system(cmd.c_str()) == 0) {
cmd = "nvdisasm " + sass.pathname();
int ret = system(cmd.c_str());
(void)ret; // Don't care if it fails
}
// Note: It works to embed the contents of the .sass file in
// the buffer instead of the ptx source, and this could help
// with app startup times. Expose via the target?
/*
{
std::ifstream f(sass.pathname());
buffer.clear();
f.seekg(0, std::ios_base::end);
std::streampos sz = f.tellg();
buffer.resize(sz);
f.seekg(0, std::ios_base::beg);
f.read(buffer.data(), sz);
}
*/
}
// Null-terminate the ptx source
buffer.push_back(0);
return buffer;
}
int CodeGen_PTX_Dev::native_vector_bits() const {
// PTX doesn't really do vectorization. The widest type is a double.
return 64;
}
string CodeGen_PTX_Dev::get_current_kernel_name() {
return get_llvm_function_name(function);
}
void CodeGen_PTX_Dev::dump() {
module->print(dbgs(), nullptr, false, true);
}
std::string CodeGen_PTX_Dev::print_gpu_name(const std::string &name) {
return name;
}
bool CodeGen_PTX_Dev::supports_atomic_add(const Type &t) const {
if (t.bits() < 32) {
// TODO: Half atomics are supported by compute capability 7.x or higher.
return false;
}
if (t.is_int_or_uint()) {
return true;
}
if (t.is_float() && t.bits() == 32) {
return true;
}
if (t.is_float() && t.bits() == 64) {
// double atomics are supported since CC6.1
return target.get_cuda_capability_lower_bound() >= 61;
}
return false;
}
} // namespace
std::unique_ptr<CodeGen_GPU_Dev> new_CodeGen_PTX_Dev(const Target &target) {
return std::make_unique<CodeGen_PTX_Dev>(target);
}
#else // WITH_PTX
std::unique_ptr<CodeGen_GPU_Dev> new_CodeGen_PTX_Dev(const Target &target) {
user_error << "PTX not enabled for this build of Halide.\n";
return nullptr;
}
#endif // WITH_PTX
} // 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 によって変換されたページ (->オリジナル) /