Support dynamic array elements in std::randomize (#6896)

This commit is contained in:
Ryszard Rozak 2026-01-08 15:02:28 +01:00 committed by GitHub
parent 6abfaf23a5
commit 97d5844f2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 3 deletions

View File

@ -184,6 +184,8 @@ class RandomizeMarkVisitor final : public VNVisitor {
exprp = arrselp->fromp();
} else if (const AstStructSel* const strselp = VN_CAST(exprp, StructSel)) {
exprp = strselp->fromp();
} else if (const AstCMethodHard* const methodp = VN_CAST(exprp, CMethodHard)) {
exprp = methodp->fromp();
} else {
break;
}
@ -359,6 +361,9 @@ class RandomizeMarkVisitor final : public VNVisitor {
} else if (AstStructSel* const structSelp = VN_CAST(exprp, StructSel)) {
exprp = structSelp->fromp();
continue; // Skip StructSel, continue traversing
} else if (AstCMethodHard* const methodp = VN_CAST(exprp, CMethodHard)) {
exprp = methodp->fromp();
continue;
} else if (AstVarRef* const varrefp = VN_CAST(exprp, VarRef)) {
randVarp = varrefp->varp();
varrefp->user1(true);

View File

@ -6781,12 +6781,18 @@ class WidthVisitor final : public VNVisitor {
// IEEE 1800-2023 (18.12) limits args to current scope variables.
// Verilator accepts this for compatibility with other simulators.
continue;
} else if (VN_IS(exprp, VarRef) || VN_IS(exprp, ArraySel) || VN_IS(exprp, StructSel)) {
}
if (VN_IS(exprp, VarRef) || VN_IS(exprp, ArraySel) || VN_IS(exprp, StructSel)) {
// Valid usage
continue;
} else {
argp->v3error("Non-variable arguments for 'std::randomize()'.");
}
if (const AstCMethodHard* const methodp = VN_CAST(exprp, CMethodHard)) {
if (methodp->method() == VCMethod::ARRAY_AT
|| methodp->method() == VCMethod::ARRAY_AT_WRITE) {
continue;
}
}
argp->v3error("Non-variable arguments for 'std::randomize()'.");
}
if (nullp) nullp->v3error("'std::randomize()' does not accept 'null' as arguments.");
}

View File

@ -9,6 +9,7 @@ typedef struct {int x;} struct_t;
class inner_class;
logic [3:0] c;
logic [3:0] d[2];
logic [3:0] dyn[];
struct_t s;
endclass
@ -20,9 +21,11 @@ endclass
module t;
initial begin
int x = 2;
test_class example;
example = new;
example.b = new;
example.b.dyn = new[3];
// Simple array element access
repeat (5) begin
@ -83,6 +86,12 @@ module t;
if (example.b.d[1] < 2 || example.b.d[1] > 6) $stop;
end
// Nested object with dynamic array: obj.b.dyn[x]
repeat (5) begin
if (std::randomize(example.b.dyn[x]) with {example.b.dyn[x] inside {[1 : 3]};} == 0) $stop;
if (example.b.dyn[x] < 1 || example.b.dyn[x] > 3) $stop;
end
// Nested object with struct: obj.b.s.x
repeat (5) begin
if (std::randomize(example.b.s.x) with {example.b.s.x inside {[7 : 9]};} == 0) $stop;