HACC 函数管理【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills1. 概述HACC 方言通过HACCFuncType枚举和HACCFunctionInterface接口管理异构函数的归属与属性。每个函数被标记为 HOST 或 DEVICE 类型并通过接口方法查询和设置函数属性。2. HACCFuncType 枚举HACCFuncType枚举定义了函数的异构归属类型。枚举值整数值助记符说明HOST1HOSTHost 端函数运行在 CPU 上DEVICE2DEVICEDevice 端函数运行在 NPU 上属性定义def HACC_FuncTypeAttr : HACC_AttrHACCFuncType, function_kind { let parameters (ins EnumParameterHACC_FuncTypeEnum:$function_kind); let assemblyFormat params ; }MLIR 表示hacc.function_kind #hacc.function_kindHOST hacc.function_kind #hacc.function_kindDEVICE3. HACCFunctionInterface 接口HACCFunctionInterface继承自FunctionOpInterface为异构函数提供统一的查询和设置方法。源码参考HACCInterfaces.td3.1 查询方法方法名返回类型参数说明getHACCFuncType()std::optionalhacc::HACCFuncType无返回函数的 HACC 类型未知时返回std::nulloptisHost()bool无判断是否为 Host 函数isDevice()bool无判断是否为 Device 函数isDeviceEntry()bool无判断是否为 Device 入口函数getHostFuncType()std::optionalhacc::HostFuncType无返回 Host 函数类型非 Host 函数返回std::nullopt3.2 设置方法方法名返回类型参数说明setDevice()void无将函数标记为 Device 函数自动移除不允许的属性setDeviceEntry()void无将函数标记为 Device 入口函数自动移除不允许的属性setHost()void无将函数标记为 Host 函数自动移除不允许的属性setHostFuncType(::mlir::hacc::HostFuncType)voidfuncType设置 Host 函数的具体角色类型3.3 参数查询方法方法名返回类型参数说明isKernelArg(int, ::mlir::hacc::KernelArgType)boolargIdx,argType判断第argIdx个参数是否具有指定的hacc.arg_type4. HACC To LLVM Translation 属性HACCToLLVMIRTranslateAttr枚举用于标记函数在 LLVM IR 翻译阶段的行为。枚举值整数值助记符说明ENTRY0hacc.entryDevice 入口函数MIX_ENTRY1hacc.mix_entry混合 Device 入口函数ALWAYS_INLINE2hacc.always_inline始终内联函数5. 辅助属性5.1 RenameFuncAttrdef HACC_RenameFuncAttr : HACC_AttrRenameFunc, rename_func { let parameters (ins AttrParameter::mlir::FlatSymbolRefAttr:$targetName); }指示当前函数应重命名为目标函数名。hacc-rename-funcPass 会据此执行重命名。5.2 InputIdxAttr / OutputIdxAttr属性参数说明hacc.input_idxunsigned:$argIdx标记函数参数为输入索引hacc.output_idxunsigned:$argIdx标记函数参数为输出索引NPU Kernel 的输出通过输入参数传递5.3 其他辅助属性属性名助记符说明ExportAsDAGexport_as_dag将函数导出为 DAGDummyFuncdummy_func虚拟函数标记ExternalFunctionPathexternal_function_path外部函数路径CachedIOcached_io标记值已被缓存 IONoIOAliasno_io_alias标记函数输入输出严格不别名BlockDimblock_dim函数的 Block 维度属性SIMTModulesimt_module标记 SIMT 模块6. 典型使用模式6.1 Host 函数func.func tiling_func(%arg0: tensor?x?xf16) - (i64, i64) attributes {hacc.function_kind #hacc.function_kindHOST} { %c arith.constant 42 : i64 return %c, %c : i64, i64 }6.2 Device 入口函数func.func kernel_entry(%arg0: tensor?x?xf16, %arg1: i64 {hacc.arg_type #hacc.arg_typetiling_data}) attributes {hacc.function_kind #hacc.function_kindDEVICE, hacc.tiling_function #hacc.tiling_functiontiling_func} { return }6.3 函数重命名func.func bar() attributes {hacc.rename_func #hacc.rename_funcfoo} { return }经过hacc-rename-funcPass 后bar将被重命名为foo。【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考