[llvm-dev] Creating a global variable for a struct array

Niddodi, Chaitra via llvm-dev llvm-dev at lists.llvm.org
Thu Oct 1 17:13:20 PDT 2020


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.


-Chaitra
________________________________
From: Niddodi, Chaitra <chaitra at illinois.edu>
Sent: Thursday, October 1, 2020 3:56:26 PM
To: Tim Northover <t.p.northover at gmail.com>
Cc: llvm-dev at lists.llvm.org <llvm-dev at lists.llvm.org>
Subject: Re: [llvm-dev] Creating a global variable for a struct array

>filenmConst's LLVM type is "[12 x i8]*" (or similar if I've
miscounted), but I assume the slot in the struct is i8*. So you need
either a bitcast or a getelementptr constant expression to convert it.

The error still persists despite adding the following cast:
filenmConst = ConstantExpr::getBitCast(filenmConst, Type::getInt8PtrTy(M.getContext()));
That's why I had omitted this line from code earlier.





Also, the same type mismatch error occurs when I try to create a GlobalVariable for a struct with 2 int fields as follows:

//struct dhash
typedef struct dhash{
int m;
int n;
}dhash;

//create global variable for int
Constant* intInit1 = ConstantInt::get(Type::getInt32Ty(M.getContext()), 11);
      Constant* intConst1 = new GlobalVariable(M, intInit1->getType(), false,
                                                    GlobalVariable::ExternalLinkage, intInit1, ".int1");

//create global variable for int
      Constant* intInit = ConstantInt::get(Type::getInt32Ty(M.getContext()), 10);
      Constant* intConst = new GlobalVariable(M, intInit->getType(), false,
                                                    GlobalVariable::ExternalLinkage, intInit, ".int");

      //create ConstantStruct for each hashtable entry
      Constant *dhashInit[] = {intConst1, intConst};//{filenmConst, intConst};
      Constant *dhashConst = ConstantStruct::get(dhashTy, dhashInit);

      Constant* htabInitArr[2];
      htabInitArr[0] = dhashConst;
      htabInitArr[1] = dhashConst;

    ArrayType *htabTy = ArrayType::get(dhashTy, 2);
    Constant *htabInit = ConstantArray::get(htabTy, makeArrayRef(htabInitArr,2));
    GlobalVariable *htabConst = new GlobalVariable(M, htabInit->getType(), false,
                                            GlobalVariable::ExternalLinkage, htabInit, "htab");




struct dhash in LLVM IR is as follows:

%struct.dhash = type { i32, i32 }


So, I'm not sure why the error occurs ?

Thanks,
Chaitra
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20201002/cf9dbb49/attachment.html>


More information about the llvm-dev mailing list