Skip to content

Commit

Permalink
[Handshake][Python] Add Python bindings for Handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
teqdruid committed Nov 19, 2024
1 parent 026976a commit 79dce73
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
16 changes: 16 additions & 0 deletions integration_test/Bindings/Python/dialects/handshake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# REQUIRES: bindings_python
# RUN: %PYTHON% %s | FileCheck %s

import circt

from circt.dialects import hw, handshake
from circt.ir import Context, Location, Module, InsertionPoint, IntegerAttr, IntegerType

with Context() as ctx, Location.unknown():
circt.register_dialects(ctx)
m = Module.create()
with InsertionPoint(m.body):
op = handshake.FuncOp.create("foo", [("a", IntegerType.get_signless(8))],
[("x", IntegerType.get_signless(1))])
# CHECK: handshake.func @foo(i8, ...) -> i1 attributes {argNames = ["a"], resNames = ["x"]}
print(m)
8 changes: 8 additions & 0 deletions lib/Bindings/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ declare_mlir_dialect_python_bindings(
dialects/esi.py
DIALECT_NAME esi)

declare_mlir_dialect_python_bindings(
ADD_TO_PARENT CIRCTBindingsPythonSources.Dialects
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
TD_FILE dialects/HandshakeOps.td
SOURCES
dialects/handshake.py
DIALECT_NAME handshake)

declare_mlir_dialect_python_bindings(
ADD_TO_PARENT CIRCTBindingsPythonSources.Dialects
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
Expand Down
14 changes: 14 additions & 0 deletions lib/Bindings/Python/dialects/HandshakeOps.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//===- HandshakeOps.td - python op bindings gen ------------*- tablegen -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef BINDINGS_PYTHON_HANDSHAKE_OPS
#define BINDINGS_PYTHON_HANDSHAKE_OPS

include "circt/Dialect/Handshake/Handshake.td"

#endif
48 changes: 48 additions & 0 deletions lib/Bindings/Python/dialects/handshake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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

from __future__ import annotations

from . import handshake
from ._handshake_ops_gen import *
from ._handshake_ops_gen import _Dialect

from ..dialects._ods_common import _cext as _ods_cext
from ..ir import ArrayAttr, FunctionType, StringAttr, Type, TypeAttr

from typing import List, Tuple, Union

from ._ods_common import (
equally_sized_accessor as _ods_equally_sized_accessor,
get_default_loc_context as _ods_get_default_loc_context,
get_op_result_or_op_results as _get_op_result_or_op_results,
get_op_result_or_value as _get_op_result_or_value,
get_op_results_or_values as _get_op_results_or_values,
segmented_accessor as _ods_segmented_accessor,
)

_ods_ir = _ods_cext.ir


@_ods_cext.register_operation(_Dialect, replace=True)
class FuncOp(FuncOp):

@staticmethod
def create(sym_name: Union[StringAttr, str],
args: List[Tuple[str, Type]],
results: List[Tuple[str, Type]],
private: bool = False) -> FuncOp:
if isinstance(sym_name, str):
sym_name = StringAttr.get(sym_name)
input_types = [t for _, t in args]
res_types = [t for _, t in results]
func_type = FunctionType.get(input_types, res_types)
func_type_attr = TypeAttr.get(func_type)
funcop = FuncOp(func_type_attr)
funcop.attributes["sym_name"] = sym_name
funcop.attributes["argNames"] = ArrayAttr.get(
[StringAttr.get(name) for name, _ in args])
funcop.attributes["resNames"] = ArrayAttr.get(
[StringAttr.get(name) for name, _ in results])
return funcop

0 comments on commit 79dce73

Please sign in to comment.