llvm / llvm/circt

[FIRParser] Accepting negative numbers where it doesn't make sense

Open
#3,260 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

FIRRTL
Dominant language
C++
Stars
2.2k
Forks
524
Avg merge
3d 2h
Merged PRs (30d)
46

Description

In the FIRRTL parser we have a function `parseIntLit`. This function happily parses `-1`, and is used in contexts where this doesn't make sense. Here are a couple of examples:

---

Negative integer literal. SFC produces "Exception in thread "main" firrtl.passes.CheckHighFormLike$NegUIntException: : [module Foo] UIntLiteral cannot be negative."

```
circuit Foo:
module Foo:
output z : UInt<8>
z <= UInt<8>(-1)
```
```mlir
firrtl.circuit "Foo" {
firrtl.module @Foo(out %z: !firrtl.uint<8>) {
%c15_ui8 = firrtl.constant 15 : !firrtl.uint<8>
firrtl.strictconnect %z, %c15_ui8 : !firrtl.uint<8>
}
}
```
---

Using a negative integer with `tail` happily compiles:

```
circuit Foo:
module Foo:
input a : UInt<100>
output z : UInt<100>
z <= tail(a, -1)
```
```
firrtl.circuit "Foo" {
firrtl.module @Foo(in %a: !firrtl.uint<100>, out %z: !firrtl.uint<100>) {
%0 = firrtl.tail %a, 15 : (!firrtl.uint<100>) -> !firrtl.uint<85>
%1 = firrtl.pad %0, 100 : (!firrtl.uint<85>) -> !firrtl.uint<100>
firrtl.strictconnect %z, %1 : !firrtl.uint<100>
}
}
```

---

Negative array length. Similar to above the `-1` is translated to `15`:
```
circuit Foo:
module Foo:
output z : UInt<8>[-1]
z is invalid
```
```mlir
firrtl.circuit "Foo" {
firrtl.module @Foo(out %z: !firrtl.vector, 15>) {
%invalid = firrtl.invalidvalue : !firrtl.vector, 15>
firrtl.strictconnect %z, %invalid : !firrtl.vector, 15>
}
}
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the FIRParser function parseIntLit and trace its uses in integer literals, tail, and array lengths. Reproduce the three examples from the issue and verify that negative values are rejected where they are invalid instead of being converted to unsigned values or accepted silently.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.