| 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 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 |