xls icon indicating copy to clipboard operation
xls copied to clipboard

[enhancement] Preserve cover semantics across repeated inlining/loop unrolling

Open ansidu opened this issue 2 months ago • 13 comments

What's hard to do? (limit 100 words)

When a function containing a cover node is inlined more than once into a caller — which happens naturally with loop unrolling or multiple call sites to the same function — the cover gets duplicated into one copy per inline instance. This silently changes coverage semantics: a cover in a loop should mean "fired at least once across all iterations," but the resulting Verilog ends up with N separate covers, each meaning "fired at iteration #i." The coverage counter is split across iterations instead of being aggregated, giving misleading coverage results.

Current best alternative workaround (limit 100 words)

There's no clean workaround today. A practical approach for already-built designs is postprocessing: parse the emitted Verilog, identify the per-iteration cover instances that share the same logical source/label, and aggregate their counts with a script. This requires stepping outside XLS and is sensitive to the exact naming generated by inlining.

Your view of the "best case XLS enhancement" (limit 100 words)

A single cover written inside a function results in exactly one coverage counter for it, counting "reached at least once on any iteration or path" — even when that function gets unrolled or called from multiple places. The coverage report stays intuitive: one line per logical cover in the source, with a count that reflects the intended meaning. Users shouldn't have to know whether or how inlining duplicated the code.


Update: motivation and example

My primary motivation for this change is source-level DSLX coverage.

Consider the following (oversimplified) proc. In the generated verilog I'd expect to see exactly one cover property matching to the source lever cover!(). Gating condition is (i == u8:1) && true. So, I'd like to have an answer for "was cover!() ever reached?"

proc MyProc {
  chan_in: chan<u8> in;
  chan_out: chan<u8> out; 
 
  init{ u8: 0 } 
 
  config(chan_in: chan<u8> in, chan_out: chan<u8> out) { (chan_in, chan_out) }
 
  next(res: u8) { 
    let tok = token(); 
    let (tok_d, data) = recv(tok, chan_in); 
 
    let result = for (i, acc): (u8, u8) in u8:0..u8:4 { 
      let acc = if i == u8:1 { 
        cover!("loop_cover", true); 
        acc + u8:1 
      } else { 
        acc 
      }; 
      acc 
    }(res); 
 
    send(tok_d, chan_out, result); 
    result 
  } 
} 

Instead, four cover property declarations are currently generated (at opt_main step, after the FE everything is fine):

__next_0: cover property (@(posedge clk) 1'h0); 
__next_1: cover property (@(posedge clk) p0_stage_done & ~rst);
__next_2: cover property (@(posedge clk) 1'h0); 
__next_3: cover property (@(posedge clk) 1'h0); 

Three of these covers are permanently disabled (1'h0). From a source-level coverage perspective they don't carry any additional information and introduce noise in the generated coverage reports.

More importantly, these four cover properties answer a different question that is "was the cover reached in iteration #0/1/2/3?". By and large, the loop even is not necessary, pure inlining duplicates cover nodes and as a result each cover property answers a question like "have it fired when the function was called from callsite #n?". The number and gating conditions of covers depend on optimization decisions rather than on the source program itself, which makes source-level coverage difficult to interpret.

ansidu avatar Sep 11 '26 14:09 ansidu

Suggested fix https://github.com/google/xls/pull/4964

ansidu avatar Sep 11 '26 14:09 ansidu

On my experimental project, this patch reduces a number of cover properties with "1'h0" condition from ~3k (result of unrolling) to just four.

ansidu avatar Sep 11 '26 14:09 ansidu

let's keep the discussion about the feature here: @allight, @grebe, @mikex-oss, @richmckeever

@ansidu motived example: https://github.com/google/xls/pull/4964#issuecomment-6057868048, maybe it would be worth adding to the issue description?

proppy avatar Oct 09 '26 06:10 proppy

@ansidu sorry for the back and forth and this, we were discussing #4963 (and #4903) as a team, and we had some follow-up questions before we consider landing the PR related to this.

My primary motivation for this change is source-level DSLX coverage

could you expand which level of simulation you'd like to get source-level DSLX coverage on? (dslx simulation, ir simulation, block ir simulation) and describe how you would ideally interact w/ the source code and tool to achieve this (ex: expression annotation, package level instrumentation, project level instrumentation)

Note: I also wanted to mention that we have a few older issues about improving coverage support (https://github.com/google/xls/issues/2139, https://github.com/google/xls/issues/1126, https://github.com/google/xls/issues/499) and I also acknowledge that our documentation is lacking around what's currently supported (just added https://github.com/google/xls/issues/5145).

proppy avatar Oct 15 '26 00:10 proppy

sorry for the back and forth and this, we were discussing https://github.com/google/xls/issues/4963 (and https://github.com/google/xls/issues/4903) as a team

Sorry for getting the whole team involved in my little coverage problem 😄

could you expand which level of simulation you'd like to get source-level DSLX coverage on? (dslx simulation, ir simulation, block ir simulation) and describe how you would ideally interact w/ the source code and tool to achieve this (ex: expression annotation, package level instrumentation, project level instrumentation)

To be honest, at all levels. I'm currently using DSLX sim, XLS IR interpreter (I've implemented very basic support for cover nodes there) and RTL sim. In general I'd expect that if at some point a cover node is inserted (by the user, autogeneration or something else), it will keep its original meaning and preserve its original semantics across all representations. Some time ago I was also thinking about autogenerating coverage for channel protocol events based on valid and ready signals, to track events such as send stalled, send idle, receive backpressure, etc. This seems possible only at block ir.

An example of my use case with #4903 and #4963 is coverage-based CI for a module under active development. This includes identifying potentially unreachable code and determining whether the test inputs exercise the relevant scenarios. The workflow is to autogenerate cover nodes, compile to RTL, and collect coverage (verilator --coverage-user flag). With #4964 fix applied, the coverage report can be used directly in this workflow. However, without #4964 fix, a single logical coverage point can be duplicated into multiple independent points, effectively splitting its hit count across the duplicates and artificially lowering the reported coverage. This especially problematic when a coverage point must be hit a certain number of times to be considered covered (e.g. 10 times). It makes the report unusable for my purposes; I'd need custom processing to deduplicate the raw cover property counters, potentially matching copies by debug location or cover name. Neither approach seems robust enough.

Do you see any semantic or implementation reasons why we shouldn't aggregate these copies? For example, is there a concern that aggregation would be hard to maintain in the optimizer? Or do you see any value in keeping these duplicates?

ansidu avatar Oct 15 '26 15:10 ansidu

Some time ago I was also thinking about autogenerating coverage for channel protocol events based on valid and ready signals, to track events such as send stalled, send idle, receive backpressure, etc.

We do have --trace_channels but afaik it's just at the interpreter/proc evaluator level (and sadly it's not really designed to be machine readable)

@grebe also prototyped a perfetto exporter for eval_ir_main traces that might be of interest: https://github.com/google/xls/blob/main/xls/tools/trace_to_perfetto_main.cc

verilator --coverage-user flag

curious if you've also tried verilator's --coverage flag? (that automatically coverage instrumentation to your verilog) what were the tradeoff that made you reach for manually-inserted cover statements instead?

proppy avatar Oct 16 '26 00:10 proppy

--coverage instruments the generated verilog (highly optimized), so the resulting coverage reflects the structure of the verilog rather than the original DSLX source. This is somewhat similar to measuring coverage by instrumenting optimized assembly instead of the original C++ source: you can learn a lot about the generated code (and for sure it's really useful, I do use it), but mapping the results back to the source-level logic is much harder.

ansidu avatar Oct 16 '26 10:10 ansidu

@ansidu there is a proto that we emit on the side with source locations: https://github.com/google/xls/blob/0b647273047d6665b63a297db974ad86a9198c16/xls/codegen/xls_metrics.proto#L62

and inline annotations in the generated verilog: https://github.com/google/xls/blob/0b647273047d6665b63a297db974ad86a9198c16/xls/codegen/vast/vast.cc#L105

does that help at all?

proppy avatar Oct 16 '26 12:10 proppy

@ansidu For what it's worth, we've had feedback from experienced RTL designers that they generally expect cover to be at the Verilog level, so they expect cover! to behave accordingly. We're not sure the semantics we have for it today are right for that, but they're closer than deduplication would have it.

It's possible we need multiple ways of supporting coverage. For example, if what you're looking for is source-level coverage data from the DSLX interpreter, maybe we should just support that directly, without anyone needing to add cover statements at all?

ericastor avatar Oct 16 '26 13:10 ericastor

we've had feedback from experienced RTL designers that they generally expect cover to be at the Verilog level, so they expect cover! to behave accordingly

I'm curious whether they've tried using exisitng cover() facilities. Don't get me wrong -- I'm not arguing, just curious and want to better understand the intended use cases to avoid spamming the XLS project with misaligned changes. Cover() inserted at DSLX level seems too high-level for the majority of cases when we're talking about generated verilog: the user has no clue how a particular piece of DSLX code will be optimized. The condition that cover() evaluates may be absent in the generated verilog due to optimizations. More precisely, the condition may exist in the generated verilog solely to serve as the argument to cover property, without being part of the actual logic. At the same time, this condition is still meaningful at the DSLX source level, but if DSLX isn't the intended target, how would users interpret these points in a relatively large project where they don't know how their DSLX code maps to the generated verilog?

source-level coverage data from the DSLX interpreter, maybe we should just support that directly, without anyone needing to add cover statements at all?

Yes, I agree that for use cases where the DSLX interpreter is sufficient, we could provide source-level coverage directly, without cover nodes insertion at all.

does that help at all?

yes, I found these annotations useful. Though they don't address the issue direcltly.

Anyway, thanks for taking the time to discuss this. Feel free to close the issue and related MR.

ansidu avatar Oct 27 '26 14:10 ansidu

yes, I found these annotations useful. Though they don't address the issue direcltly.

But couldn't this, in theory, help mapping automated --coverage results back to the DSLX source-level logic?

proppy avatar Oct 27 '26 17:10 proppy

But couldn't this, in theory, help mapping automated --coverage results back to the DSLX source-level logic?

It can help. That is what I mentioned in the issue description:

Current best alternative WA postprocessing: parse the emitted Verilog, identify the per-iteration cover instances that share the same logical source/label...

However, it means that instead of using existing tools like verilator_coverage you'll need to process everything with your own tools. At least that is how I see it.

ansidu avatar Oct 29 '26 09:10 ansidu

cover instances

Sorry for the confusion, I was referring to verilator automatic coverage, not manual cover nodes.

However, it means that instead of using existing tools like verilator_coverage

I wonder if we could "augment" verilog_coverage to parse those annotation, do they have a plugin system? /cc @rw1nkler and team have worked quite a bit with verilator maybe they know?

proppy avatar Oct 29 '26 09:10 proppy