Skip to content

Add Huawei Ascend C toolchain support#7510

Open
wuzhenqing wants to merge 1 commit intoxmake-io:devfrom
wuzhenqing:dev
Open

Add Huawei Ascend C toolchain support#7510
wuzhenqing wants to merge 1 commit intoxmake-io:devfrom
wuzhenqing:dev

Conversation

@wuzhenqing
Copy link
Copy Markdown

@wuzhenqing wuzhenqing commented Apr 27, 2026

Add toolchain support for Huawei Ascend C — the C++ extension
language for writing custom NPU operators on Ascend AI
processors (Da Vinci architecture). This uses the Bisheng compiler
driver (bisheng) from the CANN (Compute Architecture for
Neural Networks) toolkit.

Fixes #7506

  1. Toolchain (xmake/toolchains/ascendc)
  • SDK auto-detection via ASCEND_HOME_PATH / ASCEND_TOOLKIT_HOME env vars or --ascendc= config
  • Host architecture mapping (x86_64, aarch64) to CANN host directories
  • Unified bisheng compiler driver serving as compiler (asc, aicpu) and linker (ascld, ascsh)
  • Default flags: -std=c++17, -fPIC, and AI-CPU specific flags
  1. Language (xmake/languages/ascendc)
  • Two source kinds: .asc (Ascend C kernel) and .aicpu (AI-CPU custom operator)
  • Three target kinds: binary, static, shared
  • add_ascnpuarchs("dav-2201") API for NPU architecture selection
  • Auto-propagation of --npu-arch flags across compile and link stages
  1. Compiler module (xmake/modules/core/tools/bisheng)
  • Inherits from clang, with extensions for Ascend C source kinds
  • Flag detection adapted for .asc / .aicpu extensions
  1. Build rules (xmake/rules/ascendc)
  • ascendc.npuarchs: collects NPU arch from targets/options/toolchains, distributes to ascflags/shflags/ldflags
  • ascendc.build.asc / ascendc.build.aicpu: batch compilation via jobgraph
  • utils.inherit.links: automatic link inheritance from dependencies
  1. Tests
  • tests/projects/ascendc/libs/: static + shared + binary targets with ACL runtime
  • tests/projects/ascendc/mixed_binary/: mixed .asc + .aicpu compilation
  • tests/modules/language/ascendc/: check_main entry-point detection unit test
  • Tests are gated: require Linux host and ASCEND_HOME_PATH environment variable

Usage

add_rules("mode.debug", "mode.release")

target("example")
    set_kind("binary") 
    set_toolchains("ascendc")
    add_files("src/main.asc", "src/kernel.asc")
    add_ascnpuarchs("dav-2201")

xmake f --toolchain=ascendc --ascendc=/usr/local/Ascend/ascend-toolkit/latest
xmake

Verification

Tested on a real Ascend NPU server (CANN 8.5.0, aarch64):

  • Static library build: ok
  • Shared library build: ok
  • Binary (mixed static + shared deps): ok
  • Runtime (NPU execution): vector_add_demo passed on NPU

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for the Huawei Ascend C (bisheng) toolchain, adding the ascendc language and associated build rules for .asc and .aicpu files. Feedback points out a potential failure in temporary file creation due to invalid characters in filenames and a regex limitation in the main function detection logic.

Comment thread xmake/modules/core/tools/bisheng/has_flags.lua Outdated
Comment thread xmake/languages/ascendc/check_main.lua Outdated
Comment thread tests/modules/language/ascendc/test.lua Outdated
Comment thread tests/modules/language/ascendc/test.lua Outdated
Comment thread tests/modules/language/ascendc/test.lua Outdated
Comment thread tests/projects/ascendc/libs/src/lib.asc Outdated
Comment thread tests/projects/ascendc/libs/src/main.asc Outdated
Comment thread tests/projects/ascendc/libs/xmake.lua Outdated
Comment thread tests/projects/ascendc/mixed_binary/src/helper.aicpu Outdated
Comment thread tests/projects/ascendc/mixed_binary/src/main.asc Outdated
Comment thread tests/projects/ascendc/mixed_binary/src/main.asc Outdated
Comment thread xmake/platforms/linux/xmake.lua Outdated
Comment thread xmake/rules/ascendc/xmake.lua Outdated
Comment thread xmake/rules/ascendc/xmake.lua Outdated
Comment thread xmake/rules/ascendc/xmake.lua Outdated
Comment thread xmake/toolchains/ascendc/check.lua Outdated
Comment thread xmake/toolchains/ascendc/check.lua Outdated
Comment thread xmake/toolchains/ascendc/load.lua Outdated
Comment thread xmake/toolchains/ascendc/load.lua Outdated
Comment thread xmake/toolchains/ascendc/load.lua Outdated
Comment thread xmake/toolchains/ascendc/load.lua Outdated
Comment thread xmake/toolchains/ascendc/load.lua Outdated
Comment thread xmake/toolchains/ascendc/load.lua Outdated
Comment thread xmake/toolchains/ascendc/xmake.lua
Comment thread xmake/toolchains/ascendc/xmake.lua Outdated
Comment thread xmake/toolchains/ascendc/common.lua Outdated
Comment thread xmake/toolchains/ascendc/common.lua Outdated
@wuzhenqing wuzhenqing force-pushed the dev branch 3 times, most recently from 9cab4bf to 5dae47e Compare May 1, 2026 11:01
Comment thread xmake/modules/core/tools/bisheng.lua Outdated
self:set("ascshflags", "-shared")
-- default C++ standard and source kind for bisheng compiler driver
self:add("ascflags", "-std=c++17", {force = true})
self:add("aicpuflags", "-std=c++17", {force = true})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种不对,应该放到 rules 里去设置 default languages,并且用户如果配置了 set_languages ,应该优先使用,而不是这里写死。

Comment thread xmake/rules/ascendc/xmake.lua Outdated
target:add("rpathdirs", path.join(hostroot, "lib64"))
target:add("rpathdirs", path.join(hostroot, "devlib"))
target:add("rpathdirs", path.join(sdkdir, "lib64"))
target:add("syslinks", "ascendcl", "acl_rt", "dl")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不对,这种都应该放到 toolchain load 中设置

Comment thread xmake/rules/ascendc/xmake.lua Outdated
local hostroot = path.join(sdkdir, host_archdir)

-- aicpu-specific flags (SDK-path-dependent)
target:add("aicpuflags", "-O2")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不要用 flags ,跟 languages 一样处理,走 set_optimize , 并且用户可覆盖

Comment thread xmake/toolchains/ascendc/load.lua Outdated
, x64 = "x86_64-linux"
, arm64 = "aarch64-linux"
, aarch64 = "aarch64-linux"
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没必要三个地方去处理 hostdirs, 统一 check 里处理,load 里通过 toolchain:config 获取 hostroot

Comment thread xmake/toolchains/ascendc/xmake.lua Outdated

-- check toolchain
on_check(function (toolchain)
return import("toolchains.ascendc.check", {rootdir = os.programdir()})(toolchain)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还是不同,参考其他工具链

Comment thread xmake/toolchains/ascendc/xmake.lua Outdated

-- on load
on_load(function (toolchain)
import("toolchains.ascendc.load", {rootdir = os.programdir()})(toolchain)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有这

Add toolchain, language, rules, and compiler modules for the
bisheng compiler driver from CANN (Compute Architecture for
Neural Networks) toolkit. Supports .asc (Ascend C kernel) and
.aicpu (AI-CPU operator) source kinds with binary/static/shared
target types.

- Toolchain: SDK auto-detection via ASCEND_HOME_PATH env or --sdk
- Compiler: bisheng module inherits from clang, adds add_sourceflags
  for explicit sourcekind overrides
- Rules: ascendc.env (default C++17), npuarchs (--npu-arch), build
- Language: check_main detection, add_ascnpuarchs API
- Tests: unit tests for check_main, project tests for libs/mixed

Fixes xmake-io#7506
rule("ascendc.env")
after_load(function (target)
-- set default C++ standard (user can override via set_languages)
target:add("languages", "c++17")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不对,要判断用户没有设置时,才去设置它。

rule("ascendc.env")
after_load(function (target)
-- set default C++ standard (user can override via set_languages)
target:add("languages", "c++17")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外,没必要搞这么多 rules,参考 c++ rules 的。

还有这个 rule 和 ascendc.npuarchs 都没跟 sourcekinds 绑定,会全局影响所有语言。不应该在这里配置。

-- resolve sdkdir: --sdk= > ASCEND_HOME_PATH > ASCEND_TOOLKIT_HOME
local sdkroot = toolchain:sdkdir()
if not sdkroot then
sdkroot = os.getenv("ASCEND_HOME_PATH") or os.getenv("ASCEND_TOOLKIT_HOME")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里也不对,toolchain:sdkdir 是在 load 里和其他地方用的,check 阶段 ,toolchain:config("sdkdir") 都还没设置上,不能用 sdkdir 。。

另外应该在 detect.sdks. 里取做 sdk 的 find 逻辑,参考其他的,这里 check 搞得太复杂。

--

function main(toolchain)
local sdkdir = toolchain:config("sdkdir")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

load 里就直接用 toolchain:sdkdir() 了

toolchain:add("syslinks", "ascendcl", "acl_rt", "dl")

-- aicpu-specific compiler flags (SDK-path-dependent)
toolchain:add("aicpuflags", "-fvisibility-inlines-hidden")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种不应该在 toolchain 里写死,删了

-- aicpu-specific compiler flags (SDK-path-dependent)
toolchain:add("aicpuflags", "-fvisibility-inlines-hidden")
toolchain:add("aicpuflags", "-D_GLIBCXX_USE_CXX11_ABI=0")
toolchain:add("aicpuflags", "-D_GNU_SOURCE")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些也是

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

增加对昇腾 Ascend C toolchain 的支持

2 participants