Debian Codegraph @

tensilelite/Tensile/Toolchain/Assembly.py

raw symbols SCIP
1 ################################################################################
2 #
3 # Copyright (C) 2025 Advanced Micro Devices, Inc. All rights reserved.
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22 #
23 ################################################################################
24
25 import collections
26 import math
27 import shutil
28 import subprocess
29
30 from pathlib import Path
31 from typing import List, Union, NamedTuple
32
33 from Tensile.Common import print2
34 from Tensile.Common.Architectures import isaToGfx
35 from ..SolutionStructs import Solution
36
37 from .Component import Assembler, Linker, Bundler
38
39 class AssemblyToolchain(NamedTuple):
40 assembler: Assembler
41 linker: Linker
42 bundler: Bundler
43
44
45 def makeAssemblyToolchain(assembler_path, bundler_path, co_version, build_id_kind="sha1", debug=False):
46 compiler = Assembler(assembler_path, co_version, debug)
47 linker = Linker(assembler_path, build_id_kind)
48 bundler = Bundler(bundler_path)
49 return AssemblyToolchain(compiler, linker, bundler)
50
51
52 def buildAssemblyCodeObjectFiles(
53 linker: Linker,
54 bundler: Bundler,
55 kernels: List[Solution],
56 destDir: Union[Path, str],
57 asmDir: Union[Path, str],
58 compress: bool=True,
59 ):
60 """Builds code object files from assembly files
61
62 Args:
63 toolchain: The assembly toolchain object to use for building.
64 kernels: A list of the kernel objects to build.
65 writer: The KernelWriterAssembly object to use.
66 destDir: The destination directory for the code object files.
67 asmDir: The directory containing the assembly files.
68 compress: Whether to compress the code object files.
69 """
70
71 extObj = ".o"
72 extCo = ".co"
73 extCoRaw = ".co.raw"
74
75 archKernelMap = collections.defaultdict(list)
76 for k in kernels:
77 archKernelMap[tuple(k['ISA'])].append(k)
78
79 coFiles = []
80 for arch, archKernels in archKernelMap.items():
81 if len(archKernels) == 0:
82 continue
83
84 gfx = isaToGfx(arch)
85
86 objectFiles = [str(asmDir / (k["BaseName"] + extObj)) for k in archKernels if 'codeObjectFile' not in k]
87 coFileMap = collections.defaultdict(set)
88 if len(objectFiles):
89 coFileMap[asmDir / ("TensileLibrary_"+ gfx + extCoRaw)] = objectFiles
90 for kernel in archKernels:
91 coName = kernel.get("codeObjectFile", None)
92 if coName:
93 coFileMap[asmDir / (coName + extCoRaw)].add(str(asmDir / (kernel["BaseName"] + extObj)))
94
95 for coFileRaw, objFiles in coFileMap.items():
96 linker(objFiles, str(coFileRaw))
97 coFile = destDir / coFileRaw.name.replace(extCoRaw, extCo)
98 if compress:
99 bundler.compress(str(coFileRaw), str(coFile), gfx)
100 else:
101 shutil.move(coFileRaw, coFile)
102 coFiles.append(coFile)
103
104 return coFiles