Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't have a randsz_list_t of randobj #166

Closed
fdrichardson opened this issue Oct 11, 2022 · 1 comment
Closed

Can't have a randsz_list_t of randobj #166

fdrichardson opened this issue Oct 11, 2022 · 1 comment

Comments

@fdrichardson
Copy link

With a randobj having a randsz_list of randobj:

@vsc.randobj
class MyRandObject:
    def __init__(self):
        self.a =vsc.rand_bit_t(4)

@vsc.randobj
class TopRandObj:
    def __init__(self):
        self.a = vsc.randsz_list_t(MyRandObject())

    @vsc.constraint
    def a_c(self):
        self.a.size < 5
        self.a.size > 1

my_top_rand = TopRandObj()
my_top_rand.randomize()

I get:


ValueError Traceback (most recent call last)
Cell In [13], line 17
14 self.a.size > 1
16 my_top_rand = TopRandObj()
---> 17 my_top_rand.randomize()

File ~/.local/lib/python3.10/site-packages/vsc/rand_obj.py:162, in _randobj.call..randomize(self, debug, lint, solve_fail_debug)
160 model = self.get_model()
161 try:
--> 162 Randomizer.do_randomize(
163 ro_int.get_randstate(),
164 SourceInfo(frame.filename, frame.lineno),
165 [model],
166 debug=debug,
167 lint=lint,
168 solve_fail_debug=solve_fail_debug)
169 except SolveFailure as e:
170 print(e.diagnostics)

File ~/.local/lib/python3.10/site-packages/vsc/model/randomizer.py:557, in Randomizer.do_randomize(randstate, srcinfo, field_model_l, constraint_l, debug, lint, solve_fail_debug)
555 constraints_len = len(constraint_l)
556 for fm in field_model_l:
--> 557 constraint_l.extend(ArrayConstraintBuilder.build(
558 fm, bounds_v.bound_m))
559 # Now, handle dist constraints
560 DistConstraintBuilder.build(randstate, fm)

File ~/.local/lib/python3.10/site-packages/vsc/visitors/array_constraint_builder.py:48, in ArrayConstraintBuilder.build(m, bound_m)
46 builder = ArrayConstraintBuilder(bound_m)
47 builder.phase = 0
---> 48 m.accept(builder)
49 builder.phase = 1
50 m.accept(builder)

File ~/.local/lib/python3.10/site-packages/vsc/model/field_composite_model.py:154, in FieldCompositeModel.accept(self, v)
153 def accept(self, v):
--> 154 v.visit_composite_field(self)

File ~/.local/lib/python3.10/site-packages/vsc/model/model_visitor.py:64, in ModelVisitor.visit_composite_field(self, f)
61 def visit_composite_field(self, f : FieldCompositeModel):
62 # Visit fields
63 for fi in f.field_l:
---> 64 fi.accept(self)
66 # Visit constraints
67 for c in f.constraint_model_l:

File ~/.local/lib/python3.10/site-packages/vsc/model/field_array_model.py:197, in FieldArrayModel.accept(self, v)
196 def accept(self, v):
--> 197 v.visit_field_scalar_array(self)

File ~/.local/lib/python3.10/site-packages/vsc/visitors/array_constraint_builder.py:176, in ArrayConstraintBuilder.visit_field_scalar_array(self, f)
173 if len(f.field_l) < max_size:
174 # Extend the size appropriately
175 for i in range(max_size-len(f.field_l)):
--> 176 f.add_field()
177 elif self.phase == 1:
178 if not f.is_scalar:
179 # Need to recurse into sub-fields for non-scalar arrays

File ~/.local/lib/python3.10/site-packages/vsc/model/field_array_model.py:105, in FieldArrayModel.add_field(self)
100 ret = super().add_field(EnumFieldModel(
101 self.name + "[" + str(fid) + "]",
102 self.enums,
103 self.is_declared_rand))
104 else:
--> 105 ret = super().add_field(FieldScalarModel(
106 self.name + "[" + str(fid) + "]",
107 self.width,
108 self.is_signed,
109 self.is_declared_rand))
110 # Update the size
111 self._set_size(len(self.field_l))

File ~/.local/lib/python3.10/site-packages/vsc/model/field_scalar_model.py:39, in FieldScalarModel.init(self, name, width, is_signed, is_rand, rand_if)
37 super().init(name)
38 self.width = width
---> 39 self.mask = (1 << width)-1
40 self.is_signed = is_signed
41 self.is_declared_rand = is_rand

ValueError: negative shift count

mballance added a commit that referenced this issue Oct 12, 2022
- (#166) Correct rand-size list support for composite elements

Signed-off-by: Matthew Ballance <matt.ballance@gmail.com>
@mballance
Copy link
Member

@fdrichardson, thanks for the issue report and testcase. I investigated and corrected several issues around array resizing. Once the array-sizing issues were addressed, your testcase failed with a solve error since the array was empty. I updated your testcase to populate the array, and it now runs. The updated testcase is below:

def test_obj_array(self):
@vsc.randobj
class MyRandObject:
def __init__(self):
self.a =vsc.rand_bit_t(4)
@vsc.randobj
class TopRandObj:
def __init__(self):
self.a = vsc.randsz_list_t(MyRandObject())
for i in range(5):
self.a.append(MyRandObject())
@vsc.constraint
def a_c(self):
self.a.size < 5
self.a.size > 1
my_top_rand = TopRandObj()
my_top_rand.randomize(solve_fail_debug=1)
self.assertGreater(my_top_rand.a.size, 1)
self.assertLess(my_top_rand.a.size, 5)
print("Size: %d" % my_top_rand.a.size)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants