On Thursday, 18 August 2016 at 01:18:03 UTC, Yuxuan Shui wrote:
On Thursday, 18 August 2016 at 00:20:32 UTC, Chris Wright wrote:

The language can analyze all code that affects a local variable in many cases. You don't always need the language to guarantee it's impossible if the compiler can see that the user isn't doing anything funky.

That's right. But for Ali's code, the compiler is clearly not smart enough.

Perhaps not smart enough, but it is very close to being smart enough. Perhaps we, the LDC devs, weren't smart enough in using the LLVM backend. ;-)

For Ali's code, `arr` is a global symbol that can be modified by code not seen by the compiler. So no assumptions can be made about the contents of arr.ptr and arr.length, and thus `arr[i]` could index into `arr` itself.

Now, if we tell [*] the compiler that `arr` is not touched by code outside the module... the resulting machine code is identical with and without POINTER!

It's an interesting case to look further into. For example with Link-Time Optimization, delaying codegen until linktime, we can internalize many symbols (i.e. telling the compiler no other module is going to do anything with it) allowing the compiler to reason better about the code and generate faster executables. Ideally, without much user effort. It's something I am already looking into.

I don't know if we can mark `private` global variables as internal. If so, wow :-)

-Johan


[*] Unfortunately `private arr` does not have the desired effect (it does not make it an `internal` LLVM variable), so you have to hack into the LLVM IR file to make it happen.

Reply via email to