-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Handshake][Python] Add Python bindings for Handshake
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |