Skip to content

Commit b65b894

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

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
@@ -914,23 +914,14 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
914914

915915
B.setInsertionPoint(entryBlock);
916916

917-
/// Generates a reinterpret_cast for converting
918-
/// Builtin.Job -> UnownedJob
919-
/// Builtin.Executor -> UnownedSerialExecutor
920-
/// These are used by _swiftJobRun, which, ABI-wise, could take
921-
/// Builtin.Job or Builtin.Executor, but doesn't.
922-
auto createExplodyCastForCall =
923-
[this, &moduleLoc](SILValue originalValue, FuncDecl *jobRunFuncDecl,
924-
uint32_t paramIndex) -> SILValue {
925-
// The type coming from the _swiftJobRun function
926-
Type apiType = jobRunFuncDecl->getParameters()->get(paramIndex)->getType();
927-
SILType apiSILType =
928-
SILType::getPrimitiveObjectType(apiType->getCanonicalType());
917+
auto wrapCallArgs = [this, &moduleLoc](SILValue originalValue, FuncDecl *fd,
918+
uint32_t paramIndex) -> SILValue {
919+
Type parameterType = fd->getParameters()->get(paramIndex)->getType();
920+
SILType paramSILType = SILType::getPrimitiveObjectType(parameterType->getCanonicalType());
929921
// If the types are the same, we don't need to do anything!
930-
if (apiSILType == originalValue->getType())
922+
if (paramSILType == originalValue->getType())
931923
return originalValue;
932-
return this->B.createUncheckedReinterpretCast(moduleLoc, originalValue,
933-
apiSILType);
924+
return this->B.createStruct(moduleLoc, paramSILType, originalValue);
934925
};
935926

936927
// Call CreateAsyncTask
@@ -975,7 +966,7 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
975966
moduleLoc,
976967
ctx.getIdentifier(getBuiltinName(BuiltinValueKind::ConvertTaskToJob)),
977968
JobType, {}, {task});
978-
jobResult = createExplodyCastForCall(jobResult, swiftJobRunFuncDecl, 0);
969+
jobResult = wrapCallArgs(jobResult, swiftJobRunFuncDecl, 0);
979970

980971
// Get main executor
981972
FuncDecl *getMainExecutorFuncDecl = SGM.getGetMainExecutor();
@@ -985,7 +976,7 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
985976
SILValue getMainExeutorFunc =
986977
B.createFunctionRefFor(moduleLoc, getMainExeutorSILFunc);
987978
SILValue mainExecutor = B.createApply(moduleLoc, getMainExeutorFunc, {}, {});
988-
mainExecutor = createExplodyCastForCall(mainExecutor, swiftJobRunFuncDecl, 1);
979+
mainExecutor = wrapCallArgs(mainExecutor, swiftJobRunFuncDecl, 1);
989980

990981
// Run first part synchronously
991982
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)