From 3a65347e0f9687da70b2f1ac003128f3a297bdf4 Mon Sep 17 00:00:00 2001 From: Earl Ou Date: Tue, 28 Jun 2022 12:04:18 +0000 Subject: [PATCH] python: Avoid re-adding child when cloning SimObject For SimObject type param, we should avoid duplicated addChild call if it already belongs to other parent. In the original implementation, the following code: ``` class A(SimObject): ... class B(SimObject): a = Param.A(...) class Top(RealView): a = A() b = B(a=a) ``` will generate incorrect warning: ``` warn: .a already has parent not resetting parent. Note: a is not a parameter of B warn: (Previously declared as .a) ``` The code tries to add `a` as the child of `Top` as well as child of `Top.b`, which is incorrect. Change-Id: I8c55c5dd4cc0dd45c68169a2b08450ff053c07aa Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/60789 Reviewed-by: Yu-hsin Wang Maintainer: Jason Lowe-Power Tested-by: kokoro Reviewed-by: Jason Lowe-Power --- src/python/m5/SimObject.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/python/m5/SimObject.py b/src/python/m5/SimObject.py index 26147a1757..50d40aea03 100644 --- a/src/python/m5/SimObject.py +++ b/src/python/m5/SimObject.py @@ -697,7 +697,9 @@ class SimObject(object, metaclass=MetaSimObject): # are also param values get cloned properly. self._children = {} for key,val in ancestor._children.items(): - self.add_child(key, val(_memo=memo_dict)) + newval = val(_memo=memo_dict) + if not newval.has_parent(): + self.add_child(key, newval) # Inherit parameter values from class using multidict so # individual value settings can be overridden but we still