Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Test/Passes/CastsReconciliation/basic.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,19 @@
// CHECK-NEXT: "test.test"([[ARG]], [[ARG]]) : (i64, i64) -> ()
"func.return"() : () -> ()
}) : () -> ()



^10():
"func.func"() <{function_type = (i32) -> ()}> ({
^1(%0 : i32):
// Conversion cast to/from dissimilar types
%1 = "builtin.unrealized_conversion_cast"(%0) : (i32) -> !mod_arith.int<7 : i32>
%2 = "builtin.unrealized_conversion_cast"(%1) : (!mod_arith.int<7 : i32>) -> i32
"test.test"(%2) : (i32) -> ()
// CHECK: ^{{.*}}([[ARG:%.*]] : i32):
// CHECK-NEXT: "test.test"([[ARG]]) : (i32) -> ()
"func.return"() : () -> ()
}) : () -> ()



}) : () -> ()
33 changes: 33 additions & 0 deletions Test/Passes/ModArithToArith/chain.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: veir-opt %s -p="mod-arith-to-arith,reconcile-cast" | filecheck %s

"builtin.module"() ({
"func.func"() <{function_type = (!mod_arith.int<7 : i32>, !mod_arith.int<7 : i32>) -> !mod_arith.int<7 : i32>, sym_name = "main"}> ({
^bb0(%0 : !mod_arith.int<7 : i32>, %1 : !mod_arith.int<7 : i32>):
%a = "mod_arith.add"(%0, %1) : (!mod_arith.int<7 : i32>, !mod_arith.int<7 : i32>) -> !mod_arith.int<7 : i32>
%b = "mod_arith.mul"(%a, %a) : (!mod_arith.int<7 : i32>, !mod_arith.int<7 : i32>) -> !mod_arith.int<7 : i32>
"func.return"(%b) : (!mod_arith.int<7 : i32>) -> ()
}) : () -> ()
}) : () -> ()

// CHECK: ^{{.*}}([[ARG0:%.*]] : !mod_arith.int<7 : i32>, [[ARG1:%.*]] : !mod_arith.int<7 : i32>):
// input casts are kept
// CHECK-NEXT: [[C0:%.*]] = "builtin.unrealized_conversion_cast"([[ARG0]]) : (!mod_arith.int<7 : i32>) -> i32
// CHECK-NEXT: [[E0:%.*]] = "arith.extui"([[C0]]) : (i32) -> i33
// CHECK-NEXT: [[C1:%.*]] = "builtin.unrealized_conversion_cast"([[ARG1]]) : (!mod_arith.int<7 : i32>) -> i32
// CHECK-NEXT: [[E1:%.*]] = "arith.extui"([[C1]]) : (i32) -> i33

// "middle" casts are reconciled away
// CHECK-NEXT: [[QADD:%.*]] = "arith.constant"() <{"value" = 7 : i33}> : () -> i33
// CHECK-NEXT: [[SUM:%.*]] = "arith.addi"([[E0]], [[E1]]) : (i33, i33) -> i33
// CHECK-NEXT: [[SUMR:%.*]] = "arith.remui"([[SUM]], [[QADD]]) : (i33, i33) -> i33
// CHECK-NEXT: [[ADD:%.*]] = "arith.trunci"([[SUMR]]) <{"overflowFlags" = #arith.overflow<nuw>}> : (i33) -> i32
// CHECK-NEXT: [[M0:%.*]] = "arith.extui"([[ADD]]) : (i32) -> i64
// CHECK-NEXT: [[M1:%.*]] = "arith.extui"([[ADD]]) : (i32) -> i64
// CHECK-NEXT: [[QMUL:%.*]] = "arith.constant"() <{"value" = 7 : i64}> : () -> i64
// CHECK-NEXT: [[PROD:%.*]] = "arith.muli"([[M0]], [[M1]]) : (i64, i64) -> i64
// CHECK-NEXT: [[PRODR:%.*]] = "arith.remui"([[PROD]], [[QMUL]]) : (i64, i64) -> i64

// output casts are kept
// CHECK-NEXT: [[MUL:%.*]] = "arith.trunci"([[PRODR]]) <{"overflowFlags" = #arith.overflow<nuw>}> : (i64) -> i32
// CHECK-NEXT: [[RES:%.*]] = "builtin.unrealized_conversion_cast"([[MUL]]) : (i32) -> !mod_arith.int<7 : i32>
// CHECK-NEXT: "func.return"([[RES]]) : (!mod_arith.int<7 : i32>) -> ()
11 changes: 10 additions & 1 deletion Veir/Passes/CastsReconciliation/Reconciliation.lean
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ def isPreservingIntegerTypeRoundTrip (inputType interType : TypeAttr) : Bool :=
| .integerType x, .integerType y => x.bitwidth ≤ y.bitwidth
| _, _ => false


/- We reconcile cast from `!mod_arith.int< q: iN> to iM (and back) for any M -/
def isPreservingModArithToIntCast (inputType interType : TypeAttr) : Bool :=
match inputType.val, interType.val with
| .modArithType _, .integerType _ => True
| .integerType _, .modArithType _ => True
| _, _ => false


/- Reconciles round-trip casts of the form X->Y->X if allowed for these types by `legal X Y` -/
set_option warn.sorry false in
def reconcilePairingCast (legal : TypeAttr → TypeAttr → Bool) (rewriter : PatternRewriter OpCode)
Expand Down Expand Up @@ -72,7 +81,7 @@ def reconcileIdentityCast (rewriter : PatternRewriter OpCode) (op : OperationPtr

def CastReconcilePass.impl (ctx : WfIRContext OpCode) (op : OperationPtr) (_ : op.InBounds ctx.raw) :
ExceptT String IO (WfIRContext OpCode) := do
let pattern := RewritePattern.GreedyRewritePattern #[reconcilePairingCast isRiscvRegToI64Cast, reconcilePairingCast isPreservingIntegerTypeRoundTrip, reconcileIdentityCast]
let pattern := RewritePattern.GreedyRewritePattern #[reconcilePairingCast isRiscvRegToI64Cast, reconcilePairingCast isPreservingIntegerTypeRoundTrip, reconcilePairingCast isPreservingModArithToIntCast, reconcileIdentityCast]
match RewritePattern.applyInContext pattern ctx with
| none => throw "Error while applying cast reconciliation"
| some ctx => pure ctx
Expand Down
Loading