Skip to content

Commit c68ee88

Browse files
AlexVlxjsji
authored andcommitted
allocas should use the AllocaAS (#3004)
Currently, when reverse translating, we hardcode either 0 or `SPIRAS_Private` as the address space for `alloca`s. This is not ideal as it creates tight coupling with the current AS mappings. Furthermore, it is not necessary, as the DataLayout holds the AllocaAS, which is what we need. Hence, this patch changes all hardcoded callsites where we create an `AllocaInst` to use the DataLayout specified AS. It's NFC as it does not modify existing behaviour. Original commit: KhronosGroup/SPIRV-LLVM-Translator@c1a7e51fb0ea6ef
1 parent 03d9929 commit c68ee88

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,7 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
16651665
// execution instance, so emit an alloca instead of a global.
16661666
assert(BB && "OpVariable with Function storage class requires BB");
16671667
IRBuilder<> Builder(BB);
1668-
AllocaInst *AI = Builder.CreateAlloca(Ty, 0, BV->getName());
1668+
AllocaInst *AI = Builder.CreateAlloca(Ty, nullptr, BV->getName());
16691669
if (Init) {
16701670
auto *Src = transValue(Init, F, BB);
16711671
const bool IsVolatile = BVar->hasDecorate(DecorationVolatile);
@@ -1779,8 +1779,9 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
17791779
auto *VLA = static_cast<SPIRVVariableLengthArrayINTEL *>(BV);
17801780
llvm::Type *Ty = transType(BV->getType()->getPointerElementType());
17811781
llvm::Value *ArrSize = transValue(VLA->getOperand(0), F, BB);
1782-
return mapValue(
1783-
BV, new AllocaInst(Ty, SPIRAS_Private, ArrSize, BV->getName(), BB));
1782+
return mapValue(BV,
1783+
new AllocaInst(Ty, M->getDataLayout().getAllocaAddrSpace(),
1784+
ArrSize, BV->getName(), BB));
17841785
}
17851786

17861787
case OpRestoreMemoryINTEL: {
@@ -2248,9 +2249,9 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
22482249

22492250
case OpCopyObject: {
22502251
SPIRVCopyObject *CO = static_cast<SPIRVCopyObject *>(BV);
2251-
auto Ty = transType(CO->getOperand()->getType());
2252+
auto *Ty = transType(CO->getOperand()->getType());
22522253
AllocaInst *AI =
2253-
new AllocaInst(Ty, 0, "", BB);
2254+
new AllocaInst(Ty, M->getDataLayout().getAllocaAddrSpace(), "", BB);
22542255
new StoreInst(transValue(CO->getOperand(), F, BB), AI, BB);
22552256
LoadInst *LI = new LoadInst(Ty, AI, "", BB);
22562257
return mapValue(BV, LI);
@@ -2401,7 +2402,8 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
24012402
if (!HasRtValues)
24022403
return mapValue(BV, ConstantArray::get(AT, CV));
24032404

2404-
AllocaInst *Alloca = new AllocaInst(AT, SPIRAS_Private, "", BB);
2405+
AllocaInst *Alloca =
2406+
new AllocaInst(AT, M->getDataLayout().getAllocaAddrSpace(), "", BB);
24052407

24062408
// get pointer to the element of the array
24072409
// store the result of argument
@@ -2420,7 +2422,8 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
24202422
if (!HasRtValues)
24212423
return mapValue(BV, ConstantStruct::get(ST, CV));
24222424

2423-
AllocaInst *Alloca = new AllocaInst(ST, SPIRAS_Private, "", BB);
2425+
AllocaInst *Alloca =
2426+
new AllocaInst(ST, M->getDataLayout().getAllocaAddrSpace(), "", BB);
24242427

24252428
// get pointer to the element of structure
24262429
// store the result of argument
@@ -3020,7 +3023,8 @@ Value *SPIRVToLLVM::transFixedPointInst(SPIRVInstruction *BI, BasicBlock *BB) {
30203023
Args.reserve(8);
30213024
if (RetTy->getIntegerBitWidth() > 64) {
30223025
llvm::PointerType *RetPtrTy = llvm::PointerType::get(RetTy, SPIRAS_Generic);
3023-
Value *Alloca = new AllocaInst(RetTy, SPIRAS_Private, "", BB);
3026+
Value *Alloca =
3027+
new AllocaInst(RetTy, M->getDataLayout().getAllocaAddrSpace(), "", BB);
30243028
Value *RetValPtr = new AddrSpaceCastInst(Alloca, RetPtrTy, "", BB);
30253029
ArgTys.emplace_back(RetPtrTy);
30263030
Args.emplace_back(RetValPtr);
@@ -3144,7 +3148,8 @@ Value *SPIRVToLLVM::transArbFloatInst(SPIRVInstruction *BI, BasicBlock *BB,
31443148
if (RetTy->getIntegerBitWidth() > 64) {
31453149
llvm::PointerType *RetPtrTy = llvm::PointerType::get(RetTy, SPIRAS_Generic);
31463150
ArgTys.push_back(RetPtrTy);
3147-
Value *Alloca = new AllocaInst(RetTy, SPIRAS_Private, "", BB);
3151+
Value *Alloca =
3152+
new AllocaInst(RetTy, M->getDataLayout().getAllocaAddrSpace(), "", BB);
31483153
Value *RetValPtr = new AddrSpaceCastInst(Alloca, RetPtrTy, "", BB);
31493154
Args.push_back(RetValPtr);
31503155
}

llvm-spirv/lib/SPIRV/SPIRVToLLVMDbgTran.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,9 @@ SPIRVToLLVMDbgTran::transDebugIntrinsic(const SPIRVExtInst *DebugInst,
15831583
// DIBuilder::insertDeclare doesn't allow to pass nullptr for the Storage
15841584
// parameter. To work around this limitation we create a dummy temp
15851585
// alloca, use it to create llvm.dbg.declare, and then remove the alloca.
1586-
auto *AI = new AllocaInst(Type::getInt8Ty(M->getContext()), 0, "tmp", BB);
1586+
auto *AI =
1587+
new AllocaInst(Type::getInt8Ty(M->getContext()),
1588+
M->getDataLayout().getAllocaAddrSpace(), "tmp", BB);
15871589
DbgInstPtr DbgDeclare = DIB.insertDeclare(
15881590
AI, LocalVar.first, GetExpression(Ops[ExpressionIdx]),
15891591
LocalVar.second, BB);

llvm-spirv/lib/SPIRV/SPIRVToOCL20.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void SPIRVToOCL20Base::visitCallSPIRVAtomicCmpExchg(CallInst *CI) {
207207
// value by pointer passed as 2nd argument (aka expected) while SPIR-V
208208
// instructions returns this new/original value as a resulting value.
209209
AllocaInst *PExpected = new AllocaInst(
210-
MemTy, 0, "expected",
210+
MemTy, M->getDataLayout().getAllocaAddrSpace(), "expected",
211211
CI->getParent()->getParent()->getEntryBlock().getFirstInsertionPt());
212212
PExpected->setAlignment(Align(MemTy->getScalarSizeInBits() / 8));
213213

llvm-spirv/lib/SPIRV/SPIRVUtil.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,9 @@ Value *getScalarOrArrayConstantInt(BasicBlock::iterator Pos, Type *T,
15111511
auto *AT = ArrayType::get(ET, Len);
15121512
std::vector<Constant *> EV(Len, ConstantInt::get(ET, V, IsSigned));
15131513
auto *CA = ConstantArray::get(AT, EV);
1514-
auto *Alloca = new AllocaInst(AT, 0, "", Pos);
1514+
auto *Alloca = new AllocaInst(
1515+
AT, Pos->getParent()->getParent()->getDataLayout().getAllocaAddrSpace(),
1516+
"", Pos);
15151517
new StoreInst(CA, Alloca, Pos);
15161518
auto *Zero = ConstantInt::getNullValue(Type::getInt32Ty(T->getContext()));
15171519
Value *Index[] = {Zero, Zero};
@@ -2294,7 +2296,9 @@ bool postProcessBuiltinWithArrayArguments(Function *F,
22942296
auto *T = I->getType();
22952297
if (!T->isArrayTy())
22962298
continue;
2297-
auto *Alloca = new AllocaInst(T, 0, "", FBegin);
2299+
auto *Alloca = new AllocaInst(
2300+
T, F->getParent()->getDataLayout().getAllocaAddrSpace(), "",
2301+
FBegin);
22982302
new StoreInst(I, Alloca, false, CI->getIterator());
22992303
auto *Zero =
23002304
ConstantInt::getNullValue(Type::getInt32Ty(T->getContext()));

0 commit comments

Comments
 (0)