Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Fix issue where inlined cvt could cause crash #2124

Merged
merged 1 commit into from
Mar 16, 2021
Merged

Fix issue where inlined cvt could cause crash #2124

merged 1 commit into from
Mar 16, 2021

Conversation

jackkoenig
Copy link
Contributor

Due to inlining of Boolean expressions, the following circuit is handled
directly by the VerilogEmitter:

input a: UInt<4>
input b: SInt<1>
output o: UInt<5>
o <= dshl(a, asUInt(cvt(b)))

Priot to this change, this could crash due to mishandling of cvt in the
logic to inject parentheses based on Verilog precedence rules.

This is a corner case, but similar bugs would drop up if we open up the
VerilogEmitter to more expression inlining.

I considered instead changing Legalize to remove cvt (that that's perhaps still a good idea), but I wanted to solve a potential class of future issues and, as far as I can tell, this exact case is the only way to hit this bug currently.

Fixes #2035 h/t @drom for finding this issue

Contributor Checklist

  • Did you add Scaladoc to every public function/method?
  • [NA] Did you update the FIRRTL spec to include every new feature/behavior?
  • Did you add at least one test demonstrating the PR?
  • Did you delete any extraneous printlns/debugging code?
  • Did you specify the type of improvement?
  • Did you state the API impact?
  • Did you specify the code generation impact?
  • Did you request a desired merge strategy?
  • Did you add text to be included in the Release Notes for this change?

Type of Improvement

  • bug fix

API Impact

No impact

Backend Code Generation Impact

No impact

Desired Merge Strategy

  • Squash

Release Notes

Fix corner case where cvt of a 1-bit SInt (a no op) could crash the VerilogEmitter if passed as an argument to the shift amount in dshl

Reviewer Checklist (only modified by reviewer)

  • Did you add the appropriate labels?
  • Did you mark the proper milestone (1.2.x, 1.3.0, 1.4.0) ?
  • Did you review?
  • Did you check whether all relevant Contributor checkboxes have been checked?
  • Did you mark as Please Merge?

@jackkoenig jackkoenig added this to the 1.4.x milestone Mar 15, 2021
* These PrimOps emit either {..., a0, ...} or a0 so they never need parentheses
*/
private val neverParens: PrimOp => Boolean =
Set(Shl, Cat, Cvt, AsUInt, AsSInt, AsClock, AsAsyncReset)
Copy link
Contributor

Choose a reason for hiding this comment

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

What about adding Pad? I doubt this case gets hit in any code given the current inlining status, but it looks like it might be possible to get ({a0}) if you got the right tree in the emitter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call

Due to inlining of Boolean expressions, the following circuit is handled
directly by the VerilogEmitter:

input a: UInt<4>
input b: SInt<1>
output o: UInt<5>
o <= dshl(a, asUInt(cvt(b)))

Priot to this change, this could crash due to mishandling of cvt in the
logic to inject parentheses based on Verilog precedence rules.

This is a corner case, but similar bugs would drop up if we open up the
VerilogEmitter to more expression inlining.
Copy link
Contributor

@albert-magyar albert-magyar left a comment

Choose a reason for hiding this comment

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

LGTM

@jackkoenig jackkoenig merged commit 94d1bee into master Mar 16, 2021
mergify bot pushed a commit that referenced this pull request Mar 16, 2021
Due to inlining of Boolean expressions, the following circuit is handled
directly by the VerilogEmitter:

input a: UInt<4>
input b: SInt<1>
output o: UInt<5>
o <= dshl(a, asUInt(cvt(b)))

Priot to this change, this could crash due to mishandling of cvt in the
logic to inject parentheses based on Verilog precedence rules.

This is a corner case, but similar bugs would drop up if we open up the
VerilogEmitter to more expression inlining.

(cherry picked from commit 94d1bee)
@mergify mergify bot added the Backported This PR has been backported to marked stable branch label Mar 16, 2021
mergify bot added a commit that referenced this pull request Mar 16, 2021
Due to inlining of Boolean expressions, the following circuit is handled
directly by the VerilogEmitter:

input a: UInt<4>
input b: SInt<1>
output o: UInt<5>
o <= dshl(a, asUInt(cvt(b)))

Priot to this change, this could crash due to mishandling of cvt in the
logic to inject parentheses based on Verilog precedence rules.

This is a corner case, but similar bugs would drop up if we open up the
VerilogEmitter to more expression inlining.

(cherry picked from commit 94d1bee)

Co-authored-by: Jack Koenig <koenig@sifive.com>
@jackkoenig jackkoenig deleted the fix-2035 branch March 26, 2021 23:43
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Backported This PR has been backported to marked stable branch bugfix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(cvt, dshl) Exception in thread "main"
2 participants