Skip to content
New issue

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

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

Already on GitHub? # to your account

Error creating Instance inside an Instance #3708

Closed
tymcauley opened this issue Dec 31, 2023 · 2 comments · Fixed by #3726
Closed

Error creating Instance inside an Instance #3708

tymcauley opened this issue Dec 31, 2023 · 2 comments · Fixed by #3726

Comments

@tymcauley
Copy link
Contributor

Type of issue: Bug Report

Please provide the steps to reproduce the problem:

Here's a scala-cli template which will recreate the error:

//> using scala "2.13.12"
//> using lib "org.chipsalliance::chisel::6.0.0-RC1"
//> using plugin "org.chipsalliance:::chisel-plugin::6.0.0-RC1"
//> using options "-unchecked", "-deprecation", "-language:reflectiveCalls", "-feature", "-Xcheckinit", "-Xfatal-warnings", "-Ywarn-dead-code", "-Ywarn-unused", "-Ymacro-annotations"

import chisel3._
import chisel3.experimental.hierarchy.{Definition, Instance, instantiable, public}
import circt.stage.ChiselStage

@instantiable
class AddOne(val width: Int) extends Module {
  @public val width = width
  @public val in  = IO(Input(UInt(width.W)))
  @public val out = IO(Output(UInt(width.W)))
  out := in + 1.U
}

@instantiable
class AddTwo(addOneDef: Definition[AddOne]) extends Module {
  val i0 = Instance(addOneDef)
  val i1 = Instance(addOneDef)
  @public val in  = IO(Input(UInt(addOneDef.width.W)))
  @public val out = IO(Output(UInt(addOneDef.width.W)))
  i0.in := in
  i1.in := i0.out
  out   := i1.out
}

class Foo extends Module {
  val addOneDef = Definition(new AddOne(4))
  val addTwoDef = Definition(new AddTwo(addOneDef))

  val in  = IO(Input(UInt(addOneDef.width.W)))
  val out = IO(Output(UInt(addOneDef.width.W)))

  val addTwo = Instance(addTwoDef)

  addTwo.in := in
  out       := addTwo.out
}

object Main extends App {
  println(
    ChiselStage.emitSystemVerilog(
      gen = new Foo,
      firtoolOpts = Array("-disable-all-randomization", "-strip-debug-info")
    )
  )
}

What is the current behavior?

Executing this yields the following exception:

Exception in thread "main" chisel3.package$ChiselException: Imported Definition information not found - possibly forgot to add ImportDefinition annotation?
        at ... ()
        at AddTwo.$anonfun$i0$1(chisel-template.scala:20)
        at chisel3.internal.plugin.package$.autoNameRecursively(package.scala:33)
        at AddTwo.<init>(chisel-template.scala:20)
        at Foo.$anonfun$addTwoDef$1(chisel-template.scala:31)
        at chisel3.Module$.do_apply(Module.scala:62)
        at chisel3.experimental.hierarchy.core.Definition$.$anonfun$do_apply$1(Definition.scala:108)
        at chisel3.internal.Builder$.$anonfun$build$1(Builder.scala:970)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
        at chisel3.internal.Builder$.build(Builder.scala:964)
        at chisel3.experimental.hierarchy.core.Definition$.do_apply(Definition.scala:108)
        at Foo.<init>(chisel-template.scala:31)
        at Main$.$anonfun$new$7(chisel-template.scala:45)
        at ... ()
        at ... (Stack trace trimmed to user code only. Rerun with --full-stacktrace to see the full stack trace)
Program exited with return code 1.

Line 20 of chisel-template.scala refers to the val i0 = Instance(addOneDef) statement, so the first time an Instance is created inside of the AddTwo Definition.

What is the expected behavior?

The D/I API should work with nested Instances.

Please tell us about your environment:
- version: 6.0 RC-1
- OS: macOS 14.2.1

Other Information

Changing from Instance(addOneDef) to Instantiate(new AddOne(4)) makes the error go away, but both the Definition and Instantiate APIs have their uses, so I figure supporting nested Instances still makes sense.

@tymcauley
Copy link
Contributor Author

I need to investigate this a bit more to find the exact time things broke, but this code runs successfully on Chisel v3.5.3, and fails on v3.5.4 and later.

@tymcauley
Copy link
Contributor Author

Looks like #2512 / #2520 is the point where this started.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant