Skip to content

Commit 62106e9

Browse files
Fix emitted FIRRTL for dynamic index of size 0 Vec (#4275) (#4276)
This is currently a warning but not yet an error so we need to emit legal FIRRTL. (cherry picked from commit d5ccf48) Co-authored-by: Jack Koenig <[email protected]>
1 parent b0f0c5a commit 62106e9

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

core/src/main/scala/chisel3/Aggregate.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ trait VecFactory extends SourceInfoDoc {
154154
implicit sourceInfo: SourceInfo
155155
): UInt = {
156156
val w = (n - 1).bitLength
157-
if (n <= 1) 0.U
157+
if (n <= 1) WireInit(0.U) // Need the Wire otherwise we emit vec[0] which is illegal FIRRTL.
158+
// Other cases do not need a Wire because the literal is truncated to fit.
158159
else if (idx.width.known && idx.width.get <= w) idx
159160
else if (idx.width.known) idx(w - 1, 0)
160161
else (idx | 0.U(w.W))(w - 1, 0)

src/test/scala/chiselTests/Vec.scala

+14
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,18 @@ class VecSpec extends ChiselPropSpec with Utils {
466466
}))
467467
log should be("")
468468
}
469+
470+
property("Indexing a size 0 Vec should warn but also emit legal FIRRTL") {
471+
val (log, chirrtl) = grabLog(emitCHIRRTL(new RawModule {
472+
val vec = IO(Input(Vec(0, UInt(8.W))))
473+
val idx = IO(Input(UInt(2.W)))
474+
val out = IO(Output(UInt(8.W)))
475+
out := vec(idx)
476+
}))
477+
log should include("Cannot extract from Vec of size 0.")
478+
chirrtl should include("input vec : UInt<8>[0]")
479+
chirrtl should include("wire _out_WIRE : UInt")
480+
chirrtl should include("connect _out_WIRE, UInt<1>(0h0)")
481+
chirrtl should include("connect out, vec[_out_WIRE]")
482+
}
469483
}

0 commit comments

Comments
 (0)