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

Getting an error for inline randomization #44

Closed
ShraddhaDevaiya opened this issue Oct 30, 2020 · 4 comments
Closed

Getting an error for inline randomization #44

ShraddhaDevaiya opened this issue Oct 30, 2020 · 4 comments

Comments

@ShraddhaDevaiya
Copy link

Hi @mballance ,
I am trying to use following code for randomize_with().

import vsc

@vsc.randobj
class my_sub_s(object):
    def __init__(self):
        self.has_rs1 = 1
        self.has_rs2 = 1
        self.has_rd = 1
        self.avail_regs = vsc.rand_list_t(vsc.uint8_t(0), 10)
        self.reserved_rd = vsc.rand_list_t(vsc.uint8_t(0), 10)
        self.reserved_regs = vsc.rand_list_t(vsc.uint8_t(0), 10)
        self.rd = vsc.rand_uint8_t(0)
        self.rs1 = vsc.rand_uint8_t(0)
        self.rs2 = vsc.rand_uint8_t(0)
        self.format = 2

obj = my_sub_s()

with obj.randomize_with() as it:
            with vsc.if_then(obj.avail_regs.size > 0): 
                with vsc.if_then(obj.has_rs1):
                    obj.rs1.inside(vsc.rangelist(obj.avail_regs))
                with vsc.if_then(obj.has_rs2):
                    obj.rs2.inside(vsc.rangelist(obj.avail_regs))
                with vsc.if_then(obj.has_rd):
                    obj.rd.inside(vsc.rangelist(obj.avail_regs))   
            with vsc.foreach(obj.reserved_rd, idx = True) as i:
                with vsc.if_then(obj.has_rd):
                    obj.rd != obj.reserved_rd[i]
                with vsc.if_then(obj.format == 2):
                    obj.rs1 != obj.reserved_rd[i]
            with vsc.foreach(obj.reserved_regs, idx = True) as i:
                with vsc.if_then(obj.has_rd):
                    obj.rd != obj.reserved_regs[i]
                with vsc.if_then(obj.format == 2):
                    obj.rs1 != obj.reserved_regs[i]

And it is giving an error like following:

File "9usecase.py", line 30, in <module>
    with vsc.if_then(obj.format == 2):
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/constraints.py", line 146, in __init__
    to_expr(e)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/types.py", line 236, in to_expr  
    raise Exception("Element \"" + str(t) + "\" isn't recognized, and doesn't provide to_expr")
Exception: Element "True" isn't recognized, and doesn't provide to_expr

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "9usecase.py", line 36, in <module>
    obj.rs1 != obj.reserved_regs[i]
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/rand_obj.py", line 186, in __exit__
    Randomizer.do_randomize([model], [c])
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/randomizer.py", line 694, in do_randomize
    constraint_l.extend(ArrayConstraintBuilder.build(
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/array_constraint_builder.py", line 45, in build
    m.accept(builder)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/constraint_block_model.py", line 42, in accept
    v.visit_constraint_block(self)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/constraint_copy_builder.py", line 85, in visit_constraint_block
    super().visit_constraint_block(c)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/model_visitor.py", line 102, in visit_constraint_block
    self.visit_constraint_scope(c)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/constraint_override_visitor.py", line 26, in visit_constraint_scope
    cc.accept(self)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/constraint_foreach_model.py", line 47, in accept
    v.visit_constraint_foreach(self)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/array_constraint_builder.py", line 61, in visit_constraint_foreach
    self.override_constraint(scope)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/constraint_override_visitor.py", line 37, in override_constraint
    self.scope_s[-1].constraint_l[self.scope_i],
IndexError: list index out of range

I am not able to understand what is the cause, can you please take a look into this?

Thanks & Regards,
Shraddha Devaiya.

@mballance
Copy link
Member

Hi @ShraddhaDevaiya,
In order to use an expression in a constraint (eg in vsc.if_then), at least one element in the expression needs to be a vsc variable. Several of the variables in the class are non-vsc variables, so expressions evaluate to a constant when the constraint is constructed. The vast majority of the time, this behavior is undesirable in the context of constraints because the constraint is effectively eliminated.
The error message, unfortunately, is not very helpful. I'll look to see whether this can be improved.
Would it work to use vsc variables for has_rs1, has_rs2, format, etc? The constraints would be properly constructed in this case.

Best Regards,
Matthew

@ShraddhaDevaiya
Copy link
Author

Yeah, I have tried that way, has_rs1, has_rs2, format as a vsc variables.

@vsc.randobj
class my_sub_s(object):
    def __init__(self):
        self.has_rs1 = vsc.uint8_t(1)
        self.has_rs2 = vsc.uint8_t(1)
        self.has_rd = vsc.uint8_t(1)
        self.avail_regs = vsc.rand_list_t(vsc.uint8_t(0), 10)
        self.reserved_rd = vsc.rand_list_t(vsc.uint8_t(0), 10)
        self.reserved_regs = vsc.rand_list_t(vsc.uint8_t(0), 10)
        self.rd = vsc.rand_uint8_t(0)
        self.rs1 = vsc.rand_uint8_t(0)
        self.rs2 = vsc.rand_uint8_t(0)
        self.format = vsc.uint8_t(2)

obj = my_sub_s()

with obj.randomize_with() as it:
            with vsc.if_then(obj.avail_regs.size > 0): 
                with vsc.if_then(obj.has_rs1):
                    obj.rs1.inside(vsc.rangelist(obj.avail_regs))
                with vsc.if_then(obj.has_rs2):
                    obj.rs2.inside(vsc.rangelist(obj.avail_regs))
                with vsc.if_then(obj.has_rd):
                    obj.rd.inside(vsc.rangelist(obj.avail_regs))   
            with vsc.foreach(obj.reserved_rd, idx = True) as i:
                with vsc.if_then(obj.has_rd):
                    obj.rd != obj.reserved_rd[i]
                with vsc.if_then(obj.format == 2):
                    obj.rs1 != obj.reserved_rd[i]
            with vsc.foreach(obj.reserved_regs, idx = True) as i:
                with vsc.if_then(obj.has_rd):
                    obj.rd != obj.reserved_regs[i]
                with vsc.if_then(obj.format == 2):
                    obj.rs1 != obj.reserved_regs[i]

But then also it is giving an error like following:

Traceback (most recent call last):
  File "9usecase.py", line 36, in <module>
    obj.rs1 != obj.reserved_regs[i]
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/rand_obj.py", line 186, in __exit__
    Randomizer.do_randomize([model], [c])
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/randomizer.py", line 694, in do_randomize
    constraint_l.extend(ArrayConstraintBuilder.build(
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/array_constraint_builder.py", line 45, in build
    m.accept(builder)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/constraint_block_model.py", 
line 42, in accept
    v.visit_constraint_block(self)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/constraint_copy_builder.py", line 85, in visit_constraint_block
    super().visit_constraint_block(c)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/model_visitor.py", line 102, in visit_constraint_block
    self.visit_constraint_scope(c)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/constraint_override_visitor.py", line 26, in visit_constraint_scope
    cc.accept(self)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/constraint_foreach_model.py", line 47, in accept
    v.visit_constraint_foreach(self)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/array_constraint_builder.py", line 61, in visit_constraint_foreach
    self.override_constraint(scope)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/constraint_override_visitor.py", line 37, in override_constraint
    self.scope_s[-1].constraint_l[self.scope_i],
IndexError: list index out of range

What can I do for this ?

Thanks & Regards,
Shraddha Devaiya.

@mballance
Copy link
Member

Hi @ShraddhaDevaiya,
Okay, thanks for confirming, and thanks for the testcase. I'm investigating now and will let you know what I find.

Best Regards,
Matthew

@mballance
Copy link
Member

Hi @ShraddhaDevaiya,
The testcase was very helpful. It turns out there was an issue with nesting and combining if_then and foreach in certain ways. I've corrected the issue and your testcase (now part of the test suite) now passes. Release 0.2.6 contains the fix.

Thanks and Best Regards,
Matthew

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