From 5105e80dbe834e2e57d62e9f3f099339ba8d9ad3 Mon Sep 17 00:00:00 2001 From: Jiuyang Liu Date: Sat, 14 Oct 2023 21:57:33 +0800 Subject: [PATCH] fix c-api, see llvm-project/circt dadf87b742f547840f6866beb50bdf46a76d3fed --- binder/src/main/scala/PanamaCIRCT.scala | 22 ++++++++++++++----- .../src/main/scala/PanamaCIRCTConverter.scala | 1 + common.sc | 8 +++++-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/binder/src/main/scala/PanamaCIRCT.scala b/binder/src/main/scala/PanamaCIRCT.scala index 40f1ff982e0..7d6cddec531 100644 --- a/binder/src/main/scala/PanamaCIRCT.scala +++ b/binder/src/main/scala/PanamaCIRCT.scala @@ -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 = { @@ -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)) } @@ -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 { @@ -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) @@ -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()) +} + diff --git a/binder/src/main/scala/PanamaCIRCTConverter.scala b/binder/src/main/scala/PanamaCIRCTConverter.scala index 2ea8dee937f..70fa8142741 100644 --- a/binder/src/main/scala/PanamaCIRCTConverter.scala +++ b/binder/src/main/scala/PanamaCIRCTConverter.scala @@ -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)) diff --git a/common.sc b/common.sc index 3d798167e10..28bde4c6394 100644 --- a/common.sc +++ b/common.sc @@ -359,8 +359,8 @@ trait CIRCTPanamaBinderModule "firtoolOptionsGetVbToBv", "firtoolOptionsSetDedup", "firtoolOptionsGetDedup", - "firtoolOptionsSetGrandCentralInstantiateCompanionOnly", - "firtoolOptionsGetGrandCentralInstantiateCompanionOnly", + "firtoolOptionsSetCompanionMode", + "firtoolOptionsGetCompanionMode", "firtoolOptionsSetDisableAggressiveMergeConnections", "firtoolOptionsGetDisableAggressiveMergeConnections", "firtoolOptionsSetEmitOMIR", @@ -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(