kunlun.lua 3.89 KB
Newer Older
1
2
add_defines("ENABLE_KUNLUN_API")
local KUNLUN_HOME = os.getenv("KUNLUN_HOME")
zhangyue's avatar
zhangyue committed
3
4
5
local XRE_DIR = path.join(KUNLUN_HOME, "xre")
local XTDK_DIR = path.join(KUNLUN_HOME, "xtdk")
local XDNN_DIR = path.join(KUNLUN_HOME, "xhpc", "xdnn")
zhangyue's avatar
zhangyue committed
6
local XBLAS_DIR = path.join(KUNLUN_HOME, "xhpc", "xblas")
7
local XCCL_DIR = path.join(KUNLUN_HOME, "xccl")
8
9

-- Add include dirs
10
11
12
add_includedirs(path.join(XRE_DIR, "include"), {public = true})
add_includedirs(path.join(XDNN_DIR, "include"), {public = true})
add_includedirs(path.join(XTDK_DIR, "include"), {public = true})
zhangyue's avatar
zhangyue committed
13
add_includedirs(path.join(XBLAS_DIR, "include"), {public = true})
14
15

-- Add link dirs
zhangyue's avatar
zhangyue committed
16
17
add_linkdirs(path.join(XRE_DIR, "so"))
add_linkdirs(path.join(XDNN_DIR, "so"))
zhangyue's avatar
zhangyue committed
18
add_linkdirs(path.join(XBLAS_DIR, "so"))
19

20
21
22
23
24
-- Add rpath
add_rpathdirs(path.join(XRE_DIR, "so"))
add_rpathdirs(path.join(XDNN_DIR, "so"))
add_rpathdirs(path.join(XBLAS_DIR, "so"))

25
-- Add links
zhangyue's avatar
zhangyue committed
26
add_links("xpurt", "xpuapi", "xpu_blas")
27

28
29
30
31
32
rule("xpu")
    set_extensions(".xpu")
    
    on_build_file(function (target, sourcefile)

zhangyue's avatar
zhangyue committed
33
34
35
        local sourcefile_config = target:fileconfig(sourcefile) or {}
        local includedirs = sourcefile_config.includedirs or {}

36
37
38
39
        local objectfile = target:objectfile(sourcefile)
        os.mkdir(path.directory(objectfile))
        local cc = path.join(XTDK_DIR, "bin/clang++")
        local includedirs = table.concat(target:get("includedirs"), " ")
40
41
42
43
44
        local arch_map = {
            ["x86_64"] = "x86_64-linux-gnu",
            ["arm64"] = "aarch64-linux-gnu"
        }

45
46
        local args = {
            "--sysroot=/",
47
            "--target=" .. arch_map[os.arch()],
48
            "-fPIC",
zhangyue's avatar
zhangyue committed
49
50
            "--xpu-arch=xpu3",
            "-std=c++17",
51
52
53
            "-O2",
            "-fno-builtin",
            "-c", sourcefile,
zhangyue's avatar
zhangyue committed
54
            "-o", objectfile
55
56
57
58
59
60
61
        }
        
        for _, includedir in ipairs(target:get("includedirs")) do
            table.insert(args, "-I" .. includedir)
        end

        -- print(args)
zhangyue's avatar
zhangyue committed
62
63
64
        local ok, code = os.execv(cc, args)
        assert(ok == 0, "Compile failed: " .. sourcefile)

65
66
67
68
69
70
71
        table.insert(target:objectfiles(), objectfile)
        print(target:objectfiles())
    end)
rule_end()

local src_dir = path.join(os.projectdir(), "src", "infiniop")

72
73
target("infiniop-kunlun")
    set_kind("static")
PanZezhong's avatar
PanZezhong committed
74
    add_deps("infini-utils")
75
76
    on_install(function (target) end)

zhangyue's avatar
zhangyue committed
77
    add_cxflags("-lstdc++ -fPIC -Wno-error=unused-function")
78
    add_cxxflags("-lstdc++ -fPIC -Wno-error=unused-function")
79
80
81
82
    set_warnings("all", "error")

    set_languages("cxx17")
    add_files("$(projectdir)/src/infiniop/devices/kunlun/*.cc", "$(projectdir)/src/infiniop/ops/*/kunlun/*.cc")
83
84
85
    -- compile handwriting kernel
    local xpu_files = os.files(src_dir .. "/ops/*/kunlun/*.xpu")
    if #xpu_files > 0 then
zhangyue's avatar
zhangyue committed
86
87
88
        add_files(xpu_files, {
            rule = "xpu",
            includedirs = {
89
                path.join(os.projectdir(), "include"),
zhangyue's avatar
zhangyue committed
90
91
92
93
94
                path.join(XRE_DIR, "include"),
                path.join(XDNN_DIR, "include"),
                path.join(XTDK_DIR, "include")
            }
        })
95
    end
96
target_end()
zhangyue's avatar
zhangyue committed
97
98
99
100
101
102
103

target("infinirt-kunlun")
    set_kind("static")
    add_deps("infini-utils")
    set_languages("cxx17")
    on_install(function (target) end)
    -- Add include dirs
zhangyue's avatar
zhangyue committed
104
    add_files("$(projectdir)/src/infinirt/kunlun/*.cc")
zhangyue's avatar
zhangyue committed
105
    add_cxflags("-lstdc++ -Wall -Werror -fPIC")
106
    add_cxxflags("-lstdc++ -Wall -Werror -fPIC")
107
target_end()
zhangyue's avatar
zhangyue committed
108

109
110
111
112
113
114
115
116
117
118
119
120
121
target("infiniccl-kunlun")
    set_kind("static")
    add_deps("infinirt")
    add_deps("infini-utils")
    set_warnings("all", "error")
    set_languages("cxx17")
    on_install(function (target) end)
    if has_config("ccl") then
        add_includedirs(path.join(XCCL_DIR, "include"))
        add_linkdirs(path.join(XCCL_DIR, "so"))
        add_links("bkcl")
        add_files("$(projectdir)/src/infiniccl/kunlun/*.cc")
        add_cxflags("-lstdc++ -fPIC")
122
        add_cxxflags("-lstdc++ -fPIC")
123
    end
zhangyue's avatar
zhangyue committed
124
target_end()