KvCacheBlockTable constructor

KvCacheBlockTable({
  1. required Logic clk,
  2. required Logic reset,
  3. required Logic install,
  4. required KvCacheBlockBinding binding,
  5. required Logic lookup,
  6. required KvCacheBlockLookup lookupRequest,
  7. required Logic invalidate,
  8. required AssociativeInvalidationRequest invalidation,
  9. StaticOrRuntimeParameter? consumeResult,
  10. StaticOrRuntimeParameter? consumeInvalidationCompletion,
  11. int lookupReadLatency = 1,
  12. MemoryConfiguration? memoryConfiguration,
  13. String name = 'kv_cache_block_table',
})

Creates a paged KV-cache block-table core.

Implementation

KvCacheBlockTable({
  required Logic clk,
  required Logic reset,
  required Logic install,
  required KvCacheBlockBinding binding,
  required Logic lookup,
  required KvCacheBlockLookup lookupRequest,
  required Logic invalidate,
  required AssociativeInvalidationRequest invalidation,
  StaticOrRuntimeParameter? consumeResult,
  StaticOrRuntimeParameter? consumeInvalidationCompletion,
  int lookupReadLatency = 1,
  MemoryConfiguration? memoryConfiguration,
  super.name = 'kv_cache_block_table',
})  : entryCount = binding.entryCount,
      sequenceIdWidth = binding.sequenceIdWidth,
      logicalBlockWidth = binding.logicalBlockWidth,
      physicalBlockWidth = binding.physicalBlockWidth,
      referenceCountWidth = binding.referenceCountWidth,
      retainsResult = transferIsRetained(consumeResult),
      retainsInvalidationCompletion =
          transferIsRetained(consumeInvalidationCompletion),
      super(
        definitionName: 'KvCacheBlockTable_'
            'E${binding.entryCount}_S${binding.sequenceIdWidth}_'
            'L${binding.logicalBlockWidth}_'
            'P${binding.physicalBlockWidth}_'
            'R${binding.referenceCountWidth}_'
            'T$lookupReadLatency'
            '${scheduledTransferSuffix(consumeResult, 'ScheduledResult')}'
            '${scheduledTransferSuffix(
          consumeInvalidationCompletion,
          'ScheduledCompletion',
        )}',
      ) {
  validateKvCacheBlockShape(
    entryCount: entryCount,
    sequenceIdWidth: sequenceIdWidth,
    logicalBlockWidth: logicalBlockWidth,
    physicalBlockWidth: physicalBlockWidth,
    referenceCountWidth: referenceCountWidth,
  );
  if (lookupRequest.sequenceIdWidth != sequenceIdWidth ||
      lookupRequest.logicalBlockWidth != logicalBlockWidth ||
      invalidation.entryMask.width != entryCount) {
    throw ArgumentError('KV-cache block-table core shapes mismatch.');
  }
  final indexWidth = widthFor(entryCount);
  final keyWidth = sequenceIdWidth + logicalBlockWidth;
  clk = addInput('clk', clk);
  reset = addInput('reset', reset);
  install = addInput('install', install);
  lookup = addInput('lookup', lookup);
  // A runtime control becomes this core's input here and is handed to the
  // child that retains for it; a scheduled one stays a generation choice.
  final resultControl =
      resolveTransferControl(consumeResult, name: 'consumeResult')
        ..getRuntimeInput(this);
  invalidate = addInput('invalidate', invalidate);
  final completionControl = resolveTransferControl(
      consumeInvalidationCompletion,
      name: 'consumeInvalidationCompletion')
    ..getRuntimeInput(this);
  final localBinding = addTypedInput('binding', binding);
  final localLookup = addTypedInput('lookupRequest', lookupRequest);
  final localInvalidation = addTypedInput('invalidation', invalidation);
  result = addTypedOutput(
      'result',
      ({name = 'result'}) => KvCacheBlockLookupResult(
            entryCount: entryCount,
            physicalBlockWidth: physicalBlockWidth,
            referenceCountWidth: referenceCountWidth,
            name: name,
          ));

  final invalidationAdvance = Logic(name: '${name}_invalidation_advance');
  final invalidator = AssociativeInvalidator(
    clk: clk,
    reset: reset,
    load: invalidate,
    request: localInvalidation,
    advance: StaticOrRuntimeParameter.runtime(invalidationAdvance,
        name: 'advance'),
    consumeCompletion:
        completionControl.forChild(this, name: 'consumeCompletion'),
    name: '${name}_invalidator',
  );
  final invalidating = invalidator.updateValid;

  final update = AssociativeUpdateRequest(
      keyWidth: keyWidth, indexWidth: indexWidth, name: '${name}_update');
  update.index <=
      mux(invalidating, invalidator.updateIndex, localBinding.entryIndex);
  update.key <=
      mux(invalidating, Const(0, width: keyWidth),
          [localBinding.sequenceId, localBinding.logicalBlock].concat());
  update.entryValid <= ~invalidating & localBinding.entryValid;
  final associativeLookup =
      AssociativeLookupRequest(keyWidth: keyWidth, name: '${name}_lookup');
  associativeLookup.key <=
      [localLookup.sequenceId, localLookup.logicalBlock].concat();

  final memory = SramScanAssociativeMemory(
    clk: clk,
    reset: reset,
    update: invalidating | install,
    updateRequest: update,
    lookup: lookup,
    lookupRequest: associativeLookup,
    consumeResponse: resultControl.forChild(this, name: 'consumeResponse'),
    entryCount: entryCount,
    readLatency: lookupReadLatency,
    memoryConfiguration: memoryConfiguration,
    name: '${name}_associative_memory',
  );
  invalidationAdvance <= invalidating & memory.canUpdate;
  final installTaken = (install & memory.canUpdate & ~invalidating)
      .named('${name}_install_taken');

  addOutput('canInstall') <= memory.canUpdate & ~invalidating;
  addOutput('installAccepted') <= installTaken;
  addOutput('canLookup') <= memory.canLookup;
  addOutput('lookupAccepted') <= memory.lookupAccepted;
  addOutput('canInvalidate') <= invalidator.canLoad;
  addOutput('invalidateAccepted') <= invalidator.loadAccepted;
  addOutput('resultValid') <= memory.responseValid;
  addOutput('invalidationCompletionValid') <= invalidator.completionValid;
  addOutput('invalidationBusy') <= invalidator.busy;

  final physicalBlocks = [
    for (var entry = 0; entry < entryCount; entry++)
      Logic(name: '${name}_physical_block_$entry', width: physicalBlockWidth),
  ];
  final referenceCounts = [
    for (var entry = 0; entry < entryCount; entry++)
      Logic(
          name: '${name}_reference_count_$entry', width: referenceCountWidth),
  ];
  Sequential(clk, reset: reset, resetValues: {
    for (final block in physicalBlocks) block: 0,
    for (final count in referenceCounts) count: 0,
  }, [
    for (var entry = 0; entry < entryCount; entry++) ...[
      If(installTaken & localBinding.entryIndex.eq(entry), then: [
        physicalBlocks[entry] < localBinding.physicalBlock,
        referenceCounts[entry] < localBinding.referenceCount,
      ]),
      If(invalidationAdvance & invalidator.updateIndex.eq(entry), then: [
        physicalBlocks[entry] < 0,
        referenceCounts[entry] < 0,
      ]),
    ],
  ]);

  final response = memory.response;
  final selectedPhysical = physicalBlocks.selectIndex(response.index);
  final selectedReference = referenceCounts.selectIndex(response.index);
  result.hit <= response.hit;
  result.entryIndex <= response.index;
  result.physicalBlock <=
      mux(response.hit, selectedPhysical,
          Const(0, width: physicalBlockWidth));
  result.referenceCount <=
      mux(response.hit, selectedReference,
          Const(0, width: referenceCountWidth));
  result.duplicateMatch <=
      (response.matchVector &
              (response.matchVector - Const(1, width: entryCount)))
          .or();
}