Skip to content

Commit

Permalink
fix c-api, see llvm-project/circt dadf87b742f547840f6866beb50bdf46a76…
Browse files Browse the repository at this point in the history
…d3fed
  • Loading branch information
sequencer committed Oct 14, 2023
1 parent e831d95 commit 5105e80
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
22 changes: 16 additions & 6 deletions binder/src/main/scala/PanamaCIRCT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.llvm.circt.CAPI
// Wrapper for CIRCT APIs with Panama framework
class PanamaCIRCT {
// Open an arena for memory management of MLIR API calling in this context instance
private val arena = Arena.openConfined()
private val arena = Arena.ofConfined()

// Create MLIR context and register dialects we need
private val mlirCtx = {
Expand Down Expand Up @@ -41,7 +41,7 @@ class PanamaCIRCT {

private def newString(string: String): MlirStringRef = {
val bytes = string.getBytes()
val buffer = MemorySegment.allocateNative(bytes.length + 1, arena.scope())
val buffer = arena.allocate(bytes.length + 1)
buffer.copyFrom(MemorySegment.ofArray(bytes))
MlirStringRef(CAPI.mlirStringRefCreateFromCString(arena, buffer))
}
Expand All @@ -52,14 +52,14 @@ class PanamaCIRCT {
callback(MlirStringRef(message).toString)
}
}
MlirStringCallback(circt.MlirStringCallback.allocate(cb, arena.scope()))
MlirStringCallback(circt.MlirStringCallback.allocate(cb, arena))
}

private def seqToArray[T <: ForeignType[_]](xs: Seq[T]): (MemorySegment, Int) = {
if (xs.nonEmpty) {
val sizeOfT = xs(0).sizeof

val buffer = MemorySegment.allocateNative(sizeOfT * xs.length, arena.scope())
val buffer = arena.allocate(sizeOfT * xs.length)
xs.zipWithIndex.foreach {
case (x, i) =>
x.get match {
Expand Down Expand Up @@ -244,8 +244,8 @@ class PanamaCIRCT {
def firtoolOptionsGetVbToBv(options: FirtoolOptions): Boolean = CAPI.firtoolOptionsGetVbToBv(options.get)
def firtoolOptionsSetDedup(options: FirtoolOptions, value: Boolean) = CAPI.firtoolOptionsSetDedup(options.get, value)
def firtoolOptionsGetDedup(options: FirtoolOptions): Boolean = CAPI.firtoolOptionsGetDedup(options.get)
def firtoolOptionsSetGrandCentralInstantiateCompanionOnly(options: FirtoolOptions, value: Boolean) = CAPI.firtoolOptionsSetGrandCentralInstantiateCompanionOnly(options.get, value)
def firtoolOptionsGetGrandCentralInstantiateCompanionOnly(options: FirtoolOptions): Boolean = CAPI.firtoolOptionsGetGrandCentralInstantiateCompanionOnly(options.get)
def firtoolOptionsSetCompanionMode(options: FirtoolOptions, value: FirtoolCompanionMode) = CAPI.firtoolOptionsSetCompanionMode(options.get, value.get)
def firtoolOptionsGetCompanionMode(options: FirtoolOptions) = CAPI.firtoolOptionsGetCompanionMode(options.get)
def firtoolOptionsSetDisableAggressiveMergeConnections(options: FirtoolOptions, value: Boolean) = CAPI.firtoolOptionsSetDisableAggressiveMergeConnections(options.get, value)
def firtoolOptionsGetDisableAggressiveMergeConnections(options: FirtoolOptions): Boolean = CAPI.firtoolOptionsGetDisableAggressiveMergeConnections(options.get)
def firtoolOptionsSetEmitOMIR(options: FirtoolOptions, value: Boolean) = CAPI.firtoolOptionsSetEmitOMIR(options.get, value)
Expand Down Expand Up @@ -641,3 +641,13 @@ object FirtoolRandomKind {
final case object All extends FirtoolRandomKind(value = CAPI.FIRTOOL_RANDOM_KIND_ALL())
}

sealed class FirtoolCompanionMode(val value: Int) extends ForeignType[Int] {
private[circt] def get = value
private[circt] val sizeof = 4 // FIXME: jextract doesn't export type for C enum
}
object FirtoolCompanionMode {
final case object Bind extends FirtoolCompanionMode(value = CAPI.FIRTOOL_COMPANION_MODE_BIND())
final case object Instantiate extends FirtoolCompanionMode(value = CAPI.FIRTOOL_COMPANION_MODE_INSTANTIATE())
final case object Drop extends FirtoolCompanionMode(value = CAPI.FIRTOOL_COMPANION_MODE_DROP())
}

1 change: 1 addition & 0 deletions binder/src/main/scala/PanamaCIRCTConverter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ class PanamaCIRCTConverter extends CIRCTConverter {
}

val pm = circt.mlirPassManagerCreate()
val options = circt.firtoolOptionsCreateDefault()
assert_result(circt.firtoolPopulatePreprocessTransforms(pm, options))
assert_result(circt.firtoolPopulateCHIRRTLToLowFIRRTL(pm, options, mlirRootModule, "-"))
assert_result(circt.firtoolPopulateLowFIRRTLToHW(pm, options))
Expand Down
8 changes: 6 additions & 2 deletions common.sc
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ trait CIRCTPanamaBinderModule
"firtoolOptionsGetVbToBv",
"firtoolOptionsSetDedup",
"firtoolOptionsGetDedup",
"firtoolOptionsSetGrandCentralInstantiateCompanionOnly",
"firtoolOptionsGetGrandCentralInstantiateCompanionOnly",
"firtoolOptionsSetCompanionMode",
"firtoolOptionsGetCompanionMode",
"firtoolOptionsSetDisableAggressiveMergeConnections",
"firtoolOptionsGetDisableAggressiveMergeConnections",
"firtoolOptionsSetEmitOMIR",
Expand Down Expand Up @@ -460,6 +460,10 @@ trait CIRCTPanamaBinderModule
"FIRTOOL_RANDOM_KIND_MEM",
"FIRTOOL_RANDOM_KIND_REG",
"FIRTOOL_RANDOM_KIND_ALL",
// enum FirtoolCompanionMode
"FIRTOOL_COMPANION_MODE_BIND",
"FIRTOOL_COMPANION_MODE_INSTANTIATE",
"FIRTOOL_COMPANION_MODE_DROP",
))

def includeStructs = T(Seq(
Expand Down

0 comments on commit 5105e80

Please sign in to comment.