OlderStoreMaskTracker constructor

OlderStoreMaskTracker({
  1. required Logic clk,
  2. required Logic reset,
  3. required ReadyValidInterface<Logic> storeAllocation,
  4. required ReadyValidInterface<TagRelease> storeRelease,
  5. required ReadyValidInterface<Logic> loadAllocation,
  6. required ReadyValidInterface<TagRelease> loadRelease,
  7. required Logic selectedLoad,
  8. required int storeCount,
  9. required int loadCount,
  10. ReadyValidInterface<Logic>? storeCancellationMask,
  11. ReadyValidInterface<Logic>? loadCancellationMask,
  12. RegisterFileFunctor? storageImplementation,
  13. bool storeAllocationPrecedesLoad = true,
  14. String name = 'older_store_mask_tracker',
})

Creates a per-load older-store mask tracker.

storageImplementation selects the retained mask backing. The default remains synthesizable flop state.

Implementation

OlderStoreMaskTracker({
  required Logic clk,
  required Logic reset,
  required ReadyValidInterface<Logic> storeAllocation,
  required ReadyValidInterface<TagRelease> storeRelease,
  required ReadyValidInterface<Logic> loadAllocation,
  required ReadyValidInterface<TagRelease> loadRelease,
  required Logic selectedLoad,
  required this.storeCount,
  required this.loadCount,
  ReadyValidInterface<Logic>? storeCancellationMask,
  ReadyValidInterface<Logic>? loadCancellationMask,
  RegisterFileFunctor? storageImplementation,
  this.storeAllocationPrecedesLoad = true,
  super.name = 'older_store_mask_tracker',
}) : super(
        definitionName: _olderStoreMaskTrackerDefinitionName(
            storeCount,
            loadCount,
            storeAllocationPrecedesLoad,
            storeCancellationMask != null || loadCancellationMask != null,
            storageImplementation),
      ) {
  if (storeCount < 1 || loadCount < 1) {
    throw ArgumentError('Store and load counts must be positive.');
  }
  final storeWidth = max(1, storeCount.log2Ceil);
  final loadWidth = max(1, loadCount.log2Ceil);
  if (storeAllocation.data.width < storeWidth ||
      storeRelease.data.tag.width != storeAllocation.data.width ||
      loadAllocation.data.width < loadWidth ||
      loadRelease.data.tag.width != loadAllocation.data.width ||
      selectedLoad.width != loadAllocation.data.width ||
      (storeCancellationMask != null &&
          storeCancellationMask.data.width != storeCount) ||
      (loadCancellationMask != null &&
          loadCancellationMask.data.width != loadCount)) {
    throw ArgumentError(
        'Allocation and release indices must represent their queue spaces.');
  }
  for (final interface in [
    storeAllocation,
    storeRelease,
    loadAllocation,
    loadRelease
  ]) {
    interface.requireProviderComposableWith(ReadyValidContract.readyAndValid,
        consumerName: name);
  }
  storeCancellationMask?.requireProviderComposableWith(
      ReadyValidContract.readyAndValid,
      consumerName: name);
  loadCancellationMask?.requireProviderComposableWith(
      ReadyValidContract.readyAndValid,
      consumerName: name);

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final localSelectedLoad =
      addInput('selectedLoad', selectedLoad, width: selectedLoad.width);
  this.storeAllocation = addPairInterface(storeAllocation, PairRole.consumer,
      uniquify: (original) => 'store_allocation_$original');
  this.storeRelease = addPairInterface(storeRelease, PairRole.consumer,
      uniquify: (original) => 'store_release_$original');
  this.loadAllocation = addPairInterface(loadAllocation, PairRole.consumer,
      uniquify: (original) => 'load_allocation_$original');
  this.loadRelease = addPairInterface(loadRelease, PairRole.consumer,
      uniquify: (original) => 'load_release_$original');
  if (storeCancellationMask == null) {
    this.storeCancellationMask =
        ReadyAndValidInterface(Logic(width: storeCount));
    this.storeCancellationMask.valid <= Const(0);
    this.storeCancellationMask.data <= Const(0, width: storeCount);
  } else {
    this.storeCancellationMask = addPairInterface(
        storeCancellationMask, PairRole.consumer,
        uniquify: (original) => 'store_cancellation_mask_$original');
  }
  if (loadCancellationMask == null) {
    this.loadCancellationMask =
        ReadyAndValidInterface(Logic(width: loadCount));
    this.loadCancellationMask.valid <= Const(0);
    this.loadCancellationMask.data <= Const(0, width: loadCount);
  } else {
    this.loadCancellationMask = addPairInterface(
        loadCancellationMask, PairRole.consumer,
        uniquify: (original) => 'load_cancellation_mask_$original');
  }
  addOutput('activeStores', width: storeCount);
  addOutput('selectedOlderStores', width: storeCount);
  addOutput('selectedLoadValid');
  loadOlderStoreMasks = [
    for (var load = 0; load < loadCount; load++)
      addOutput('loadOlderStoreMask$load', width: storeCount)
  ];
  addOutput('storeAllocationError');
  addOutput('storeReleaseError');
  addOutput('loadAllocationError');
  addOutput('loadReleaseError');
  addOutput('cancelInactiveStores', width: storeCount);
  addOutput('cancelInactiveLoads', width: loadCount);

  for (final interface in [
    this.storeAllocation,
    this.storeRelease,
    this.loadAllocation,
    this.loadRelease,
    this.storeCancellationMask,
    this.loadCancellationMask
  ]) {
    interface.ready <= Const(1);
  }
  Logic inRange(Logic index, int count, String operation) {
    final widthFillsSpace = count == (1 << index.width);
    return widthFillsSpace
        ? Const(1)
        : index
            .lt(Const(count, width: index.width))
            .named('${name}_${operation}_in_range');
  }

  final storeAllocationInRange =
      inRange(this.storeAllocation.data, storeCount, 'store_allocation');
  final storeReleaseInRange =
      inRange(this.storeRelease.data.tag, storeCount, 'store_release');
  final loadAllocationInRange =
      inRange(this.loadAllocation.data, loadCount, 'load_allocation');
  final loadReleaseInRange =
      inRange(this.loadRelease.data.tag, loadCount, 'load_release');
  final selectedLoadInRange =
      inRange(localSelectedLoad, loadCount, 'selected_load');
  final acceptedStoreAllocation =
      (this.storeAllocation.accepted & storeAllocationInRange)
          .named('${name}_store_allocation');
  final acceptedStoreRelease =
      (this.storeRelease.accepted & storeReleaseInRange)
          .named('${name}_store_release');
  final acceptedLoadAllocation =
      (this.loadAllocation.accepted & loadAllocationInRange)
          .named('${name}_load_allocation');
  final acceptedLoadRelease = (this.loadRelease.accepted & loadReleaseInRange)
      .named('${name}_load_release');
  final acceptedStoreCancellation = mux(this.storeCancellationMask.accepted,
          this.storeCancellationMask.data, Const(0, width: storeCount))
      .named('${name}_store_cancellation');
  final acceptedLoadCancellation = mux(this.loadCancellationMask.accepted,
          this.loadCancellationMask.data, Const(0, width: loadCount))
      .named('${name}_load_cancellation');
  final stores = Logic(name: '${name}_active_stores', width: storeCount);
  final loads = Logic(name: '${name}_active_loads', width: loadCount);
  final storedMasks = [
    for (var load = 0; load < loadCount; load++)
      Logic(name: '${name}_load_${load}_older_stores', width: storeCount)
  ];
  Logic storeWasActive = Const(0);
  Logic releasedStoreWasActive = Const(0);
  for (var store = storeCount - 1; store >= 0; store--) {
    storeWasActive = mux(
        this.storeAllocation.data.eq(store), stores[store], storeWasActive);
    releasedStoreWasActive = mux(this.storeRelease.data.tag.eq(store),
        stores[store], releasedStoreWasActive);
  }
  Logic loadWasActive = Const(0);
  Logic releasedLoadWasActive = Const(0);
  Logic selectedIsValid = Const(0);
  Logic selectedMask = Const(0, width: storeCount);
  for (var load = loadCount - 1; load >= 0; load--) {
    loadWasActive =
        mux(this.loadAllocation.data.eq(load), loads[load], loadWasActive);
    releasedLoadWasActive = mux(this.loadRelease.data.tag.eq(load),
        loads[load], releasedLoadWasActive);
    selectedIsValid =
        mux(localSelectedLoad.eq(load), loads[load], selectedIsValid);
    selectedMask =
        mux(localSelectedLoad.eq(load), storedMasks[load], selectedMask);
  }
  final replacingStore = (acceptedStoreRelease &
          this.storeRelease.data.tag.eq(this.storeAllocation.data))
      .named('${name}_replacing_store');
  final replacingLoad = (acceptedLoadRelease &
          this.loadRelease.data.tag.eq(this.loadAllocation.data))
      .named('${name}_replacing_load');
  final releasedStores = [
    for (var store = storeCount - 1; store >= 0; store--)
      acceptedStoreRelease & this.storeRelease.data.tag.eq(store)
  ].concat().named('${name}_released_stores');
  final allocatedStores = [
    for (var store = storeCount - 1; store >= 0; store--)
      acceptedStoreAllocation & this.storeAllocation.data.eq(store)
  ].concat().named('${name}_allocated_stores');
  final storesAfterRelease =
      (stores & ~releasedStores).named('${name}_stores_after_release');
  final storesBeforeCancellation = (storesAfterRelease | allocatedStores)
      .named('${name}_stores_before_cancel');
  final nextStores = (storesBeforeCancellation & ~acceptedStoreCancellation)
      .named('${name}_next_stores');
  final capturedStores = storeAllocationPrecedesLoad
      ? nextStores
      : storesAfterRelease & ~acceptedStoreCancellation;
  final releasedLoads = [
    for (var load = loadCount - 1; load >= 0; load--)
      acceptedLoadRelease & this.loadRelease.data.tag.eq(load)
  ].concat().named('${name}_released_loads');
  final allocatedLoads = [
    for (var load = loadCount - 1; load >= 0; load--)
      acceptedLoadAllocation & this.loadAllocation.data.eq(load)
  ].concat().named('${name}_allocated_loads');
  final loadsBeforeCancellation = ((loads & ~releasedLoads) | allocatedLoads)
      .named('${name}_loads_before_cancel');
  final nextLoads = (loadsBeforeCancellation & ~acceptedLoadCancellation)
      .named('${name}_next_loads');

  activeStores <= stores;
  selectedOlderStores <= selectedMask;
  selectedLoadValid <= selectedLoadInRange & selectedIsValid;
  for (var load = 0; load < loadCount; load++) {
    loadOlderStoreMasks[load] <= storedMasks[load];
  }
  storeAllocationError <=
      this.storeAllocation.accepted &
          (~storeAllocationInRange | (storeWasActive & ~replacingStore));
  storeReleaseError <=
      this.storeRelease.accepted &
          (~storeReleaseInRange | ~releasedStoreWasActive);
  loadAllocationError <=
      this.loadAllocation.accepted &
          (~loadAllocationInRange | (loadWasActive & ~replacingLoad));
  loadReleaseError <=
      this.loadRelease.accepted &
          (~loadReleaseInRange | ~releasedLoadWasActive);
  cancelInactiveStores <=
      acceptedStoreCancellation & ~storesBeforeCancellation;
  cancelInactiveLoads <= acceptedLoadCancellation & ~loadsBeforeCancellation;

  final nextMasks = storageImplementation == null
      ? storedMasks
      : [
          for (var load = 0; load < loadCount; load++)
            Logic(name: '${name}_next_mask_$load', width: storeCount)
        ];
  final maskActions = <Conditional>[
    for (var load = 0; load < loadCount; load++) ...[
      If(acceptedLoadRelease & this.loadRelease.data.tag.eq(load), then: [
        nextMasks[load] < 0,
      ]),
      If(acceptedLoadAllocation & this.loadAllocation.data.eq(load), then: [
        nextMasks[load] < capturedStores,
      ]),
      If(acceptedLoadCancellation[load], then: [
        nextMasks[load] < 0,
      ]),
    ],
  ];
  Sequential(localClk, reset: localReset, resetValues: {
    stores: 0,
    loads: 0,
    if (storageImplementation == null)
      for (final mask in storedMasks) mask: 0,
  }, [
    If(
        acceptedStoreAllocation |
            acceptedStoreRelease |
            acceptedStoreCancellation.or(),
        then: [
          stores < nextStores,
        ]),
    If(
        acceptedLoadAllocation |
            acceptedLoadRelease |
            acceptedLoadCancellation.or(),
        then: [
          loads < nextLoads,
        ]),
    if (storageImplementation == null) ...maskActions,
  ]);
  if (storageImplementation != null) {
    Combinational([
      for (var load = 0; load < loadCount; load++)
        nextMasks[load] < storedMasks[load],
      ...maskActions,
    ]);
    connectRegisterFileBank(
      clk: localClk,
      reset: localReset,
      current: storedMasks,
      next: nextMasks,
      resetValues: List.filled(
          loadCount, LogicValue.filled(storeCount, LogicValue.zero)),
      implementation: storageImplementation,
      name: '${name}_mask_storage',
    );
  }
}