<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Thank you very much. The code to initialize h1 to non-zero values was what I was looking for.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
It's almost working except for a type mismatch wrt dlist* llist field of h1<span style="color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt;">.</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt;"><span style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important">dlist
 static_lst[10] = { {1, 5, NULL}, ... };</span><br>
</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt;"><span style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important"><span style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; background-color: rgb(255, 255, 255); display: inline !important">dhash
 h1[10] = {{"myfile.txt", static_lst}, ... };</span><br>
</span></span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 0); font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt;"><br>
</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Along the lines of the code you had sent, I created a GlobalVariable* llist of type
<i>[10 x %struct.dlist]* </i>for the field <span style="font-family: Calibri, Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255); display: inline !important">dlist* llist in h1. But this results in an
<i>LLVM ERROR: Type mismatch in constant table!.</i></span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
How do I fix this error ?</div>
<div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div id="Signature">
<div>
<meta content="text/html; charset=UTF-8">
<div id="divtagdefaultwrapper" dir="ltr" style="font-size:12pt; color:#000000; font-family:Calibri,Helvetica,sans-serif">
<div style="font-family:Tahoma; font-size:13px">
<div style=""><font face="Times New Roman" size="2" style=""><span style="font-family:"Times New Roman",Times,serif; font-size:12pt">Thanks,
</span></font><span style="font-family:"Times New Roman",Times,serif; font-size:12pt"></span>
<div style=""><font face="Times New Roman" size="2" style=""><span style="font-family:"Times New Roman",Times,serif; font-size:12pt">Chaitra</span></font></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div id="appendonsend"></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Tim Northover <t.p.northover@gmail.com><br>
<b>Sent:</b> Wednesday, September 30, 2020 4:09 AM<br>
<b>To:</b> Niddodi, Chaitra <chaitra@illinois.edu><br>
<b>Cc:</b> llvm-dev@lists.llvm.org <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> Re: [llvm-dev] Creating a global variable for a struct array</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt">
<div class="PlainText">On Wed, 30 Sep 2020 at 01:47, Niddodi, Chaitra <chaitra@illinois.edu> wrote:<br>
> Let me clarify my question.<br>
<br>
Some C or C++ code showing what you want to do in LLVM would really be<br>
most helpful there.<br>
<br>
> I have a struct array h1 as follows:<br>
> dhash h1[10];<br>
><br>
> I want to get a Constant* to variable h1.<br>
<br>
Do you mean you want to create a GlobalVariable "h1" with a Constant*<br>
initializer? If so, we need to know what you want to initialize it to.<br>
Writing IR for<br>
<br>
    dhash h1[10] = {0};<br>
<br>
would be very different from<br>
<br>
    dlist static_lst = {1, 5, NULL};<br>
    dhash h1[10] = {{"myfile.txt", &static_list}, 0 };<br>
<br>
> It looks like I can use ConstantStruct::get(StructType*, ArrayRef<Constant *>) to do this.<br>
<br>
It would be involved in the second example above, but because h1 is an<br>
array you'd need a whole ContantArray of them.<br>
<br>
> My question is how to get the second argument of type ArrayRef<Constant *> from the above variable h1.<br>
<br>
In the second example I gave above you'd write something like<br>
<br>
    // First create a global variable to hold the filename string.<br>
    Constant *FileNameInit = ConstantDataArray::getString("myfile.txt");<br>
    Constant *FileName = new GlobalVariable(Module,<br>
FileNameInit->getType(), true, GlobalValue::PrivateLinkage,<br>
FileNameInit, ".str");<br>
    FileName = ConstantExpr::getBitCast(FileName, Int8PtrTy);<br>
<br>
    // Look up the previously created static_list variable (code to<br>
produce it omitted for brevity).<br>
    GlobalVariable *StaticList = Module->getNamedValue("static_list");<br>
<br>
    // Create the ConstantStruct that will initialize the first<br>
element of the array.<br>
    Constant *FirstInitArr[] = { FileName, StaticList };<br>
    ConstantStruct *FirstInit = ConstantStruct::get(DHashTy, FirstInitArr);<br>
<br>
    // Create an all-zero struct for the rest of the array.<br>
    Constant *OtherInits = ConstantAggregateZero::get(DHashTy);<br>
<br>
    // Create the global variable.<br>
    Type *H1Ty = ArrayType::get(DHashTy, 10);<br>
    Constant *H1InitArr[] = {FirstInit, OtherInits, OtherInits,<br>
OtherInits, OtherInits, OtherInits, OtherInits, OtherInits,<br>
OtherInits, OtherInits};<br>
    Constant *H1Init = ConstantArray::get(H1Ty, H1InitArr);<br>
    GlobalVariable *H1 = new GlobalVariable(Module, H1Ty, false,<br>
GlobalVariable::ExternalLinkage, H1Init, "h1");<br>
<br>
which would produce LLVM IR looking like this:<br>
<br>
     @.str = private unnamed_addr constant [11 x i8] c"myfile.txt\00"<br>
     @h1 = global [10 x %dhash] [%dhash { i8* bitcast([11 x i8]* @.str<br>
to i8*), %dlist* @static_list}, %dhash zeroinitializer, %dhash<br>
zeroinitializer, ...}<br>
<br>
I think the critical point that might be missing is that if you have a<br>
pointer you want to initialize to something other than NULL (in this<br>
case the filename and the dlist pointer), you're going to need a<br>
separate global variable that provides real storage for it. You can't<br>
create a ConstantStruct and use that directly because a ConstantStruct<br>
has %struct type, but your field is %struct*.<br>
<br>
Or I might have gone off at a wild tangent that has nothing to do with<br>
what you're asking.<br>
<br>
Cheers.<br>
<br>
Tim.<br>
</div>
</span></font></div>
</div>
</body>
</html>