Skip to content

Commit

Permalink
Fix emitted FIRRTL for dynamic index of size 0 Vec
Browse files Browse the repository at this point in the history
This is currently a warning but not yet an error so we need to emit
legal FIRRTL.
  • Loading branch information
jackkoenig committed Jul 12, 2024
1 parent c107e31 commit 3612b74
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/src/main/scala/chisel3/Aggregate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ trait VecFactory extends SourceInfoDoc {
implicit sourceInfo: SourceInfo
): UInt = {
val w = (n - 1).bitLength
if (n <= 1) 0.U
if (n <= 1) WireInit(UInt(0.W), DontCare) // Need the Wire otherwise we emit vec[0] which is illegal FIRRTL.
// Other cases do not need a Wire because the literal is truncated to fit.
else if (idx.width.known && idx.width.get <= w) idx
else if (idx.width.known) idx(w - 1, 0)
else (idx | 0.U(w.W))(w - 1, 0)
Expand Down
14 changes: 14 additions & 0 deletions src/test/scala/chiselTests/Vec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,18 @@ class VecSpec extends ChiselPropSpec with Utils {
}))
log should be("")
}

property("Indexing a size 0 Vec should warn but also emit legal FIRRTL") {
val (log, chirrtl) = grabLog(emitCHIRRTL(new RawModule {
val vec = IO(Input(Vec(0, UInt(8.W))))
val idx = IO(Input(UInt(2.W)))
val out = IO(Output(UInt(8.W)))
out := vec(idx)
}))
log should include("Cannot extract from Vec of size 0.")
chirrtl should include("input vec : UInt<8>[0]")
chirrtl should include("wire _out_WIRE : UInt<0>")
chirrtl should include("invalidate _out_WIRE")
chirrtl should include("connect out, vec[_out_WIRE]")
}
}

0 comments on commit 3612b74

Please sign in to comment.