Documentation

Lean.Compiler.IR.Basic

Implements (extended) λPure and λRc proposed in the article "Counting Immutable Beans", Sebastian Ullrich and Leonardo de Moura.

The Lean to IR transformation produces λPure code, and this part is implemented in C++. The procedures described in the paper above are implemented in Lean.

@[reducible, inline]

Function identifier

Equations
Instances For
    @[reducible, inline]
    Equations
    Instances For
      structure Lean.IR.VarId :

      Variable identifier

      Instances For
        Equations
        Instances For

          Join point identifier

          Instances For
            @[reducible, inline]
            abbrev Lean.IR.Index.lt (a b : Index) :
            Equations
            Instances For
              inductive Lean.IR.IRType :

              Low Level IR types. Most are self explanatory.

              • usize represents the C++ size_t Type. We have it here because it is 32-bit in 32-bit machines, and 64-bit in 64-bit machines, and we want the C++ backend for our Compiler to generate platform independent code.

              • erased for Lean types, propositions and proofs.

              • object a pointer to a value in the heap.

              • tagged a tagged pointer (i.e., the least significant bit is 1) storing a scalar value.

              • tobject an object or a tagged pointer

              • struct and union are used to return small values (e.g., Option, Prod, Except) on the stack.

              Remark: the RC operations for tobject are slightly more expensive because we first need to test whether the tobject is really a pointer or not.

              Remark: the Lean runtime assumes that sizeof(void*) == sizeof(sizeT). Lean cannot be compiled on old platforms where this is not True.

              Since values of type struct and union are only used to return values, We assume they must be used/consumed "linearly". We use the term "linear" here to mean "exactly once" in each execution. That is, given x : S, where S is a struct, then one of the following must hold in each (execution) branch. 1- x occurs only at a single ret x instruction. That is, it is being consumed by being returned. 2- x occurs only at a single ctor. That is, it is being "consumed" by being stored into another struct/union. 3- We extract (aka project) every single field of x exactly once. That is, we are consuming x by consuming each of one of its components. Minor refinement: we don't need to consume scalar fields or struct/union fields that do not contain object fields.

              Instances For
                inductive Lean.IR.Arg :

                Arguments to applications, constructors, etc. We use erased for Lean types, propositions and proofs that have been erased. Recall that for a Function f, we also generate f._rarg which does not take erased arguments. However, f._rarg is only safe to be used in full applications.

                Instances For
                  Equations
                  Instances For
                    inductive Lean.IR.LitVal :
                    Instances For

                      Constructor information.

                      • name is the Name of the Constructor in Lean.
                      • cidx is the Constructor index (aka tag).
                      • size is the number of arguments of type object/tobject.
                      • usize is the number of arguments of type usize.
                      • ssize is the number of bytes used to store scalar values.

                      Recall that a Constructor object contains a header, then a sequence of pointers to other Lean objects, a sequence of USize (i.e., size_t) scalar values, and a sequence of other scalar values.

                      Instances For
                        Equations
                        Instances For
                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            Equations
                            Instances For
                              Equations
                              Instances For
                                inductive Lean.IR.Expr :
                                • ctor (i : CtorInfo) (ys : Array Arg) : Expr

                                  We use ctor mainly for constructing Lean object/tobject values lean_ctor_object in the runtime. This instruction is also used to creat struct and union return values. For union, only i.cidx is relevant. For struct, i is irrelevant.

                                • reset (n : Nat) (x : VarId) : Expr
                                • reuse (x : VarId) (i : CtorInfo) (updtHeader : Bool) (ys : Array Arg) : Expr

                                  reuse x in ctor_i ys instruction in the paper.

                                • proj (i : Nat) (x : VarId) : Expr

                                  Extract the tobject value at Position sizeof(void*)*i from x. We also use proj for extracting fields from struct return values, and casting union return values.

                                • uproj (i : Nat) (x : VarId) : Expr

                                  Extract the Usize value at Position sizeof(void*)*i from x.

                                • sproj (n offset : Nat) (x : VarId) : Expr

                                  Extract the scalar value at Position sizeof(void*)*n + offset from x.

                                • fap (c : FunId) (ys : Array Arg) : Expr

                                  Full application.

                                • pap (c : FunId) (ys : Array Arg) : Expr

                                  Partial application that creates a pap value (aka closure in our nonstandard terminology).

                                • ap (x : VarId) (ys : Array Arg) : Expr

                                  Application. x must be a pap value.

                                • box (ty : IRType) (x : VarId) : Expr

                                  Given x : ty where ty is a scalar type, this operation returns a value of Type tobject. For small scalar values, the Result is a tagged pointer, and no memory allocation is performed.

                                • unbox (x : VarId) : Expr

                                  Given x : [t]object, obtain the scalar value.

                                • lit (v : LitVal) : Expr
                                • isShared (x : VarId) : Expr

                                  Return 1 : uint8 Iff RC(x) > 1

                                Instances For
                                  structure Lean.IR.Param :
                                  Instances For
                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      inductive Lean.IR.Alt :
                                      Instances For
                                        inductive Lean.IR.FnBody :
                                        Instances For
                                          @[reducible, inline]
                                          Equations
                                          Instances For
                                            Equations
                                            Instances For
                                              Equations
                                              Instances For
                                                @[inline]
                                                Equations
                                                Instances For
                                                  @[inline]

                                                  If b is a non terminal, then return a pair (c, b') s.t. b == c <;> b', and c.body == FnBody.nil

                                                  Equations
                                                  Instances For
                                                    Equations
                                                    Instances For
                                                      Equations
                                                      Instances For
                                                        partial def Lean.IR.reshapeAux (a : Array FnBody) (i : Nat) (b : FnBody) :
                                                        Equations
                                                        Instances For
                                                          @[inline]
                                                          Equations
                                                          Instances For
                                                            @[inline]
                                                            def Lean.IR.modifyJPsM {m : TypeType} [Monad m] (bs : Array FnBody) (f : FnBodym FnBody) :
                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            Instances For

                                                              Extra information associated with a declaration.

                                                              • sorryDep? : Option Name

                                                                If some <blame>, then declaration depends on <blame> which uses a sorry axiom.

                                                              Instances For
                                                                inductive Lean.IR.Decl :
                                                                Instances For
                                                                  Equations
                                                                  Instances For
                                                                    Equations
                                                                    Instances For
                                                                      Equations
                                                                      Instances For
                                                                        Equations
                                                                        Instances For
                                                                          Equations
                                                                          Instances For
                                                                            Equations
                                                                            Instances For
                                                                              @[reducible, inline]

                                                                              Set of variable and join point names

                                                                              Equations
                                                                              Instances For
                                                                                Instances For
                                                                                  Equations
                                                                                  Instances For
                                                                                    Equations
                                                                                    Instances For
                                                                                      Equations
                                                                                      Instances For
                                                                                        Equations
                                                                                        Instances For
                                                                                          Equations
                                                                                          Instances For
                                                                                            class Lean.IR.AlphaEqv (α : Type) :
                                                                                            Instances
                                                                                              Equations
                                                                                              Instances For
                                                                                                def Lean.IR.args.alphaEqv (ρ : IndexRenaming) (args₁ args₂ : Array Arg) :
                                                                                                Equations
                                                                                                Instances For
                                                                                                  Equations
                                                                                                  Instances For
                                                                                                    Equations
                                                                                                    Instances For
                                                                                                      Equations
                                                                                                      Instances For
                                                                                                        Equations
                                                                                                        • One or more equations did not get rendered due to their size.
                                                                                                        Instances For
                                                                                                          def Lean.IR.FnBody.beq (b₁ b₂ : FnBody) :
                                                                                                          Equations
                                                                                                          Instances For
                                                                                                            @[reducible, inline]
                                                                                                            Equations
                                                                                                            Instances For
                                                                                                              def Lean.IR.mkIf (x : VarId) (t e : FnBody) :
                                                                                                              Equations
                                                                                                              • One or more equations did not get rendered due to their size.
                                                                                                              Instances For