python,util: Pull param struct generation code out of SimObject.

Change-Id: I9f9c3b858a214650f6f07e6127bb316a227982a0
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/49450
Reviewed-by: Gabe Black <gabe.black@gmail.com>
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Gabe Black <gabe.black@gmail.com>
Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabe Black
2021-08-19 21:59:22 -07:00
parent f42b198371
commit df540f0dbf
3 changed files with 387 additions and 356 deletions

View File

@@ -1,5 +1,18 @@
# Copyright 2004-2006 The Regents of The University of Michigan
# Copyright 2010-20013 Advanced Micro Devices, Inc.
# Copyright 2013 Mark D. Hill and David A. Wood
# Copyright 2017-2020 ARM Limited
# Copyright 2021 Google, Inc.
#
# The license below extends only to copyright in the software and shall
# not be construed as granting a license to any other intellectual
# property including but not limited to intellectual property relating
# to a hardware implementation of the functionality of the software
# licensed hereunder. You may use the software subject to the license
# terms below provided that you ensure that this notice is replicated
# unmodified and in its entirety in all distributions of the software,
# modified or unmodified, in source code or in binary form.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
@@ -57,6 +70,204 @@ importer.install()
module = importlib.import_module(args.modpath)
sim_object = getattr(module, sim_object_name)
from m5.objects.SimObject import PyBindProperty
code = code_formatter()
sim_object.params_create_decl(code, use_python)
py_class_name = sim_object.pybind_class
# The 'local' attribute restricts us to the params declared in
# the object itself, not including inherited params (which
# will also be inherited from the base class's param struct
# here). Sort the params based on their key
params = list(map(lambda k_v: k_v[1],
sorted(sim_object._params.local.items())))
ports = sim_object._ports.local
# only include pybind if python is enabled in the build
if use_python:
code('''#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
#include <type_traits>
#include "base/compiler.hh"
#include "params/$sim_object.hh"
#include "sim/init.hh"
#include "sim/sim_object.hh"
#include "${{sim_object.cxx_header}}"
''')
else:
code('''
#include <type_traits>
#include "base/compiler.hh"
#include "params/$sim_object.hh"
#include "${{sim_object.cxx_header}}"
''')
# only include the python params code if python is enabled.
if use_python:
for param in params:
param.pybind_predecls(code)
code('''namespace py = pybind11;
namespace gem5
{
static void
module_init(py::module_ &m_internal)
{
py::module_ m = m_internal.def_submodule("param_${sim_object}");
''')
code.indent()
if sim_object._base:
code('py::class_<${sim_object}Params, ' \
'${{sim_object._base.type}}Params, ' \
'std::unique_ptr<${{sim_object}}Params, py::nodelete>>(' \
'm, "${sim_object}Params")')
else:
code('py::class_<${sim_object}Params, ' \
'std::unique_ptr<${sim_object}Params, py::nodelete>>(' \
'm, "${sim_object}Params")')
code.indent()
if not hasattr(sim_object, 'abstract') or not sim_object.abstract:
code('.def(py::init<>())')
code('.def("create", &${sim_object}Params::create)')
param_exports = sim_object.cxx_param_exports + [
PyBindProperty(k)
for k, v in sorted(sim_object._params.local.items())
] + [
PyBindProperty(f"port_{port.name}_connection_count")
for port in ports.values()
]
for exp in param_exports:
exp.export(code, f"{sim_object}Params")
code(';')
code()
code.dedent()
bases = []
if 'cxx_base' in sim_object._value_dict:
# If the c++ base class implied by python inheritance was
# overridden, use that value.
if sim_object.cxx_base:
bases.append(sim_object.cxx_base)
elif sim_object._base:
# If not and if there was a SimObject base, use its c++ class
# as this class' base.
bases.append(sim_object._base.cxx_class)
# Add in any extra bases that were requested.
bases.extend(sim_object.cxx_extra_bases)
if bases:
base_str = ", ".join(bases)
code('py::class_<${{sim_object.cxx_class}}, ${base_str}, ' \
'std::unique_ptr<${{sim_object.cxx_class}}, py::nodelete>>(' \
'm, "${py_class_name}")')
else:
code('py::class_<${{sim_object.cxx_class}}, ' \
'std::unique_ptr<${{sim_object.cxx_class}}, py::nodelete>>(' \
'm, "${py_class_name}")')
code.indent()
for exp in sim_object.cxx_exports:
exp.export(code, sim_object.cxx_class)
code(';')
code.dedent()
code()
code.dedent()
code('}')
code()
code('static EmbeddedPyBind '
'embed_obj("${0}", module_init, "${1}");',
sim_object, sim_object._base.type if sim_object._base else "")
code()
code('} // namespace gem5')
# include the create() methods whether or not python is enabled.
if not hasattr(sim_object, 'abstract') or not sim_object.abstract:
if 'type' in sim_object.__dict__:
code('''
namespace gem5
{
namespace
{
/*
* If we can't define a default create() method for this params
* struct because the SimObject doesn't have the right
* constructor, use template magic to make it so we're actually
* defining a create method for this class instead.
*/
class Dummy${sim_object}ParamsClass
{
public:
${{sim_object.cxx_class}} *create() const;
};
template <class CxxClass, class Enable=void>
class Dummy${sim_object}Shunt;
/*
* This version directs to the real Params struct and the
* default behavior of create if there's an appropriate
* constructor.
*/
template <class CxxClass>
class Dummy${sim_object}Shunt<CxxClass, std::enable_if_t<
std::is_constructible_v<CxxClass, const ${sim_object}Params &>>>
{
public:
using Params = ${sim_object}Params;
static ${{sim_object.cxx_class}} *
create(const Params &p)
{
return new CxxClass(p);
}
};
/*
* This version diverts to the DummyParamsClass and a dummy
* implementation of create if the appropriate constructor does
* not exist.
*/
template <class CxxClass>
class Dummy${sim_object}Shunt<CxxClass, std::enable_if_t<
!std::is_constructible_v<CxxClass, const ${sim_object}Params &>>>
{
public:
using Params = Dummy${sim_object}ParamsClass;
static ${{sim_object.cxx_class}} *
create(const Params &p)
{
return nullptr;
}
};
} // anonymous namespace
/*
* An implementation of either the real Params struct's create
* method, or the Dummy one. Either an implementation is
* mandantory since this was shunted off to the dummy class, or
* one is optional which will override this weak version.
*/
[[maybe_unused]] ${{sim_object.cxx_class}} *
Dummy${sim_object}Shunt<${{sim_object.cxx_class}}>::Params::create() const
{
return Dummy${sim_object}Shunt<${{sim_object.cxx_class}}>::create(*this);
}
} // namespace gem5
''')
code.write(args.param_cc)

View File

@@ -1,5 +1,18 @@
# Copyright 2004-2006 The Regents of The University of Michigan
# Copyright 2010-20013 Advanced Micro Devices, Inc.
# Copyright 2013 Mark D. Hill and David A. Wood
# Copyright 2017-2020 ARM Limited
# Copyright 2021 Google, Inc.
#
# The license below extends only to copyright in the software and shall
# not be construed as granting a license to any other intellectual
# property including but not limited to intellectual property relating
# to a hardware implementation of the functionality of the software
# licensed hereunder. You may use the software subject to the license
# terms below provided that you ensure that this notice is replicated
# unmodified and in its entirety in all distributions of the software,
# modified or unmodified, in source code or in binary form.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
@@ -45,6 +58,167 @@ importer.install()
module = importlib.import_module(args.modpath)
sim_object = getattr(module, sim_object_name)
from m5.objects.SimObject import SimObject
from m5.params import Enum
code = code_formatter()
sim_object.cxx_param_decl(code)
# The 'local' attribute restricts us to the params declared in
# the object itself, not including inherited params (which
# will also be inherited from the base class's param struct
# here). Sort the params based on their key
params = list(map(lambda k_v: k_v[1],
sorted(sim_object._params.local.items())))
ports = sim_object._ports.local
try:
ptypes = [p.ptype for p in params]
except:
print(sim_object, p, p.ptype_str)
print(params)
raise
warned_about_nested_templates = False
class CxxClass(object):
def __init__(self, sig, template_params=[]):
# Split the signature into its constituent parts. This could
# potentially be done with regular expressions, but
# it's simple enough to pick appart a class signature
# manually.
parts = sig.split('<', 1)
base = parts[0]
t_args = []
if len(parts) > 1:
# The signature had template arguments.
text = parts[1].rstrip(' \t\n>')
arg = ''
# Keep track of nesting to avoid splitting on ","s embedded
# in the arguments themselves.
depth = 0
for c in text:
if c == '<':
depth = depth + 1
if depth > 0 and not warned_about_nested_templates:
warned_about_nested_templates = True
print('Nested template argument in cxx_class.'
' This feature is largely untested and '
' may not work.')
elif c == '>':
depth = depth - 1
elif c == ',' and depth == 0:
t_args.append(arg.strip())
arg = ''
else:
arg = arg + c
if arg:
t_args.append(arg.strip())
# Split the non-template part on :: boundaries.
class_path = base.split('::')
# The namespaces are everything except the last part of the class path.
self.namespaces = class_path[:-1]
# And the class name is the last part.
self.name = class_path[-1]
self.template_params = template_params
self.template_arguments = []
# Iterate through the template arguments and their values. This
# will likely break if parameter packs are used.
for arg, param in zip(t_args, template_params):
type_keys = ('class', 'typename')
# If a parameter is a type, parse it recursively. Otherwise
# assume it's a constant, and store it verbatim.
if any(param.strip().startswith(kw) for kw in type_keys):
self.template_arguments.append(CxxClass(arg))
else:
self.template_arguments.append(arg)
def declare(self, code):
# First declare any template argument types.
for arg in self.template_arguments:
if isinstance(arg, CxxClass):
arg.declare(code)
# Re-open the target namespace.
for ns in self.namespaces:
code('namespace $ns {')
# If this is a class template...
if self.template_params:
code('template <${{", ".join(self.template_params)}}>')
# The actual class declaration.
code('class ${{self.name}};')
# Close the target namespaces.
for ns in reversed(self.namespaces):
code('} // namespace $ns')
code('''\
#ifndef __PARAMS__${sim_object}__
#define __PARAMS__${sim_object}__
''')
# The base SimObject has a couple of params that get
# automatically set from Python without being declared through
# the normal Param mechanism; we slip them in here (needed
# predecls now, actual declarations below)
if sim_object == SimObject:
code('''#include <string>''')
cxx_class = CxxClass(sim_object._value_dict['cxx_class'],
sim_object._value_dict['cxx_template_params'])
# A forward class declaration is sufficient since we are just
# declaring a pointer.
cxx_class.declare(code)
for param in params:
param.cxx_predecls(code)
for port in ports.values():
port.cxx_predecls(code)
code()
if sim_object._base:
code('#include "params/${{sim_object._base.type}}.hh"')
code()
for ptype in ptypes:
if issubclass(ptype, Enum):
code('#include "enums/${{ptype.__name__}}.hh"')
code()
code('namespace gem5')
code('{')
code('')
# now generate the actual param struct
code("struct ${sim_object}Params")
if sim_object._base:
code(" : public ${{sim_object._base.type}}Params")
code("{")
if not hasattr(sim_object, 'abstract') or not sim_object.abstract:
if 'type' in sim_object.__dict__:
code(" ${{sim_object.cxx_type}} create() const;")
code.indent()
if sim_object == SimObject:
code('''
SimObjectParams() {}
virtual ~SimObjectParams() {}
std::string name;
''')
for param in params:
param.cxx_decl(code)
for port in ports.values():
port.cxx_decl(code)
code.dedent()
code('};')
code()
code('} // namespace gem5')
code()
code('#endif // __PARAMS__${sim_object}__')
code.write(args.param_hh)