<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<div dir="ltr">
<div></div>
<div>
<div>
<div style="direction: ltr;">The type mismatch is because the struct field type is i32 and GlobalVariable type is i32*. The issue is resolved after using proper casts as you pointed out. Thanks.</div>
</div>
<div><br>
</div>
<div class="ms-outlook-ios-signature">
<div style="direction: ltr;" dir="ltr"><br>
</div>
<div style="direction: ltr;">-Chaitra</div>
</div>
</div>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Niddodi, Chaitra <chaitra@illinois.edu><br>
<b>Sent:</b> Thursday, October 1, 2020 3:56:26 PM<br>
<b>To:</b> Tim Northover <t.p.northover@gmail.com><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>
<style type="text/css" style="display:none">
<!--
p
{margin-top:0;
margin-bottom:0}
-->
</style>
<div dir="ltr">
<div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="color:rgb(32,31,30); font-size:14.6667px; background-color:rgb(255,255,255); display:inline!important">>filenmConst's LLVM type is "[12 x i8]*" (or similar if I've</span><br style="color:rgb(32,31,30); font-size:14.6667px; background-color:rgb(255,255,255)">
<span style="color:rgb(32,31,30); font-size:14.6667px; background-color:rgb(255,255,255); display:inline!important">miscounted), but I assume the slot in the struct is i8*. So you need</span><br style="color:rgb(32,31,30); font-size:14.6667px; background-color:rgb(255,255,255)">
<span style="color:rgb(32,31,30); font-size:14.6667px; background-color:rgb(255,255,255); display:inline!important">either a bitcast or a getelementptr constant expression to convert it.</span><br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="color:rgb(32,31,30); font-size:14.6667px; background-color:rgb(255,255,255); display:inline!important"><br>
</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="color:rgb(32,31,30); font-size:14.6667px; background-color:rgb(255,255,255); display:inline!important">
<div dir="ltr" style="margin:0px; font-size:15px; background-color:rgb(255,255,255)">
The error still persists despite adding the following cast:</div>
<div dir="ltr" style="margin:0px; font-size:15px; background-color:rgb(255,255,255)">
<span style="font-size:15px">filenmConst = ConstantExpr::getBitCast(filenmConst, Type::getInt8PtrTy(M.getContext()));</span><br>
</div>
<div dir="ltr" style="margin:0px; font-size:15px; background-color:rgb(255,255,255)">
<span style="font-size:15px">That's why I had omitted this line from code earlier.</span><br>
</div>
<br>
</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)">
<br>
</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)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Also, the same type mismatch error occurs when I try to create a <i>GlobalVariable</i> for a struct with 2 int fields as follows:</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)">
//struct dhash</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
typedef struct dhash{
<div>int m;</div>
<div>int n;</div>
}dhash;<br>
</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)">
<span style="font-family:Calibri,Arial,Helvetica,sans-serif; background-color:rgb(255,255,255); display:inline!important">//create global variable for int</span><br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Constant* intInit1 = ConstantInt::get(Type::getInt32Ty(M.getContext()), 11);
<div> Constant* intConst1 = new GlobalVariable(M, intInit1->getType(), false,
</div>
<div> GlobalVariable::ExternalLinkage, intInit1, ".int1");</div>
<div> </div>
<div>//create global variable for int</div>
<div> Constant* intInit = ConstantInt::get(Type::getInt32Ty(M.getContext()), 10);</div>
<div> Constant* intConst = new GlobalVariable(M, intInit->getType(), false, </div>
<div> GlobalVariable::ExternalLinkage, intInit, ".int");</div>
<div><br>
</div>
<div> //create ConstantStruct for each hashtable entry</div>
<div> Constant *dhashInit[] = {intConst1, intConst};//{filenmConst, intConst};</div>
<div> Constant *dhashConst = ConstantStruct::get(dhashTy, dhashInit);</div>
<div><br>
</div>
<div> Constant* htabInitArr[2];</div>
<div> htabInitArr[0] = dhashConst;</div>
<div> htabInitArr[1] = dhashConst;</div>
<div><br>
</div>
<div> ArrayType *htabTy = ArrayType::get(dhashTy, 2);</div>
<div> Constant *htabInit = ConstantArray::get(htabTy, makeArrayRef(htabInitArr,2));</div>
<div> GlobalVariable *htabConst = new GlobalVariable(M, htabInit->getType(), false,
</div>
GlobalVariable::ExternalLinkage, htabInit, "htab");<br>
</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)">
<br>
</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)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
struct dhash in LLVM IR is as follows:</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)">
%struct.dhash = type { i32, i32 }<br>
</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)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
So, I'm not sure why the error occurs ?</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div id="x_Signature">
<div>
<meta content="text/html; charset=UTF-8">
<div id="x_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>
</body>
</html>