global variable

Hi all,

I am newbie for llvm. I just create a global variable, there are some statements in my pass like:

LoadInst* int64_64 = new LoadInst(pthreadPID, “”, false, OptAplusOne);
int64_64->setAlignment(8);

int64_64->dump();
LoadInst* int32_65 = new LoadInst(gvar_int32_myFlag, “”, false, OptAplusOne);
int32_65->setAlignment(4);

int32_65->dump();

But when I run my pass, it generates an error, I don’t know why.

@myFlag = global i32 0, align 4
@myPid = common global [20 x i64] zeroinitializer, align 16 //this is the global,
%117 = load i64* %t, align 8
%118 = load i32* @myFlag, align 4
While deleting: i32* %myFlag
Use still stuck around after Def is destroyed:@myFlag = global i32 <null operand!>, align 4

I am sure I initialize the global variable myFlag, I don’t delete this global variable.

Can anyone give some hints, or suggestions?

Yuxi

What code is printing the “While deleting:” text? Is it your code that prints that, or is it some existing LLVM pass? It looks like something is trying to remove the myFlag global variable, but I don’t know what code is trying to do that. I suggest you find that code and figure out why it’s trying to remove a global variable that you’re still using. Regards, John Criswell

Hi John,

Thanks for your reply. “While deleting” is not in my code.
I have carefully checked my pass code, and test.bc which I will run for test, there is no global variable named myFlag. It’s a bit weird. This global variable is added by myself in the pass. And the operation I do is just to load the global variable and store it. I really do not know the reason.

Module* JFIX::insertCounterMod(Module* mod,Instruction* OptAplusOne,Value* pthreadPID){ //insert myPid[flag++] = pid,after Operation A

//type definition, myPid[20]
ArrayType* ArrayTy_3 = ArrayType::get(IntegerType::get(mod->getContext(), 64), 20);

ConstantInt* const_int32_20 = ConstantInt::get(mod->getContext(), APInt(32, StringRef(“0”), 10)); //constant definition
ConstantAggregateZero* const_array_23 = ConstantAggregateZero::get(ArrayTy_3);
ConstantInt* const_int32_24 = ConstantInt::get(mod->getContext(), APInt(32, StringRef(“1”), 10));

GlobalVariable* gvar_int32_myFlag = getGlobalFromMap(mod->getModuleIdentifier(),“myFlag”);
GlobalVariable* gvar_array_myPid = getGlobalFromMap(mod->getModuleIdentifier(),“myPid”);
gvar_int32_myFlag->setInitializer(const_int32_20); //Global Variable Definitions
gvar_array_myPid->setInitializer(const_array_23);

gvar_int32_myFlag->dump();
gvar_array_myPid->dump();

LoadInst* int64_64 = new LoadInst(pthreadPID, “”, false, OptAplusOne); //do myPid[flag++] = pthreadID
int64_64->setAlignment(8);
LoadInst* int32_65 = new LoadInst(gvar_int32_myFlag, “”, false, OptAplusOne);
int32_65->setAlignment(4);
BinaryOperator* int32_inc = BinaryOperator::Create(Instruction::Add, int32_65, const_int32_24, “inc”, OptAplusOne);
StoreInst* void_66 = new StoreInst(int32_inc, gvar_int32_myFlag, false, OptAplusOne);
void_66->setAlignment(4);
CastInst* int64_idxprom = new SExtInst(int32_65, IntegerType::get(mod->getContext(), 64), “idxprom”, OptAplusOne);
std::vector<Value*> ptr_arrayidx_indices;
ptr_arrayidx_indices.push_back(const_int32_20);
ptr_arrayidx_indices.push_back(int64_idxprom);
Instruction* ptr_arrayidx = GetElementPtrInst::Create(gvar_array_myPid, ptr_arrayidx_indices, “arrayidx”, OptAplusOne);
StoreInst* void_67 = new StoreInst(int64_64, ptr_arrayidx, false, OptAplusOne);
void_67->setAlignment(8);
return mod;
}

Best,
Yuxi

Hi John,

Thanks for your reply. “While deleting” is not in my code.
I have carefully checked my pass code, and test.bc which I will run for test, there is no global variable named myFlag. It’s a bit weird. This global variable is added by myself in the pass. And the operation I do is just to load the global variable and store it. I really do not know the reason.

Module* JFIX::insertCounterMod(Module* mod,Instruction* OptAplusOne,Value* pthreadPID){ //insert myPid[flag++] = pid,after Operation A

//type definition, myPid[20]
ArrayType* ArrayTy_3 = ArrayType::get(IntegerType::get(mod->getContext(), 64), 20);

ConstantInt* const_int32_20 = ConstantInt::get(mod->getContext(), APInt(32, StringRef(“0”), 10)); //constant definition
ConstantAggregateZero* const_array_23 = ConstantAggregateZero::get(ArrayTy_3);
ConstantInt* const_int32_24 = ConstantInt::get(mod->getContext(), APInt(32, StringRef(“1”), 10));

GlobalVariable* gvar_int32_myFlag = getGlobalFromMap(mod->getModuleIdentifier(),“myFlag”);
GlobalVariable* gvar_array_myPid = getGlobalFromMap(mod->getModuleIdentifier(),“myPid”);
gvar_int32_myFlag->setInitializer(const_int32_20); //Global Variable Definitions
gvar_array_myPid->setInitializer(const_array_23);

gvar_int32_myFlag->dump();
gvar_array_myPid->dump();

LoadInst* int64_64 = new LoadInst(pthreadPID, “”, false, OptAplusOne); //do myPid[flag++] = pthreadID
int64_64->setAlignment(8);
LoadInst* int32_65 = new LoadInst(gvar_int32_myFlag, “”, false, OptAplusOne);
int32_65->setAlignment(4);
BinaryOperator* int32_inc = BinaryOperator::Create(Instruction::Add, int32_65, const_int32_24, “inc”, OptAplusOne);
StoreInst* void_66 = new StoreInst(int32_inc, gvar_int32_myFlag, false, OptAplusOne);
void_66->setAlignment(4);
CastInst* int64_idxprom = new SExtInst(int32_65, IntegerType::get(mod->getContext(), 64), “idxprom”, OptAplusOne);
std::vector<Value*> ptr_arrayidx_indices;
ptr_arrayidx_indices.push_back(const_int32_20);
ptr_arrayidx_indices.push_back(int64_idxprom);
Instruction* ptr_arrayidx = GetElementPtrInst::Create(gvar_array_myPid, ptr_arrayidx_indices, “arrayidx”, OptAplusOne);
StoreInst* void_67 = new StoreInst(int64_64, ptr_arrayidx, false, OptAplusOne);
void_67->setAlignment(8);
return mod;
}

Best,
Yuxi

You should run grep on the LLVM source code to find which code is deleting the global variable (it is probably one of the LLVM optimization passes). Once you find the code that is deleting the global variable, you can look at the code and determine why it is deleting the global variable. Once you know why the global variable is being deleted, you can figure out how to keep it from being deleted (perhaps the code using it is dead, or perhaps it has the wrong linkage type, etc, etc.). Regards, John Criswell