Skip to content

Commit b22407e

Browse files
committed
Add a builtin to convert a Task* to a Job*.
1 parent 10e0dfc commit b22407e

File tree

10 files changed

+69
-2
lines changed

10 files changed

+69
-2
lines changed

include/swift/AST/Builtins.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,11 @@ BUILTIN_MISC_OPERATION(AtomicStore, "atomicstore", "", Special)
621621
/// AtomicRMW has type (Builtin.RawPointer, T) -> T.
622622
BUILTIN_MISC_OPERATION(AtomicRMW, "atomicrmw", "", IntegerOrRawPointer)
623623

624+
/// convertTaskToJob : (Builtin.NativePointer) -> Builtin.Job
625+
///
626+
/// Convert a task pointer into a job pointer.
627+
BUILTIN_MISC_OPERATION(ConvertTaskToJob, "convertTaskToJob", "n", Special)
628+
624629
/// ExtractElement has type (Vector<N, T>, Int32) -> T
625630
BUILTIN_MISC_OPERATION(ExtractElement, "extractelement", "n", Special)
626631

lib/AST/Builtins.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "swift/AST/Builtins.h"
1818
#include "swift/AST/ASTContext.h"
19+
#include "swift/AST/ASTSynthesis.h"
1920
#include "swift/AST/FileUnit.h"
2021
#include "swift/AST/Module.h"
2122
#include "swift/AST/ParameterList.h"
@@ -180,6 +181,28 @@ getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType) {
180181
return FD;
181182
}
182183

184+
template <class ExtInfoS, class ParamsS, class ResultS>
185+
static FuncDecl *
186+
getBuiltinFunction(ASTContext &ctx, Identifier id,
187+
const ExtInfoS &extInfoS, const ParamsS &paramsS,
188+
const ResultS &resultS) {
189+
ModuleDecl *M = ctx.TheBuiltinModule;
190+
DeclContext *DC = &M->getMainFile(FileUnitKind::Builtin);
191+
192+
SynthesisContext SC(ctx, DC);
193+
auto params = synthesizeParameterList(SC, paramsS);
194+
auto extInfo = synthesizeExtInfo(SC, extInfoS);
195+
auto resultType = synthesizeType(SC, resultS);
196+
197+
DeclName name(ctx, id, params);
198+
auto *FD = FuncDecl::createImplicit(
199+
ctx, StaticSpellingKind::None, name, /*NameLoc=*/SourceLoc(),
200+
extInfo.isAsync(), extInfo.isThrowing(),
201+
/*GenericParams=*/nullptr, params, resultType, DC);
202+
FD->setAccess(AccessLevel::Public);
203+
return FD;
204+
}
205+
183206
namespace {
184207

185208
enum class BuiltinThrowsKind : uint8_t {
@@ -1409,6 +1432,13 @@ static ValueDecl *getCreateAsyncTaskFuture(ASTContext &ctx, Identifier id) {
14091432
return builder.build(id);
14101433
}
14111434

1435+
static ValueDecl *getConvertTaskToJob(ASTContext &ctx, Identifier id) {
1436+
return getBuiltinFunction(ctx, id,
1437+
_thin,
1438+
_parameters(_owned(_nativeObject)),
1439+
_job);
1440+
}
1441+
14121442
static ValueDecl *getAutoDiffCreateLinearMapContext(ASTContext &ctx,
14131443
Identifier id) {
14141444
return getBuiltinFunction(
@@ -2589,6 +2619,9 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
25892619
case BuiltinValueKind::CreateAsyncTaskFuture:
25902620
return getCreateAsyncTaskFuture(Context, Id);
25912621

2622+
case BuiltinValueKind::ConvertTaskToJob:
2623+
return getConvertTaskToJob(Context, Id);
2624+
25922625
case BuiltinValueKind::PoundAssert:
25932626
return getPoundAssert(Context, Id);
25942627

lib/IRGen/GenBuiltin.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
255255
return;
256256
}
257257

258+
if (Builtin.ID == BuiltinValueKind::ConvertTaskToJob) {
259+
auto task = args.claimNext();
260+
// The job object starts immediately past the heap-object header.
261+
auto bytes = IGF.Builder.CreateBitCast(task, IGF.IGM.Int8PtrTy);
262+
auto offset = IGF.IGM.RefCountedStructSize;
263+
bytes = IGF.Builder.CreateInBoundsGEP(bytes, IGF.IGM.getSize(offset));
264+
auto job = IGF.Builder.CreateBitCast(bytes, IGF.IGM.SwiftJobPtrTy);
265+
out.add(job);
266+
return;
267+
}
258268

259269
// If this is an LLVM IR intrinsic, lower it to an intrinsic call.
260270
const IntrinsicInfo &IInfo = IGF.getSILModule().getIntrinsicInfo(FnId);

lib/IRGen/GenType.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,8 +1662,7 @@ const LoadableTypeInfo &TypeConverter::getJobTypeInfo() {
16621662
// they're valid until they are scheduled, and then they're responsible
16631663
// for destroying themselves. (Jobs are often interior pointers into
16641664
// an AsyncTask*, but that's not guaranteed.)
1665-
auto ty = llvm::StructType::create(IGM.getLLVMContext(), "swift.job")
1666-
->getPointerTo();
1665+
auto ty = IGM.SwiftJobPtrTy;
16671666
auto pointeeAlign = Alignment(2 * IGM.getPointerAlignment().getValue());
16681667
JobTI = createAlignedPointerTypeInfo(IGM, ty, pointeeAlign);
16691668
JobTI->NextConverted = FirstType;

lib/IRGen/IRGenModule.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,11 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
610610
SwiftContextPtrTy = SwiftContextTy->getPointerTo(DefaultAS);
611611
SwiftTaskPtrTy = SwiftTaskTy->getPointerTo(DefaultAS);
612612
SwiftExecutorPtrTy = SwiftExecutorTy->getPointerTo(DefaultAS);
613+
SwiftJobTy = createStructType(*this, "swift.job", {
614+
SizeTy, // flags
615+
Int8PtrTy // execution function pointer
616+
});
617+
SwiftJobPtrTy = SwiftJobTy->getPointerTo();
613618

614619
// using TaskContinuationFunction =
615620
// SWIFT_CC(swift)

lib/IRGen/IRGenModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,10 +726,12 @@ class IRGenModule {
726726
llvm::StructType *AsyncFunctionPointerTy; // { i32, i32 }
727727
llvm::StructType *SwiftContextTy;
728728
llvm::StructType *SwiftTaskTy;
729+
llvm::StructType *SwiftJobTy;
729730
llvm::StructType *SwiftExecutorTy;
730731
llvm::PointerType *AsyncFunctionPointerPtrTy;
731732
llvm::PointerType *SwiftContextPtrTy;
732733
llvm::PointerType *SwiftTaskPtrTy;
734+
llvm::PointerType *SwiftJobPtrTy;
733735
llvm::PointerType *SwiftExecutorPtrTy;
734736
llvm::FunctionType *TaskContinuationFunctionTy;
735737
llvm::PointerType *TaskContinuationFunctionPtrTy;

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,7 @@ CONSTANT_OWNERSHIP_BUILTIN(Guaranteed, NonLifetimeEnding, CreateAsyncTaskFuture)
878878
CONSTANT_OWNERSHIP_BUILTIN(None, NonLifetimeEnding, AutoDiffCreateLinearMapContext)
879879
CONSTANT_OWNERSHIP_BUILTIN(Guaranteed, NonLifetimeEnding, AutoDiffAllocateSubcontext)
880880
CONSTANT_OWNERSHIP_BUILTIN(Guaranteed, NonLifetimeEnding, AutoDiffProjectTopLevelSubcontext)
881+
CONSTANT_OWNERSHIP_BUILTIN(Owned, LifetimeEnding, ConvertTaskToJob)
881882

882883
#undef CONSTANT_OWNERSHIP_BUILTIN
883884

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ CONSTANT_OWNERSHIP_BUILTIN(None, GetCurrentAsyncTask)
545545
CONSTANT_OWNERSHIP_BUILTIN(None, CancelAsyncTask)
546546
CONSTANT_OWNERSHIP_BUILTIN(Owned, CreateAsyncTask)
547547
CONSTANT_OWNERSHIP_BUILTIN(Owned, CreateAsyncTaskFuture)
548+
CONSTANT_OWNERSHIP_BUILTIN(None, ConvertTaskToJob)
548549
CONSTANT_OWNERSHIP_BUILTIN(Owned, AutoDiffCreateLinearMapContext)
549550
CONSTANT_OWNERSHIP_BUILTIN(None, AutoDiffProjectTopLevelSubcontext)
550551
CONSTANT_OWNERSHIP_BUILTIN(None, AutoDiffAllocateSubcontext)

lib/SILOptimizer/Transforms/AccessEnforcementReleaseSinking.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ static bool isBarrier(SILInstruction *inst) {
169169
case BuiltinValueKind::CancelAsyncTask:
170170
case BuiltinValueKind::CreateAsyncTask:
171171
case BuiltinValueKind::CreateAsyncTaskFuture:
172+
case BuiltinValueKind::ConvertTaskToJob:
172173
case BuiltinValueKind::AutoDiffProjectTopLevelSubcontext:
173174
case BuiltinValueKind::AutoDiffAllocateSubcontext:
174175
return true;

test/IRGen/builtins.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,5 +839,15 @@ func globalStringTablePointerUse(_ str: String) -> Builtin.RawPointer {
839839
}
840840

841841

842+
// CHECK-LABEL: define {{.*}}convertTaskToJob
843+
// CHECK: call %swift.refcounted* @swift_retain(%swift.refcounted* returned %0)
844+
// CHECK-NEXT: [[T0:%.*]] = bitcast %swift.refcounted* %0 to i8*
845+
// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i64 16
846+
// CHECK-NEXT: [[T2:%.*]] = bitcast i8* [[T1]] to %swift.job*
847+
// CHECK-NEXT: ret %swift.job* [[T2]]
848+
func convertTaskToJob(_ task: Builtin.NativeObject) -> Builtin.Job {
849+
return Builtin.convertTaskToJob(task)
850+
}
851+
842852

843853
// CHECK: ![[R]] = !{i64 0, i64 9223372036854775807}

0 commit comments

Comments
 (0)