Skip to content

Aggregate constraints on an array of structs: what with() can and can’t do here

write-from-scratch / tool-limitation / multi-constraint / array / statistical

Reduction and locator methods with with (...) are the usual tool for aggregate constraints (“exactly N elements are X”, “the total stays under a cap”). On the current toolchain they don’t work here, confirmed directly:

  • find() with in a constraint compiles but is dropped (CONSTRAINTIGN): randomize() succeeds and enforces nothing. True for any array.
  • sum() with on any struct-typed array, even a one-field struct with no cast, produces a query the solver rejects (“Sorts … are incompatible”), and randomize() returns 0. The same reduction on a plain array works.
  • Comparing an enum field to an enum value (item.cmd_type == WRITE) inside any with (…) crashes the compiler, struct or not.

What works is a prefix sum: a separate rand int array, one element per cmd_list entry, each tied to the one before it in a foreach (running[i] == running[i-1] + (...)). Its last element is the total to bound.

Fix the class below.

Requirements

  1. cmd_list.size() is between 15 and 20, inclusive; hi_count and dma_running, the prefix-sum arrays, are the same length.
  2. Every cmd_type is READ, WRITE or FLUSH, never INVALID.
  3. Exactly 3 elements have prio == PRIO_HIGH, counted in hi_count: hi_count[0] is 1 or 0 from cmd_list[0].prio, each later hi_count[i] adds 1 or 0 to hi_count[i-1], and the last is 3.
  4. Among PRIO_LOW elements, cmd_type favors READ and WRITE over FLUSH, about 3:3:1.
  5. dma_running sums dma_size over WRITE elements only, the same way, and its last element (total WRITE bandwidth) is at most 4096. dma_size is 8 bits and would wrap; dma_running is an int so the total doesn’t.

Source on GitHub

command_stream_bandwidth_cap.sv