The constantfold Reference Manual

Next: , Previous: , Up: (dir)   [Contents][Index]

The constantfold Reference Manual

This is the constantfold Reference Manual, version 0.1, generated automatically by Declt version 4.0 beta 2 "William Riker" on Mon Aug 15 04:20:09 2022 GMT+0.

Table of Contents


1 Introduction


* Constantfold - User-defined constant folding facility 

This is a supporting library for NUMCL.
It provides a single macro, =(constantfold name &key commutative associative copier)=.

=constantfold= registers the function =NAME= as an additional form that is
considered as a candidate for a constant.  For example,

#+begin_src lisp
(defstruct point2d
  (x 0 :type 'real)
  (y 0 :type 'real))

(defun point2d-add/2 (p1 p2)
  (ematch* (p1 p2)
    (((point2d :x x1 :y y1)
      (point2d :x x2 :y y2))
     (make-point2d :x (+ x1 x2) :y (+ y1 y2)))))

(defun point2d-add (&rest args)
  (reduce #'point2d-add/2 args))

(constantfold make-point2d :copier copy-point2d)
(constantfold point2d-add  :copier copy-point2d :commutative t :associative t)
#+end_src

This allows the compiler to constant fold

#+begin_src lisp
(defun fn1 () (point2d-add (make-point2d :x 1 :y 2) (make-point2d :x 3 :y 4)))
#+end_src

and

#+begin_src lisp
(defun fn1 () (point2d-add #S(point2d :x 1 :y 2) #S(point2d :x 3 :y 4)))
#+end_src

into

#+begin_src lisp
(defun fn1 () (copy-point2d #S(point2d :x 4 :y 6))).
#+end_src

When =:associative= and/or =:commutative= is true, it performs the corresponding
code morphing and tries to fold constants as much as possible.  For example,

#+begin_src lisp
(defun fn2 (p1 p2 x y)
  (point2d-add (make-point2d :x 1 :y 2)
               p1
               (make-point2d :x 3 :y 4)
               p2
               (make-point2d :x x :y y)))
#+end_src

gets compiled into

#+begin_src lisp
(defun fn2 (p1 p2 x y)
  (point2d-add (copy-point2d #S(point2d :x 4 :y 6))
               p1
               p2
               (make-point2d :x x :y y)))
#+end_src

due to commutativity.

This library preserves the original semantics of the code. For example,

#+begin_src lisp
(defun fn3 ()
  (make-point2d :x 1 :y 2))
#+end_src

originally always instantiates a new object. This is compiled into

#+begin_src lisp
(defun fn3 ()
  (copy-point2d #S(point2d :x 1 :y 2)))
#+end_src

which also instantiates a new object.
The reason why =fn1/fn2= are allowed to remove some instance creations while
=fn3= does not perform the pruning is that the result of =make-point2d= in =fn1/fn2=
are so-called *rvalues* which are not visible to the user
while the result of =(make-point2d :x 1 :y 2)= is going to be returned to the user
and is thus an *lvalue*.

It might then be confusing why =fn2= does not completely remove the instantiation.
This is because =point2d-add= may perform a destructive operation on the argument,
and thus the value (e.g. slots) of the constant could be modified.
=copy-point2d= is called in order to avoid the effect of this destructive operation.

COPIER could be ommited when the object is known to be immutable
and there is no need for copying the object.
In this case the compilation result always returns the same object under =EQ=.

Note that if the result of the constant folding is an object of class
=CL:STANDARD-OBJECT= / =CL:STRUCTURE-OBJECT= / =CL:CONDITION= / =CL:CLASS=, the
corresponding =CL:MAKE-LOAD-FORM= method should be defined.

** Dependencies
This library is at least tested on implementation listed below:

+ SBCL 1.4.12 on X86-64 Linux 4.4.0-141-generic (author's environment)
+ SBCL 1.5.1  on X86-64 Linux 4.4.0-141-generic (author's environment)

Also, it depends on the following libraries:

+ trivia by *Masataro Asai* :
    NON-optimized pattern matcher compatible with OPTIMA, with extensible optimizer interface and clean codebase
+ alexandria by *Nikodemus Siivola , and others.* :
    Alexandria is a collection of portable public domain utilities.
+ iterate by ** :
    Jonathan Amsterdam's iterator/gatherer/accumulator facility
+ lisp-namespace by *Masataro Asai* :
    Provides LISP-N --- extensible namespaces in Common Lisp.

[[./constantfold.png]]

** Author, License, Copyright

Masataro Asai (guicho2.71828@gmail.com)

Licensed under LGPL v3.

Copyright (c) 2019 IBM Corporation


2 Systems

The main system appears first, followed by any subsystem dependency.


Previous: , Up: Systems   [Contents][Index]

2.1 constantfold

User-defined constant folding facility

Author

Masataro Asai

Contact

guicho2.71828@gmail.com

License

LGPL

Version

0.1

Dependencies
  • trivia (system).
  • alexandria (system).
  • iterate (system).
  • lisp-namespace (system).
Source

constantfold.asd.

Child Component

package.lisp (file).


3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


Previous: , Up: Files   [Contents][Index]

3.1 Lisp


Next: , Previous: , Up: Lisp   [Contents][Index]

3.1.1 constantfold/constantfold.asd

Source

constantfold.asd.

Parent Component

constantfold (system).

ASDF Systems

constantfold.


3.1.2 constantfold/package.lisp

Source

constantfold.asd.

Parent Component

constantfold (system).

Packages

constantfold.

Public Interface
Internals

4 Packages

Packages are listed by definition order.


Previous: , Up: Packages   [Contents][Index]

4.1 constantfold

Source

package.lisp.

Use List
  • alexandria.
  • common-lisp.
  • iterate.
  • lisp-namespace.
  • trivia.level2.
Public Interface
Internals

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


Next: , Previous: , Up: Definitions   [Contents][Index]

5.1 Public Interface


5.1.1 Macros

Macro: constantfold (name &key commutative associative copier)

Registers a constant folding compiler macros to the function NAME.

Package

constantfold.

Source

package.lisp.


Previous: , Up: Public Interface   [Contents][Index]

5.1.2 Ordinary functions

Function: unfold (name)

Unregister the compiler macro

Package

constantfold.

Source

package.lisp.


5.2 Internals


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.1 Special variables

Special Variable: *constant-form-doc-table*
Package

constantfold.

Source

package.lisp.

Special Variable: *constant-form-table*
Package

constantfold.

Source

package.lisp.


5.2.2 Compiler macros

Compiler Macro: foldable-associative-fn (&rest args)

Performs constant-folding for associative functions. Following optimization is performed:

(+ a 2 3 b) –> (+ a (+ 2 3) b) –> (+ a 5 b)

Package

constantfold.

Source

package.lisp.

Compiler Macro: foldable-commutative-associative-fn (&rest args)

Performs constant-folding for commutative and associative functions. Following optimization is performed:

(+ a 2 3 b 1) –> (+ (+ 2 3 1) a b) –> (+ 6 a b)

Package

constantfold.

Source

package.lisp.

Compiler Macro: foldable-fn (&rest args)

Performs a simple constant-folding. Constantfold when all arguments are constants.

Package

constantfold.

Source

package.lisp.


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.3 Ordinary functions

Function: %foldable-associative-fn (whole env)
Package

constantfold.

Source

package.lisp.

Function: %foldable-commutative-associative-fn (whole env)
Package

constantfold.

Source

package.lisp.

Function: %foldable-fn (whole args env)
Package

constantfold.

Source

package.lisp.

Function: compiler-macro-form-name (whole)

Extracts the name of the function from the &whole argument of a compiler macro.

Package

constantfold.

Source

package.lisp.

Function: constant-form-boundp (symbol)

Automatically defined boolean function.

Package

constantfold.

Source

package.lisp.

Function: constant-form-copier (name)
Package

constantfold.

Source

package.lisp.

Function: constant-form-p (form &optional env)

Recursively checks if the form is a constant form.
Returns a boolean. When FORM is a standard constant form reconized by constantp, it also returns T as the secondary value.

Package

constantfold.

Source

package.lisp.

Function: evaluate-constants (name args)
Package

constantfold.

Source

package.lisp.

Function: flatten-associative-nested-fn (form)

Flattens a tree of forms of the same functions. For example, (+ (+ 2 3) (+ 5 6)) into (+ 2 3 5 6).

Package

constantfold.

Source

package.lisp.

Function: register-constant-form (name &optional copier)
Package

constantfold.

Source

package.lisp.

Function: symbol-constant-form (symbol &optional default)

Automatically defined getter function. When DEFAULT is supplied, the value is set automatically.

Package

constantfold.

Source

package.lisp.

Function: (setf symbol-constant-form) (symbol)

Automatically defined setter function.

Package

constantfold.

Source

package.lisp.

Function: wrap-inline (name form)

Allows further calls to the compiler macro.

Package

constantfold.

Source

package.lisp.

Function: wrap-notinline (name form)

Suppresses further calls to the compiler macro which otherwise causes an infinite loop.

Package

constantfold.

Source

package.lisp.


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.4 Conditions

Condition: unbound-constant-form
Package

constantfold.

Source

package.lisp.

Direct superclasses

unbound-variable.


Previous: , Up: Internals   [Contents][Index]

5.2.5 Types

Type: constant-form-type ()
Package

constantfold.

Source

package.lisp.


Appendix A Indexes


Next: , Previous: , Up: Indexes   [Contents][Index]

A.1 Concepts


Next: , Previous: , Up: Indexes   [Contents][Index]

A.2 Functions

Jump to:   %   (  
C   E   F   M   R   S   U   W  
Index Entry  Section

%
%foldable-associative-fn: Private ordinary functions
%foldable-commutative-associative-fn: Private ordinary functions
%foldable-fn: Private ordinary functions

(
(setf symbol-constant-form): Private ordinary functions

C
Compiler Macro, foldable-associative-fn: Private compiler macros
Compiler Macro, foldable-commutative-associative-fn: Private compiler macros
Compiler Macro, foldable-fn: Private compiler macros
compiler-macro-form-name: Private ordinary functions
constant-form-boundp: Private ordinary functions
constant-form-copier: Private ordinary functions
constant-form-p: Private ordinary functions
constantfold: Public macros

E
evaluate-constants: Private ordinary functions

F
flatten-associative-nested-fn: Private ordinary functions
foldable-associative-fn: Private compiler macros
foldable-commutative-associative-fn: Private compiler macros
foldable-fn: Private compiler macros
Function, %foldable-associative-fn: Private ordinary functions
Function, %foldable-commutative-associative-fn: Private ordinary functions
Function, %foldable-fn: Private ordinary functions
Function, (setf symbol-constant-form): Private ordinary functions
Function, compiler-macro-form-name: Private ordinary functions
Function, constant-form-boundp: Private ordinary functions
Function, constant-form-copier: Private ordinary functions
Function, constant-form-p: Private ordinary functions
Function, evaluate-constants: Private ordinary functions
Function, flatten-associative-nested-fn: Private ordinary functions
Function, register-constant-form: Private ordinary functions
Function, symbol-constant-form: Private ordinary functions
Function, unfold: Public ordinary functions
Function, wrap-inline: Private ordinary functions
Function, wrap-notinline: Private ordinary functions

M
Macro, constantfold: Public macros

R
register-constant-form: Private ordinary functions

S
symbol-constant-form: Private ordinary functions

U
unfold: Public ordinary functions

W
wrap-inline: Private ordinary functions
wrap-notinline: Private ordinary functions

Jump to:   %   (  
C   E   F   M   R   S   U   W