我在使用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在数组中移动值的正确方法是什么?
发布于 2017-12-30 10:47:21
您需要指定您的全局变量为thread_local
。
这样做的方法是将线程本地模式添加到全局创建中:
arrVariable = new GlobalVariable(M, PointerType::get(Int32Ty, 0), false,
GlobalValue::ExternalLinkage, 0, "__arr", nullptr, InitialExecTLSModel);
确切的模式取决于你的目标是什么,但我认为InitialExecTLSModel
通常是一个“很好的起点”。
https://stackoverflow.com/questions/48033085
复制