Repository: opencompl/lean-mlir
Pull request #1024: feat: move LLVM dialect to new elaborator
Author: login: alexkeizer
Created: 2025-03-11T09:43:33Z | Merged: 2025-03-26T11:31:35Z

This PR changes the `[llvm ..]` macro to use the new elaborator from #1023, reimplementing the metavariable instantiation in metacode. This has the benefit that the elaborator now gives the fully instantiated program, removing the need for (brittle) automation like `simp_alive_meta`. Anecdotally, elaboration of `AliveAutoGenerated.lean` now also feels a decent bit faster.


Diff:
diff --git a/SSA/Core/Util/ConcreteOrMVar.lean b/SSA/Core/Util/ConcreteOrMVar.lean
index 4c07bb4065..003797cd40 100644
--- a/SSA/Core/Util/ConcreteOrMVar.lean
+++ b/SSA/Core/Util/ConcreteOrMVar.lean
@@ -12,7 +12,7 @@ import Mathlib.Data.Vector.Basic
 inductive ConcreteOrMVar (α : Type u) (φ : Nat)
   | concrete (a : α)
   | mvar (i : Fin φ)
-  deriving DecidableEq, Repr, Inhabited
+  deriving DecidableEq, Repr, Inhabited, Lean.ToExpr
 
 instance [ToString α] : ToString (ConcreteOrMVar α n) where
   toString
diff --git a/SSA/Projects/InstCombine/AliveAutoGenerated.lean b/SSA/Projects/InstCombine/AliveAutoGenerated.lean
index 13e1f1c801..8ec2cb565a 100644
--- a/SSA/Projects/InstCombine/AliveAutoGenerated.lean
+++ b/SSA/Projects/InstCombine/AliveAutoGenerated.lean
@@ -14,8 +14,7 @@ namespace AliveAutoGenerated
 set_option pp.proofs false
 set_option pp.proofs.withType false
 set_option linter.deprecated false
-
-
+set_option linter.style.nameCheck false
 
 -- Name:AddSub:1043
 -- precondition: true
diff --git a/SSA/Projects/InstCombine/AliveHandwrittenLargeExamples.lean b/SSA/Projects/InstCombine/AliveHandwrittenLargeExamples.lean
index a109e640e3..732d25fb39 100644
--- a/SSA/Projects/InstCombine/AliveHandwrittenLargeExamples.lean
+++ b/SSA/Projects/InstCombine/AliveHandwrittenLargeExamples.lean
@@ -39,7 +39,6 @@ def alive_DivRemOfSelect_tgt (w : Nat) :=
 theorem alive_DivRemOfSelect (w : Nat) :
     alive_DivRemOfSelect_src w ⊑ alive_DivRemOfSelect_tgt w := by
   unfold alive_DivRemOfSelect_src alive_DivRemOfSelect_tgt
-  simp_alive_meta
   simp_alive_ssa
   simp_alive_undef
   simp_alive_case_bash
diff --git a/SSA/Projects/InstCombine/Base.lean b/SSA/Projects/InstCombine/Base.lean
index ed8b0cb537..3cac487c60 100644
--- a/SSA/Projects/InstCombine/Base.lean
+++ b/SSA/Projects/InstCombine/Base.lean
@@ -34,7 +34,7 @@ abbrev Width φ := ConcreteOrMVar Nat φ
 
 inductive MTy (φ : Nat)
   | bitvec (w : Width φ) : MTy φ
-  deriving DecidableEq, Inhabited
+  deriving DecidableEq, Inhabited, Lean.ToExpr
 
 abbrev Ty := MTy 0
 
@@ -87,7 +87,7 @@ inductive MOp.UnaryOp (φ : Nat) : Type
   | trunc (w' : Width φ) (noWrapFlags : NoWrapFlags := {nsw := false, nuw := false} )
   | zext  (w' : Width φ) (nneg : NonNegFlag := {nneg := false} )
   | sext  (w' : Width φ)
-deriving Repr, DecidableEq, Inhabited
+deriving Repr, DecidableEq, Inhabited, Lean.ToExpr
 
 /-- Homogeneous, binary operations -/
 inductive MOp.BinaryOp : Type
@@ -104,7 +104,7 @@ inductive MOp.BinaryOp : Type
   | sub  (nswnuw : NoWrapFlags := {nsw := false, nuw := false} )
   | sdiv (exact : ExactFlag := {exact := false} )
   | udiv (exact : ExactFlag := {exact := false} )
-deriving DecidableEq, Inhabited
+deriving DecidableEq, Inhabited, Lean.ToExpr
 
 open Std (Format) in
 /--
@@ -151,7 +151,7 @@ inductive MOp (φ : Nat) : Type
   | icmp    (c : IntPredicate) (w : Width φ) : MOp φ
   /-- Since the width of the const might not be known, we just store the value as an `Int` -/
   | const (w : Width φ) (val : ℤ) : MOp φ
-deriving Repr, DecidableEq, Inhabited
+deriving Repr, DecidableEq, Inhabited, Lean.ToExpr
 
 namespace MOp
 
@@ -376,6 +376,13 @@ instance {φ} : DialectSignature (MetaLLVM φ) where
 instance : DialectSignature LLVM where
   signature op := ⟨op.sig, [], op.outTy, .pure⟩
 
+instance {φ} : DialectToExpr (MetaLLVM φ) where
+  toExprM := .const ``Id [0]
+  toExprDialect := .app (.const ``MetaLLVM []) (Lean.toExpr φ)
+instance : DialectToExpr LLVM where
+  toExprM := .const ``Id [0]
+  toExprDialect := .const ``LLVM []
+
 @[simp]
 def Op.denote (o : LLVM.Op) (op : HVector TyDenote.toType (DialectSignature.sig o)) :
     (TyDenote.toType <| DialectSignature.outTy o) :=
diff --git a/SSA/Projects/InstCombine/LLVM/EDSL.lean b/SSA/Projects/InstCombine/LLVM/EDSL.lean
index b3cef40496..f9cc87a88b 100644
--- a/SSA/Projects/InstCombine/LLVM/EDSL.lean
+++ b/SSA/Projects/InstCombine/LLVM/EDSL.lean
@@ -3,7 +3,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
 -/
 import Qq
 import SSA.Projects.InstCombine.Base
-import SSA.Core.MLIRSyntax.EDSL
+import SSA.Core.MLIRSyntax.EDSL2
 import SSA.Projects.InstCombine.LLVM.CLITests
 
 open Qq Lean Meta Elab.Term Elab Command
@@ -245,79 +245,69 @@ def mkReturn (Γ : Ctxt (MetaLLVM φ).Ty) (opStx : MLIR.AST.Op φ) :
 instance : AST.TransformReturn (MetaLLVM φ) φ where
   mkReturn := mkReturn
 
-
 /-!
   ## Instantiation
   Finally, we show how to instantiate a family of programs to a concrete program
 -/
 
-def instantiateMTy (vals : List.Vector Nat φ) : (MetaLLVM φ).Ty → LLVM.Ty
-  | .bitvec w => .bitvec <| w.instantiate vals
-
-def instantiateMOp (vals : List.Vector Nat φ) : (MetaLLVM φ).Op → LLVM.Op
-  | .binary w binOp => .binary (w.instantiate vals) binOp
-  | .unary w unOp => .unary (w.instantiate vals) (unOp.instantiate vals)
-  | .select w => .select (w.instantiate vals)
-  | .icmp c w => .icmp c (w.instantiate vals)
-  | .const w val => .const (w.instantiate vals) val
-
-def instantiateCtxt (vals : List.Vector Nat φ) (Γ : Ctxt (MetaLLVM φ).Ty) :
-    Ctxt InstCombine.Ty :=
-  Γ.map (instantiateMTy vals)
-
-open InstCombine in
-def MOp.instantiateCom (vals : List.Vector Nat φ) : DialectMorphism (MetaLLVM φ) LLVM where
-  mapOp := instantiateMOp vals
-  mapTy := instantiateMTy vals
-  preserves_signature op := by
-    have h1 : ∀ (φ : Nat), 1 = ConcreteOrMVar.concrete (φ := φ) 1 := by intros φ; rfl
-    cases op <;>
-      (try casesm MOp.UnaryOp _) <;>
-      simp only [instantiateMTy, instantiateMOp, ConcreteOrMVar.instantiate, (· <$> ·), signature,
-      InstCombine.MOp.sig, InstCombine.MOp.outTy, Function.comp_apply, List.map,
-      Signature.mk, Signature.mkEffectful.injEq,
-      List.map_cons, List.map_nil, and_self, MTy.bitvec,
-      List.cons.injEq, MTy.bitvec.injEq, and_true, true_and,
-      RegionSignature.map, Signature.map, MOp.UnaryOp.instantiate, MOp.UnaryOp.outTy, h1]
-
-open InstCombine in
-def mkComInstantiate (reg : MLIR.AST.Region φ) :
-    MLIR.AST.ExceptM (MetaLLVM φ) (List.Vector Nat φ → Σ Γ eff ty, Com LLVM Γ eff ty) := do
-  let ⟨Γ, eff, ty, com⟩ ← MLIR.AST.mkCom reg
-  return fun vals =>
-    let Γ' := instantiateCtxt vals Γ
-    let ty' := instantiateMTy vals ty
-    let com' := com.changeDialect (MOp.instantiateCom vals)
-    ⟨Γ', eff, ty', com'⟩
+open InstCombine Qq in
+def MetaLLVM.instantiate (vals : Vector Expr φ) : DialectMetaMorphism (MetaLLVM φ) q(LLVM) where
+  mapTy := fun
+  | .bitvec w =>
+    mkApp (mkConst ``Ty.bitvec) <| w.metaInstantiate vals
+  mapOp :=
+    fun
+    | .binary w binOp =>
+      let w := w.metaInstantiate vals
+      mkApp2 (mkConst ``Op.binary) w (toExpr binOp)
+    | .unary w unOp =>
+      let w := w.metaInstantiate vals
+
+      /- NOTE: `Op` contructors expect a `Nat` argument to indicate the width,
+          but `MOp.UnaryOp` constructors expect `ConcreteOrMVar Nat 0`.
+          Hence, we define `mapWidth` to construct the latter
+      -/
+      let mapWidth (w : ConcreteOrMVar Nat φ) : Q(ConcreteOrMVar Nat 0) :=
+        let w : Q(Nat) := w.metaInstantiate vals
+        q(.concrete $w)
+      open MOp (UnaryOp) in
+      let unOp : Q(UnaryOp 0) := match unOp with
+        | .neg => q(.neg)
+        | .not => q(.not)
+        | .copy => q(.copy)
+        | .trunc w' flags => q(.trunc $(mapWidth w') $flags)
+        | .zext w' nneg => q(.zext $(mapWidth w') $nneg)
+        | .sext w' => q(.sext $(mapWidth w'))
+      mkApp2 (mkConst ``Op.unary) w unOp
+    | .select w =>
+      let w : Q(Nat) := w.metaInstantiate vals
+      mkApp (mkConst ``Op.select) w
+    | .icmp c w =>
+      let w := w.metaInstantiate vals
+      let c := toExpr c
+      mkApp2 (mkConst ``Op.icmp) c w
+    | .const w val =>
+      let w := w.metaInstantiate vals
+      let val := toExpr val
+      mkApp2 (mkConst ``Op.const) w val
 
 end InstcombineTransformDialect
 
-/-
-https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Cannot.20Find.20.60Real.2Eadd.60/near/402089561
-> I would recommend avoiding Qq for pattern matching.
-> That part of the Qq implementation is spicy.
 
-Therefore, we choose to match on raw `Expr`.
--/
 open SSA InstcombineTransformDialect InstCombine in
 elab "[llvm(" mvars:term,* ")| " reg:mlir_region "]" : term => do
-  have φ : Nat := mvars.getElems.size
-  -- HACK: QQ needs `φ` to be `have`-bound, rather than `let`-bound, otherwise `elabIntoCom` fails
-  let mcom ← withTraceNode `llvm (return m!"{exceptEmoji ·} elabIntoCom") <|
-    SSA.elabIntoCom reg q(MetaLLVM $φ)
+  withTraceNode `LeanMLIR.Elab (pure m!"{exceptEmoji ·} elaborate LLVM program") <| do
 
-  let mvalues : Q(List.Vector Nat $φ) ←
-    withTraceNode `llvm (return m!"{exceptEmoji ·} elaborating mvalues") <| do
-      let mvalues ← `(⟨[$mvars,*], by rfl⟩)
-      elabTermEnsuringType mvalues q(List.Vector Nat $φ)
+  let φ : Nat := mvars.getElems.size
+  let ⟨_, _, _, mcom⟩ ← SSA.elabIntoComObj reg (MetaLLVM φ)
 
-  let com ← withTraceNode `llvm (return m!"{exceptEmoji ·} building final Expr") <| do
-    let instantiateFun ← mkAppM ``MOp.instantiateCom #[mvalues]
-    let com ← mkAppM ``Com.changeDialect #[instantiateFun, mcom]
-    synthesizeSyntheticMVarsNoPostponing
-    return com
+  let res ← mcom.metaMap <| MetaLLVM.instantiate <| ←do
+    let mvars : Vector _ φ := ⟨mvars.getElems, rfl⟩
+    mvars.mapM fun (stx : Term) =>
+      elabTermEnsuringType stx (mkConst ``Nat)
 
-  return com
+  trace[LeanMLIR.Elab] "elaborated expression: {res}"
+  return res
 
 macro "[llvm| " reg:mlir_region "]" : term => `([llvm()| $reg])
 
@@ -325,3 +315,17 @@ macro "deftest" name:ident " := " test_reg:mlir_region : command => do
   `(@[reducible, llvmTest $name] def $(name) : ConcreteCliTest :=
        let code := [llvm()| $test_reg]
        { name := $(quote name.getId), ty := code.ty, context := code.ctxt, code := code, })
+
+section Test
+open InstCombine Ty
+
+/-- Assert that the elaborator respects variable ordering correctly -/
+private def variable_order1 : Com LLVM [bitvec 2, bitvec 1]  .pure (bitvec 1) := [llvm()| {
+    ^bb0(%arg1: i1, %arg2 : i2):
+      "llvm.return"(%arg1) : (i1) -> ()
+  }]
+/-- Assert that the elaborator respects variable ordering correctly -/
+private def variable_order2 : Com LLVM [bitvec 2, bitvec 1] .pure (bitvec 2) := [llvm()| {
+    ^bb0(%arg1: i1, %arg2 : i2):
+      "llvm.return"(%arg2) : (i2) -> ()
+  }]
diff --git a/SSA/Projects/InstCombine/LLVM/PrettyEDSL.lean b/SSA/Projects/InstCombine/LLVM/PrettyEDSL.lean
index 408b1b8523..81181b257f 100644
--- a/SSA/Projects/InstCombine/LLVM/PrettyEDSL.lean
+++ b/SSA/Projects/InstCombine/LLVM/PrettyEDSL.lean
@@ -239,14 +239,17 @@ private def pretty_test_exact :=
   }]
 
 example : pretty_test = prettier_test_generic 32 := by
-  unfold pretty_test prettier_test_generic
-  simp_alive_meta
   rfl
 
 example : pretty_test_generic = prettier_test_generic    := rfl
 
 /-! ## antiquotations test -/
 
+/-
+TODO: Constant value antiquotations don't work with the new elaborator.
+            To properly support these test-cases, we should include constant values
+            into the existing deeply-embedded metavariable instantiation
+
 private def antiquot_test (x) := -- antiquotated constant value in generic syntax
   [llvm| {
     %0 = "llvm.mlir.constant"() { value = $(.int (x : Nat) (.i _ 42)) } : () -> (i42)
@@ -259,6 +262,8 @@ private def antiquot_test_pretty (x : Nat) := -- antiquotated constant value in
   }]
 example : antiquot_test = antiquot_test_pretty := rfl
 
+-/
+
 end Test
 
 end EDSL
diff --git a/SSA/Projects/InstCombine/LLVM/Semantics.lean b/SSA/Projects/InstCombine/LLVM/Semantics.lean
index be9c3abb9d..20365bf956 100644
--- a/SSA/Projects/InstCombine/LLVM/Semantics.lean
+++ b/SSA/Projects/InstCombine/LLVM/Semantics.lean
@@ -43,7 +43,7 @@ theorem or?_eq : LLVM.or? a b  = some (a ||| b) := rfl
 
 structure DisjointFlag where
   disjoint : Bool := false
-  deriving Repr, DecidableEq
+  deriving Repr, DecidableEq, Lean.ToExpr
 
 @[simp_llvm_option]
 def or {w : Nat} (x y : IntW w)  (flag : DisjointFlag := {disjoint := false}) : IntW w := do
@@ -87,7 +87,7 @@ theorem add?_eq : LLVM.add? a b  = some (a + b) := rfl
 structure NoWrapFlags where
   nsw : Bool := false
   nuw : Bool := false
-  deriving Repr, DecidableEq
+  deriving Repr, DecidableEq, Lean.ToExpr
 
 @[simp_llvm_option]
 def add {w : Nat} (x y : IntW w) (flags : NoWrapFlags := {nsw := false , nuw := false}) : IntW w := do
@@ -185,7 +185,7 @@ def udiv? {w : Nat} (x y : BitVec w) : IntW w :=
 
 structure ExactFlag where
   exact : Bool := false
-  deriving Repr, DecidableEq
+  deriving Repr, DecidableEq, Lean.ToExpr
 
 @[simp_llvm_option]
 def udiv {w : Nat} (x y : IntW w) (flag : ExactFlag := {exact := false}) : IntW w := do
@@ -424,7 +424,7 @@ inductive IntPredicate where
   | sge
   | slt
   | sle
-deriving Inhabited, DecidableEq, Repr
+deriving Inhabited, DecidableEq, Repr, Lean.ToExpr
 
 instance : ToString IntPredicate where
   toString
@@ -580,7 +580,7 @@ def trunc {w: Nat} (w': Nat) (x: IntW w) (noWrapFlags : NoWrapFlags := {nsw := f
 
 structure NonNegFlag where
   nneg : Bool := false
-  deriving Repr, DecidableEq
+  deriving Repr, DecidableEq, Lean.ToExpr
 
 @[simp_llvm]
 def zext? {w: Nat} (w': Nat) (x: BitVec w) : IntW w' := do
diff --git a/SSA/Projects/InstCombine/Tactic.lean b/SSA/Projects/InstCombine/Tactic.lean
index f1edcbcb71..ed7b45b15a 100644
--- a/SSA/Projects/InstCombine/Tactic.lean
+++ b/SSA/Projects/InstCombine/Tactic.lean
@@ -10,49 +10,6 @@ import SSA.Core.Tactic
 
 open MLIR AST
 
-/-- We eliminate our alive framework's metavarible machinery.
-At the end of this pass, all `InstcombineTransformDialect.instantiate*` must be eliminated,
-and all `Width.mvar` should be resolved into `Width.concrete`.  -/
-macro "simp_alive_meta" : tactic =>
- `(tactic|
-     (
-      try simp (config := {failIfUnchanged := false }) only [Com.changeDialect_ret,
-        Com.changeDialect_var, Expr.changeDialect]
-      try simp (config := {failIfUnchanged := false }) only [(HVector.changeDialect_nil)]
-      try simp (config := {failIfUnchanged := false }) only [HVector.map']
-      try dsimp (config := {failIfUnchanged := false }) only [Functor.map]
-      try dsimp (config := {failIfUnchanged := false }) only [Ctxt.Var.succ_eq_toSnoc]
-      try dsimp (config := {failIfUnchanged := false }) only [Ctxt.Var.zero_eq_last]
-      try dsimp (config := {failIfUnchanged := false }) only [Ctxt.Var.toMap_last]
-      try dsimp (config := {failIfUnchanged := false }) only [Ctxt.DerivedCtxt.snoc_ctxt_eq_ctxt_snoc]
-      try dsimp (config := {failIfUnchanged := false }) only [List.map]
-      try dsimp (config := {failIfUnchanged := false }) only [Width.mvar]
-      try dsimp (config := {failIfUnchanged := false }) only [Ctxt.map_snoc, Ctxt.map_nil]
-      try dsimp (config := {failIfUnchanged := false }) only [Ctxt.get?]
-      try dsimp (config := {failIfUnchanged := false }) only [Ctxt.map, Ctxt.snoc]
-      try dsimp (config := {failIfUnchanged := false }) only [Ctxt.Var.toSnoc_toMap]
-      try dsimp (config := {failIfUnchanged := false }) only [Ctxt.Var.toMap_last]
-      try dsimp (config := {failIfUnchanged := false }) only [Ctxt.map_cons]
-      try dsimp (config := {failIfUnchanged := false }) only
-        [InstcombineTransformDialect.MOp.instantiateCom]
-      try dsimp (config := {failIfUnchanged := false }) only
-        [InstcombineTransformDialect.instantiateMTy]
-      try dsimp (config := {failIfUnchanged := false }) only [Fin.zero_eta, List.map_cons]
-      try dsimp (config := {failIfUnchanged := false }) only
-        [InstcombineTransformDialect.instantiateMOp]
-      try dsimp (config := {failIfUnchanged := false }) only [ConcreteOrMVar.instantiate_mvar_zero]
-      try dsimp (config := {failIfUnchanged := false }) only [ConcreteOrMVar.instantiate_mvar_zero']
-      try dsimp (config := {failIfUnchanged := false }) only [ConcreteOrMVar.instantiate_mvar_zero'']
-      try dsimp (config := {failIfUnchanged := false }) only [ConcreteOrMVar.instantiate]
-      try dsimp (config := {failIfUnchanged := false }) only
-        [InstcombineTransformDialect.instantiateMTy]
-      try dsimp (config := {failIfUnchanged := false }) only [ConcreteOrMVar.instantiate_mvar_zero'']
-      -- How can I avoid this `simp! only` and instead use a plain `simp only`?
-      try dsimp (config := {failIfUnchanged := false }) only [ConcreteOrMVar.ofNat_eq_concrete]
-      try simp! (config := {failIfUnchanged := false }) only [ConcreteOrMVar.instantiate_ofNat_eq]
-   )
- )
-
 /-- Eliminate the SSA structure of the program
 - We first simplify `Com.refinement` to see the context `Γv`.
 - We `simp_peephole Γv` to simplify context accesses by variables.
@@ -88,7 +45,6 @@ where `com₁` and `com₂` are programs in the `LLVM` dialect. -/
 macro "simp_alive_peephole" : tactic =>
   `(tactic|
       (
-        simp_alive_meta
         simp_alive_ssa
       )
    )
diff --git a/SSA/Projects/InstCombine/Test.lean b/SSA/Projects/InstCombine/Test.lean
index a2c9cda176..d0f49d37f8 100644
--- a/SSA/Projects/InstCombine/Test.lean
+++ b/SSA/Projects/InstCombine/Test.lean
@@ -232,10 +232,7 @@ def bb0IcomGeneric (w : Nat) := [llvm(w)|
   }]
 
 /-- Indeed, the concrete program is an instantiation of the generic program -/
-example : bb0IcomGeneric 32 = bb0IcomConcrete := by
-  unfold bb0IcomGeneric bb0IcomConcrete
-  simp_alive_meta
-  rfl
+example : bb0IcomGeneric 32 = bb0IcomConcrete := rfl
 
 /-
   Simple example of the denotation of `GenericWidth`.
@@ -244,12 +241,7 @@ example : bb0IcomGeneric 32 = bb0IcomConcrete := by
   can use `denote`. In this way, we indirectly give semantics to the family of programs that
   `GenericWidth` represents.
 -/
-example (w Γv) : (GenericWidth w).denote Γv = some (BitVec.ofNat w 0) := by
-  unfold GenericWidth
-  revert Γv
-  simp_alive_meta
-  simp_alive_ssa
-  rfl
+example (w Γv) : (GenericWidth w).denote Γv = some (BitVec.ofNat w 0) := rfl
 
 open ComWrappers
 
@@ -273,14 +265,12 @@ def one_inst_com_proof (w : Nat) :
     one_inst_com w ⊑ one_inst_com w := by
   unfold one_inst_com
   simp only [simp_llvm_wrap]
-  simp_alive_meta
   simp_alive_ssa
   apply one_inst_stmt
 
 def one_inst_macro_proof (w : Nat) :
     one_inst_macro w ⊑ one_inst_macro w := by
   unfold one_inst_macro
-  simp_alive_meta
   simp_alive_ssa
   apply one_inst_stmt
 
@@ -306,14 +296,12 @@ def two_inst_com_proof (w : Nat) :
     two_inst_com w ⊑ two_inst_com w := by
   unfold two_inst_com
   simp only [simp_llvm_wrap]
-  simp_alive_meta
   simp_alive_ssa
   apply two_inst_stmt
 
 def two_inst_macro_proof (w : Nat) :
     two_inst_macro w ⊑ two_inst_macro w := by
   unfold two_inst_macro
-  simp_alive_meta
   simp_alive_ssa
   apply two_inst_stmt
 
@@ -342,14 +330,12 @@ def three_inst_com_proof (w : Nat) :
     three_inst_com w ⊑ three_inst_com w := by
   unfold three_inst_com
   simp only [simp_llvm_wrap]
-  simp_alive_meta
   simp_alive_ssa
   apply three_inst_stmt
 
 def three_inst_macro_proof (w : Nat) :
     three_inst_macro w ⊑ three_inst_macro w := by
   unfold three_inst_macro
-  simp_alive_meta
   simp_alive_ssa
   apply three_inst_stmt
 
@@ -373,14 +359,12 @@ def one_inst_concrete_com_proof :
     one_inst_concrete_com ⊑ one_inst_concrete_com := by
   unfold one_inst_concrete_com
   simp only [simp_llvm_wrap]
-  simp_alive_meta
   simp_alive_ssa
   apply one_inst_concrete_stmt
 
 def one_inst_concrete_macro_proof :
     one_inst_concrete_macro ⊑ one_inst_concrete_macro := by
   unfold one_inst_concrete_macro
-  simp_alive_meta
   simp_alive_ssa
   apply one_inst_concrete_stmt
 
@@ -406,14 +390,12 @@ def two_inst_concrete_com_proof :
     two_inst_concrete_com w ⊑ two_inst_concrete_com w := by
   unfold two_inst_concrete_com
   simp only [simp_llvm_wrap]
-  simp_alive_meta
   simp_alive_ssa
   apply two_inst_concrete_stmt
 
 def two_inst_concrete_macro_proof :
     two_inst_concrete_macro ⊑ two_inst_concrete_macro := by
   unfold two_inst_concrete_macro
-  simp_alive_meta
   simp_alive_ssa
   apply two_inst_concrete_stmt
 
@@ -442,14 +424,12 @@ def three_inst_concrete_com_proof :
     three_inst_concrete_com ⊑ three_inst_concrete_com := by
   unfold three_inst_concrete_com
   simp only [simp_llvm_wrap]
-  simp_alive_meta
   simp_alive_ssa
   apply three_inst_concrete_stmt
 
 def three_inst_concrete_macro_proof :
     three_inst_concrete_macro ⊑ three_inst_concrete_macro := by
   unfold three_inst_concrete_macro
-  simp_alive_meta
   simp_alive_ssa
   apply three_inst_concrete_stmt
 
@@ -469,7 +449,6 @@ def two_ne_stmt (a b : LLVM.IntW w) :
 def two_ne_macro_proof (w : Nat) :
     two_ne_macro w ⊑ two_ne_macro w := by
   unfold two_ne_macro
-  simp_alive_meta
   simp_alive_ssa
   apply two_ne_stmt
 
@@ -499,6 +478,5 @@ def constant_stmt :
 def constant_macro_proof (w : Nat) :
     constant_macro w ⊑ constant_macro w := by
   unfold constant_macro
-  simp_alive_meta
   simp_alive_ssa
   apply constant_stmt


Reviews:
user: login: tobiasgrosser
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-18T14:13:51Z
user: login: tobiasgrosser
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-18T14:14:18Z
user: login: alexkeizer
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-18T14:27:58Z
user: login: tobiasgrosser
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-18T15:23:33Z
user: login: alexkeizer
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-26T11:18:07Z

Review comments:
diff_hunk: @@ -239,14 +239,15 @@ private def pretty_test_exact :=
   }]
 
 example : pretty_test = prettier_test_generic 32 := by
-  unfold pretty_test prettier_test_generic
-  simp_alive_meta
   rfl
 
 example : pretty_test_generic = prettier_test_generic    := rfl
 
 /-! ## antiquotations test -/
 
+/-
+TODO: antiquotations don't work with the new elaborator ;(
+
path: SSA/Projects/InstCombine/LLVM/PrettyEDSL.lean
user: login: tobiasgrosser
body: What precisely do we loose here? Can we still express bitwidth generic rewrites?
created_at: 2025-03-18T14:13:51Z
author_association: COLLABORATOR
diff_hunk: @@ -239,14 +239,15 @@ private def pretty_test_exact :=
   }]
 
 example : pretty_test = prettier_test_generic 32 := by
-  unfold pretty_test prettier_test_generic
-  simp_alive_meta
path: SSA/Projects/InstCombine/LLVM/PrettyEDSL.lean
user: login: tobiasgrosser
body: Very cool. I would like a demo, maybe in our 1:1 today?
created_at: 2025-03-18T14:14:17Z
author_association: COLLABORATOR
diff_hunk: @@ -239,14 +239,15 @@ private def pretty_test_exact :=
   }]
 
 example : pretty_test = prettier_test_generic 32 := by
-  unfold pretty_test prettier_test_generic
-  simp_alive_meta
   rfl
 
 example : pretty_test_generic = prettier_test_generic    := rfl
 
 /-! ## antiquotations test -/
 
+/-
+TODO: antiquotations don't work with the new elaborator ;(
+
path: SSA/Projects/InstCombine/LLVM/PrettyEDSL.lean
user: login: alexkeizer
body: > Can we still express bitwidth generic rewrites?

Yes, we have a deep embedding of meta-variables that allow us to express these generic rewrites.
This deep embedding works as intended in the new elaborator.
We lose the ability to piggy-back on Lean's syntax-antiquotations to construct "arbitrary" parts of the MLIR AST (in "s because in practice we could only use such anti-quotations in particular spots or risk horribly breaking the elaborator).

In practice, we only used this ability to insert arbitrary constant values, as in the example. We could still recover this ability as part of the new elaborator, but we'd have to put in the effort to enable our deep embedding to be used for constant values as well.

In general, it means that dialect implementations that want to depend on Lean variables for particular components of their programs will have to put in the effort to make such a deep embedding (as we did by having `MetaLLVM` and `LLVM` dialects). The benefit being that once the effort has been put in, there is a very clear distinction between what is robustly supported and what is not supported at all.
created_at: 2025-03-18T14:27:58Z
author_association: COLLABORATOR
diff_hunk: @@ -239,14 +239,15 @@ private def pretty_test_exact :=
   }]
 
 example : pretty_test = prettier_test_generic 32 := by
-  unfold pretty_test prettier_test_generic
-  simp_alive_meta
   rfl
 
 example : pretty_test_generic = prettier_test_generic    := rfl
 
 /-! ## antiquotations test -/
 
+/-
+TODO: antiquotations don't work with the new elaborator ;(
+
path: SSA/Projects/InstCombine/LLVM/PrettyEDSL.lean
user: login: tobiasgrosser
body: This sounds great to me.
created_at: 2025-03-18T15:23:33Z
author_association: COLLABORATOR
diff_hunk: @@ -239,14 +239,15 @@ private def pretty_test_exact :=
   }]
 
 example : pretty_test = prettier_test_generic 32 := by
-  unfold pretty_test prettier_test_generic
-  simp_alive_meta
   rfl
 
 example : pretty_test_generic = prettier_test_generic    := rfl
 
 /-! ## antiquotations test -/
 
+/-
+TODO: antiquotations don't work with the new elaborator ;(
path: SSA/Projects/InstCombine/LLVM/PrettyEDSL.lean
user: login: alexkeizer
body: ```suggestion
TODO: Constant value antiquotations don't work with the new elaborator.
            To properly support these test-cases, we should include constant values
            into the existing deeply-embedded metavariable instantiation
```
created_at: 2025-03-26T11:18:07Z
author_association: COLLABORATOR

PR comments:
user: login: github-actions[bot]
created_at: 2025-03-18T16:09:53Z
body: Alive Statistics: 90 / 93 (3 failed)
author_association: CONTRIBUTOR
user: login: github-actions[bot]
created_at: 2025-03-26T11:23:58Z
body: Alive Statistics: 89 / 93 (4 failed)
author_association: CONTRIBUTOR
user: login: github-actions[bot]
created_at: 2025-03-26T11:24:49Z
body: Alive Statistics: 89 / 93 (4 failed)
author_association: CONTRIBUTOR