On Fri, Aug 01, 2014 at 03:34:00PM -0700, Walter Bright via Digitalmars-d wrote:
> On 8/1/2014 2:50 PM, Jonathan M Davis wrote:
> >Thinking about it, I'm actually wondering if I should use assertions
> >_more_ so that the compiler might be able to do better optimizations
> >in -release.
> 
> I think this will be a productive strategy once this gets implemented
> in optimizers.
> 
> Of course, using them effectively will not be easy unless one is
> willing to understand how data flow analysis works and is willing to
> examine the generated code.

Actually, I'm thinking of ways of extending this even further, such that
asserts can become the vehicle for user-defined types to declare
high-level optimizations.

For example, a matrix type could declare that certain matrix expressions
are equivalent, thereby allowing the optimizer to substitute one for the
other to simplify matrix expressions where it couldn't before, because
it doesn't know how to infer such equivalences when overloaded operators
may have arbitrary code in their implementation.

I haven't thought about the syntax for this yet, but the idea is
something like this:

        struct Matrix {
                // Implement overloaded operators here
                Matrix opBinary(string op)(Matrix m) { ... }

                Matrix transpose() { ... }

                // (THIS IS TENTATIVE, HYPOTHETICAL SYNTAX) Declare
                // identities the optimizer might use for Matrix
                // expressions
                invariant(Matrix a, Matrix b, Matrix c) {
                        // Tell optimizer that Matrix obeys
                        // distributivity law
                        assert(a*b + a*c == a*(b+c));

                        // Tell optimizer about multiplicative identity
                        assert(!a.isIdentity || a*b == b);

                        // Tell optimizer about idempotence of
                        // .transpose
                        assert(a.transpose().transpose() == a);
                }
        }

        void main() {
                Matrix x, y, z;
                auto r1 = x*y + x*z; // gets simplified into w = x*(y+z)

                auto I = Matrix.identity();
                assert(I.isIdentity);
                auto r2 = I*x; // gets simplified to r2 = x

                auto r3 = x.transpose;
                auto r4 = r3.transpose; // gets simplified to r4 = x
        }

I think this will be a very powerful feature to further narrow the gap
between built-in types vs. library-defined types.


T

-- 
Never trust an operating system you don't have source for! -- Martin Schulze

Reply via email to