Skip to content

Commit bb80f9f

Browse files
committed
Replace reinterpret cast with struct
A single-element struct is structurally the same as the member type itself.
1 parent d9baba9 commit bb80f9f

File tree

2 files changed

+10
-19
lines changed

2 files changed

+10
-19
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -868,23 +868,14 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
868868

869869
B.setInsertionPoint(entryBlock);
870870

871-
/// Generates a reinterpret_cast for converting
872-
/// Builtin.Job -> UnownedJob
873-
/// Builtin.Executor -> UnownedSerialExecutor
874-
/// These are used by _swiftJobRun, which, ABI-wise, could take
875-
/// Builtin.Job or Builtin.Executor, but doesn't.
876-
auto createExplodyCastForCall =
877-
[this, &moduleLoc](SILValue originalValue, FuncDecl *jobRunFuncDecl,
878-
uint32_t paramIndex) -> SILValue {
879-
// The type coming from the _swiftJobRun function
880-
Type apiType = jobRunFuncDecl->getParameters()->get(paramIndex)->getType();
881-
SILType apiSILType =
882-
SILType::getPrimitiveObjectType(apiType->getCanonicalType());
871+
auto wrapCallArgs = [this, &moduleLoc](SILValue originalValue, FuncDecl *fd,
872+
uint32_t paramIndex) -> SILValue {
873+
Type parameterType = fd->getParameters()->get(paramIndex)->getType();
874+
SILType paramSILType = SILType::getPrimitiveObjectType(parameterType->getCanonicalType());
883875
// If the types are the same, we don't need to do anything!
884-
if (apiSILType == originalValue->getType())
876+
if (paramSILType == originalValue->getType())
885877
return originalValue;
886-
return this->B.createUncheckedReinterpretCast(moduleLoc, originalValue,
887-
apiSILType);
878+
return this->B.createStruct(moduleLoc, paramSILType, originalValue);
888879
};
889880

890881
// Call CreateAsyncTask
@@ -929,7 +920,7 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
929920
moduleLoc,
930921
ctx.getIdentifier(getBuiltinName(BuiltinValueKind::ConvertTaskToJob)),
931922
JobType, {}, {task});
932-
jobResult = createExplodyCastForCall(jobResult, swiftJobRunFuncDecl, 0);
923+
jobResult = wrapCallArgs(jobResult, swiftJobRunFuncDecl, 0);
933924

934925
// Get main executor
935926
FuncDecl *getMainExecutorFuncDecl = SGM.getGetMainExecutor();
@@ -939,7 +930,7 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
939930
SILValue getMainExeutorFunc =
940931
B.createFunctionRefFor(moduleLoc, getMainExeutorSILFunc);
941932
SILValue mainExecutor = B.createApply(moduleLoc, getMainExeutorFunc, {}, {});
942-
mainExecutor = createExplodyCastForCall(mainExecutor, swiftJobRunFuncDecl, 1);
933+
mainExecutor = wrapCallArgs(mainExecutor, swiftJobRunFuncDecl, 1);
943934

944935
// Run first part synchronously
945936
B.createApply(moduleLoc, swiftJobRunFunc, {}, {jobResult, mainExecutor});

test/Concurrency/async_main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ func asyncFunc() async {
6969
// CHECK-SIL-NEXT: // function_ref swift_job_run
7070
// CHECK-SIL-NEXT: %12 = function_ref @swift_job_run : $@convention(thin) (UnownedJob, UnownedSerialExecutor) -> ()
7171
// CHECK-SIL-NEXT: %13 = builtin "convertTaskToJob"(%11 : $Builtin.NativeObject) : $Builtin.Job
72-
// CHECK-SIL-NEXT: %14 = unchecked_trivial_bit_cast %13 : $Builtin.Job to $UnownedJob
72+
// CHECK-SIL-NEXT: %14 = struct $UnownedJob (%13 : $Builtin.Job)
7373
// CHECK-SIL-NEXT: // function_ref swift_task_getMainExecutor
7474
// CHECK-SIL-NEXT: %15 = function_ref @swift_task_getMainExecutor : $@convention(thin) () -> Builtin.Executor
7575
// CHECK-SIL-NEXT: %16 = apply %15() : $@convention(thin) () -> Builtin.Executor
76-
// CHECK-SIL-NEXT: %17 = unchecked_trivial_bit_cast %16 : $Builtin.Executor to $UnownedSerialExecutor
76+
// CHECK-SIL-NEXT: %17 = struct $UnownedSerialExecutor (%16 : $Builtin.Executor)
7777
// CHECK-SIL-NEXT: %18 = apply %12(%14, %17) : $@convention(thin) (UnownedJob, UnownedSerialExecutor) -> ()
7878
// CHECK-SIL-NEXT: // function_ref swift_task_asyncMainDrainQueue
7979
// CHECK-SIL-NEXT: %19 = function_ref @swift_task_asyncMainDrainQueue : $@convention(thin) () -> Never

0 commit comments

Comments
 (0)