-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 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
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,56 @@ | ||
from __future__ import annotations | ||
from dataclasses import dataclass | ||
|
||
from xdsl.dialects.builtin import * | ||
from xdsl.irdl import * | ||
from xdsl.ir import * | ||
|
||
|
||
@dataclass | ||
class CMath: | ||
ctx: MLContext | ||
|
||
def __post_init__(self): | ||
self.ctx.register_attr(ComplexType) | ||
|
||
self.ctx.register_op(Norm) | ||
self.ctx.register_op(Mul) | ||
|
||
|
||
@irdl_attr_definition | ||
class ComplexType(ParametrizedAttribute): | ||
name = "cmath.complex" | ||
data: ParameterDef[Float64Type | Float32Type] | ||
|
||
|
||
@irdl_op_definition | ||
class Norm(Operation): | ||
name: str = "cmath.norm" | ||
|
||
op = OperandDef( | ||
ParamAttrConstraint(ComplexType, [AnyOf([Float32Type, Float64Type])])) | ||
res = ResultDef(AnyOf([Float32Type, Float64Type])) | ||
|
||
# TODO replace with trait | ||
def verify_(self) -> None: | ||
if self.op.typ.data != self.res.typ: | ||
raise VerifyException( | ||
"expect all input and output types to be equal") | ||
|
||
|
||
@irdl_op_definition | ||
class Mul(Operation): | ||
name: str = "cmath.mul" | ||
|
||
lhs = OperandDef( | ||
ParamAttrConstraint(ComplexType, [AnyOf([Float32Type, Float64Type])])) | ||
rhs = OperandDef( | ||
ParamAttrConstraint(ComplexType, [AnyOf([Float32Type, Float64Type])])) | ||
res = ResultDef( | ||
ParamAttrConstraint(ComplexType, [AnyOf([Float32Type, Float64Type])])) | ||
|
||
# TODO replace with trait | ||
def verify_(self) -> None: | ||
if self.lhs != self.rhs.typ and self.rhs.typ != self.res.typ: | ||
raise VerifyException( | ||
"expect all input and output types to be equal") |
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,34 @@ | ||
// RUN: xdsl-opt %s | xdsl-opt | filecheck %s | ||
|
||
module() { | ||
|
||
func.func() ["sym_name" = "conorm", "function_type" = !fun<[!cmath.complex<!f32>, !cmath.complex<!f32>], [!f32]>, "sym_visibility" = "private"] { | ||
^0(%p: !cmath.complex<!f32>, %q: !cmath.complex<!f32>): | ||
%norm_p : !f32 = cmath.norm(%p : !cmath.complex<!f32>) | ||
%norm_q : !f32 = cmath.norm(%q : !cmath.complex<!f32>) | ||
%pq : !f32 = arith.mulf(%norm_p : !f32, %norm_q : !f32) | ||
func.return(%pq : !f32) | ||
} | ||
|
||
// CHECK: func.func() ["sym_name" = "conorm", | ||
// CHECK-NEXT: ^{{.*}}({{.*}}: !cmath.complex<!f32>, {{.*}}: !cmath.complex<!f32>): | ||
// CHECK-NEXT: %{{.*}} : !f32 = cmath.norm(%{{.*}} : !cmath.complex<!f32>) | ||
// CHECK-NEXT: %{{.*}} : !f32 = cmath.norm(%{{.*}} : !cmath.complex<!f32>) | ||
// CHECK-NEXT: %{{.*}} : !f32 = arith.mulf(%{{.*}} : !f32, %{{.*}} : !f32) | ||
// CHECK-NEXT: func.return(%{{.*}} : !f32) | ||
// CHECK-NEXT: } | ||
|
||
func.func() ["sym_name" = "conorm2", "function_type" = !fun<[!cmath.complex<!f32>], [!f32]>, "sym_visibility" = "private"] { | ||
^1(%a: !cmath.complex<!f32>, %b: !cmath.complex<!f32>): | ||
%ab : !cmath.complex<!f32> = cmath.mul(%a : !cmath.complex<!f32>, %b : !cmath.complex<!f32>) | ||
%conorm : !f32 = cmath.norm(%ab : !cmath.complex<!f32>) | ||
func.return(%conorm : !f32) | ||
} | ||
// CHECK: func.func() ["sym_name" = "conorm2", | ||
// CHECK-NEXT: ^{{.*}}(%{{.*}}: !cmath.complex<!f32>, %{{.*}}: !cmath.complex<!f32>): | ||
// CHECK-NEXT: %{{.*}} : !cmath.complex<!f32> = cmath.mul(%{{.*}} : !cmath.complex<!f32>, %{{.*}} : !cmath.complex<!f32>) | ||
// CHECK-NEXT: %{{.*}} : !f32 = cmath.norm(%{{.*}} : !cmath.complex<!f32>) | ||
// CHECK-NEXT: func.return(%{{.*}} : !f32) | ||
// CHECK-NEXT: } | ||
|
||
} |