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

Reduce splitAt to undefined in illegal contexts #2835

Merged
merged 1 commit into from
Nov 2, 2024
Merged

Conversation

christiaanb
Copy link
Member

Previously, the Clash compiler would try to reduce

splitAt d1 Nil

to something of type

(Vec 1 a, Vec (0-1) a)

by trying to project the head and the tail out of the Nil constructor. This of course does not work resulting in an out-of-bounds indexing error reported in:

#2831

The compiler now reduces above expressions to:

undefined :: (Vec 1 a, Vec (0-1) a)

Which is morally equivalent to the run-time exception Haskell evaluation would have thrown if the circuit description was evaluated like a regular Haskell program.

Fixes #2831

Still TODO:

  • Write a changelog entry (see changelog/README.md)
  • Check copyright notices are up to date in edited files

Previously, the Clash compiler would try to reduce

```
splitAt d1 Nil
```
to something of type
```
(Vec 1 a, Vec (0-1) a)
```
by trying to project the head and the tail out of the `Nil`
constructor. This of course does not work resulting in an
out-of-bounds indexing error reported in:

#2831

The compiler now reduces above expressions to:

```
undefined :: (Vec 1 a, Vec (0-1) a)
```

Which is morally equivalent to the run-time exception Haskell
evaluation would have thrown if the circuit description was
evaluated like a regular Haskell program.

Fixes #2831
@rowanG077
Copy link
Member

Perhaps reduce to clashCompileError? So that if it would latter end up in the circuit it would error out.

@christiaanb
Copy link
Member Author

Perhaps reduce to clashCompileError? So that if it would latter end up in the circuit it would error out.

Why wouldn't we want to generate HDL for something like splitAt d1 (unsafeCoerce Nil :: Vec 1 Bool) ?

@rowanG077
Copy link
Member

rowanG077 commented Oct 29, 2024

Because I would say that is nonsensical to generate hardware for. Or rather, my question would be. Why would you want to generate HDL for that? Essentially you have a malformed term, which is something different then something undefined.

@christiaanb
Copy link
Member Author

I do not want the compiler to generate clashCompileError. Let's say the compiler misses some optimization opportunity, and the clashCompileError is not elided before we get to HDL generation. In that case we don't generate the HDL just because of some (performance) bug in the compiler. Anyone hitting that bug then has to wait for someone to fix it and use what's probably a development version of the compiler to continue.

If we just generate undefined, the downstream logic synthesis tools will very likely optimize the logic away and neither the functionality nor the size of the circuit are affected. Users can now just happily synthesize the generated HDL and not bother with having to wait for some fix and run a development version of the compiler.

@christiaanb christiaanb merged commit 10f26ff into master Nov 2, 2024
13 checks passed
@christiaanb christiaanb deleted the fix_2831 branch November 2, 2024 06:11
mergify bot pushed a commit that referenced this pull request Nov 2, 2024
Previously, the Clash compiler would try to reduce

```
splitAt d1 Nil
```
to something of type
```
(Vec 1 a, Vec (0-1) a)
```
by trying to project the head and the tail out of the `Nil`
constructor. This of course does not work resulting in an
out-of-bounds indexing error reported in:

#2831

The compiler now reduces above expressions to:

```
undefined :: (Vec 1 a, Vec (0-1) a)
```

Which is morally equivalent to the run-time exception Haskell
evaluation would have thrown if the circuit description was
evaluated like a regular Haskell program.

Fixes #2831

(cherry picked from commit 10f26ff)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prelude.!!: index to large
2 participants