|
| 1 | +//===--- GenConcurrency.cpp - IRGen for concurrency features --------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file implements IR generation for concurrency features (other than |
| 14 | +// basic async function lowering, which is more spread out). |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#include "GenConcurrency.h" |
| 19 | + |
| 20 | +#include "BitPatternBuilder.h" |
| 21 | +#include "ExtraInhabitants.h" |
| 22 | +#include "GenType.h" |
| 23 | +#include "IRGenFunction.h" |
| 24 | +#include "IRGenModule.h" |
| 25 | +#include "LoadableTypeInfo.h" |
| 26 | +#include "ScalarPairTypeInfo.h" |
| 27 | +#include "swift/ABI/MetadataValues.h" |
| 28 | + |
| 29 | +using namespace swift; |
| 30 | +using namespace irgen; |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +/// A TypeInfo implementation for Builtin.Executor. |
| 35 | +class ExecutorTypeInfo : |
| 36 | + public TrivialScalarPairTypeInfo<ExecutorTypeInfo, LoadableTypeInfo> { |
| 37 | + |
| 38 | +public: |
| 39 | + ExecutorTypeInfo(llvm::StructType *storageType, |
| 40 | + Size size, Alignment align, SpareBitVector &&spareBits) |
| 41 | + : TrivialScalarPairTypeInfo(storageType, size, std::move(spareBits), |
| 42 | + align, IsPOD, IsFixedSize) {} |
| 43 | + |
| 44 | + static Size getFirstElementSize(IRGenModule &IGM) { |
| 45 | + return IGM.getPointerSize(); |
| 46 | + } |
| 47 | + static StringRef getFirstElementLabel() { |
| 48 | + return ".identity"; |
| 49 | + } |
| 50 | + |
| 51 | + TypeLayoutEntry *buildTypeLayoutEntry(IRGenModule &IGM, |
| 52 | + SILType T) const override { |
| 53 | + return IGM.typeLayoutCache.getOrCreateScalarEntry(*this, T); |
| 54 | + } |
| 55 | + |
| 56 | + static Size getSecondElementOffset(IRGenModule &IGM) { |
| 57 | + return IGM.getPointerSize(); |
| 58 | + } |
| 59 | + static Size getSecondElementSize(IRGenModule &IGM) { |
| 60 | + return IGM.getPointerSize(); |
| 61 | + } |
| 62 | + static StringRef getSecondElementLabel() { |
| 63 | + return ".impl"; |
| 64 | + } |
| 65 | + |
| 66 | + // The identity pointer is a heap object reference, but it's |
| 67 | + // nullable because of the generic executor. |
| 68 | + bool mayHaveExtraInhabitants(IRGenModule &IGM) const override { |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + PointerInfo getPointerInfo(IRGenModule &IGM) const { |
| 73 | + return PointerInfo::forHeapObject(IGM).withNullable(IsNullable); |
| 74 | + } |
| 75 | + unsigned getFixedExtraInhabitantCount(IRGenModule &IGM) const override { |
| 76 | + return getPointerInfo(IGM).getExtraInhabitantCount(IGM); |
| 77 | + } |
| 78 | + APInt getFixedExtraInhabitantValue(IRGenModule &IGM, |
| 79 | + unsigned bits, |
| 80 | + unsigned index) const override { |
| 81 | + return getPointerInfo(IGM) |
| 82 | + .getFixedExtraInhabitantValue(IGM, bits, index, 0); |
| 83 | + } |
| 84 | + llvm::Value *getExtraInhabitantIndex(IRGenFunction &IGF, Address src, |
| 85 | + SILType T, |
| 86 | + bool isOutlined) const override { |
| 87 | + src = projectFirstElement(IGF, src); |
| 88 | + return getPointerInfo(IGF.IGM).getExtraInhabitantIndex(IGF, src); |
| 89 | + } |
| 90 | + APInt getFixedExtraInhabitantMask(IRGenModule &IGM) const override { |
| 91 | + auto pointerSize = IGM.getPointerSize(); |
| 92 | + auto mask = BitPatternBuilder(IGM.Triple.isLittleEndian()); |
| 93 | + mask.appendSetBits(pointerSize.getValueInBits()); |
| 94 | + mask.appendClearBits(pointerSize.getValueInBits()); |
| 95 | + return mask.build().getValue(); |
| 96 | + } |
| 97 | + void storeExtraInhabitant(IRGenFunction &IGF, llvm::Value *index, |
| 98 | + Address dest, SILType T, |
| 99 | + bool isOutlined) const override { |
| 100 | + dest = projectFirstElement(IGF, dest); |
| 101 | + getPointerInfo(IGF.IGM).storeExtraInhabitant(IGF, index, dest); |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +} // end anonymous namespace |
| 106 | + |
| 107 | +const LoadableTypeInfo &IRGenModule::getExecutorTypeInfo() { |
| 108 | + return Types.getExecutorTypeInfo(); |
| 109 | +} |
| 110 | + |
| 111 | +const LoadableTypeInfo &TypeConverter::getExecutorTypeInfo() { |
| 112 | + if (ExecutorTI) return *ExecutorTI; |
| 113 | + |
| 114 | + auto ty = IGM.SwiftExecutorTy; |
| 115 | + |
| 116 | + SpareBitVector spareBits; |
| 117 | + spareBits.append(IGM.getHeapObjectSpareBits()); |
| 118 | + spareBits.appendClearBits(IGM.getPointerSize().getValueInBits()); |
| 119 | + |
| 120 | + ExecutorTI = |
| 121 | + new ExecutorTypeInfo(ty, IGM.getPointerSize() * 2, |
| 122 | + IGM.getPointerAlignment(), |
| 123 | + std::move(spareBits)); |
| 124 | + ExecutorTI->NextConverted = FirstType; |
| 125 | + FirstType = ExecutorTI; |
| 126 | + return *ExecutorTI; |
| 127 | +} |
| 128 | + |
| 129 | +void irgen::emitBuildSerialExecutorRef(IRGenFunction &IGF, |
| 130 | + llvm::Value *actor, |
| 131 | + SILType actorType, |
| 132 | + Explosion &out) { |
| 133 | + auto cls = actorType.getClassOrBoundGenericClass(); |
| 134 | + |
| 135 | + // HACK: if the actor type is Swift.MainActor, treat it specially. |
| 136 | + if (cls && cls->getNameStr() == "MainActor" && |
| 137 | + cls->getDeclContext()->isModuleScopeContext() && |
| 138 | + cls->getModuleContext()->getName().str() == SWIFT_CONCURRENCY_NAME) { |
| 139 | + llvm::Value *identity = llvm::ConstantInt::get(IGF.IGM.SizeTy, |
| 140 | + unsigned(ExecutorRefFlags::MainActorIdentity)); |
| 141 | + identity = IGF.Builder.CreateIntToPtr(identity, IGF.IGM.ExecutorFirstTy); |
| 142 | + out.add(identity); |
| 143 | + |
| 144 | + llvm::Value *impl = IGF.IGM.getSize(Size(0)); |
| 145 | + out.add(impl); |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + llvm::Value *identity = |
| 150 | + IGF.Builder.CreateBitCast(actor, IGF.IGM.ExecutorFirstTy); |
| 151 | + llvm::Value *impl = IGF.IGM.getSize(Size(0)); |
| 152 | + |
| 153 | + unsigned flags = 0; |
| 154 | + |
| 155 | + // FIXME: this isn't how we should be doing any of this |
| 156 | + flags |= unsigned(ExecutorRefFlags::DefaultActor); |
| 157 | + |
| 158 | + if (flags) { |
| 159 | + impl = IGF.Builder.CreateOr(impl, IGF.IGM.getSize(Size(flags))); |
| 160 | + } |
| 161 | + out.add(identity); |
| 162 | + out.add(impl); |
| 163 | +} |
| 164 | + |
| 165 | +void irgen::emitGetCurrentExecutor(IRGenFunction &IGF, Explosion &out) { |
| 166 | + auto *call = IGF.Builder.CreateCall(IGF.IGM.getTaskGetCurrentExecutorFn(), |
| 167 | + {}); |
| 168 | + call->setDoesNotThrow(); |
| 169 | + call->setCallingConv(IGF.IGM.SwiftCC); |
| 170 | + |
| 171 | + IGF.emitAllExtractValues(call, IGF.IGM.SwiftExecutorTy, out); |
| 172 | +} |
0 commit comments