开源 企业版 高校版 私有云 模力方舟 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_RISCV.cpp
Halide
/
src
/
CodeGen_RISCV.cpp
CodeGen_RISCV.cpp 16.11 KB
一键复制 编辑 原始数据 按行查看 历史
Steven Johnson 提交于 2023年01月19日 05:56 +08:00 . Drop support for MIPS (#7287) (#7289)
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
#include "CSE.h"
#include "CodeGen_Internal.h"
#include "CodeGen_Posix.h"
#include "ConciseCasts.h"
#include "Debug.h"
#include "IREquality.h"
#include "IRMatch.h"
#include "IROperator.h"
#include "IRPrinter.h"
#include "LLVM_Headers.h"
#include "Simplify.h"
#include "Substitute.h"
#include "Util.h"
namespace Halide {
namespace Internal {
using std::string;
using std::vector;
#if defined(WITH_RISCV)
namespace {
constexpr int max_intrinsic_args = 4;
struct IntrinsicArgPattern {
enum TypePattern {
Undefined, // Invalid value for sentinel.
Fixed, // Argument is a fixed width vector.
Scalable, // Argument is a scalable vector.
AllTypeWidths, // Argument generalizes to all bit widths of type.
} type_pattern;
Type type;
int relative_scale;
IntrinsicArgPattern(const Type &type)
: type_pattern(type.is_vector() ? Fixed : Scalable),
type(type), relative_scale(1) {
}
IntrinsicArgPattern(halide_type_code_t code)
: type_pattern(AllTypeWidths),
type(code, 8, 1), relative_scale(1) {
}
IntrinsicArgPattern(halide_type_code_t code, int relative_scale)
: type_pattern(AllTypeWidths),
type(code, 8, 1), relative_scale(relative_scale) {
}
IntrinsicArgPattern()
: type_pattern(Undefined),
type(), relative_scale(0) {
}
};
struct RISCVIntrinsic {
const char *riscv_name;
IntrinsicArgPattern ret_type;
const char *name;
IntrinsicArgPattern arg_types[max_intrinsic_args];
int flags;
enum {
AddVLArg = 1 << 0, // Add a constant full size vector length argument
RoundDown = 1 << 1, // Set vxrm rounding mode to down (rdn) before intrinsic.
RoundUp = 1 << 2, // Set vxrm rounding mode to up (rdu) before intrinsic.
MangleReturnType = 1 << 3, // Put return type mangling at start of type list.
ReverseBinOp = 1 << 4, // Switch first two arguments to handle asymmetric ops.
};
};
Type concretize_fixed_or_scalable(const IntrinsicArgPattern &f_or_v, int type_width_scale, int vector_bits) {
if (f_or_v.type_pattern == IntrinsicArgPattern::Fixed) {
return f_or_v.type;
}
int bit_width = f_or_v.type.bits() * f_or_v.relative_scale * type_width_scale;
return Type(f_or_v.type.code(), bit_width, (vector_bits * f_or_v.relative_scale) / bit_width);
}
// Produce LLVM IR intrisic type name mangling for Halide type, with vector codegen info provided.
std::string mangle_vector_argument_type(const Type &arg_type, bool scalable, int effective_vscale) {
std::string result;
if (arg_type.is_vector()) {
int lanes = arg_type.lanes();
if (!scalable) {
result = "v" + std::to_string(lanes);
} else {
result = "nxv" + std::to_string(lanes / effective_vscale);
}
}
if (arg_type.is_int() || arg_type.is_uint()) {
result += "i";
} else {
result += "f";
}
result += std::to_string(arg_type.bits());
return result;
}
/** A code generator that emits RISC-V code from a given Halide stmt. */
class CodeGen_RISCV : public CodeGen_Posix {
public:
/** Create a RISC-V code generator. Processor features can be
* enabled using the appropriate flags in the target struct. */
CodeGen_RISCV(const Target &);
llvm::Function *define_riscv_intrinsic_wrapper(const RISCVIntrinsic &intrin,
int type_width_scale);
protected:
using CodeGen_Posix::visit;
void init_module() override;
string mcpu_target() const override;
string mcpu_tune() const override;
string mattrs() const override;
string mabi() const override;
bool use_soft_float_abi() const override;
int native_vector_bits() const override;
int maximum_vector_bits() const override;
int target_vscale() const override;
};
CodeGen_RISCV::CodeGen_RISCV(const Target &t)
: CodeGen_Posix(t) {
use_llvm_vp_intrinsics = true;
}
string CodeGen_RISCV::mcpu_target() const {
return "";
}
string CodeGen_RISCV::mcpu_tune() const {
return mcpu_target();
}
string CodeGen_RISCV::mattrs() const {
// Note: the default march is "rv[32|64]imafdc",
// which includes standard extensions:
// +m Integer Multiplication and Division,
// +a Atomic Instructions,
// +f Single-Precision Floating-Point,
// +d Double-Precision Floating-Point,
// +c Compressed Instructions,
string arch_flags = "+m,+a,+f,+d,+c";
if (target.has_feature(Target::RVV)) {
arch_flags += ",+v";
#if LLVM_VERSION >= 160
if (target.vector_bits != 0) {
arch_flags += ",+zvl" + std::to_string(target.vector_bits) + "b";
}
#endif
}
return arch_flags;
}
string CodeGen_RISCV::mabi() const {
string abi;
if (target.bits == 32) {
abi = "ilp32";
} else {
abi = "lp64";
}
if (!target.has_feature(Target::SoftFloatABI)) {
abi += "d";
}
return abi;
}
bool CodeGen_RISCV::use_soft_float_abi() const {
return target.has_feature(Target::SoftFloatABI);
}
int CodeGen_RISCV::native_vector_bits() const {
if (target.vector_bits != 0 &&
target.has_feature(Target::RVV)) {
return target.vector_bits;
}
return 0;
}
int CodeGen_RISCV::maximum_vector_bits() const {
return native_vector_bits() * 8;
}
int CodeGen_RISCV::target_vscale() const {
if (target.vector_bits != 0 &&
target.has_feature(Target::RVV)) {
internal_assert((target.vector_bits % 64) == 0);
return target.vector_bits / 64;
}
return 0;
}
const RISCVIntrinsic intrinsic_defs[] = {
{"vaadd", Type::Int, "halving_add", {Type::Int, Type::Int}, RISCVIntrinsic::AddVLArg | RISCVIntrinsic::RoundDown},
{"vaaddu", Type::UInt, "halving_add", {Type::UInt, Type::UInt}, RISCVIntrinsic::AddVLArg | RISCVIntrinsic::RoundDown},
{"vaadd", Type::Int, "rounding_halving_add", {Type::Int, Type::Int}, RISCVIntrinsic::AddVLArg | RISCVIntrinsic::RoundUp},
{"vaaddu", Type::UInt, "rounding_halving_add", {Type::UInt, Type::UInt}, RISCVIntrinsic::AddVLArg | RISCVIntrinsic::RoundUp},
{"vwadd", {Type::Int, 2}, "widening_add", {Type::Int, Type::Int}, RISCVIntrinsic::AddVLArg | RISCVIntrinsic::MangleReturnType},
{"vwaddu", {Type::UInt, 2}, "widening_add", {Type::UInt, Type::UInt}, RISCVIntrinsic::AddVLArg | RISCVIntrinsic::MangleReturnType},
{"vwsub", {Type::Int, 2}, "widening_sub", {Type::Int, Type::Int}, RISCVIntrinsic::AddVLArg | RISCVIntrinsic::MangleReturnType},
{"vwsubu", {Type::UInt, 2}, "widening_sub", {Type::UInt, Type::UInt}, RISCVIntrinsic::AddVLArg | RISCVIntrinsic::MangleReturnType},
{"vwmul", {Type::Int, 2}, "widening_mul", {Type::Int, Type::Int}, RISCVIntrinsic::AddVLArg | RISCVIntrinsic::MangleReturnType},
{"vwmulu", {Type::UInt, 2}, "widening_mul", {Type::UInt, Type::UInt}, RISCVIntrinsic::AddVLArg | RISCVIntrinsic::MangleReturnType},
{"vwmulsu", {Type::Int, 2}, "widening_mul", {Type::Int, Type::UInt}, RISCVIntrinsic::AddVLArg | RISCVIntrinsic::MangleReturnType},
{"vwmulsu", {Type::Int, 2}, "widening_mul", {Type::UInt, Type::Int}, RISCVIntrinsic::AddVLArg | RISCVIntrinsic::MangleReturnType | RISCVIntrinsic::ReverseBinOp},
};
void CodeGen_RISCV::init_module() {
CodeGen_Posix::init_module();
int effective_vscale = target_vscale();
if (effective_vscale != 0) {
for (const RISCVIntrinsic &intrin : intrinsic_defs) {
std::vector<Type> arg_types;
arg_types.reserve(max_intrinsic_args);
if (intrin.ret_type.type_pattern == IntrinsicArgPattern::AllTypeWidths) {
// Iterate over 8/16/32/64 bit integer type widths via log2 shift amount.
// TODO: Will need to add floating point bit widths when an intrinsic is added.
// Not doing this now as it is there would be no coverage, it requires
// deciding whether to get floatness from an argument or return type,
// and it probably has to check target flags to figure out Float(16)
// and BFloat(16) availability.
for (int log2_of_scale = 0; log2_of_scale < 4; log2_of_scale++) {
int bit_width_scale = 1 << log2_of_scale;
Type ret_type = concretize_fixed_or_scalable(intrin.ret_type, bit_width_scale,
target.vector_bits);
if ((intrin.ret_type.relative_scale * bit_width_scale * intrin.ret_type.type.bits()) > 64) {
break;
}
for (const auto &arg_type : intrin.arg_types) {
if (arg_type.type_pattern == IntrinsicArgPattern::Undefined) {
break;
}
if ((arg_type.relative_scale * bit_width_scale * arg_type.type.bits()) > 64) {
break;
}
arg_types.push_back(concretize_fixed_or_scalable(arg_type, bit_width_scale,
target.vector_bits));
}
llvm::Function *intrin_impl = define_riscv_intrinsic_wrapper(intrin, bit_width_scale);
declare_intrin_overload(intrin.name, ret_type, intrin_impl, arg_types);
arg_types.clear();
}
} else {
llvm::Function *intrin_impl = define_riscv_intrinsic_wrapper(intrin, 1);
Type ret_type = concretize_fixed_or_scalable(intrin.ret_type, 1,
target.vector_bits);
for (const auto &arg_type : intrin.arg_types) {
if (arg_type.type_pattern == IntrinsicArgPattern::Undefined) {
break;
}
arg_types.push_back(concretize_fixed_or_scalable(arg_type, 1, target.vector_bits));
}
declare_intrin_overload(intrin.name, ret_type, intrin_impl, arg_types);
arg_types.clear();
}
}
}
}
llvm::Function *CodeGen_RISCV::define_riscv_intrinsic_wrapper(const RISCVIntrinsic &intrin,
int bit_width_scale) {
int effective_vscale = target_vscale();
llvm::Type *xlen_type = target.bits == 32 ? i32_t : i64_t;
// Produce intrinsic name and type mangling.
std::vector<llvm::Type *> llvm_arg_types;
std::string mangled_name = "llvm.riscv.";
mangled_name += intrin.riscv_name;
Type ret_type = concretize_fixed_or_scalable(intrin.ret_type, bit_width_scale,
target.vector_bits);
if (intrin.flags & RISCVIntrinsic::MangleReturnType) {
bool scalable = (intrin.ret_type.type_pattern != IntrinsicArgPattern::Fixed);
mangled_name += "." + mangle_vector_argument_type(ret_type, scalable, effective_vscale);
}
llvm::Type *llvm_ret_type;
if (ret_type.is_vector()) {
int lanes = ret_type.lanes();
bool scalable = (intrin.ret_type.type_pattern != IntrinsicArgPattern::Fixed);
if (scalable) {
lanes /= effective_vscale;
}
llvm_ret_type = llvm::VectorType::get(llvm_type_of(ret_type.element_of()),
lanes, scalable);
} else {
llvm_ret_type = llvm_type_of(ret_type);
}
llvm_arg_types.push_back(llvm_ret_type);
for (const auto &arg_type_pattern : intrin.arg_types) {
if (arg_type_pattern.type_pattern == IntrinsicArgPattern::Undefined) {
break;
}
Type arg_type = concretize_fixed_or_scalable(arg_type_pattern, bit_width_scale, target.vector_bits);
bool scalable = (arg_type_pattern.type_pattern != IntrinsicArgPattern::Fixed);
mangled_name += "." + mangle_vector_argument_type(arg_type, scalable, effective_vscale);
llvm::Type *llvm_type;
if (arg_type.is_vector()) {
int lanes = arg_type.lanes();
if (scalable) {
lanes /= effective_vscale;
}
llvm_type = llvm::VectorType::get(llvm_type_of(arg_type.element_of()),
lanes, scalable);
} else {
llvm_type = llvm_type_of(arg_type);
}
llvm_arg_types.push_back(llvm_type);
}
if (intrin.flags & RISCVIntrinsic::ReverseBinOp) {
internal_assert(llvm_arg_types.size() > 2);
std::swap(llvm_arg_types[1], llvm_arg_types[2]);
}
if (intrin.flags & RISCVIntrinsic::AddVLArg) {
mangled_name += (target.bits == 64) ? ".i64" : ".i32";
llvm_arg_types.push_back(xlen_type);
}
llvm::Function *inner =
get_llvm_intrin(llvm_ret_type, mangled_name, llvm_arg_types);
llvm::FunctionType *inner_ty = inner->getFunctionType();
// Remove vector tail preservation argument.
llvm_arg_types.erase(llvm_arg_types.begin());
// Remove vector length argument passed to inrinsic for wrapper.
// Wrapper will supply a constant for the fixed vector length.
if (intrin.flags & RISCVIntrinsic::AddVLArg) {
llvm_arg_types.resize(llvm_arg_types.size() - 1);
}
string wrapper_name = unique_name(std::string(intrin.name) + "_wrapper");
llvm::FunctionType *wrapper_ty = llvm::FunctionType::get(
inner_ty->getReturnType(), llvm_arg_types, false);
llvm::Function *wrapper =
llvm::Function::Create(wrapper_ty, llvm::GlobalValue::InternalLinkage,
wrapper_name, module.get());
llvm::BasicBlock *block =
llvm::BasicBlock::Create(module->getContext(), "entry", wrapper);
llvm::IRBuilderBase::InsertPoint here = builder->saveIP();
builder->SetInsertPoint(block);
// Set vector fixed-point rounding flag if needed for intrinsic.
bool round_down = intrin.flags & RISCVIntrinsic::RoundDown;
bool round_up = intrin.flags & RISCVIntrinsic::RoundUp;
if (round_down || round_up) {
internal_assert(!(round_down && round_up));
llvm::Value *rounding_mode = llvm::ConstantInt::get(xlen_type, round_down ? 2 : 0);
// See https://github.com/riscv/riscv-v-spec/releases/download/v1.0/riscv-v-spec-1.0.pdf page 15
// for discussion of fixed-point rounding mode.
// TODO: When LLVM finally fixes the instructions to take rounding modes,
// this will have to change to passing the rounding mode to the intrinsic.
// https://github.com/halide/Halide/issues/7123
llvm::FunctionType *csrw_llvm_type = llvm::FunctionType::get(void_t, {xlen_type}, false);
llvm::InlineAsm *inline_csrw = llvm::InlineAsm::get(csrw_llvm_type, "csrw vxrm,${0:z}", "rJ,~{memory}", true);
builder->CreateCall(inline_csrw, {rounding_mode});
}
// Call the LLVM intrinsic.
int actual_lanes = ret_type.lanes();
llvm::Constant *vtype = llvm::ConstantInt::get(xlen_type, actual_lanes);
// Add an initial argument to handle tail propagation. Only done if result is vector type.
int left_arg = 0;
int right_arg = 1;
if (intrin.flags & RISCVIntrinsic::ReverseBinOp) {
std::swap(left_arg, right_arg);
}
llvm::Value *ret = builder->CreateCall(inner, {llvm::UndefValue::get(llvm_ret_type),
wrapper->getArg(left_arg), wrapper->getArg(right_arg),
vtype});
builder->CreateRet(ret);
// Always inline these wrappers.
wrapper->addFnAttr(llvm::Attribute::AlwaysInline);
builder->restoreIP(here);
function_does_not_access_memory(wrapper);
wrapper->addFnAttr(llvm::Attribute::NoUnwind);
llvm::verifyFunction(*wrapper);
return wrapper;
}
} // anonymous namespace
std::unique_ptr<CodeGen_Posix> new_CodeGen_RISCV(const Target &target) {
return std::make_unique<CodeGen_RISCV>(target);
}
#else // WITH_RISCV
std::unique_ptr<CodeGen_Posix> new_CodeGen_RISCV(const Target &target) {
user_error << "RISCV not enabled for this build of Halide.\n";
return nullptr;
}
#endif // WITH_RISCV
} // 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 によって変換されたページ (->オリジナル) /