|
View:
New views
12 Messages
—
Rating Filter:
Alert me
|
|
|
[scala-tools] Compiler booksCan anyone recommend a good book or online resource on compilers that
would help understand the Scala compiler? I want to create a plug-in that does vector math optimizations*, but I've never studied compilers and many things in the compiler seem really foreign to me. * I think scalar replacement is what it's called -- given the type Vector2(x: Float, y: Float) { /* operations */ }, I want to replace 1) local variables: val v = new Vector2(1f, 2f) --> val v$x = 1f; val v$y = 2f 2) any operations on Vectors with operations on Floats 3) any escaping vectors with new Vector2(v$x, v$y) -- most of the time they will not escape Or should I just wait until JIT can do this better? :) Erkki |
|
|
Re: [scala-tools] Compiler booksOn Sun, Jun 28, 2009 at 11:26 PM, Erkki Lindpere<erkki@...> wrote:
> * I think scalar replacement is what it's called -- given the type > Vector2(x: Float, y: Float) { /* operations */ }, I want to replace > 1) local variables: > val v = new Vector2(1f, 2f) --> val v$x = 1f; val v$y = 2f > 2) any operations on Vectors with operations on Floats > 3) any escaping vectors with new Vector2(v$x, v$y) -- most of the time they > will not escape > > Or should I just wait until JIT can do this better? :) Before trying to do this by hand, have you tried the -XX:+DoEscapeAnalysis option of recent Sun JVMs? Cheers, Miles -- Miles Sabin tel: +44 (0)7813 944 528 skype: milessabin http://www.chuusai.com/ http://twitter.com/milessabin |
|
|
Re: [scala-tools] Compiler booksYes. It helps (~15% overall improvement), but not as much as manual
scalar replacement in source code has helped. Erkki Miles Sabin wrote: > On Sun, Jun 28, 2009 at 11:26 PM, Erkki Lindpere<erkki@...> wrote: > >> * I think scalar replacement is what it's called -- given the type >> Vector2(x: Float, y: Float) { /* operations */ }, I want to replace >> 1) local variables: >> val v = new Vector2(1f, 2f) --> val v$x = 1f; val v$y = 2f >> 2) any operations on Vectors with operations on Floats >> 3) any escaping vectors with new Vector2(v$x, v$y) -- most of the time they >> will not escape >> >> Or should I just wait until JIT can do this better? :) >> > > Before trying to do this by hand, have you tried the > -XX:+DoEscapeAnalysis option of recent Sun JVMs? > > Cheers, > > > Miles > > |
|
|
Re: [scala-tools] Compiler booksAlso, I'm counting Vector2 allocations (in a companion object field) and
turning on -XX:+DoEscapeAnalysis does not remove a single allocation. The Vector2 class actually looks something like this: case class Vector2(x: Float, y: Float) { def +(a: Float) = Vector2(x + a, y + a) def -(a: Float) = Vector2(x - a, y - a) def *(a: Float) = Vector2(x * a, y * a) def /(a: Float) = Vector2(x / a, y / a) // rest of operations } I also tried making the class, it's methods and fields final but that didn't change anything either. Scala's -optimize and -Yinline also had no effect on performance (with relevant methods marked @inline final). Erkki Erkki Lindpere wrote: > Yes. It helps (~15% overall improvement), but not as much as manual > scalar replacement in source code has helped. > > Erkki > > Miles Sabin wrote: >> On Sun, Jun 28, 2009 at 11:26 PM, Erkki Lindpere<erkki@...> wrote: >> >>> * I think scalar replacement is what it's called -- given the type >>> Vector2(x: Float, y: Float) { /* operations */ }, I want to replace >>> 1) local variables: >>> val v = new Vector2(1f, 2f) --> val v$x = 1f; val v$y = 2f >>> 2) any operations on Vectors with operations on Floats >>> 3) any escaping vectors with new Vector2(v$x, v$y) -- most of the >>> time they >>> will not escape >>> >>> Or should I just wait until JIT can do this better? :) >>> >> >> Before trying to do this by hand, have you tried the >> -XX:+DoEscapeAnalysis option of recent Sun JVMs? >> >> Cheers, >> >> >> Miles >> >> > |
|
|
Re: [scala-tools] Compiler booksOn Sun, Jun 28, 2009 at 11:54 PM, Erkki Lindpere<erkki@...> wrote:
> Also, I'm counting Vector2 allocations (in a companion object field) and > turning on -XX:+DoEscapeAnalysis does not remove a single allocation. As in you have the ctor increment a companion object field? -XX:+DoEscapeAnalysis shouldn't have any effect on that count whether it manages to inline objects into the stack or not. Cheers, Miles -- Miles Sabin tel: +44 (0)7813 944 528 skype: milessabin http://www.chuusai.com/ http://twitter.com/milessabin |
|
|
Re: [scala-tools] Compiler booksOf course, I should have thought of this: the optimizer mustn't remove
side effects. Anyway, even with escape analysis turned on, I'm still getting bigger improvements from manually inlining (doing scalar replacement on) the vector operations. Miles Sabin wrote: > On Sun, Jun 28, 2009 at 11:54 PM, Erkki Lindpere<erkki@...> wrote: > >> Also, I'm counting Vector2 allocations (in a companion object field) and >> turning on -XX:+DoEscapeAnalysis does not remove a single allocation. >> > > As in you have the ctor increment a companion object field? > -XX:+DoEscapeAnalysis shouldn't have any effect on that count whether > it manages to inline objects into the stack or not. > > Cheers, > > > Miles > > |
|
|
Re: [scala-tools] Compiler booksI can give you some general compiler book suggestions. None of these will specifically help you with Scala, but they should give you a decent idea of what needs to happen in any good compiler:
Daniel On Sun, Jun 28, 2009 at 5:26 PM, Erkki Lindpere <erkki@...> wrote: Can anyone recommend a good book or online resource on compilers that would help understand the Scala compiler? |
|
|
Re: [scala-tools] Compiler booksOn 29/06/2009, at 8:26 AM, Erkki Lindpere wrote:
> Can anyone recommend a good book or online resource on compilers > that would help understand the Scala compiler? For a good introduction that is easy to read but also covers quite a bit of depth, I can recommend Modern Compiler Design by Grune et al. (http://www.cs.vu.nl/~dick/MCD.html ). cheers, Tony |
|
|
Re: [scala-tools] Compiler booksThanks for the suggestions! I'll order some of these books.
However, while they arrive, could someone give me a hint how to do scalar replacement in a transformer: // Given this method def compute(v1: Vector2) = { val v2 = v1 + 2f; v2 } // I want it to transform it to this def compute(v1: Vector2) = { val v2$x = v1.x + 2f val v2$y = v2.y + 2f Vector2(v2$x, v2$y) } // Nevermind that in this particular case there is no performance improvement In a transformer if I match on ValDef(...) I can do the replacement, but what should I return? I must return a single tree, but I have two trees that need to replace the first one. Maybe I should do this with a different method, not with the tree transformer class? Or perhaps I should create a new tree subtype that is eliminated in a next phase? Sorry if these are dumb questions :) Erkki Erkki Lindpere wrote: > Can anyone recommend a good book or online resource on compilers that > would help understand the Scala compiler? > > I want to create a plug-in that does vector math optimizations*, but > I've never studied compilers and many things in the compiler seem > really foreign to me. > > * I think scalar replacement is what it's called -- given the type > Vector2(x: Float, y: Float) { /* operations */ }, I want to replace > 1) local variables: > val v = new Vector2(1f, 2f) --> val v$x = 1f; val v$y = 2f > 2) any operations on Vectors with operations on Floats > 3) any escaping vectors with new Vector2(v$x, v$y) -- most of the time > they will not escape > > Or should I just wait until JIT can do this better? :) > > Erkki > |
|
|
Re: [scala-tools] Compiler booksSorry, nevermind, I got it working. Returned Sequence(List(xValDef,
yValDef)) and later "flattened" it in the block where it appeared as a statement. Erkki Lindpere wrote: > Thanks for the suggestions! I'll order some of these books. > > However, while they arrive, could someone give me a hint how to do > scalar replacement in a transformer: > > // Given this method > def compute(v1: Vector2) = { val v2 = v1 + 2f; v2 } > // I want it to transform it to this > def compute(v1: Vector2) = { > val v2$x = v1.x + 2f > val v2$y = v2.y + 2f > Vector2(v2$x, v2$y) > } > // Nevermind that in this particular case there is no performance > improvement > > In a transformer if I match on ValDef(...) I can do the replacement, > but what should I return? > I must return a single tree, but I have two trees that need to replace > the first one. > > Maybe I should do this with a different method, not with the tree > transformer class? > Or perhaps I should create a new tree subtype that is eliminated in a > next phase? > > Sorry if these are dumb questions :) > > Erkki > > Erkki Lindpere wrote: >> Can anyone recommend a good book or online resource on compilers that >> would help understand the Scala compiler? >> >> I want to create a plug-in that does vector math optimizations*, but >> I've never studied compilers and many things in the compiler seem >> really foreign to me. >> >> * I think scalar replacement is what it's called -- given the type >> Vector2(x: Float, y: Float) { /* operations */ }, I want to replace >> 1) local variables: >> val v = new Vector2(1f, 2f) --> val v$x = 1f; val v$y = 2f >> 2) any operations on Vectors with operations on Floats >> 3) any escaping vectors with new Vector2(v$x, v$y) -- most of the >> time they will not escape >> >> Or should I just wait until JIT can do this better? :) >> >> Erkki >> > |
|
|
Re: [scala-tools] Compiler booksMy optimizer is progressing nicely. But I'm stuck with something:
occasionally I cache some expressions in synthetic method-local variables, and I want to replace other occurences of these expressions/trees with Ident(varName) of the variable where the tree is already cached. I'm trying to use for this purpose an extended HashMap: val cache = new collection.mutable.HashMap[Tree, Name] { override def elemEquals(key1: Tree, key2: Tree) = key1 equalsStructure key2 override def elemHashCode(key: Tree) = key.hashCodeStructure } However, it seems that even if the trees I want to replace produce equal hashCodes with the one cached (using hashCodeStructure), they do not equal (using equalsStructure). Is this correct? And if so, is there something else I can use or must I implement my own tree comparison? Erkki L Erkki Lindpere wrote: > I want to create a plug-in that does vector math optimizations*, but > I've never studied compilers and many things in the compiler seem > really foreign to me. > > * I think scalar replacement is what it's called -- given the type > Vector2(x: Float, y: Float) { /* operations */ }, I want to replace > 1) local variables: > val v = new Vector2(1f, 2f) --> val v$x = 1f; val v$y = 2f > 2) any operations on Vectors with operations on Floats > 3) any escaping vectors with new Vector2(v$x, v$y) -- most of the time > they will not escape > > Or should I just wait until JIT can do this better? :) > > Erkki > |
|
|
Re: [scala-tools] Compiler booksSorry, user error :) It works just fine!
Erkki Erkki Lindpere wrote: > My optimizer is progressing nicely. But I'm stuck with something: > occasionally I cache some expressions in synthetic method-local > variables, and I want to replace other occurences of these > expressions/trees with Ident(varName) of the variable where the tree > is already cached. > > I'm trying to use for this purpose an extended HashMap: > val cache = new collection.mutable.HashMap[Tree, Name] { > override def elemEquals(key1: Tree, key2: Tree) = key1 > equalsStructure key2 > override def elemHashCode(key: Tree) = key.hashCodeStructure > } > > However, it seems that even if the trees I want to replace produce > equal hashCodes with the one cached (using hashCodeStructure), they do > not equal (using equalsStructure). Is this correct? And if so, is > there something else I can use or must I implement my own tree > comparison? > > Erkki L > > Erkki Lindpere wrote: >> I want to create a plug-in that does vector math optimizations*, but >> I've never studied compilers and many things in the compiler seem >> really foreign to me. >> >> * I think scalar replacement is what it's called -- given the type >> Vector2(x: Float, y: Float) { /* operations */ }, I want to replace >> 1) local variables: >> val v = new Vector2(1f, 2f) --> val v$x = 1f; val v$y = 2f >> 2) any operations on Vectors with operations on Floats >> 3) any escaping vectors with new Vector2(v$x, v$y) -- most of the >> time they will not escape >> >> Or should I just wait until JIT can do this better? :) >> >> Erkki >> > |
| Free embeddable forum powered by Nabble | Forum Help |