Skip to content

Commit

Permalink
dialects: (llvm) Add a bunch of float methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLydike committed Jan 23, 2025
1 parent 9cdc462 commit 47272fd
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 39 deletions.
22 changes: 21 additions & 1 deletion tests/filecheck/dialects/llvm/arithmetic.mlir
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: XDSL_ROUNDTRIP

%arg0, %arg1 = "test.op"() : () -> (i32, i32)
%arg0, %arg1, %f1 = "test.op"() : () -> (i32, i32, f32)

%add_both = llvm.add %arg0, %arg1 {"overflowFlags" = #llvm.overflow<nsw, nuw>} : i32
// CHECK: %add_both = llvm.add %arg0, %arg1 {overflowFlags = #llvm.overflow<nsw,nuw>} : i32
Expand Down Expand Up @@ -121,3 +121,23 @@

%icmp_uge = llvm.icmp "uge" %arg0, %arg1 : i32
// CHECK: %icmp_uge = llvm.icmp "uge" %arg0, %arg1 : i32

// float arith:

%fmul = llvm.fmul %f1, %f1 : f32
// CHECK: %fmul = llvm.fmul %f1, %f1 : f32

%fmul_fast = llvm.fmul %f1, %f1 {fastmathFlags = #llvm.fastmath<fast>} : f32
// CHECK: %fmul_fast = llvm.fmul %f1, %f1 {fastmathFlags = #llvm.fastmath<fast>} : f32

%fdiv = llvm.fdiv %f1, %f1 : f32
// CHECK: %fdiv = llvm.fdiv %f1, %f1 : f32

%fadd = llvm.fadd %f1, %f1 : f32
// CHECK: %fadd = llvm.fadd %f1, %f1 : f32

%fsub = llvm.fsub %f1, %f1 : f32
// CHECK: %fsub = llvm.fsub %f1, %f1 : f32

%frem = llvm.frem %f1, %f1 : f32
// CHECK: %frem = llvm.frem %f1, %f1 : f32
8 changes: 8 additions & 0 deletions tests/filecheck/dialects/llvm/example.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ builtin.module {

// CHECK: %val = "test.op"() : () -> i32
// CHECK-NEXT: %fval = llvm.bitcast %val : i32 to f32

%fval2 = llvm.sitofp %val : i32 to f32

// CHECK-NEXT: %fval2 = llvm.sitofp %val : i32 to f32

%fval3 = llvm.fpext %fval : f32 to f64

// CHECK-NEXT: %fval3 = llvm.fpext %fval : f32 to f64
}
151 changes: 113 additions & 38 deletions xdsl/dialects/llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
StringAttr,
SymbolRefAttr,
UnitAttr,
AnyFloat,
i1,
i32,
i64,
)
from xdsl.dialects.utils import FastMathAttrBase
from xdsl.dialects.utils import FastMathAttrBase, FastMathFlag
from xdsl.ir import (
Attribute,
BitEnumAttribute,
Expand All @@ -46,6 +47,7 @@
irdl_attr_definition,
irdl_op_definition,
operand_def,
opt_attr_def,
opt_operand_def,
opt_prop_def,
opt_result_def,
Expand Down Expand Up @@ -1710,10 +1712,7 @@ class ZeroOp(IRDLOperation):
res = result_def(LLVMTypeConstr)


@irdl_op_definition
class BitcastOp(IRDLOperation):
name = "llvm.bitcast"

class GenericCastOp(IRDLOperation, ABC):
arg = operand_def(Attribute)
"""
LLVM-compatible non-aggregate type
Expand All @@ -1735,56 +1734,132 @@ def __init__(self, val: Operation | SSAValue, res_type: Attribute):
)


class AbstractFloatArithOp(IRDLOperation, ABC):
T: ClassVar = VarConstraint("T", BaseAttr(AnyFloat))

Check failure on line 1738 in xdsl/dialects/llvm.py

View workflow job for this annotation

GitHub Actions / build (3.10)

Type of "T" is partially unknown   Type of "T" is "VarConstraint[Unknown]" (reportUnknownVariableType)

Check failure on line 1738 in xdsl/dialects/llvm.py

View workflow job for this annotation

GitHub Actions / build (3.10)

Argument type is partially unknown   Argument corresponds to parameter "constraint" in function "__init__"   Argument type is "BaseAttr[Unknown]" (reportUnknownArgumentType)

Check failure on line 1738 in xdsl/dialects/llvm.py

View workflow job for this annotation

GitHub Actions / build (3.10)

Argument of type "AnyFloat" cannot be assigned to parameter "attr" of type "type[AttributeCovT@BaseAttr]" in function "__init__"   Type "UnionType" is not assignable to type "type[AttributeCovT@BaseAttr]" (reportArgumentType)

lhs = operand_def(T)

Check failure on line 1740 in xdsl/dialects/llvm.py

View workflow job for this annotation

GitHub Actions / build (3.10)

Argument type is partially unknown   Argument corresponds to parameter "constraint" in function "operand_def"   Argument type is "VarConstraint[Unknown]" (reportUnknownArgumentType)
rhs = operand_def(T)

Check failure on line 1741 in xdsl/dialects/llvm.py

View workflow job for this annotation

GitHub Actions / build (3.10)

Argument type is partially unknown   Argument corresponds to parameter "constraint" in function "operand_def"   Argument type is "VarConstraint[Unknown]" (reportUnknownArgumentType)
res = result_def(T)

Check failure on line 1742 in xdsl/dialects/llvm.py

View workflow job for this annotation

GitHub Actions / build (3.10)

Argument type is partially unknown   Argument corresponds to parameter "constraint" in function "result_def"   Argument type is "VarConstraint[Unknown]" (reportUnknownArgumentType)

fastmathFlags = opt_attr_def(FastMathAttr)

traits = traits_def(NoMemoryEffect())

assembly_format = "$lhs `,` $rhs attr-dict `:` type($lhs)"

def __init__(
self,
lhs: SSAValue | Operation,
rhs: SSAValue | Operation,
fast_math: FastMathAttr | FastMathFlag | None,
):
if isinstance(fast_math, FastMathFlag | str):
fast_math = FastMathAttr(fast_math)

super().__init__(
operands=[lhs, rhs],
result_types=[SSAValue.get(lhs).type],
attributes={"fastmathFlags": fast_math},
)


@irdl_op_definition
class FAddOp(AbstractFloatArithOp):
name = "llvm.fadd"


@irdl_op_definition
class FMulOp(AbstractFloatArithOp):
name = "llvm.fmul"


@irdl_op_definition
class FDivOp(AbstractFloatArithOp):
name = "llvm.fdiv"


@irdl_op_definition
class FSubOp(AbstractFloatArithOp):
name = "llvm.fsub"


@irdl_op_definition
class FRemOp(AbstractFloatArithOp):
name = "llvm.frem"


@irdl_op_definition
class BitcastOp(GenericCastOp):
name = "llvm.bitcast"


@irdl_op_definition
class SIToFPOp(GenericCastOp):
name = "llvm.sitofp"


@irdl_op_definition
class FPExtOp(GenericCastOp):
name = "llvm.fpext"


LLVM = Dialect(
"llvm",
[
AShrOp,
AddOp,
AddressOfOp,
AllocaOp,
AndOp,
BitcastOp,
SubOp,
CallIntrinsicOp,
CallOp,
ConstantOp,
ExtractValueOp,
FAddOp,
FDivOp,
FMulOp,
FPExtOp,
FRemOp,
FSubOp,
FuncOp,
GEPOp,
GlobalOp,
ICmpOp,
InlineAsmOp,
InsertValueOp,
IntToPtrOp,
LShrOp,
LoadOp,
MulOp,
UDivOp,
NullOp,
OrOp,
ReturnOp,
SDivOp,
URemOp,
SExtOp,
SIToFPOp,
SRemOp,
AndOp,
OrOp,
XOrOp,
ShlOp,
LShrOp,
AShrOp,
StoreOp,
SubOp,
TruncOp,
ZExtOp,
SExtOp,
ICmpOp,
ExtractValueOp,
InsertValueOp,
InlineAsmOp,
UDivOp,
URemOp,
UndefOp,
AllocaOp,
GEPOp,
IntToPtrOp,
NullOp,
LoadOp,
StoreOp,
GlobalOp,
AddressOfOp,
FuncOp,
CallOp,
ReturnOp,
ConstantOp,
CallIntrinsicOp,
XOrOp,
ZExtOp,
ZeroOp,
],
[
LLVMStructType,
LLVMPointerType,
CallingConventionAttr,
FastMathAttr,
LLVMArrayType,
LLVMVoidType,
LLVMFunctionType,
LLVMPointerType,
LLVMStructType,
LLVMVoidType,
LinkageAttr,
CallingConventionAttr,
TailCallKindAttr,
FastMathAttr,
OverflowAttr,
TailCallKindAttr,
],
)

0 comments on commit 47272fd

Please sign in to comment.