| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |