circt icon indicating copy to clipboard operation
circt copied to clipboard

[crict-verilog][Mem2Reg] Lowering for `llhd.combinational` with peculiar CFG

Open AlviseDeFaveri opened this issue 4 months ago • 7 comments

While examining the circt-verilog output for Ibex I've noticed the following case (minimal reproducer)

module moduleName (
);
  localparam int unsigned MHPMCOUNTER_BASE = 3;
  logic [31:0] mhpmevent [32];

  always_comb begin : gen_mhpmevent
    for (int i = 0; i < 32; i++)
    begin : gen_mhpmevent_active
      mhpmevent[i] = '0; // <- remove this to make llhd block disapper

      if (i >= MHPMCOUNTER_BASE) begin
        mhpmevent[i][i - MHPMCOUNTER_BASE] = 1'b1;
      end
    end
  end

endmodule

Which generates an llhd.combinational block that cannot be lowered further by the pipeline:

Expand to see full output
module {
  hw.module @moduleName() {
    %c14_i5 = hw.constant 14 : i5
    %c13_i5 = hw.constant 13 : i5
    %c15_i5 = hw.constant 15 : i5
    %c12_i5 = hw.constant 12 : i5
    %c-16_i5 = hw.constant -16 : i5
    %c11_i5 = hw.constant 11 : i5
    %c-15_i5 = hw.constant -15 : i5
    %c10_i5 = hw.constant 10 : i5
    %c-14_i5 = hw.constant -14 : i5
    %c9_i5 = hw.constant 9 : i5
    %c-13_i5 = hw.constant -13 : i5
    %c8_i5 = hw.constant 8 : i5
    %c-12_i5 = hw.constant -12 : i5
    %c7_i5 = hw.constant 7 : i5
    %c-11_i5 = hw.constant -11 : i5
    %c6_i5 = hw.constant 6 : i5
    %c-10_i5 = hw.constant -10 : i5
    %c5_i5 = hw.constant 5 : i5
    %c-9_i5 = hw.constant -9 : i5
    %c4_i5 = hw.constant 4 : i5
    %c-8_i5 = hw.constant -8 : i5
    %c3_i5 = hw.constant 3 : i5
    %c-7_i5 = hw.constant -7 : i5
    %c2_i5 = hw.constant 2 : i5
    %c-6_i5 = hw.constant -6 : i5
    %c1_i5 = hw.constant 1 : i5
    %c-5_i5 = hw.constant -5 : i5
    %c0_i5 = hw.constant 0 : i5
    %c-4_i5 = hw.constant -4 : i5
    %c-3_i5 = hw.constant -3 : i5
    %c-2_i5 = hw.constant -2 : i5
    %c-1_i5 = hw.constant -1 : i5
    %0 = llhd.constant_time <0ns, 0d, 1e>
    %true = hw.constant true
    %c0_i32 = hw.constant 0 : i32
    %1 = hw.aggregate_constant [0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32] : !hw.array<32xi32>
    %mhpmevent = llhd.sig %1 : !hw.array<32xi32>
    llhd.combinational {
      %2 = llhd.sig.array_get %mhpmevent[%c-1_i5] : <!hw.array<32xi32>>
      llhd.drv %2, %c0_i32 after %0 : i32
      %3 = llhd.sig.array_get %mhpmevent[%c-2_i5] : <!hw.array<32xi32>>
      llhd.drv %3, %c0_i32 after %0 : i32
      %4 = llhd.sig.array_get %mhpmevent[%c-3_i5] : <!hw.array<32xi32>>
      llhd.drv %4, %c0_i32 after %0 : i32
      %5 = llhd.sig.array_get %mhpmevent[%c-4_i5] : <!hw.array<32xi32>>
      llhd.drv %5, %c0_i32 after %0 : i32
      %6 = llhd.sig.extract %5 from %c0_i5 : <i32> -> <i1>
      llhd.drv %6, %true after %0 : i1
      %7 = llhd.sig.array_get %mhpmevent[%c-5_i5] : <!hw.array<32xi32>>
      llhd.drv %7, %c0_i32 after %0 : i32
      %8 = llhd.sig.extract %7 from %c1_i5 : <i32> -> <i1>
      llhd.drv %8, %true after %0 : i1
      %9 = llhd.sig.array_get %mhpmevent[%c-6_i5] : <!hw.array<32xi32>>
      llhd.drv %9, %c0_i32 after %0 : i32
      %10 = llhd.sig.extract %9 from %c2_i5 : <i32> -> <i1>
      llhd.drv %10, %true after %0 : i1
      %11 = llhd.sig.array_get %mhpmevent[%c-7_i5] : <!hw.array<32xi32>>
      llhd.drv %11, %c0_i32 after %0 : i32
      %12 = llhd.sig.extract %11 from %c3_i5 : <i32> -> <i1>
      llhd.drv %12, %true after %0 : i1
      %13 = llhd.sig.array_get %mhpmevent[%c-8_i5] : <!hw.array<32xi32>>
      llhd.drv %13, %c0_i32 after %0 : i32
      %14 = llhd.sig.extract %13 from %c4_i5 : <i32> -> <i1>
      llhd.drv %14, %true after %0 : i1
      %15 = llhd.sig.array_get %mhpmevent[%c-9_i5] : <!hw.array<32xi32>>
      llhd.drv %15, %c0_i32 after %0 : i32
      %16 = llhd.sig.extract %15 from %c5_i5 : <i32> -> <i1>
      llhd.drv %16, %true after %0 : i1
      %17 = llhd.sig.array_get %mhpmevent[%c-10_i5] : <!hw.array<32xi32>>
      llhd.drv %17, %c0_i32 after %0 : i32
      %18 = llhd.sig.extract %17 from %c6_i5 : <i32> -> <i1>
      llhd.drv %18, %true after %0 : i1
      %19 = llhd.sig.array_get %mhpmevent[%c-11_i5] : <!hw.array<32xi32>>
      llhd.drv %19, %c0_i32 after %0 : i32
      %20 = llhd.sig.extract %19 from %c7_i5 : <i32> -> <i1>
      llhd.drv %20, %true after %0 : i1
      %21 = llhd.sig.array_get %mhpmevent[%c-12_i5] : <!hw.array<32xi32>>
      llhd.drv %21, %c0_i32 after %0 : i32
      %22 = llhd.sig.extract %21 from %c8_i5 : <i32> -> <i1>
      llhd.drv %22, %true after %0 : i1
      %23 = llhd.sig.array_get %mhpmevent[%c-13_i5] : <!hw.array<32xi32>>
      llhd.drv %23, %c0_i32 after %0 : i32
      %24 = llhd.sig.extract %23 from %c9_i5 : <i32> -> <i1>
      llhd.drv %24, %true after %0 : i1
      %25 = llhd.sig.array_get %mhpmevent[%c-14_i5] : <!hw.array<32xi32>>
      llhd.drv %25, %c0_i32 after %0 : i32
      %26 = llhd.sig.extract %25 from %c10_i5 : <i32> -> <i1>
      llhd.drv %26, %true after %0 : i1
      %27 = llhd.sig.array_get %mhpmevent[%c-15_i5] : <!hw.array<32xi32>>
      llhd.drv %27, %c0_i32 after %0 : i32
      %28 = llhd.sig.extract %27 from %c11_i5 : <i32> -> <i1>
      llhd.drv %28, %true after %0 : i1
      %29 = llhd.sig.array_get %mhpmevent[%c-16_i5] : <!hw.array<32xi32>>
      llhd.drv %29, %c0_i32 after %0 : i32
      %30 = llhd.sig.extract %29 from %c12_i5 : <i32> -> <i1>
      llhd.drv %30, %true after %0 : i1
      %31 = llhd.sig.array_get %mhpmevent[%c15_i5] : <!hw.array<32xi32>>
      llhd.drv %31, %c0_i32 after %0 : i32
      %32 = llhd.sig.extract %31 from %c13_i5 : <i32> -> <i1>
      llhd.drv %32, %true after %0 : i1
      %33 = llhd.sig.array_get %mhpmevent[%c14_i5] : <!hw.array<32xi32>>
      llhd.drv %33, %c0_i32 after %0 : i32
      %34 = llhd.sig.extract %33 from %c14_i5 : <i32> -> <i1>
      llhd.drv %34, %true after %0 : i1
      %35 = llhd.sig.array_get %mhpmevent[%c13_i5] : <!hw.array<32xi32>>
      llhd.drv %35, %c0_i32 after %0 : i32
      %36 = llhd.sig.extract %35 from %c15_i5 : <i32> -> <i1>
      llhd.drv %36, %true after %0 : i1
      %37 = llhd.sig.array_get %mhpmevent[%c12_i5] : <!hw.array<32xi32>>
      llhd.drv %37, %c0_i32 after %0 : i32
      %38 = llhd.sig.extract %37 from %c-16_i5 : <i32> -> <i1>
      llhd.drv %38, %true after %0 : i1
      %39 = llhd.sig.array_get %mhpmevent[%c11_i5] : <!hw.array<32xi32>>
      llhd.drv %39, %c0_i32 after %0 : i32
      %40 = llhd.sig.extract %39 from %c-15_i5 : <i32> -> <i1>
      llhd.drv %40, %true after %0 : i1
      %41 = llhd.sig.array_get %mhpmevent[%c10_i5] : <!hw.array<32xi32>>
      llhd.drv %41, %c0_i32 after %0 : i32
      %42 = llhd.sig.extract %41 from %c-14_i5 : <i32> -> <i1>
      llhd.drv %42, %true after %0 : i1
      %43 = llhd.sig.array_get %mhpmevent[%c9_i5] : <!hw.array<32xi32>>
      llhd.drv %43, %c0_i32 after %0 : i32
      %44 = llhd.sig.extract %43 from %c-13_i5 : <i32> -> <i1>
      llhd.drv %44, %true after %0 : i1
      %45 = llhd.sig.array_get %mhpmevent[%c8_i5] : <!hw.array<32xi32>>
      llhd.drv %45, %c0_i32 after %0 : i32
      %46 = llhd.sig.extract %45 from %c-12_i5 : <i32> -> <i1>
      llhd.drv %46, %true after %0 : i1
      %47 = llhd.sig.array_get %mhpmevent[%c7_i5] : <!hw.array<32xi32>>
      llhd.drv %47, %c0_i32 after %0 : i32
      %48 = llhd.sig.extract %47 from %c-11_i5 : <i32> -> <i1>
      llhd.drv %48, %true after %0 : i1
      %49 = llhd.sig.array_get %mhpmevent[%c6_i5] : <!hw.array<32xi32>>
      llhd.drv %49, %c0_i32 after %0 : i32
      %50 = llhd.sig.extract %49 from %c-10_i5 : <i32> -> <i1>
      llhd.drv %50, %true after %0 : i1
      %51 = llhd.sig.array_get %mhpmevent[%c5_i5] : <!hw.array<32xi32>>
      llhd.drv %51, %c0_i32 after %0 : i32
      %52 = llhd.sig.extract %51 from %c-9_i5 : <i32> -> <i1>
      llhd.drv %52, %true after %0 : i1
      %53 = llhd.sig.array_get %mhpmevent[%c4_i5] : <!hw.array<32xi32>>
      llhd.drv %53, %c0_i32 after %0 : i32
      %54 = llhd.sig.extract %53 from %c-8_i5 : <i32> -> <i1>
      llhd.drv %54, %true after %0 : i1
      %55 = llhd.sig.array_get %mhpmevent[%c3_i5] : <!hw.array<32xi32>>
      llhd.drv %55, %c0_i32 after %0 : i32
      %56 = llhd.sig.extract %55 from %c-7_i5 : <i32> -> <i1>
      llhd.drv %56, %true after %0 : i1
      %57 = llhd.sig.array_get %mhpmevent[%c2_i5] : <!hw.array<32xi32>>
      llhd.drv %57, %c0_i32 after %0 : i32
      %58 = llhd.sig.extract %57 from %c-6_i5 : <i32> -> <i1>
      llhd.drv %58, %true after %0 : i1
      %59 = llhd.sig.array_get %mhpmevent[%c1_i5] : <!hw.array<32xi32>>
      llhd.drv %59, %c0_i32 after %0 : i32
      %60 = llhd.sig.extract %59 from %c-5_i5 : <i32> -> <i1>
      llhd.drv %60, %true after %0 : i1
      %61 = llhd.sig.array_get %mhpmevent[%c0_i5] : <!hw.array<32xi32>>
      llhd.drv %61, %c0_i32 after %0 : i32
      %62 = llhd.sig.extract %61 from %c-4_i5 : <i32> -> <i1>
      llhd.drv %62, %true after %0 : i1
      llhd.yield
    }
    hw.output
  }
}

From my triaging it looks like Mem2Reg fails to promote because of the control-flow structure here. By adding another round of Mem2Reg after RemoveControlFlow in the pipeline this seems to be solved:

module {
  hw.module @moduleName() {
    hw.output
  }
}

I'm not sure if the "real" solution would be to make Mem2Reg more permissive or just moving/duplicating Mem2Reg makes sense, so leaving this up to debate.

AlviseDeFaveri avatar Jul 28 '26 15:07 AlviseDeFaveri

@AlviseDeFaveri and I chatted about this a bit today. Looks like this is a bug in LLHD's Mem2Reg pass. Maybe something about not looking through projections in different blocks properly.

fabianschuiki avatar Jul 29 '26 16:07 fabianschuiki

Updated example

Bit more info here using --mlir-print-before-all

Bug

In this case, Mem2Reg doesn't seem to able able to promote the mhpmevent extraction.

SystemVerilog testcase:

module moduleName (
);
  localparam int unsigned MHPMCOUNTER_BASE = 3;
  logic [31:0] mhpmevent [32];

  always_comb begin
    for (int i = 0; i < 32; i++)
    begin
      mhpmevent[i] = '0;

      if (i >= MHPMCOUNTER_BASE) begin
        mhpmevent[i][i - MHPMCOUNTER_BASE] = 1'b1;
      end
    end
  end

endmodule
IR Dump Before Mem2RegPass
// -----// IR Dump Before Mem2RegPass: llhd-mem2reg //----- //
hw.module @moduleName() {
  %c-3_i32 = hw.constant -3 : i32
  %c2_i32 = hw.constant 2 : i32
  %0 = llhd.constant_time <0ns, 0d, 1e>
  %c-1_i5 = hw.constant -1 : i5
  %c0_i27 = hw.constant 0 : i27
  %c1_i32 = hw.constant 1 : i32
  %true = hw.constant true
  %c0_i32 = hw.constant 0 : i32
  %c31_i32 = hw.constant 31 : i32
  %c32_i32 = hw.constant 32 : i32
  %1 = hw.aggregate_constant [0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32] : !hw.array<32xi32>
  %mhpmevent = llhd.sig %1 : !hw.array<32xi32>
  %2 = llhd.prb %mhpmevent : !hw.array<32xi32>
  llhd.process {
    cf.br ^bb2(%c0_i32 : i32)
  ^bb1:  // pred: ^bb6
    cf.br ^bb2(%c0_i32 : i32)
  ^bb2(%3: i32):  // 3 preds: ^bb0, ^bb1, ^bb5
    %4 = comb.icmp slt %3, %c32_i32 : i32
    cf.cond_br %4, ^bb3, ^bb6
  ^bb3:  // pred: ^bb2
    %5 = comb.sub %c31_i32, %3 : i32
    %6 = comb.extract %5 from 5 : (i32) -> i27
    %7 = comb.icmp eq %6, %c0_i27 : i27
    %8 = comb.extract %5 from 0 : (i32) -> i5
    %9 = comb.mux %7, %8, %c-1_i5 : i5
    %10 = llhd.sig.array_get %mhpmevent[%9] : <!hw.array<32xi32>>
    llhd.drv %10, %c0_i32 after %0 : i32
    %11 = comb.icmp ugt %3, %c2_i32 : i32
    cf.cond_br %11, ^bb4, ^bb5
  ^bb4:  // pred: ^bb3
    %12 = comb.add %3, %c-3_i32 : i32
    %13 = comb.extract %12 from 5 : (i32) -> i27
    %14 = comb.icmp eq %13, %c0_i27 : i27
    %15 = comb.extract %12 from 0 : (i32) -> i5
    %16 = comb.mux %14, %15, %c-1_i5 : i5
    %17 = llhd.sig.extract %10 from %16 : <i32> -> <i1>
    llhd.drv %17, %true after %0 : i1
    cf.br ^bb5
  ^bb5:  // 2 preds: ^bb3, ^bb4
    %18 = comb.add %3, %c1_i32 : i32
    cf.br ^bb2(%18 : i32)
  ^bb6:  // pred: ^bb2
    llhd.wait (%2 : !hw.array<32xi32>), ^bb1
  }
  hw.output
}
IR Dump After Mem2RegPass (has `llhd.sig.extract`)
// -----// IR Dump Before HoistSignalsPass: llhd-hoist-signals //----- //
hw.module @moduleName() {
  %c-3_i32 = hw.constant -3 : i32
  %c2_i32 = hw.constant 2 : i32
  %0 = llhd.constant_time <0ns, 0d, 1e>
  %c-1_i5 = hw.constant -1 : i5
  %c0_i27 = hw.constant 0 : i27
  %c1_i32 = hw.constant 1 : i32
  %true = hw.constant true
  %c0_i32 = hw.constant 0 : i32
  %c31_i32 = hw.constant 31 : i32
  %c32_i32 = hw.constant 32 : i32
  %1 = hw.aggregate_constant [0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32] : !hw.array<32xi32>
  %mhpmevent = llhd.sig %1 : !hw.array<32xi32>
  %2 = llhd.prb %mhpmevent : !hw.array<32xi32>
  llhd.process {
    cf.br ^bb2(%c0_i32 : i32)
  ^bb1:  // pred: ^bb6
    cf.br ^bb2(%c0_i32 : i32)
  ^bb2(%3: i32):  // 3 preds: ^bb0, ^bb1, ^bb5
    %4 = comb.icmp slt %3, %c32_i32 : i32
    cf.cond_br %4, ^bb3, ^bb6
  ^bb3:  // pred: ^bb2
    %5 = comb.sub %c31_i32, %3 : i32
    %6 = comb.extract %5 from 5 : (i32) -> i27
    %7 = comb.icmp eq %6, %c0_i27 : i27
    %8 = comb.extract %5 from 0 : (i32) -> i5
    %9 = comb.mux %7, %8, %c-1_i5 : i5
    %10 = llhd.sig.array_get %mhpmevent[%9] : <!hw.array<32xi32>>
    llhd.drv %10, %c0_i32 after %0 : i32
    %11 = comb.icmp ugt %3, %c2_i32 : i32
    cf.cond_br %11, ^bb4, ^bb5
  ^bb4:  // pred: ^bb3
    %12 = comb.add %3, %c-3_i32 : i32
    %13 = comb.extract %12 from 5 : (i32) -> i27
    %14 = comb.icmp eq %13, %c0_i27 : i27
    %15 = comb.extract %12 from 0 : (i32) -> i5
    %16 = comb.mux %14, %15, %c-1_i5 : i5
    %17 = llhd.sig.extract %10 from %16 : <i32> -> <i1>
    llhd.drv %17, %true after %0 : i1
    cf.br ^bb5
  ^bb5:  // 2 preds: ^bb3, ^bb4
    %18 = comb.add %3, %c1_i32 : i32
    cf.br ^bb2(%18 : i32)
  ^bb6:  // pred: ^bb2
    llhd.wait (%2 : !hw.array<32xi32>), ^bb1
  }
  hw.output
}

Expected Behavior

If we comment out the assignment to '0 we get:

module moduleName (
);
  localparam int unsigned MHPMCOUNTER_BASE = 3;
  logic [31:0] mhpmevent [32];

  always_comb begin
    for (int i = 0; i < 32; i++)
    begin
      // mhpmevent[i] = '0; // <- Removed this

      if (i >= MHPMCOUNTER_BASE) begin
        mhpmevent[i][i - MHPMCOUNTER_BASE] = 1'b1;
      end
    end
  end

endmodule
IR Dump Before Mem2RegPass
// -----// IR Dump Before Mem2RegPass: llhd-mem2reg //----- //
hw.module @moduleName() {
  %c-3_i32 = hw.constant -3 : i32
  %c2_i32 = hw.constant 2 : i32
  %0 = llhd.constant_time <0ns, 0d, 1e>
  %c-1_i5 = hw.constant -1 : i5
  %c0_i27 = hw.constant 0 : i27
  %c1_i32 = hw.constant 1 : i32
  %true = hw.constant true
  %c31_i32 = hw.constant 31 : i32
  %c32_i32 = hw.constant 32 : i32
  %c0_i32 = hw.constant 0 : i32
  %1 = hw.aggregate_constant [0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32] : !hw.array<32xi32>
  %mhpmevent = llhd.sig %1 : !hw.array<32xi32>
  %2 = llhd.prb %mhpmevent : !hw.array<32xi32>
  llhd.process {
    cf.br ^bb2(%c0_i32 : i32)
  ^bb1:  // pred: ^bb6
    cf.br ^bb2(%c0_i32 : i32)
  ^bb2(%3: i32):  // 3 preds: ^bb0, ^bb1, ^bb5
    %4 = comb.icmp slt %3, %c32_i32 : i32
    cf.cond_br %4, ^bb3, ^bb6
  ^bb3:  // pred: ^bb2
    %5 = comb.icmp ugt %3, %c2_i32 : i32
    cf.cond_br %5, ^bb4, ^bb5
  ^bb4:  // pred: ^bb3
    %6 = comb.sub %c31_i32, %3 : i32
    %7 = comb.extract %6 from 5 : (i32) -> i27
    %8 = comb.icmp eq %7, %c0_i27 : i27
    %9 = comb.extract %6 from 0 : (i32) -> i5
    %10 = comb.mux %8, %9, %c-1_i5 : i5
    %11 = llhd.sig.array_get %mhpmevent[%10] : <!hw.array<32xi32>>
    %12 = comb.add %3, %c-3_i32 : i32
    %13 = comb.extract %12 from 5 : (i32) -> i27
    %14 = comb.icmp eq %13, %c0_i27 : i27
    %15 = comb.extract %12 from 0 : (i32) -> i5
    %16 = comb.mux %14, %15, %c-1_i5 : i5
    %17 = llhd.sig.extract %11 from %16 : <i32> -> <i1>
    llhd.drv %17, %true after %0 : i1
    cf.br ^bb5
  ^bb5:  // 2 preds: ^bb3, ^bb4
    %18 = comb.add %3, %c1_i32 : i32
    cf.br ^bb2(%18 : i32)
  ^bb6:  // pred: ^bb2
    llhd.wait (%2 : !hw.array<32xi32>), ^bb1
  }
  hw.output
}

IR Dump After Mem2RegPass (lowered to `hw.array_inject`)
// -----// IR Dump Before HoistSignalsPass: llhd-hoist-signals //----- //
hw.module @moduleName() {
  %c-3_i32 = hw.constant -3 : i32
  %c2_i32 = hw.constant 2 : i32
  %c-1_i5 = hw.constant -1 : i5
  %c0_i27 = hw.constant 0 : i27
  %c1_i32 = hw.constant 1 : i32
  %c31_i32 = hw.constant 31 : i32
  %c32_i32 = hw.constant 32 : i32
  %c0_i32 = hw.constant 0 : i32
  %0 = hw.aggregate_constant [0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32, 0 : i32] : !hw.array<32xi32>
  %mhpmevent = llhd.sig %0 : !hw.array<32xi32>
  %1 = llhd.prb %mhpmevent : !hw.array<32xi32>
  llhd.process {
    %false = hw.constant false
    %2 = llhd.prb %mhpmevent : !hw.array<32xi32>
    cf.br ^bb2(%c0_i32, %2, %false : i32, !hw.array<32xi32>, i1)
  ^bb1:  // pred: ^bb6
    %false_0 = hw.constant false
    %3 = llhd.prb %mhpmevent : !hw.array<32xi32>
    cf.br ^bb2(%c0_i32, %3, %false_0 : i32, !hw.array<32xi32>, i1)
  ^bb2(%4: i32, %5: !hw.array<32xi32>, %6: i1):  // 3 preds: ^bb0, ^bb1, ^bb5
    %7 = comb.icmp slt %4, %c32_i32 : i32
    cf.cond_br %7, ^bb3, ^bb6
  ^bb3:  // pred: ^bb2
    %8 = comb.icmp ugt %4, %c2_i32 : i32
    cf.cond_br %8, ^bb4, ^bb5(%5, %6 : !hw.array<32xi32>, i1)
  ^bb4:  // pred: ^bb3
    %true = hw.constant true
    %9 = comb.sub %c31_i32, %4 : i32
    %10 = comb.extract %9 from 5 : (i32) -> i27
    %11 = comb.icmp eq %10, %c0_i27 : i27
    %12 = comb.extract %9 from 0 : (i32) -> i5
    %13 = comb.mux %11, %12, %c-1_i5 : i5
    %14 = comb.add %4, %c-3_i32 : i32
    %15 = comb.extract %14 from 5 : (i32) -> i27
    %16 = comb.icmp eq %15, %c0_i27 : i27
    %17 = comb.extract %14 from 0 : (i32) -> i5
    %18 = comb.mux %16, %17, %c-1_i5 : i5
    %19 = hw.array_get %5[%13] : !hw.array<32xi32>, i5
    %c0_i27_1 = hw.constant 0 : i27
    %20 = comb.concat %c0_i27_1, %18 : i27, i5
    %c1_i32_2 = hw.constant 1 : i32
    %21 = comb.shl %c1_i32_2, %20 : i32
    %c-1_i32 = hw.constant -1 : i32
    %22 = comb.xor bin %21, %c-1_i32 : i32
    %23 = comb.and %19, %22 : i32
    %c0_i31 = hw.constant 0 : i31
    %c1_i32_3 = hw.constant 1 : i32
    %24 = comb.shl %c1_i32_3, %20 : i32
    %25 = comb.or %23, %24 : i32
    %26 = hw.array_inject %5[%13], %25 : !hw.array<32xi32>, i5
    cf.br ^bb5(%26, %true : !hw.array<32xi32>, i1)
  ^bb5(%27: !hw.array<32xi32>, %28: i1):  // 2 preds: ^bb3, ^bb4
    %29 = comb.add %4, %c1_i32 : i32
    cf.br ^bb2(%29, %27, %28 : i32, !hw.array<32xi32>, i1)
  ^bb6:  // pred: ^bb2
    %30 = llhd.constant_time <0ns, 0d, 1e>
    llhd.drv %mhpmevent, %5 after %30 if %6 : !hw.array<32xi32>
    llhd.wait (%1 : !hw.array<32xi32>), ^bb1
  }
  hw.output
}

Root cause

Indeed Mem2Reg contains this comment:

// Projection operations are okay, as long as nested projections
// stay in the same block. Cross-block nested projections would break
// during promotion because the projection chain gets severed when
// Mem2Reg rewrites signal references into SSA block arguments.

I need to get accustomed a bit more with this pass to know how to deal with it.

AlviseDeFaveri avatar Jul 29 '26 18:07 AlviseDeFaveri

@fabianschuiki can you help me understand why a nested projection shouldn't cross a wait boundary? I'm looking at this testcase in mem2reg.mlir

// Don't promote a signal if a projection op (like sig.array_get) has a nested
// projection user (like sig.extract) in a different block. Mem2Reg rewrites
// signal references across block boundaries and would break the projection
// chain, causing getProjections to encounter a BlockArgument.
// CHECK-LABEL: @NestedProjectionAcrossBlocks
hw.module @NestedProjectionAcrossBlocks(in %v : i4) {
  %t = llhd.constant_time <0ns, 0d, 1e>
  %d = llhd.constant_time <1ns, 0d, 0e>
  %true = hw.constant true
  %c0 = hw.constant 0 : i8
  %c4 = hw.constant -4 : i3
  %init = hw.aggregate_constant [0 : i8, 0 : i8] : !hw.array<2xi8>
  // CHECK: %mem = llhd.sig
  %mem = llhd.sig %init : !hw.array<2xi8>
  llhd.process {
    %e = llhd.sig.array_get %mem[%true] : <!hw.array<2xi8>>
    llhd.drv %e, %c0 after %t : i8
    llhd.wait delay %d, ^bb1
  ^bb1:
    // This sig.extract is a nested projection of %e, but in a different block.
    // CHECK: llhd.sig.extract
    %sub = llhd.sig.extract %e from %c4 : <i8> -> <i4>
    llhd.drv %sub, %v after %t : i4
    llhd.halt
  }
}

AlviseDeFaveri avatar Jul 29 '26 18:07 AlviseDeFaveri

The wait boundaries are annoying because SSA values in the hw.module (outside the llhd.process) can change. If you had a %e = llhd.sig.array_get %mem[%v] (instead of %true as index), the array_get would take the current value of %v and return a reference for that value. But then wait suspends execution and resumes a bit later in ^bb1. When that block then uses %e, the question is what that reference refers to: it should refer to the "old" index %v from before the wait, not the current value of %v that might have changed.

The challenge is dealing with the llhd.drv. Mem2Reg turns all drives into an update of an SSA value. Instead of driving to a reference, the pass tracks what the current value of that reference is and updates it when it's driven. If you drive an entire signal it can just store the new value -- easy. But if you have a projection like sig.array_get or sig.extract, driving that means you're not updating the entire value of a signal, but only the value of a single field or array element. So at the location of the drive, the pass has to take the existing value it has for the whole reference, then figure out which field needs updating based on the projection ops, and use that as new value. That last part is the crux. Your projections may be defined in other blocks, but Mem2Reg needs to have the operands of the projection (e.g. %v in the array_get) available at the drive to compute the updated value:

llhd.process {
  %e = llhd.sig.array_get %mem[%v] : <!hw.array<2xi8>>
  llhd.drv %e, %c0 after %t : i8
  llhd.wait delay %d, ^bb1
^bb1:
  %sub = llhd.sig.extract %e from %u : <i8> -> <i4>
  llhd.drv %sub, %k after %t : i4
  
  // To implement the drive we need %v and %u here:
  %0 = hw.array_get %mem_value[%v]                     // get current value at %v
  %1 = hw.inject %0 from %u, %k : i8, i4               // update the bit range starting at %u
  %mem_value_new = hw.array_inject %mem_value[%v], %1  // create new value for entire memory

  llhd.halt
}

To implement this properly, llhd.wait would have to pass the %v from the array_get into ^bb1 as a block argument, such that ^bb1 would see the "old" value of %v and can reconstruct what the %e projection used to point at.

I don't think we need to support projections passed across llhd.wait at the moment, though. In your example above, the problem is that the two projections are in separate blocks:

llhd.process {
  // ...
  %10 = llhd.sig.array_get %mhpmevent[%9] : <!hw.array<32xi32>>
  llhd.drv %10, %c0_i32 after %0 : i32
  // ...
^bb4:  // <--- this block boundary is in the way
  // ...
  %17 = llhd.sig.extract %10 from %16 : <i32> -> <i1>
  llhd.drv %17, %true after %0 : i1
  // ...
}

That would be perfectly legal to support though. The projections %10 and %17 do not cross any llhd.wait ops, so we can just use the indices/offset operands of the projections, %9 and %16, directly when we compute the updated value at the drive. It's just that the code checking whether we can promote a certain signal with Mem2Reg sees that %10 is in a different block than the drive and gives up. But we could make it a bit more clever, and only give up if SSA values are in different blocks and they are alive across a wait boundary. (We should already be computing a Liveness for Mem2Reg to figure out which values need to be capture across waits. We could just ask that liveness if a projection SSA value crosses a wait when it's in a different block, and if it doesn't cross a wait, we don't give up but continue to promote the signal.)

You can add --debug-only=llhd-mem2reg to the circt-opt call to get some more details printed from Mem2Reg. (Feel free to add your own debug prints there if it helps!)

The problem is likely this specific part of findPromotableSlots: https://github.com/llvm/circt/blob/main/lib/Dialect/LLHD/Transforms/Mem2Reg.cpp#L937-L939

fabianschuiki avatar Jul 29 '26 19:07 fabianschuiki

Thanks a lot for the detailed explanation! Very cool! I was trying to understand which are the cases where this is legal.

The fix for the above case seems to be working, thanks!

However, I have some bad news 😅 I am also hitting another case where the projection happens in a nested region. Here's the example taken from the Ibex core

 // Write data calculation
    for (genvar i = 0; i < PMPNumRegions; i++) begin : g_pmp_csrs
      // ...
      // Outside
      assign pmp_cfg_wdata[i].lock  = csr_wdata_int[(i%4)*PMP_CFG_W+7];
      always_comb begin
        // Inside
        unique case (csr_wdata_int[(i%4)*PMP_CFG_W+3+:2])
          2'b00   : pmp_cfg_wdata[i].mode = PMP_MODE_OFF;
          2'b01   : pmp_cfg_wdata[i].mode = PMP_MODE_TOR;
          2'b10   : pmp_cfg_wdata[i].mode = (PMPGranularity == 0) ? PMP_MODE_NA4:
                                                                    PMP_MODE_OFF;
          2'b11   : pmp_cfg_wdata[i].mode = PMP_MODE_NAPOT;
          default : pmp_cfg_wdata[i].mode = PMP_MODE_OFF;
        endcase
      end

Which gets lowered to

  %1807 = llhd.sig.array_get %g_pmp_registers.pmp_cfg_wdata[%c-1_i2] : <!hw.array<4xstruct<lock: i1, mode: i2, exec: i1, write: i1, read: i1>>>
    %1808 = llhd.sig.struct_extract %1807["lock"] : <!hw.struct<lock: i1, mode: i2, exec: i1, write: i1, read: i1>>
    llhd.drv %1808, %1409 after %0 : i1
    llhd.combinational {
      %1994 = comb.extract %28 from 3 : (i32) -> i2
      %1995 = comb.icmp ceq %1994, %c0_i2 : i2
      cf.cond_br %1995, ^bb1(%c0_i2 : i2), ^bb2
    ^bb1(%1996: i2):  // 4 preds: ^bb0, ^bb2, ^bb3, ^bb3
      %1997 = llhd.sig.struct_extract %1807["mode"] : <!hw.struct<lock: i1, mode: i2, exec: i1, write: i1, read: i1>>
      llhd.drv %1997, %1996 after %0 : i2
      llhd.yield
    ^bb2:  // pred: ^bb0
      %1998 = comb.icmp ceq %1994, %c1_i2 : i2
      cf.cond_br %1998, ^bb1(%c1_i2 : i2), ^bb3
    ^bb3:  // pred: ^bb2
      %1999 = comb.icmp ceq %1994, %c-2_i2 : i2
      cf.cond_br %1999, ^bb1(%c-2_i2 : i2), ^bb1(%c-1_i2 : i2)
    }

As you can see %1807 is defined outside of the llhd.combinational but the projection happens inside.

I'm almost wondering it's CSE being too aggressive? I'm not really sure what are the type of things that can happen if you check across region boundaries. If you have opinions I'm happy to hear them.

AlviseDeFaveri avatar Jul 30 '26 15:07 AlviseDeFaveri

I wonder if the issue in this new case is that findPromotableSlots sees that the pmp_cfg_wdata[...] struct has assignments and projections both inside and outside the llhd.process, and gives up. My initial attempts at Mem2Reg were all very conservative to make sure I don't accidentally break things (still managed to break things though 😛). We could make findPromotableSlots more clever and allow promotion of parts of signals. The important part is that we don't promote signals that can alias each other. For example, if lock was assigned inside and outside the process, or the process assigns to mode but some external assignment targets the entire struct, that kind of thing.

If I remember correctly, findPromotableSlots roughly looks through projection operations and in the worst case gives up. We might be able to make it trace through all projections until it hits a signal definition, and keep track of what the stack of projections was (we have to do this in other places, too). And then we use this stack of projections to find refs inside the process that alias each other. We can also use it to figure out what the "most coarse" assignment to a struct is: if a process assigns a[x].foo and a[x], we should make a[x] the "promotable slot" and treat a[x].foo as updating part of the value. But if we only ever assign a[x].foo and a[x].bar, we can keep those as two separate promotable slots. That should allow an llhd.process to keep assigned struct fields and array elements as separate promoted slots if they don't overlap (meaning that the process will return separate result values for each which are then assigned to each field in a separate drive outside the process), and if they overlap as in a[x].foo and a[x], to produce a result for the coarsest assignment (a[x] in this case). I think Verilog calls this "longest static prefix" in the spec.

Does that make sense? I'm just trying to make stuff up on the spot 😝

(I don't exactly remember when the conversion to llhd.combinational happens. If you print the IR just before llhd-mem2reg, things might still be in llhd.process form. Doesn't really matter to be honest, but looking at exactly what goes into mem2reg can often simplify the problem analysis a bit.)

fabianschuiki avatar Jul 30 '26 17:07 fabianschuiki

So, my understanding is that there's two main points to touch in order to support the above case:

First, the findPromotableSlots() looks at each Op in a Process region and checks if the defining Op is a Signal. In the above case, the defining op for %1807 is actually a projection.

https://github.com/llvm/circt/blob/901ed907bae5325c5fb2b018823e8709842d30ff/lib/Dialect/LLHD/Transforms/Mem2Reg.cpp#L913-L917

So I guess the idea would be to change findPromotableSlots() so that, for each op:

  1. walk the projection chain backwards until you find a signal
  2. walk the projection chain of the original signal forwards
  3. check if the current projection aliases with others in the chain

But at this point it seems to me that the analysis would span across multiple regions (e.g. in the case above, you go back to the signal definition in the top module, then you would check all users, which can be anywhere) -- is that sound?

Hope this was reasonably clear

AlviseDeFaveri avatar Jul 31 '26 16:07 AlviseDeFaveri