Skip to content

Commit 24837b4

Browse files
committed
IRGen: change a few enum utility functions to be able to create enums in static initializers
Instead of passing `IRGenFunction`, pass the `IRGenModule` and the `IRBuilder`. This makes enum creation not dependent on the presence of a function. NFC, just refactoring.
1 parent 3747a28 commit 24837b4

14 files changed

+150
-117
lines changed

lib/IRGen/EnumPayload.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,18 @@ static APInt createElementMask(const llvm::DataLayout &DL,
120120
return mask;
121121
}
122122

123-
void EnumPayload::insertValue(IRGenFunction &IGF, llvm::Value *value,
123+
void EnumPayload::insertValue(IRGenModule &IGM,
124+
IRBuilder &builder, llvm::Value *value,
124125
unsigned payloadOffset) {
125-
auto &DL = IGF.IGM.DataLayout;
126+
auto &DL = IGM.DataLayout;
126127

127128
// Create a mask for the value we are going to insert.
128129
auto type = value->getType();
129130
auto payloadSize = getAllocSizeInBits(DL);
130131
auto mask = createElementMask(DL, type, payloadOffset, payloadSize);
131132

132133
// Scatter the value into the payload.
133-
emitScatterBits(IGF, mask, value);
134+
emitScatterBits(IGM, builder, mask, value);
134135
}
135136

136137
llvm::Value *EnumPayload::extractValue(IRGenFunction &IGF, llvm::Type *type,
@@ -175,13 +176,14 @@ void EnumPayload::explode(IRGenModule &IGM, Explosion &out) const {
175176
}
176177
}
177178

178-
void EnumPayload::packIntoEnumPayload(IRGenFunction &IGF,
179+
void EnumPayload::packIntoEnumPayload(IRGenModule &IGM,
180+
IRBuilder &builder,
179181
EnumPayload &outerPayload,
180182
unsigned bitOffset) const {
181-
auto &DL = IGF.IGM.DataLayout;
183+
auto &DL = IGM.DataLayout;
182184
for (auto &value : PayloadValues) {
183185
auto v = forcePayloadValue(value);
184-
outerPayload.insertValue(IGF, v, bitOffset);
186+
outerPayload.insertValue(IGM, builder, v, bitOffset);
185187
bitOffset += DL.getTypeSizeInBits(v->getType());
186188
}
187189
}
@@ -419,12 +421,13 @@ EnumPayload::emitApplyAndMask(IRGenFunction &IGF, const APInt &mask) {
419421
}
420422

421423
void
422-
EnumPayload::emitApplyOrMask(IRGenFunction &IGF, const APInt &mask) {
424+
EnumPayload::emitApplyOrMask(IRGenModule &IGM,
425+
IRBuilder &builder, const APInt &mask) {
423426
// Early exit if the mask has no effect.
424427
if (mask == 0)
425428
return;
426429

427-
auto &DL = IGF.IGM.DataLayout;
430+
auto &DL = IGM.DataLayout;
428431
auto maskReader = BitPatternReader(mask, DL.isLittleEndian());
429432

430433
for (auto &pv : PayloadValues) {
@@ -438,21 +441,21 @@ EnumPayload::emitApplyOrMask(IRGenFunction &IGF, const APInt &mask) {
438441
if (maskPiece == 0)
439442
continue;
440443

441-
auto payloadIntTy = llvm::IntegerType::get(IGF.IGM.getLLVMContext(), size);
444+
auto payloadIntTy = llvm::IntegerType::get(IGM.getLLVMContext(), size);
442445
auto maskConstant = llvm::ConstantInt::get(payloadIntTy, maskPiece);
443446

444447
// If the payload value is vacant, or the mask is all ones,
445448
// we can adopt the mask value directly.
446449
if (pv.is<llvm::Type *>() || maskPiece.isAllOnesValue()) {
447-
pv = IGF.Builder.CreateBitOrPointerCast(maskConstant, payloadTy);
450+
pv = builder.CreateBitOrPointerCast(maskConstant, payloadTy);
448451
continue;
449452
}
450453

451454
// Otherwise, apply the mask to the existing value.
452455
auto v = pv.get<llvm::Value*>();
453-
v = IGF.Builder.CreateBitOrPointerCast(v, payloadIntTy);
454-
v = IGF.Builder.CreateOr(v, maskConstant);
455-
v = IGF.Builder.CreateBitOrPointerCast(v, payloadTy);
456+
v = builder.CreateBitOrPointerCast(v, payloadIntTy);
457+
v = builder.CreateOr(v, maskConstant);
458+
v = builder.CreateBitOrPointerCast(v, payloadTy);
456459
pv = v;
457460
}
458461
}
@@ -490,11 +493,11 @@ EnumPayload::emitApplyOrMask(IRGenFunction &IGF,
490493
}
491494
}
492495

493-
void EnumPayload::emitScatterBits(IRGenFunction &IGF,
496+
void EnumPayload::emitScatterBits(IRGenModule &IGM,
497+
IRBuilder &builder,
494498
const APInt &mask,
495499
llvm::Value *value) {
496-
auto &DL = IGF.IGM.DataLayout;
497-
auto &B = IGF.Builder;
500+
auto &DL = IGM.DataLayout;
498501

499502
unsigned valueBits = DL.getTypeSizeInBits(value->getType());
500503
auto totalBits = std::min(valueBits, mask.countPopulation());
@@ -519,20 +522,20 @@ void EnumPayload::emitScatterBits(IRGenFunction &IGF,
519522
if (DL.isBigEndian()) {
520523
offset = totalBits - partCount - usedBits;
521524
}
522-
auto partValue = irgen::emitScatterBits(IGF, partMask, value, offset);
525+
auto partValue = irgen::emitScatterBits(IGM, builder, partMask, value, offset);
523526

524527
// If necessary OR with the existing value.
525528
if (auto existingValue = pv.dyn_cast<llvm::Value*>()) {
526529
if (partType != partValue->getType()) {
527-
existingValue = B.CreateBitOrPointerCast(existingValue,
530+
existingValue = builder.CreateBitOrPointerCast(existingValue,
528531
partValue->getType());
529532
}
530-
partValue = B.CreateOr(partValue, existingValue);
533+
partValue = builder.CreateOr(partValue, existingValue);
531534
}
532535

533536
// Convert the integer result to the target type.
534537
if (partType != partValue->getType()) {
535-
partValue = B.CreateBitOrPointerCast(partValue, partType);
538+
partValue = builder.CreateBitOrPointerCast(partValue, partType);
536539
}
537540

538541
// Update this payload element.

lib/IRGen/EnumPayload.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ class EnumPayload {
104104
/// If \p numBitsUsedInValue is non-negative denotes the actual number of bits
105105
/// that need storing in \p value otherwise the full bit-width of \p value
106106
/// will be stored.
107-
void insertValue(IRGenFunction &IGF,
107+
void insertValue(IRGenModule &IGM,
108+
IRBuilder &builder,
108109
llvm::Value *value, unsigned bitOffset);
109110

110111
/// Extract a value from the enum payload.
@@ -120,7 +121,8 @@ class EnumPayload {
120121
void explode(IRGenModule &IGM, Explosion &out) const;
121122

122123
/// Pack into another enum payload.
123-
void packIntoEnumPayload(IRGenFunction &IGF,
124+
void packIntoEnumPayload(IRGenModule &IGM,
125+
IRBuilder &builder,
124126
EnumPayload &dest,
125127
unsigned bitOffset) const;
126128

@@ -153,15 +155,17 @@ class EnumPayload {
153155
void emitApplyAndMask(IRGenFunction &IGF, const APInt &mask);
154156

155157
/// Apply an OR mask to the payload.
156-
void emitApplyOrMask(IRGenFunction &IGF, const APInt &mask);
158+
void emitApplyOrMask(IRGenModule &IGM,
159+
IRBuilder &builder, const APInt &mask);
157160

158161
/// Apply an OR mask to the payload.
159162
void emitApplyOrMask(IRGenFunction &IGF, EnumPayload mask);
160163

161164
/// Scatter the bits in value to the bit positions indicated by the
162165
/// mask. The new bits are added using OR, so an emitApplyAndMask
163166
/// call should be used first if existing bits need to be cleared.
164-
void emitScatterBits(IRGenFunction &IGF,
167+
void emitScatterBits(IRGenModule &IGM,
168+
IRBuilder &builder,
165169
const APInt &mask,
166170
llvm::Value *value);
167171

lib/IRGen/GenCall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3860,7 +3860,7 @@ bool irgen::addNativeArgument(IRGenFunction &IGF,
38603860
// Pass the argument explosion directly, mapping into the native swift
38613861
// calling convention.
38623862
Explosion nonNativeParam;
3863-
ti.reexplode(IGF, in, nonNativeParam);
3863+
ti.reexplode(in, nonNativeParam);
38643864
Explosion nativeParam = nativeSchema.mapIntoNative(
38653865
IGF.IGM, IGF, nonNativeParam, paramType, isOutlined);
38663866
nativeParam.transferInto(out, nativeParam.size());

0 commit comments

Comments
 (0)