Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use MuxLookup default for full mapping #1201

Merged
merged 2 commits into from
Nov 6, 2019
Merged

Conversation

seldridge
Copy link
Member

@seldridge seldridge commented Oct 1, 2019

If you use MuxLookup with a full mapping, i.e., you fully enumerate all possible values for a key, you still wind up with unused hardware being generated in the case of a Mux failure case that is never used and which FIRRTL does not optimize away.

This PR modifies MuxLookup to examine if the user has provided a full mapping and to not emit the additional failure case for the initial Mux. In effect, the default is immediately discarded and the first mapping becomes the default.

This generally improves Verilog generation for users that are using MuxLookup like a Verilog case statement.

This would benefit from tests, but there weren't any tests already written. 😭 I can write them (read: I have not fully tested this). I just figured I would drop the PR first.

Example

For the following Chisel:

import chisel3._
import chisel3.util.MuxLookup
import chisel3.stage.{ChiselGeneratorAnnotation, ChiselStage}

class Qux extends RawModule {
  val in = IO(Input(Vec(2, UInt(2.W))))
  val sel = IO(Input(UInt(1.W)))
  val out = IO(Output(UInt(2.W)))

  val mapping = Seq( 0.U -> in(0),
                     1.U -> in(1) )

  out := MuxLookup(sel, DontCare, mapping)
}

(new ChiselStage).execute(Array("-X", "verilog"), Seq(ChiselGeneratorAnnotation(() => new Qux)))

Without this PR:

module Qux(
  input  [1:0] in_0,
  input  [1:0] in_1,
  input        sel,
  output [1:0] out
);
  wire [1:0] _T_1; // @[Mux.scala 68:16]
  wire  _T_3; // @[Mux.scala 68:19]
  assign _T_1 = sel ? in_1 : 2'h0; // @[Mux.scala 68:16]
  assign _T_3 = 1'h0 == sel; // @[Mux.scala 68:19]
  assign out = _T_3 ? in_0 : _T_1; // @[<pastie> 29:7]
endmodule

With this PR:

module Qux(
  input  [1:0] in_0,
  input  [1:0] in_1,
  input        sel,
  output [1:0] out
);
  assign out = sel ? in_1 : in_0; // @[<console> 23:7]
endmodule

Related issue:

Type of change: other enhancement

Impact: API modification (in terms of generating different Verilog)

Development Phase: implementation

Release Notes

  • Improved MuxLookup code generation

src/main/scala/chisel3/util/Mux.scala Outdated Show resolved Hide resolved
@albert-magyar
Copy link
Contributor

@jackkoenig I would review this, but I wrote some of the code, so you should take a look. The literal exhaustiveness check handles the bitwidth corner case oddities, too.

Copy link
Contributor

@jackkoenig jackkoenig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 question/concern, otherwise, LGTM!

src/main/scala/chisel3/util/Mux.scala Outdated Show resolved Hide resolved
seldridge and others added 2 commits November 5, 2019 22:42
This modifies MuxLookup to not use the 'default' mapping argument if a
"full" mapping is provided. A "full" mapping enumerates all possible
cases for a 'key' argument of a known size. This will check literal
values to ensure exhaustiveness holds.

Signed-off-by: Schuyler Eldridge <schuyler.eldridge@ibm.com>
Co-authored-by: Schuyler Eldridge <schuyler.eldridge@ibm.com>
Co-authored-by: Albert Magyar <albert.magyar@gmail.com>
Signed-off-by: Schuyler Eldridge <schuyler.eldridge@ibm.com>
Co-authored-by: Schuyler Eldridge <schuyler.eldridge@ibm.com>
Co-authored-by: Albert Magyar <albert.magyar@gmail.com>
@seldridge seldridge added Merge with merge commit Please merge with a merge commit, do not squash and merg API Modification labels Nov 6, 2019
@seldridge seldridge added this to the 3.3.0 milestone Nov 6, 2019
Copy link
Contributor

@jackkoenig jackkoenig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@seldridge seldridge merged commit 6f04b48 into master Nov 6, 2019
@seldridge seldridge deleted the full-MuxLookup branch November 6, 2019 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Modification Merge with merge commit Please merge with a merge commit, do not squash and merg
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants