Debian Codegraph @

tensilelite/Tensile/Toolchain/Source.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 re
26 import shutil
27
28 from pathlib import Path
29 from timeit import default_timer as timer
30 from typing import List, Union, NamedTuple
31
32 from ..Common import print1, ensurePath
33
34 from .Component import Compiler, Bundler
35
36 class SourceToolchain(NamedTuple):
37 compiler: Compiler
38 bundler: Bundler
39
40
41 def makeSourceToolchain(compiler_path, bundler_path, asan_build=False, build_id_kind="sha1", save_temps=False):
42 compiler = Compiler(compiler_path, build_id_kind, asan_build, save_temps)
43 bundler = Bundler(bundler_path)
44 return SourceToolchain(compiler, bundler)
45
46
47 def _computeSourceCodeObjectFilename(target: str, base: str, buildPath: Union[Path, str], arch: str) -> Union[Path, None]:
48 """Generates a code object file path using the target, base, and build path.
49
50 Args:
51 target: The target triple.
52 base: The base name for the output file (name without extension).
53 buildPath: The build directory path.
54
55 Returns:
56 Path to the code object file.
57 """
58 coPath = None
59 buildPath = Path(buildPath)
60 if "TensileLibrary" in base and "fallback" in base:
61 coPath = buildPath / "{0}_{1}.hsaco.raw".format(base, arch)
62 elif "TensileLibrary" in base:
63 variant = [t for t in ["", "xnack-", "xnack+"] if t in target][-1]
64 baseVariant = base + "-" + variant if variant else base
65 if arch in baseVariant:
66 coPath = buildPath / (baseVariant + ".hsaco.raw")
67 else:
68 coPath= buildPath / "{0}.so-000-{1}.hsaco.raw".format(base, arch)
69
70 return coPath
71
72
73 def buildSourceCodeObjectFiles(
74 compiler: Compiler,
75 bundler: Bundler,
76 destDir: Union[Path, str],
77 tmpObjDir: Union[Path, str],
78 includeDir: Union[Path, str],
79 kernelPath: Union[Path, str],
80 cmdlineArchs: List[str]
81 ) -> List[str]:
82 """Compiles a HIP source code file into a code object file.
83
84 Args:
85 toolchain: The source toolchain.
86 destDir: The destination directory where HSA code object files are placed.
87 tmpObjDir: The directory where HIP source object files are created.
88 includeDir: The include directory path.
89 kernelPath: The path to the kernel source file.
90
91 Returns:
92 List of paths to the created code objects.
93 """
94 start = timer()
95
96 tmpObjDir = Path(ensurePath(tmpObjDir))
97 destDir = Path(ensurePath(destDir))
98 kernelPath = Path(kernelPath)
99
100 objFilename = kernelPath.stem + '.o'
101 coPathsRaw = []
102 coPaths= []
103
104 objPath = str(tmpObjDir / objFilename)
105 compiler(str(includeDir), cmdlineArchs, str(kernelPath), objPath)
106
107 for target in bundler.targets(objPath):
108 match = re.search("gfx.*$", target)
109 if match:
110 arch = re.sub(":", "-", match.group())
111 coPathRaw = _computeSourceCodeObjectFilename(target, kernelPath.stem, tmpObjDir, arch)
112 if not coPathRaw: continue
113 bundler(target, objPath, str(coPathRaw))
114
115 coPath = str(destDir / coPathRaw.stem)
116 coPathsRaw.append(coPathRaw)
117 coPaths.append(coPath)
118
119 for src, dst in zip(coPathsRaw, coPaths):
120 shutil.move(src, dst)
121
122 stop = timer()
123 print1(f"buildSourceCodeObjectFile time (s): {(stop-start):3.2f}")
124
125 return coPaths