<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>On Apr 19, 2008, at 22:45, Talin wrote:</div><div><br></div><div><blockquote type="cite">Suppose I have a global variable which points to a constant, variable length array.</blockquote><div><br></div><div>This is somewhat contradictory. Do you want an 'vla *g;' in C? If so, your global will be of type %vla** (you'll load a pointer to the array from the global). Its initializer would be a bitcast of a global which has some type similar to %vla. If not, and you indeed want a global of type %vla*, read on…</div><br><blockquote type="cite">The question is, how can I assign an array of type "{ i32, [5 x float]}" to a global of type "{ i32, [0 x float]}"?</blockquote><div><br></div><div>You can't, but declarations and definitions can have differing types. For instance:</div><div><br></div></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><span class="Apple-style-span" style="font-family: 'Courier New'; font-weight: bold; ">$ cat a.ll</span><br><span class="Apple-style-span" style="font-family: 'Courier New'; ">@g = external global {i32, [0 x float]}</span><br><br><span class="Apple-style-span" style="font-family: 'Courier New'; ">define {i32, [0 x float]}* @f() {</span><br><span class="Apple-style-span" style="font-family: 'Courier New'; ">entry:</span><br><span class="Apple-tab-span" style="white-space: pre; "><font class="Apple-style-span" face="'Courier New'"> </font></span><font class="Apple-style-span" face="'Courier New'">ret {i32, [0 x float]}* @g</font><br><span class="Apple-style-span" style="font-family: 'Courier New'; ">}</span><br><span class="Apple-style-span" style="font-family: 'Courier New'; font-weight: bold; ">$ cat b.ll</span><br><span class="Apple-style-span" style="font-family: 'Courier New'; ">@g = constant {i32, [5 x float]} zeroinitializer</span></blockquote><div><div><br></div>If this is for separate compilation, the linker will automatically resolve the type conflict. It does so by replacing all uses of the declaration with bitcasts of the definition. Using the same example:<div><br></div><blockquote class="webkit-indent-blockquote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 40px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span class="Apple-style-span" style="font-weight: bold; "><font class="Apple-style-span" face="'Courier New'">$ llvm-as a.ll<span class="Apple-style-span" style="font-weight: normal;">;</span></font></span><span class="Apple-style-span" style="font-weight: bold; "><font class="Apple-style-span" face="'Courier New'"> llvm-as b.ll<span class="Apple-style-span" style="font-weight: normal;">;</span></font></span><span class="Apple-style-span" style="font-weight: bold; "><font class="Apple-style-span" face="'Courier New'"> llvm-link a.bc b.bc | llvm-dis</font></span><font class="Apple-style-span" face="'Courier New'"><br>; ModuleID = '<stdin>'<br>@g = constant { i32, [5 x float] } zeroinitializer</font><span class="Apple-tab-span" style="white-space: pre; "><font class="Apple-style-span" face="'Courier New'"> </font></span><font class="Apple-style-span" face="'Courier New'">; <{ i32, [5 x float] }*> [#uses=1]<br><br>define { i32, [0 x float] }* @f() {<br>entry:<br></font><span class="Apple-tab-span" style="white-space: pre; "><font class="Apple-style-span" face="'Courier New'"> </font></span><font class="Apple-style-span" face="'Courier New'">ret { i32, [0 x float] }* bitcast ({ i32, [5 x float] }* @g to { i32, [0 x float] }*)<br>}</font></blockquote><div><div><div><br></div><div>Internally to your front end, you could do the same thing: Declare the global early with its abstract type, and later when the concrete storage type is known, use ConstantExpr::getBitCast, replaceAllUsesWith, and takeName to do the same thing the linker did above.</div></div><div><br></div><div>— Gordon</div></div></div><div><div><div apple-content-edited="true"> </div><br></div></div></body></html>