File: analyzer.py

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (54 lines) | stat: -rw-r--r-- 2,232 bytes parent folder | download | duplicates (7)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python

import subprocess
import os.path
import yaml
import io
import re

def parseKernelUsages(usageStr, usageDict):
    demangler = 'c++filt -p'

    def getKernelMem(usages):
        match = re.search(r"([0-9]+) bytes cmem\[0\]", usages)
        return match.group(1) if match else None
    def getSharedMem(usages):
        match = re.search(r"([0-9]+) bytes smem", usages)
        return match.group(1) if match else None
    def getRegisters(usages):
        match = re.search(r"[Uu]sed ([0-9]+) registers", usages)
        return match.group(1) if match else None
    def demangle(fn):
        expr = re.compile("__omp_offloading_[a-zA-Z0-9]*_[a-zA-Z0-9]*_(_Z.*_)_l[0-9]*$")
        match = expr.search(fn)
        function = match.group(1) if match else fn
        output = subprocess.run(demangler.split(' ') + [function], check=True, stdout=subprocess.PIPE)
        return output.stdout.decode('utf-8').strip()
    def getLine(fn):
        expr = re.compile("__omp_offloading_[a-zA-Z0-9]*_[a-zA-Z0-9]*_.*_l([0-9]*)$")
        match = expr.search(fn)
        return match.group(1) if match else 0

    expr = re.compile("Function properties for \'?([a-zA-Z0-9_]*)\'?\n(.*,.*)\n")
    for (fn, usages) in expr.findall(usageStr):
        info = usageDict[fn] if fn in usageDict else dict()
        info["Name"] = demangle(fn)
        info["DebugLoc"] = {"File" : "unknown", "Line": getLine(fn), "Column" : 0}
        info["Usage"] = {"Registers" : getRegisters(usages), "Shared" : getSharedMem(usages), "Kernel" : getKernelMem(usages)}
        usageDict[fn] = info

def getKernelUsage(stderr, fname='usage.yaml'):
    remarks = [line for line in stderr.split('\n') if re.search(r"^remark:", line)]
    ptxas = '\n'.join([line.split(':')[1].strip() for line in stderr.split('\n') if re.search(r"^ptxas info *:", line)])
    nvlink = '\n'.join([line.split(':')[1].strip() for line in stderr.split('\n') if re.search(r"^nvlink info *:", line)])

    if os.path.exists(fname):
        with io.open(fname, 'r', encoding = 'utf-8') as f:
            usage = yaml.load(f, Loader=yaml.Loader)
    else:
        usage = dict()

    parseKernelUsages(ptxas, usage)
    parseKernelUsages(nvlink, usage)

    return usage