[LLVMdev] GHC, aliases, and LLVM HEAD

Ben Gamari bgamari.foss at gmail.com
Mon May 26 17:05:28 PDT 2014


Rafael EspĂ­ndola <rafael.espindola at gmail.com> writes:

> On 25 May 2014 21:29, Ben Gamari <bgamari.foss at gmail.com> wrote:
>> Sure. I think the only reason our use of aliases worked previously was
>> that the optimizer elided them long before they could make it into an
>> object file.
>
> If that is the case, you should be able to just directly replace alias
> with aliasee, no? In general you should not depend on an optimization
> being run to produce correct code.
>
I absolutely agree. As far as I understand we ended up with the current
situation as no one could figure out a better way to resolve the typing
issue I clarify below. I'm trying to find a better solution.

>>> That should also work in llvm IR. You can create a function without a
>>> body or a GlobalVariable without an initializer and add it afterwards.
>>>
>> I'm not sure I follow. If I attempt to compile,
>>
>>     declare i32 @main()
>>     define i32 @main() {
>>         ret i32 0
>>     }
>>
>> It fails with,
>>
>>     llc: test.ll:3:12: error: invalid redefinition of function 'main'
>>     define i32 @main() {
>
> There are no redeclarations in LLVM IR. You can just put  top level
> entities in any order:
>
Alright, I misunderstood your point in that case. Thanks for the
clarification!

>> The problem here is that we don't know the type of the symbol at the
>> point of use so I need to assume it is something (e.g. i8*). Take for
>> instance the following example,
>>
>>
>>     define i32 @main() {
>>         // We don't know the type of f a priori, thus we assume
>>         // it is i8*
>>         %f = bitcast i8* f$alias to i32 ()*
>>         call i32 %f()
>>         ret i32 0
>>     }
>
> Instead of having an f$alias, you could just have produced a
>
> declare void f()
>
> since you know the type it is being called with.
>
Bah, that was a poor choice of example on my part. A better one might be
the following:

Our C-- representation might refer to `f` without calling it (e.g. when
building a thunk). In this case we don't have access to the function's
signature, which we can only infer from a `call` node. For this reason,
we currently demote all pointers to some common type (i8* currently) so
we don't run into LLVM's type system in cases where we can't infer a
value's type.

> No, the idea is to not have f$alias at all. Once you find that f has
> to be defined, you just set its body (which turns it into a
> definition).
>
See above for why I believe we need the alias.

> I guess a better example might have been clang compiling:
>
> void f(void);
> void g(void) {
>   f();
> }
> void f(void) {
> }
>
> In here f will be converted from a declaration to a definition.
>
That is to say that nothing is emitted until the entire compilation unit
is parsed (so we know which items are definitions and which are declarations)?

Cheers,

- Ben

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 472 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140526/71fe467f/attachment.sig>


More information about the llvm-dev mailing list