Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
weberlo committed Nov 11, 2019
1 parent b1008f2 commit 5b86677
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 49 deletions.
23 changes: 11 additions & 12 deletions python/tvm/contrib/binutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import os
import subprocess
from . import util
from .._ffi.base import py_str
from ..api import register_func

RELOCATION_LD_SCRIPT_TEMPLATE = """
Expand Down Expand Up @@ -84,9 +83,9 @@ def run_cmd(cmd):
resulting stdout capture from the subprocess
"""
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
(output, _) = proc.communicate()
output = output.decode('utf-8')
if proc.returncode != 0:
Expand Down Expand Up @@ -140,8 +139,8 @@ def tvm_callback_get_section_size(binary_path, section_name, toolchain_prefix):
continue
entry_name = tokens[0]
entry_size = int(tokens[1])
for section_name in sections_to_sum:
if entry_name.startswith(section_name):
for section in sections_to_sum:
if entry_name.startswith(section):
section_size += entry_size
break

Expand Down Expand Up @@ -212,12 +211,12 @@ def tvm_callback_relocate_binary(
if 'riscv' in toolchain_prefix:
ld_script_contents += 'OUTPUT_ARCH( "riscv" )\n\n'
ld_script_contents += RELOCATION_LD_SCRIPT_TEMPLATE.format(
word_size=word_size,
text_start=text_start,
rodata_start=rodata_start,
data_start=data_start,
bss_start=bss_start,
stack_pointer_init=stack_pointer_init)
word_size=word_size,
text_start=text_start,
rodata_start=rodata_start,
data_start=data_start,
bss_start=bss_start,
stack_pointer_init=stack_pointer_init)

tmp_dir = util.tempdir()
rel_obj_path = tmp_dir.relpath('relocated.obj')
Expand Down
10 changes: 4 additions & 6 deletions python/tvm/micro/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@

from __future__ import absolute_import

import logging
import os
import sys
from enum import Enum
from pathlib import Path

import tvm
from tvm.contrib import util as _util
from tvm.contrib import cc as _cc
from .._ffi.function import _init_api
from .._ffi.libinfo import find_include_path

class LibType(Enum):
"""Enumeration of library types that can be compiled and loaded onto a device"""
Expand Down Expand Up @@ -62,7 +59,8 @@ def __init__(self, config):
# TODO(weberlo): add config validation

# grab a binutil instance from the ID in the config
self.create_micro_lib = tvm.micro.device.get_device_funcs(config['device_id'])['create_micro_lib']
dev_funcs = tvm.micro.device.get_device_funcs(config['device_id'])
self.create_micro_lib = dev_funcs['create_micro_lib']
self.toolchain_prefix = config['toolchain_prefix']
self.mem_layout = config['mem_layout']
self.word_size = config['word_size']
Expand Down Expand Up @@ -129,8 +127,8 @@ def create_micro_mod(self, c_mod):
temp_dir = _util.tempdir()
lib_obj_path = temp_dir.relpath('dev_lib.obj')
c_mod.export_library(
lib_obj_path,
fcompile=cross_compiler(self.create_micro_lib, LibType.OPERATOR))
lib_obj_path,
fcompile=cross_compiler(self.create_micro_lib, LibType.OPERATOR))
micro_mod = tvm.module.load(lib_obj_path)
return micro_mod

Expand Down
3 changes: 2 additions & 1 deletion python/tvm/micro/device/arm/stm32f746xx.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def create_micro_lib(obj_path, src_path, lib_type, options=None):
'-mthumb',
'-gdwarf-5',
]
create_micro_lib_base(obj_path, src_path, TOOLCHAIN_PREFIX, DEVICE_ID, lib_type, options=options)
create_micro_lib_base(
obj_path, src_path, TOOLCHAIN_PREFIX, DEVICE_ID, lib_type, options=options)


def default_config(server_addr, server_port):
Expand Down
47 changes: 23 additions & 24 deletions python/tvm/micro/device/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
"""Base definitions for MicroTVM config"""
import glob
import os
import sys
from enum import Enum
from pathlib import Path

from tvm.contrib import util as _util
Expand Down Expand Up @@ -63,15 +61,15 @@ def get_device_funcs(device_id):
return device_funcs


def create_micro_lib_base(obj_path, src_path, toolchain_prefix, device_id, lib_type, options=None):
def create_micro_lib_base(out_obj_path, in_src_path, toolchain_prefix, device_id, lib_type, options=None):
"""Compiles code into a binary for the target micro device.
Parameters
----------
obj_path : str
out_obj_path : str
path to generated object file
src_path : str
in_src_path : str
path to source file
toolchain_prefix : str
Expand All @@ -90,27 +88,28 @@ def create_micro_lib_base(obj_path, src_path, toolchain_prefix, device_id, lib_t
additional options to pass to GCC
"""
base_compile_cmd = [
f'{toolchain_prefix}gcc',
'-std=c11',
'-Wall',
'-Wextra',
'--pedantic',
'-c',
'-O0',
'-g',
'-nostartfiles',
'-nodefaultlibs',
'-nostdlib',
'-fdata-sections',
'-ffunction-sections',
]
f'{toolchain_prefix}gcc',
'-std=c11',
'-Wall',
'-Wextra',
'--pedantic',
'-c',
'-O0',
'-g',
'-nostartfiles',
'-nodefaultlibs',
'-nostdlib',
'-fdata-sections',
'-ffunction-sections',
]
if options is not None:
base_compile_cmd += options

src_paths = []
include_paths = find_include_path() + [get_micro_host_driven_dir()]
ld_script_path = None
tmp_dir = _util.tempdir()
# we might transform the src path in one of the branches below
new_in_src_path = in_src_path
if lib_type == LibType.RUNTIME:
dev_dir = _get_device_source_dir(device_id)
dev_src_paths = glob.glob(f'{dev_dir}/*.[csS]')
Expand All @@ -122,17 +121,17 @@ def create_micro_lib_base(obj_path, src_path, toolchain_prefix, device_id, lib_t
# create a temporary copy of the source, so we can inject the dev lib
# header without modifying the original.
temp_src_path = tmp_dir.relpath('temp.c')
with open(src_path, 'r') as f:
with open(in_src_path, 'r') as f:
src_lines = f.read().splitlines()
src_lines.insert(0, '#include "utvm_device_dylib_redirect.c"')
with open(temp_src_path, 'w') as f:
f.write('\n'.join(src_lines))
src_path = temp_src_path
new_in_src_path = temp_src_path
base_compile_cmd += ['-c']
else:
raise RuntimeError('unknown lib type')

src_paths += [src_path]
src_paths += [new_in_src_path]

for path in include_paths:
base_compile_cmd += ['-I', path]
Expand All @@ -147,7 +146,7 @@ def create_micro_lib_base(obj_path, src_path, toolchain_prefix, device_id, lib_t

ld_cmd = [f'{toolchain_prefix}ld', '-relocatable']
ld_cmd += prereq_obj_paths
ld_cmd += ['-o', obj_path]
ld_cmd += ['-o', out_obj_path]
run_cmd(ld_cmd)


Expand Down
3 changes: 2 additions & 1 deletion python/tvm/micro/device/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def create_micro_lib(obj_path, src_path, lib_type, options=None):
options = []
if sys.maxsize > 2**32 and sys.platform.startswith('linux'):
options += ['-mcmodel=large']
create_micro_lib_base(obj_path, src_path, TOOLCHAIN_PREFIX, DEVICE_ID, lib_type, options=options)
create_micro_lib_base(
obj_path, src_path, TOOLCHAIN_PREFIX, DEVICE_ID, lib_type, options=options)


def default_config():
Expand Down
12 changes: 9 additions & 3 deletions python/tvm/micro/device/riscv_spike.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ def create_micro_lib(obj_path, src_path, lib_type, options=None):
options : Optional[List[str]]
additional options to pass to GCC
"""
create_micro_lib_base(obj_path, src_path, TOOLCHAIN_PREFIX, DEVICE_ID, lib_type, options=options)
create_micro_lib_base(
obj_path,
src_path,
TOOLCHAIN_PREFIX,
DEVICE_ID,
lib_type,
options=options)


def default_config(base_addr, server_addr, server_port):
Expand Down Expand Up @@ -100,8 +106,8 @@ def default_config(base_addr, server_addr, server_port):
curr_offset = 0
mem_layout = res['mem_layout']
for section_name, region_dict in mem_layout:
mem_layout[section_name]['start'] = base_addr + curr_offset
curr_offset += mem_layout[section_name]['size']
region_dict['start'] = base_addr + curr_offset
curr_offset += region_dict['size']
return res


Expand Down
3 changes: 1 addition & 2 deletions python/tvm/micro/rpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

import argparse
import ast
import ctypes
import json
import logging
import os
import tvm
from tvm import rpc
from tvm import micro
Expand Down Expand Up @@ -71,6 +69,7 @@ def main():

@tvm.register_func('tvm.rpc.server.start', override=True)
def server_start():
# pylint: disable=unused-variable
session = micro.Session(dev_config)
session._enter()

Expand Down

0 comments on commit 5b86677

Please sign in to comment.