refine and update tests

This commit is contained in:
Yilou Wang 2025-07-20 11:14:50 +02:00
parent 8c19f896b9
commit a0f4259233
6 changed files with 20 additions and 15 deletions

View File

@ -579,7 +579,9 @@ public:
void dump() const;
#endif
};
//=============================================================================
// VlStdRandomizer provides a light wrapper for RNG used by std::randomize()
// to support scope-level randomization.
class VlStdRandomizer final {
// MEMBERS
VlRNG __Vm_rng;
@ -593,7 +595,7 @@ public:
template <typename T>
bool basicStdRandomization(T& value, size_t width) {
value = ((1ULL << width) - 1) & VL_RANDOM_RNG_I(__Vm_rng);
value = VL_MASK_I(width) & VL_RANDOM_RNG_I(__Vm_rng);
return true;
}
};

View File

@ -390,8 +390,9 @@ class RandomizeMarkVisitor final : public VNVisitor {
while (backp && (!VN_IS(backp, Class) && !VN_IS(backp, NodeModule))) {
backp = backp->backp();
}
VN_IS(backp, Class) ? VN_AS(backp, Class)->user1(IS_STD_RANDOMIZED)
: VN_AS(backp, NodeModule)->user1(IS_STD_RANDOMIZED);
UASSERT_OBJ(VN_IS(backp, NodeModule), randVarp,
"No class or module found for rand variable");
backp->user1(IS_STD_RANDOMIZED);
}
}
return;

View File

@ -200,6 +200,7 @@ public:
#define accept in_WidthVisitor_use_AstNode_iterate_instead_of_AstNode_accept
//######################################################################
class WidthVisitor final : public VNVisitor {
// TYPES
using TableMap = std::map<std::pair<const AstNodeDType*, VAttrType>, AstVar*>;
@ -6263,7 +6264,7 @@ class WidthVisitor final : public VNVisitor {
nextp = pinp->nextp();
AstArg* const argp = VN_CAST(pinp, Arg);
if (!argp) continue;
AstNodeExpr* exprp = argp->exprp();
AstNodeExpr* const exprp = argp->exprp();
if (AstConst* const constp = VN_CAST(exprp, Const)) {
if (constp->num().isNull()) {
nullp = constp;
@ -6273,16 +6274,17 @@ class WidthVisitor final : public VNVisitor {
if (VN_IS(exprp, MemberSel)) {
argp->v3warn(CONSTRAINTIGN, "std::randomize ("
<< exprp->prettyTypeName()
<< ") is non-LRM compliant"
<< " but supported for compatibility");
<< ") is non-LRM compliant (IEEE 1800-2023 18.12)."
<< "Args should be in current scope, "
<< "but are supported in Verilator for compatibility.");
} else {
if (!VN_IS(exprp, VarRef)) {
argp->v3error("Invalid argument for 'std::randomize()'.");
argp->v3error("Non-variable arguments for 'std::randomize()'.");
}
}
if (!argp) continue;
}
if (nullp) { nullp->v3error("Invalid argument for 'std::randomize()'."); }
if (nullp) { nullp->v3error("'std::randomize()' does not accept 'null' as arguments."); }
}
void visit(AstNodeFTaskRef* nodep) override {
// For arguments, is assignment-like context; see IEEE rules in AstNodeAssign
@ -6310,7 +6312,7 @@ class WidthVisitor final : public VNVisitor {
handleStdRandomizeArgs(nodep); // Provided args should be in current scope
if (withp) {
// TODO: std::randomize()with{}
nodep->v3warn(CONSTRAINTIGN, "with ignored (unsupported)");
nodep->v3warn(CONSTRAINTIGN, "Unsupported: std::randomize()'s 'with'");
nodep->replaceWith(new AstConst{nodep->fileline(), 0});
VL_DO_DANGLING(pushDeletep(nodep), nodep);
return;

View File

@ -1,9 +1,9 @@
%Error: t/t_std_randomize_bad1.v:12:36: Invalid argument for 'std::randomize()'.
%Error: t/t_std_randomize_bad1.v:12:36: Non-variable arguments for 'std::randomize()'.
: ... note: In instance 't_std_randomize_bad1'
12 | success = std::randomize(a + 1);
| ^
... See the manual at https://verilator.org/verilator_doc.html?v=latest for more assistance.
%Error: t/t_std_randomize_bad1.v:19:30: Invalid argument for 'std::randomize()'.
%Error: t/t_std_randomize_bad1.v:19:30: 'std::randomize()' does not accept 'null' as arguments.
: ... note: In instance 't_std_randomize_bad1'
19 | void'(std::randomize(null));
| ^~~~

View File

@ -1,10 +1,10 @@
%Warning-CONSTRAINTIGN: t/t_std_randomize_bad2.v:48:38: std::randomize (MEMBERSEL 'addr') is non-LRM compliant but supported for compatibility
%Warning-CONSTRAINTIGN: t/t_std_randomize_bad2.v:48:38: std::randomize (MEMBERSEL 'addr') is non-LRM compliant (IEEE 1800-2023 18.12).Args should be in current scope, but are supported in Verilator for compatibility.
: ... note: In instance 't_std_randomize_bad2'
48 | success = std::randomize(test.addr, test.data);
| ^
... For warning description see https://verilator.org/warn/CONSTRAINTIGN?v=latest
... Use "/* verilator lint_off CONSTRAINTIGN */" and lint_on around source to disable this message.
%Warning-CONSTRAINTIGN: t/t_std_randomize_bad2.v:48:49: std::randomize (MEMBERSEL 'data') is non-LRM compliant but supported for compatibility
%Warning-CONSTRAINTIGN: t/t_std_randomize_bad2.v:48:49: std::randomize (MEMBERSEL 'data') is non-LRM compliant (IEEE 1800-2023 18.12).Args should be in current scope, but are supported in Verilator for compatibility.
: ... note: In instance 't_std_randomize_bad2'
48 | success = std::randomize(test.addr, test.data);
| ^

View File

@ -1,4 +1,4 @@
%Warning-CONSTRAINTIGN: t/t_std_randomize_unsup_bad.v:11:16: with ignored (unsupported)
%Warning-CONSTRAINTIGN: t/t_std_randomize_unsup_bad.v:11:16: Unsupported: std::randomize()'s 'with'
: ... note: In instance 't'
11 | if (std::randomize(a, b) with { 2 < a; a < 7; b < a; } != 1) $stop;
| ^~~~~~~~~