Skip to content

Commit c1a7e51

Browse files
authored
[NFC] 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.
1 parent c9a7f13 commit c1a7e51

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

lib/SPIRV/SPIRVReader.cpp

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

17841785
case OpRestoreMemoryINTEL: {
@@ -2247,7 +2248,8 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
22472248
case OpCopyObject: {
22482249
SPIRVCopyObject *CO = static_cast<SPIRVCopyObject *>(BV);
22492250
auto *Ty = transType(CO->getOperand()->getType());
2250-
AllocaInst *AI = new AllocaInst(Ty, 0, "", BB);
2251+
AllocaInst *AI =
2252+
new AllocaInst(Ty, M->getDataLayout().getAllocaAddrSpace(), "", BB);
22512253
new StoreInst(transValue(CO->getOperand(), F, BB), AI, BB);
22522254
LoadInst *LI = new LoadInst(Ty, AI, "", BB);
22532255
return mapValue(BV, LI);
@@ -2398,7 +2400,8 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
23982400
if (!HasRtValues)
23992401
return mapValue(BV, ConstantArray::get(AT, CV));
24002402

2401-
AllocaInst *Alloca = new AllocaInst(AT, SPIRAS_Private, "", BB);
2403+
AllocaInst *Alloca =
2404+
new AllocaInst(AT, M->getDataLayout().getAllocaAddrSpace(), "", BB);
24022405

24032406
// get pointer to the element of the array
24042407
// store the result of argument
@@ -2417,7 +2420,8 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
24172420
if (!HasRtValues)
24182421
return mapValue(BV, ConstantStruct::get(ST, CV));
24192422

2420-
AllocaInst *Alloca = new AllocaInst(ST, SPIRAS_Private, "", BB);
2423+
AllocaInst *Alloca =
2424+
new AllocaInst(ST, M->getDataLayout().getAllocaAddrSpace(), "", BB);
24212425

24222426
// get pointer to the element of structure
24232427
// store the result of argument
@@ -3017,7 +3021,8 @@ Value *SPIRVToLLVM::transFixedPointInst(SPIRVInstruction *BI, BasicBlock *BB) {
30173021
Args.reserve(8);
30183022
if (RetTy->getIntegerBitWidth() > 64) {
30193023
llvm::PointerType *RetPtrTy = llvm::PointerType::get(RetTy, SPIRAS_Generic);
3020-
Value *Alloca = new AllocaInst(RetTy, SPIRAS_Private, "", BB);
3024+
Value *Alloca =
3025+
new AllocaInst(RetTy, M->getDataLayout().getAllocaAddrSpace(), "", BB);
30213026
Value *RetValPtr = new AddrSpaceCastInst(Alloca, RetPtrTy, "", BB);
30223027
ArgTys.emplace_back(RetPtrTy);
30233028
Args.emplace_back(RetValPtr);
@@ -3141,7 +3146,8 @@ Value *SPIRVToLLVM::transArbFloatInst(SPIRVInstruction *BI, BasicBlock *BB,
31413146
if (RetTy->getIntegerBitWidth() > 64) {
31423147
llvm::PointerType *RetPtrTy = llvm::PointerType::get(RetTy, SPIRAS_Generic);
31433148
ArgTys.push_back(RetPtrTy);
3144-
Value *Alloca = new AllocaInst(RetTy, SPIRAS_Private, "", BB);
3149+
Value *Alloca =
3150+
new AllocaInst(RetTy, M->getDataLayout().getAllocaAddrSpace(), "", BB);
31453151
Value *RetValPtr = new AddrSpaceCastInst(Alloca, RetPtrTy, "", BB);
31463152
Args.push_back(RetValPtr);
31473153
}

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);

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

lib/SPIRV/SPIRVUtil.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,9 @@ Value *getScalarOrArrayConstantInt(BasicBlock::iterator Pos, Type *T,
15091509
auto *AT = ArrayType::get(ET, Len);
15101510
std::vector<Constant *> EV(Len, ConstantInt::get(ET, V, IsSigned));
15111511
auto *CA = ConstantArray::get(AT, EV);
1512-
auto *Alloca = new AllocaInst(AT, 0, "", Pos);
1512+
auto *Alloca = new AllocaInst(
1513+
AT, Pos->getParent()->getParent()->getDataLayout().getAllocaAddrSpace(),
1514+
"", Pos);
15131515
new StoreInst(CA, Alloca, Pos);
15141516
auto *Zero = ConstantInt::getNullValue(Type::getInt32Ty(T->getContext()));
15151517
Value *Index[] = {Zero, Zero};
@@ -2292,7 +2294,9 @@ bool postProcessBuiltinWithArrayArguments(Function *F,
22922294
auto *T = I->getType();
22932295
if (!T->isArrayTy())
22942296
continue;
2295-
auto *Alloca = new AllocaInst(T, 0, "", FBegin);
2297+
auto *Alloca = new AllocaInst(
2298+
T, F->getParent()->getDataLayout().getAllocaAddrSpace(), "",
2299+
FBegin);
22962300
new StoreInst(I, Alloca, false, CI->getIterator());
22972301
auto *Zero =
22982302
ConstantInt::getNullValue(Type::getInt32Ty(T->getContext()));

0 commit comments

Comments
 (0)