Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added verify to the integer conversion operations to follow llvm semantics #3341

Merged
merged 8 commits into from
Oct 25, 2024
Merged
32 changes: 32 additions & 0 deletions tests/filecheck/dialects/llvm/arith_invalid.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: xdsl-opt %s --split-input-file --verify-diagnostics | filecheck %s

"builtin.module"() ({

%arg0 = "test.op"() : () -> (i32)

%trunc = llvm.trunc %arg0 : i32 to i64
// CHECK: invalid cast opcode for cast from i32 to i64

}) : () -> ()

// -----

"builtin.module"() ({

%arg0 = "test.op"() : () -> (i32)

%zext = llvm.zext %arg0 : i32 to i16
// CHECK: invalid cast opcode for cast from i32 to i16

}) : () -> ()

// -----

"builtin.module"() ({

%arg0 = "test.op"() : () -> (i32)

%sext = llvm.sext %arg0 : i32 to i16
// CHECK: invalid cast opcode for cast from i32 to i16

}) : () -> ()
27 changes: 27 additions & 0 deletions xdsl/dialects/llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,16 +571,43 @@ class AShrOp(ArithmeticBinOperation):
class TruncOp(IntegerConversionOp):
name = "llvm.trunc"

def verify(self, verify_nested_ops: bool = True):
assert isinstance(self.arg.type, IntegerType)
assert isinstance(self.res.type, IntegerType)
if self.arg.type.bitwidth <= self.res.type.bitwidth:
raise VerifyException(
f"invalid cast opcode for cast from {self.arg.type} to {self.res.type}"
)
super().verify(verify_nested_ops)


@irdl_op_definition
class ZExtOp(IntegerConversionOp):
name = "llvm.zext"

def verify(self, verify_nested_ops: bool = True):
assert isinstance(self.arg.type, IntegerType)
assert isinstance(self.res.type, IntegerType)
if self.arg.type.bitwidth >= self.res.type.bitwidth:
raise VerifyException(
f"invalid cast opcode for cast from {self.arg.type} to {self.res.type}"
)
super().verify(verify_nested_ops)


@irdl_op_definition
class SExtOp(IntegerConversionOp):
name = "llvm.sext"

def verify(self, verify_nested_ops: bool = True):
assert isinstance(self.arg.type, IntegerType)
assert isinstance(self.res.type, IntegerType)
if self.arg.type.bitwidth >= self.res.type.bitwidth:
raise VerifyException(
f"invalid cast opcode for cast from {self.arg.type} to {self.res.type}"
)
super().verify(verify_nested_ops)


@irdl_op_definition
class GEPOp(IRDLOperation):
Expand Down
Loading