[PyCDE] Emit header guards for typedefs (#2221)

This commit is contained in:
John Demme 2021-11-19 14:42:51 -08:00 committed by GitHub
parent 6c921321ba
commit d2e29b5363
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 10 deletions

View File

@ -5,7 +5,7 @@
from collections import OrderedDict
import mlir.ir
from circt.dialects import hw
from circt.dialects import hw, sv
import circt.support
@ -60,15 +60,30 @@ class _Types:
if not self.registered_aliases:
return
type_scopes = [
op for op in mod.body.operations if isinstance(op, hw.TypeScopeOp)
]
if len(type_scopes) == 0:
with mlir.ir.InsertionPoint.at_block_begin(mod.body):
type_scopes.append(hw.TypeScopeOp.create(self.TYPE_SCOPE))
type_scopes = list()
for op in mod.body.operations:
if isinstance(op, hw.TypeScopeOp):
type_scopes.append(op)
continue
if isinstance(op, sv.IfDefOp):
if len(op.elseRegion.blocks) == 0:
continue
for ifdef_op in op.elseRegion.blocks[0]:
if isinstance(ifdef_op, hw.TypeScopeOp):
type_scopes.append(ifdef_op)
assert len(type_scopes) <= 1
if len(type_scopes) == 1:
type_scope = type_scopes[0]
else:
guard_name = "__PYCDE_TYPES__"
ifdef = sv.IfDefOp(mlir.ir.StringAttr.get(guard_name),
ip=mlir.ir.InsertionPoint.at_block_begin(mod.body))
with mlir.ir.InsertionPoint.at_block_begin(ifdef.elseRegion.blocks[0]):
sv.VerbatimOp(mlir.ir.StringAttr.get("`define " + guard_name), [],
mlir.ir.ArrayAttr.get([]))
type_scope = hw.TypeScopeOp.create(self.TYPE_SCOPE)
assert len(type_scopes) == 1
type_scope = type_scopes[0]
with mlir.ir.InsertionPoint(type_scope.body):
for (name, type) in self.registered_aliases.items():
declared_aliases = [

View File

@ -108,7 +108,9 @@ declare_mlir_dialect_python_bindings(
ADD_TO_PARENT CIRCTBindingsPythonSources.Dialects
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
TD_FILE circt/dialects/SVOps.td
SOURCES circt/dialects/sv.py
SOURCES
circt/dialects/sv.py
circt/dialects/_sv_ops_ext.py
DIALECT_NAME sv)
################################################################################

View File

@ -0,0 +1,24 @@
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import mlir.ir as _ir
class IfDefOp:
def __init__(self, cond: _ir.Attribute, *, loc=None, ip=None):
operands = []
results = []
attributes = {"cond": cond}
regions = 2
super().__init__(
self.build_generic(attributes=attributes,
results=results,
operands=operands,
successors=None,
regions=regions,
loc=loc,
ip=ip))
self.regions[0].blocks.append()
self.regions[1].blocks.append()