LLVM IR Array使用c ++ api移动

问题描述 投票:0回答:1

我有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在数组中移动值的正确方法是什么?

c++ llvm llvm-ir llvm-c++-api
1个回答
0
投票

您需要指定您的全局变量是thread_local

这样做的方法是将线程局部模式添加到全局创建中:

arrVariable = new GlobalVariable(M, PointerType::get(Int32Ty, 0), false,
                 GlobalValue::ExternalLinkage, 0, "__arr", nullptr, InitialExecTLSModel);

确切的模式取决于你的目标是什么,但我认为InitialExecTLSModel通常是一个“良好的起点”。

© www.soinside.com 2019 - 2024. All rights reserved.