[LLVMdev] Initialising global Array

Andreas Neustifter e0325716 at student.tuwien.ac.at
Thu Jun 18 13:07:34 PDT 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

Slobodan Pejic wrote:
> I ran the following two functions in a ModulePass.  The Initializer 
> initialization is the difference.
> 
> static void andiTest1(Module &M) {
>   int NumEdges = 4;
>   const ArrayType *ATy = ArrayType::get(Type::Int32Ty, NumEdges);
> 
>   std::vector<Constant*> Initializer; Initializer.reserve(NumEdges);
>   APInt zero(32,0);      Constant* zeroc = ConstantInt::get(zero);
>   APInt minusone(32,-1); Constant* minusonec = ConstantInt::get(minusone);
> 
>   GlobalVariable *Counters = new GlobalVariable(ATy, false,
>       GlobalValue::InternalLinkage, Constant::getNullValue(ATy),
>       "OptimalEdgeProfCounters1", &M);
> 
>   Initializer[0] = zeroc;
>   Initializer[1] = minusonec;
>   Initializer[2] = minusonec;
>   Initializer[3] = zeroc;
> 
>   Constant *init = llvm::ConstantArray::get(ATy, Initializer);
>   Counters->setInitializer(init);
>   DOUT << "Initializer:: \n";
>   for( int i = 0; i < Initializer.size(); ++i ) {
>     DOUT << "Initializer["<<i<<"]: " << Initializer[i] << "\n";
>   }
> }
> 
> static void andiTest2(Module &M) {
>   int NumEdges = 4;
>   const ArrayType *ATy = ArrayType::get(Type::Int32Ty, NumEdges);
> 
>   std::vector<Constant*> Initializer = std::vector<Constant*>(NumEdges);
>   APInt zero(32,0);      Constant* zeroc = ConstantInt::get(zero);
>   APInt minusone(32,-1); Constant* minusonec = ConstantInt::get(minusone);
> 
>   GlobalVariable *Counters = new GlobalVariable(ATy, false,
>       GlobalValue::InternalLinkage, Constant::getNullValue(ATy),
>       "OptimalEdgeProfCounters2", &M);
> 
>   Initializer[0] = zeroc;
>   Initializer[1] = minusonec;
>   Initializer[2] = minusonec;
>   Initializer[3] = zeroc;
> 
>   Constant *init = llvm::ConstantArray::get(ATy, Initializer);
>   Counters->setInitializer(init);
>   DOUT << "Initializer:: \n";
>   for( int i = 0; i < Initializer.size(); ++i ) {
>     DOUT << "Initializer["<<i<<"]: " << Initializer[i] << "\n";
>   }
> }

Yes, the first variant (mine) does not work, the second does. (Don't get
it why, tough...)

But when I use the initialisation of "Initializer" you suggested in my
"production code" it still does not work :-(

But thanks for your effort, I really appreciate it!

Andi
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAko6nj4ACgkQPiYq0rq7s/DpnQCfflDWnUG/C0qTD1DoltnwbfW9
6g8AmQGfV3PVok15n+5upZShYDlLooGv
=wF1S
-----END PGP SIGNATURE-----



More information about the llvm-dev mailing list