我对llvm和c ++有以下问题:给定一个数组,我想将该数组的每个条目向右移一,即,我想实现以下c代码:
int arr[5];
for(int i=1;i<5;++i){
arr[i] = arr[i-1];
}
我尝试了以下c ++代码:
IntegerType *Int32Ty = IntegerType::getInt32Ty(M.getContext(););
GlobalVariable *arrVariable;
arrVariable = new GlobalVariable(M, PointerType::get(Int32Ty, 0), false,
GlobalValue::ExternalLinkage, 0, "__arr");
for(int i=0;i<ARRAY_LENGTH;++i){
Constant* fromIdx = Constant::getIntegerValue(Int32Ty, llvm::APInt(32, i-1));
Value *fromLocPtrIdx = IRB.CreateGEP(arrVariable, fromIdx);
Constant* toIdx = Constant::getIntegerValue(Int32Ty, llvm::APInt(32, i));
Value *toLocPtrIdx = IRB.CreateGEP(arrVariable, toIdx);
StoreInst *MoveStoreInst = IRB.CreateStore(fromLocPtrIdx,toLocPtrIdx);
}
其中__arr
定义为:
__thread u32 __arr[ARRAY_LENGTH];
但是,在编译时,会产生以下错误消息:
/usr/bin/ld: __arr: TLS definition in ../inject.o section .tbss mismatches non-TLS reference in /tmp/test-inject.o
使用llvm c ++ api?
在数组中移动值的正确方法是什么答案 0 :(得分:0)
您需要指定全局变量为thread_local
。
这样做的方法是将线程局部模式添加到全局创建中:
arrVariable = new GlobalVariable(M, PointerType::get(Int32Ty, 0), false,
GlobalValue::ExternalLinkage, 0, "__arr", nullptr, InitialExecTLSModel);
确切模式取决于您的目标是什么,但我认为InitialExecTLSModel
通常是“良好的起点”。