From 1d4e224ea7903b8927b94a85e30d036599ee4bb1 Mon Sep 17 00:00:00 2001 From: Matthew Ballance Date: Mon, 22 Jun 2026 04:18:51 +0000 Subject: [PATCH] Properly handle '$' as bin-range bounds Signed-off-by: Matthew Ballance --- src/V3Covergroup.cpp | 86 ++++++++++++---------- test_regress/t/t_covergroup_array_bins.out | 6 ++ test_regress/t/t_covergroup_array_bins.v | 36 +++++++++ 3 files changed, 90 insertions(+), 38 deletions(-) diff --git a/src/V3Covergroup.cpp b/src/V3Covergroup.cpp index 0d43e9f60..054f27c34 100644 --- a/src/V3Covergroup.cpp +++ b/src/V3Covergroup.cpp @@ -655,22 +655,54 @@ class FunctionalCoverageVisitor final : public VNVisitor { return refp; } - // Individual equality targets of an array bin (bins b[N] = {values/ranges}), in order. - std::vector extractArrayValues(AstCoverBin* arrayBinp, AstNodeExpr* exprp) { + // Individual equality targets of an array bin (bins b[] = {values/ranges}), in order. + // An open-ended bound ('$', AstUnbounded) resolves to the coverpoint domain: '[lo:$]' + // covers [lo:maxVal] and '[$:hi]' covers [0:hi]. One target is produced per value; a + // range whose resolved size would exceed COVER_BINS_LIMIT (e.g. an open '[lo:$]' over a + // wide coverpoint) is unsupported -- emits COVERIGN, sets unsupportedOut, yields nothing. + std::vector extractArrayValues(AstCoverBin* arrayBinp, AstNodeExpr* exprp, + bool& unsupportedOut) { + unsupportedOut = false; + const int width = exprp->width(); + const uint64_t maxVal = (width >= 64) ? UINT64_MAX : ((1ULL << width) - 1); std::vector values; for (AstNode* rangep = arrayBinp->rangesp(); rangep; rangep = rangep->nextp()) { if (AstInsideRange* const irp = VN_CAST(rangep, InsideRange)) { - AstConst* const minp = VN_CAST(V3Const::constifyEdit(irp->lhsp()), Const); - AstConst* const maxp = VN_CAST(V3Const::constifyEdit(irp->rhsp()), Const); - if (!minp || !maxp) { + AstNodeExpr* const lhsp = V3Const::constifyEdit(irp->lhsp()); + AstNodeExpr* const rhsp = V3Const::constifyEdit(irp->rhsp()); + const bool loUnb = VN_IS(lhsp, Unbounded); + const bool hiUnb = VN_IS(rhsp, Unbounded); + AstConst* const minp = VN_CAST(lhsp, Const); + AstConst* const maxp = VN_CAST(rhsp, Const); + if ((!minp && !loUnb) || (!maxp && !hiUnb)) { arrayBinp->v3error("Non-constant expression in array bins range; " "range bounds must be constants"); return values; } - for (int val = minp->toSInt(); val <= maxp->toSInt(); ++val) - values.push_back(new AstConst{irp->fileline(), AstConst::WidthedValue{}, - static_cast(exprp->width()), - static_cast(val)}); + if ((minp && minp->num().isFourState()) || (maxp && maxp->num().isFourState())) { + arrayBinp->v3error("Four-state (x/z) value in array bins range bound; " + "range bounds must be two-state constants"); + return values; + } + const uint64_t lo = loUnb ? 0 : minp->toUQuad(); + const uint64_t hi = hiUnb ? maxVal : maxp->toUQuad(); + if (hi < lo) continue; // empty range contributes no bins + // Guard against a '$'-bounded or otherwise huge range exploding the bin count. + const uint64_t span = hi - lo; // == valueCount - 1 (no overflow: hi >= lo) + if (span >= static_cast(COVER_BINS_LIMIT) + || values.size() + span + 1 > static_cast(COVER_BINS_LIMIT)) { + arrayBinp->v3warn(COVERIGN, "Unsupported: array 'bins' covering more than " + << COVER_BINS_LIMIT + << " values (e.g. an open '[lo:$]' range over " + "a wide coverpoint); bin ignored"); + unsupportedOut = true; + for (AstNodeExpr* const vp : values) VL_DO_DANGLING(pushDeletep(vp), vp); + values.clear(); + return values; + } + for (uint64_t v = lo; v <= hi; ++v) + values.push_back(new AstConst{irp->fileline(), AstConst::WidthedValue{}, width, + static_cast(v)}); } else { values.push_back(VN_AS(rangep->cloneTree(false), NodeExpr)); } @@ -744,7 +776,9 @@ class FunctionalCoverageVisitor final : public VNVisitor { continue; } if (cbinp->isArray()) { // value array: bins b[N] = {...} -> b[0]..b[N-1] - std::vector values = extractArrayValues(cbinp, exprp); + bool unsupported = false; + std::vector values = extractArrayValues(cbinp, exprp, unsupported); + if (unsupported) continue; // bin ignored (COVERIGN emitted); reserve no slot namerStmts.push_back(makeNamer(cpVarp, cbinp, static_cast(values.size()))); for (AstNodeExpr* valuep : values) { emitConvHitIf(coverpointp, cbinp, cpVarp, idx++, @@ -1056,34 +1090,10 @@ class FunctionalCoverageVisitor final : public VNVisitor { int atLeastValue) { UINFO(4, " Generating array bins for: " << arrayBinp->name()); - // Extract all values from the range list - std::vector values; - for (AstNode* rangep = arrayBinp->rangesp(); rangep; rangep = rangep->nextp()) { - if (AstInsideRange* const insideRangep = VN_CAST(rangep, InsideRange)) { - // For InsideRange [min:max], create bins for each value - AstNodeExpr* const minp = V3Const::constifyEdit(insideRangep->lhsp()); - AstNodeExpr* const maxp = V3Const::constifyEdit(insideRangep->rhsp()); - AstConst* const minConstp = VN_CAST(minp, Const); - AstConst* const maxConstp = VN_CAST(maxp, Const); - if (minConstp && maxConstp) { - const int minVal = minConstp->toSInt(); - const int maxVal = maxConstp->toSInt(); - UINFO(6, " Expanding InsideRange [" << minVal << ":" << maxVal << "]"); - for (int val = minVal; val <= maxVal; ++val) { - values.push_back(new AstConst{insideRangep->fileline(), - AstConst::WidthedValue{}, - (int)exprp->width(), (uint32_t)val}); - } - } else { - arrayBinp->v3error("Non-constant expression in array bins range; " - "range bounds must be constants"); - return; - } - } else { - // Single value - should be an expression - values.push_back(VN_AS(rangep->cloneTree(false), NodeExpr)); - } - } + // Extract all values from the range list (resolves '$', caps/ignores huge ranges) + bool unsupported = false; + std::vector values = extractArrayValues(arrayBinp, exprp, unsupported); + if (unsupported) return; // bin ignored (COVERIGN emitted) // Create a separate bin for each value int index = 0; diff --git a/test_regress/t/t_covergroup_array_bins.out b/test_regress/t/t_covergroup_array_bins.out index ccbbbc297..dc4ff0790 100644 --- a/test_regress/t/t_covergroup_array_bins.out +++ b/test_regress/t/t_covergroup_array_bins.out @@ -10,3 +10,9 @@ cg3.cp.range_sized[0]: 1 cg3.cp.range_sized[1]: 1 cg3.cp.range_sized[2]: 1 cg3.cp.range_sized[3]: 0 +cg4.cp.all_vals[0]: 1 +cg4.cp.all_vals[1]: 1 +cg4.cp.all_vals[2]: 1 +cg4.cp.all_vals[3]: 0 +cg5.cp.hi_vals[0]: 1 +cg5.cp.hi_vals[1]: 0 diff --git a/test_regress/t/t_covergroup_array_bins.v b/test_regress/t/t_covergroup_array_bins.v index 84033319d..33da79ea9 100644 --- a/test_regress/t/t_covergroup_array_bins.v +++ b/test_regress/t/t_covergroup_array_bins.v @@ -13,6 +13,7 @@ module t; bit [7:0] data; + bit [1:0] sel; covergroup cg; coverpoint data { @@ -38,14 +39,33 @@ module t; } endgroup + // cg4: array bins with '$' (open range) - '$' resolves to the coverpoint domain max. + // For 2-bit sel, {[0:$]} == {[0:3]}: one bin per value -> 4 bins (issue #7750). + covergroup cg4; + cp: coverpoint sel { + bins all_vals[] = {[0 : $]}; + } + endgroup + + // cg5: lower-open range {[lo:$]} == {[lo:maxVal]} -> bins for 2 and 3 + covergroup cg5; + cp: coverpoint sel { + bins hi_vals[] = {[2 : $]}; + } + endgroup + initial begin cg cg_inst; cg2 cg2_inst; cg3 cg3_inst; + cg4 cg4_inst; + cg5 cg5_inst; cg_inst = new(); cg2_inst = new(); cg3_inst = new(); + cg4_inst = new(); + cg5_inst = new(); // Hit first array bin value (1) data = 1; @@ -94,6 +114,22 @@ module t; cg3_inst.sample(); `checkr(cg3_inst.get_inst_coverage(), 75.0); + // Hit cg4 '$' bins ([0:$] == [0:3], 4 bins): cover 3 of 4 + sel = 0; + cg4_inst.sample(); + `checkr(cg4_inst.get_inst_coverage(), 25.0); + sel = 1; + cg4_inst.sample(); + `checkr(cg4_inst.get_inst_coverage(), 50.0); + sel = 2; + cg4_inst.sample(); + `checkr(cg4_inst.get_inst_coverage(), 75.0); + + // Hit cg5 lower-open bins ([2:$] == [2:3], 2 bins): cover 1 of 2 + sel = 2; + cg5_inst.sample(); + `checkr(cg5_inst.get_inst_coverage(), 50.0); + $write("*-* All Finished *-*\n"); $finish; end