<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 24, 2016, at 4:27 AM, Johan Wehrli via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi,<div class=""><br class=""></div><div class="">I am trying to update the initializer of a global value and I have encounter two issues:</div><div class=""><br class=""></div><div class="">The first one is that I can not change the type of the global value. Let say that I have the following variable: <i class="">@.str = private unnamed_addr constant [6 x i8] c”Test0A\00", align 1</i></div><div class=""><i class=""><br class=""></i></div><div class="">How can I change the <i class="">”Test0A\00” </i>to<i class=""> </i><i class="">”OtherTest0A\00”. </i>Is this possible? I know that you can change the <i class="">initializer</i> with <i class="">setInitializer</i> but the global value’s type will not be the same.</div></div></div></blockquote><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">For now, the only way I found was to create a new global value and to change all theses uses.</div></div></div></blockquote><div><br class=""></div><div><div>That’s the right thing to do: you need to create a new GV and replace the old one with the new one.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"></div></blockquote></div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">The second problem that I have is with the function <i class="">Verifier::visitGlobalVariable</i>. Sometimes, the verifier tells me that the initializer type does not match the global variable type even when this is the case.</div><div class=""><br class=""></div><div class=""> This function will do the following check: </div><div class=""><br class=""></div><div class=""><div class=""><i class="">if (GV.hasInitializer()) {</i></div><div class=""><i class="">    Assert(GV.getInitializer()->getType() == GV.getType()->getElementType(),</i></div><div class=""><i class="">           "Global variable initializer type does not match global "</i></div><div class=""><i class="">           "variable type!",</i></div><div class=""><i class="">           &GV);</i></div></div><div class=""><br class=""></div><div class="">But I did not find any overload for the comparator operator (in the type class). So this will only check if the addresses of the type are the same and not if this is the same type.</div><div class=""><br class=""></div><div class="">To have the same type, I need to create the initializer with the same context as the global variable:</div><div class=""><br class=""></div><div class=""><div class=""><i class="">LLVMContext &C = gv->getContext();</i></div><div class=""><i class="">ConstantDataArray *data = cast<ConstantDataArray>(ConstantDataArray::getString(C, ref, false));       </i></div><div class=""><i class="">gv->setInitializer(data);</i></div></div><div class=""><br class=""></div><div class="">Is this normal? I am missing something?</div></div></div></blockquote></div><br class=""><div class="">Yes it is normal you *have* to use the same context to manipulate IR inside a Module. The context owns the module and destroying the context destroys everything that is created in the context. It is not clear to me what other context you could even manage to use here conceptually.</div><div class="">Anyway it is rooted quite deeply in LLVM that type can be compared by pointer thanks to this context uniquing.</div><div class=""><br class=""></div><div class="">You can have different LLVMContext when you manipulate multiple Modules (Context are here to enable multi-threading environment), but you won’t be able to compare type from one module to the other for example.</div><div class=""><br class=""></div><div class="">— </div><div class="">Mehdi</div><div class=""><br class=""></div></body></html>