Skip to content

Commit

Permalink
change syntax parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Atticus Kuhn committed Jul 17, 2024
1 parent ce2e0d9 commit 5730a60
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
14 changes: 11 additions & 3 deletions SSA/Core/MLIRSyntax/GenericParser.lean
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,13 @@ syntax "@" ident : mlir_attr_val_symbol
syntax "@" str : mlir_attr_val_symbol
syntax "#" ident : mlir_attr_val -- alias
syntax "#" strLit : mlir_attr_val -- aliass

syntax "#" ident "<" strLit ">" : mlir_attr_val -- opaqueAttr
declare_syntax_cat dialect_attribute_contents
syntax mlir_attr_val : dialect_attribute_contents
syntax "(" dialect_attribute_contents + ")" : dialect_attribute_contents
syntax "[" dialect_attribute_contents + "]": dialect_attribute_contents
syntax "{" dialect_attribute_contents + "}": dialect_attribute_contents
-- syntax [^\[<({\]>)}\0]+ : dialect_attribute_contents
syntax "#" ident "<" dialect_attribute_contents ">" : mlir_attr_val -- opaqueAttr
syntax "#opaque<" ident "," strLit ">" ":" mlir_type : mlir_attr_val -- opaqueElementsAttr
syntax mlir_attr_val_symbol "::" mlir_attr_val_symbol : mlir_attr_val_symbol

Expand Down Expand Up @@ -582,7 +587,10 @@ macro_rules
| `([mlir_attr_val| # $dialect:ident < $opaqueData:str > ]) => do
let dialect := Lean.quote dialect.getId.toString
`(AttrValue.opaque_ $dialect $opaqueData)

| `([mlir_attr_val| # $dialect:ident < $opaqueData:ident > ]) => do
let d := Lean.quote dialect.getId.toString
let g : TSyntax `str := Lean.Syntax.mkStrLit (toString opaqueData.getId)
`(AttrValue.opaque_ $d $g)
macro_rules
| `([mlir_attr_val| #opaque< $dialect:ident, $opaqueData:str> : $t:mlir_type ]) => do
let dialect := Lean.quote dialect.getId.toString
Expand Down
18 changes: 11 additions & 7 deletions SSA/Projects/InstCombine/Base.lean
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ inductive MOp.BinaryOp : Type
| ashr
| urem
| srem
| add
| add (nsw : Bool) (nuw : Bool)
| mul
| sub
| sdiv
Expand Down Expand Up @@ -127,7 +127,7 @@ namespace MOp
@[match_pattern] def ashr (w : Width φ) : MOp φ := .binary w .ashr
@[match_pattern] def urem (w : Width φ) : MOp φ := .binary w .urem
@[match_pattern] def srem (w : Width φ) : MOp φ := .binary w .srem
@[match_pattern] def add (w : Width φ) : MOp φ := .binary w .add
@[match_pattern] def add (nsw : Bool) (nuw : Bool) (w : Width φ) : MOp φ := .binary w (.add nsw nuw)
@[match_pattern] def mul (w : Width φ) : MOp φ := .binary w .mul
@[match_pattern] def sub (w : Width φ) : MOp φ := .binary w .sub
@[match_pattern] def sdiv (w : Width φ) : MOp φ := .binary w .sdiv
Expand All @@ -146,7 +146,7 @@ def deepCasesOn {motive : ∀ {φ}, MOp φ → Sort*}
(ashr : ∀ {φ} {w : Width φ}, motive (ashr w))
(urem : ∀ {φ} {w : Width φ}, motive (urem w))
(srem : ∀ {φ} {w : Width φ}, motive (srem w))
(add : ∀ {φ} {w : Width φ}, motive (add w))
(add : ∀ {φ nsw nuw} {w : Width φ}, motive (add nsw nuw w))
(mul : ∀ {φ} {w : Width φ}, motive (mul w))
(sub : ∀ {φ} {w : Width φ}, motive (sub w))
(sdiv : ∀ {φ} {w : Width φ}, motive (sdiv w))
Expand All @@ -166,7 +166,7 @@ def deepCasesOn {motive : ∀ {φ}, MOp φ → Sort*}
| _, .ashr _ => ashr
| _, .urem _ => urem
| _, .srem _ => srem
| _, .add _ => add
| n, .add nsw nuw w => @add n nsw nuw w
| _, .mul _ => mul
| _, .sub _ => sub
| _, .sdiv _ => sdiv
Expand All @@ -189,7 +189,7 @@ instance : ToString (MOp φ) where
| .urem _ => "urem"
| .srem _ => "srem"
| .select _ => "select"
| .add _ => "add"
| .add _ _ _ => "add"
| .mul _ => "mul"
| .sub _ => "sub"
| .neg _ => "neg"
Expand All @@ -216,7 +216,7 @@ namespace Op
@[match_pattern] abbrev urem : Nat → Op := MOp.urem ∘ .concrete
@[match_pattern] abbrev srem : Nat → Op := MOp.srem ∘ .concrete
@[match_pattern] abbrev select : Nat → Op := MOp.select ∘ .concrete
@[match_pattern] abbrev add : Nat → Op := MOp.add ∘ .concrete
@[match_pattern] abbrev add (nuw : Bool := false) (nsw : Bool := false) : Nat → Op := (MOp.add nsw nuw) ∘ .concrete
@[match_pattern] abbrev mul : Nat → Op := MOp.mul ∘ .concrete
@[match_pattern] abbrev sub : Nat → Op := MOp.sub ∘ .concrete
@[match_pattern] abbrev neg : Nat → Op := MOp.neg ∘ .concrete
Expand Down Expand Up @@ -275,14 +275,18 @@ def Op.denote (o : LLVM.Op) (op : HVector TyDenote.toType (DialectSignature.sig
| Op.lshr _ => LLVM.lshr (op.getN 0) (op.getN 1)
| Op.ashr _ => LLVM.ashr (op.getN 0) (op.getN 1)
| Op.sub _ => LLVM.sub (op.getN 0) (op.getN 1)
| Op.add _ => LLVM.add (op.getN 0) (op.getN 1)
-- | Op.add _ => LLVM.add (op.getN 0) (op.getN 1)
| Op.add a b _ => LLVM.add (op.getN 0) (op.getN 1) {nsw := a, nuw := b}

-- | (@MOp.binary (ConcreteOrMVar.concrete _) (@MOp.BinaryOp.add true true)), _ => sorry
| Op.mul _ => LLVM.mul (op.getN 0) (op.getN 1)
| Op.sdiv _ => LLVM.sdiv (op.getN 0) (op.getN 1)
| Op.udiv _ => LLVM.udiv (op.getN 0) (op.getN 1)
| Op.urem _ => LLVM.urem (op.getN 0) (op.getN 1)
| Op.srem _ => LLVM.srem (op.getN 0) (op.getN 1)
| Op.icmp c _ => LLVM.icmp c (op.getN 0) (op.getN 1)
| Op.select _ => LLVM.select (op.getN 0) (op.getN 1) (op.getN 2)
-- | (@MOp.binary (ConcreteOrMVar.concrete _) (@MOp.BinaryOp.add true false)), _ => sorry

instance : DialectDenote LLVM := ⟨
fun o args _ => Op.denote o args
Expand Down
15 changes: 13 additions & 2 deletions SSA/Projects/InstCombine/LLVM/EDSL.lean
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def mkExpr (Γ : Ctxt (MetaLLVM φ).Ty) (opStx : MLIR.AST.Op φ) :
else
let v₂ : Γ.Var (.bitvec w) := (by simpa using hty) ▸ v₂

let (op : MOp.BinaryOp ⊕ LLVM.IntPredicate) ← match opStx.name with
let (op : (MOp.BinaryOp) ⊕ LLVM.IntPredicate) ← match opStx.name with
| "llvm.and" => pure <| Sum.inl .and
| "llvm.or" => pure <| Sum.inl .or
| "llvm.xor" => pure <| Sum.inl .xor
Expand All @@ -106,7 +106,18 @@ def mkExpr (Γ : Ctxt (MetaLLVM φ).Ty) (opStx : MLIR.AST.Op φ) :
| "llvm.ashr" => pure <| Sum.inl .ashr
| "llvm.urem" => pure <| Sum.inl .urem
| "llvm.srem" => pure <| Sum.inl .srem
| "llvm.add" => pure <| Sum.inl .add
| "llvm.add" => do
-- sorry
let att := opStx.attrs.getAttr "overflowFlags"
match att with
| .none => pure <| Sum.inl (MOp.BinaryOp.add false false)
| .some y => match y with
| (.opaque_ "llvm.overflow" "nsw") => pure <| Sum.inl (MOp.BinaryOp.add true false)
| (.opaque_ "llvm.overflow" "nuw") => pure <| Sum.inl (MOp.BinaryOp.add false true)
| (.opaque_ "llvm.overflow" s ) =>throw <| .generic s!"flag {s} not allowed"
| _ => throw <| .generic s!"flag not allowed"
-- sorry

| "llvm.mul" => pure <| Sum.inl .mul
| "llvm.sub" => pure <| Sum.inl .sub
| "llvm.sdiv" => pure <| Sum.inl .sdiv
Expand Down
2 changes: 1 addition & 1 deletion SSA/Projects/InstCombine/LLVM/Semantics.lean
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ theorem or?_eq : LLVM.or? a b = .some (BitVec.or a b) := rfl
def or {w : Nat} (x y : IntW w) (disjoint : Bool := false) : IntW w := do
let x' ← x
let y' ← y
if disjoint ∧ BitVec.toNat ( x' &&& y') = 0 then
if disjoint ∧ x' &&& y' 0 then
.none
else
or? x' y'
Expand Down

0 comments on commit 5730a60

Please sign in to comment.