|
26 | 26 | visit_value_or_value_wrapper_by_direction as
|
27 | 27 | visit_magma_value_or_value_wrapper_by_direction,
|
28 | 28 | )
|
| 29 | +from magma.backend.mlir.mem_utils import ( |
| 30 | + make_mem_reg, |
| 31 | + make_mem_read, |
| 32 | + emit_conditional_assign, |
| 33 | + make_index_op, |
| 34 | +) |
29 | 35 | from magma.backend.mlir.mlir import (
|
30 | 36 | MlirType, MlirValue, MlirSymbol, MlirAttribute, MlirBlock, push_block,
|
31 | 37 | push_location
|
@@ -366,29 +372,35 @@ def make_concat(self, operands, result):
|
366 | 372 | def visit_coreir_mem(self, module: ModuleWrapper) -> bool:
|
367 | 373 | inst = module.module
|
368 | 374 | defn = type(inst)
|
369 |
| - assert defn.coreir_name == "mem" |
| 375 | + assert ( |
| 376 | + defn.coreir_name == "mem" |
| 377 | + or defn.coreir_name == "sync_read_mem" |
| 378 | + ) |
370 | 379 | # TODO(rsetaluri): Add support for initialization.
|
371 | 380 | if defn.coreir_genargs["has_init"]:
|
372 | 381 | raise NotImplementedError("coreir.mem init not supported")
|
373 | 382 | width = defn.coreir_genargs["width"]
|
374 | 383 | depth = defn.coreir_genargs["depth"]
|
375 |
| - raddr, waddr, wdata, clk, wen = module.operands |
| 384 | + is_sync_read_mem = (defn.coreir_name == "sync_read_mem") |
| 385 | + raddr, waddr, wdata, clk, wen = module.operands[:5] |
376 | 386 | rdata = module.results[0]
|
377 |
| - elt_type = hw.InOutType(builtin.IntegerType(width)) |
378 |
| - reg_type = hw.InOutType(hw.ArrayType((depth,), elt_type.T)) |
379 |
| - reg = self.ctx.new_value(reg_type) |
380 |
| - sv.RegOp(name=inst.name, results=[reg]) |
| 387 | + mem = make_mem_reg( |
| 388 | + self._ctx, inst.name, depth, builtin.IntegerType(width) |
| 389 | + ) |
381 | 390 | # Register read logic.
|
382 |
| - read = self.ctx.new_value(elt_type) |
383 |
| - sv.ArrayIndexInOutOp(operands=[reg, raddr], results=[read]) |
384 |
| - sv.ReadInOutOp(operands=[read], results=[rdata]) |
| 391 | + read = make_index_op(self._ctx, mem, raddr) |
| 392 | + read_reg, read_temp = make_mem_read( |
| 393 | + self._ctx, read, rdata, is_sync_read_mem |
| 394 | + ) |
385 | 395 | # Register write logic.
|
386 |
| - write = self.ctx.new_value(elt_type) |
387 |
| - sv.ArrayIndexInOutOp(operands=[reg, waddr], results=[write]) |
| 396 | + write = make_index_op(self._ctx, mem, waddr) |
| 397 | + # Always logic. |
388 | 398 | always = sv.AlwaysFFOp(operands=[clk], clock_edge="posedge").body_block
|
389 | 399 | with push_block(always):
|
390 |
| - with push_block(sv.IfOp(operands=[wen]).then_block): |
391 |
| - sv.PAssignOp(operands=[write, wdata]) |
| 400 | + emit_conditional_assign(write, wdata, wen) |
| 401 | + if is_sync_read_mem: |
| 402 | + ren = module.operands[-1] |
| 403 | + emit_conditional_assign(read_reg, read_temp, ren) |
392 | 404 | return True
|
393 | 405 |
|
394 | 406 | @wrap_with_not_implemented_error
|
@@ -498,8 +510,15 @@ def _visit(value, counter):
|
498 | 510 | def visit_coreir_primitive(self, module: ModuleWrapper) -> bool:
|
499 | 511 | inst = module.module
|
500 | 512 | defn = type(inst)
|
501 |
| - assert (defn.coreir_lib == "coreir" or defn.coreir_lib == "corebit") |
502 |
| - if defn.coreir_name == "mem": |
| 513 | + assert ( |
| 514 | + defn.coreir_lib == "coreir" |
| 515 | + or defn.coreir_lib == "corebit" |
| 516 | + or defn.coreir_lib == "memory" |
| 517 | + ) |
| 518 | + if ( |
| 519 | + defn.coreir_name == "mem" |
| 520 | + or defn.coreir_name == "sync_read_mem" |
| 521 | + ): |
503 | 522 | return self.visit_coreir_mem(module)
|
504 | 523 | if defn.coreir_name == "not":
|
505 | 524 | return self.visit_coreir_not(module)
|
@@ -660,7 +679,11 @@ def visit_primitive(self, module: ModuleWrapper) -> bool:
|
660 | 679 | inst = module.module
|
661 | 680 | defn = type(inst)
|
662 | 681 | assert isprimitive(defn)
|
663 |
| - if defn.coreir_lib == "coreir" or defn.coreir_lib == "corebit": |
| 682 | + if ( |
| 683 | + defn.coreir_lib == "coreir" |
| 684 | + or defn.coreir_lib == "corebit" |
| 685 | + or defn.coreir_lib == "memory" |
| 686 | + ): |
664 | 687 | return self.visit_coreir_primitive(module)
|
665 | 688 | if defn.coreir_lib == "commonlib":
|
666 | 689 | return self.visit_commonlib_primitive(module)
|
|
0 commit comments