Skip to content

Commit e7ceddc

Browse files
committed
[Distributed] IRGen: Provide argument/result types to the accessor
1 parent 206969a commit e7ceddc

File tree

4 files changed

+54
-32
lines changed

4 files changed

+54
-32
lines changed

lib/IRGen/GenDistributed.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "ScalarPairTypeInfo.h"
3434
#include "swift/ABI/MetadataValues.h"
3535
#include "swift/AST/ExtInfo.h"
36+
#include "swift/AST/GenericEnvironment.h"
3637
#include "swift/AST/ProtocolConformanceRef.h"
3738
#include "swift/IRGen/Linking.h"
3839
#include "swift/SIL/SILFunction.h"
@@ -92,7 +93,9 @@ class DistributedAccessor {
9293
void emit();
9394

9495
private:
95-
void computeArguments(llvm::Value *argumentBuffer, Explosion &arguments);
96+
void computeArguments(llvm::Value *argumentBuffer,
97+
llvm::Value *argumentTypes,
98+
Explosion &arguments);
9699

97100
FunctionPointer getPointerToTarget() const;
98101

@@ -132,7 +135,9 @@ static CanSILFunctionType getAccessorType(IRGenModule &IGM,
132135
/*genericSignature=*/nullptr, extInfo, SILCoroutineKind::None,
133136
ParameterConvention::Direct_Guaranteed,
134137
{/*argumentBuffer=*/getRawPointerParameter(),
138+
/*argumentTypes=*/getRawPointerParameter(),
135139
/*resultBuffer=*/getRawPointerParameter(),
140+
/*resultType=*/getRawPointerParameter(),
136141
/*actor=*/targetTy->getParameters().back()},
137142
/*Yields=*/{},
138143
/*Results=*/{},
@@ -181,6 +186,7 @@ DistributedAccessor::DistributedAccessor(IRGenFunction &IGF,
181186
FunctionPointer::BasicKind::AsyncFunctionPointer))) {}
182187

183188
void DistributedAccessor::computeArguments(llvm::Value *argumentBuffer,
189+
llvm::Value *argumentTypes,
184190
Explosion &arguments) {
185191
auto fnType = Target->getLoweredFunctionType();
186192

@@ -301,8 +307,12 @@ void DistributedAccessor::emit() {
301307

302308
// UnsafeRawPointer that holds all of the argument values.
303309
auto *argBuffer = params.claimNext();
310+
// `swift.type**` that holds the argument types that correspond to values.
311+
auto *argTypes = params.claimNext();
304312
// UnsafeRawPointer that is used to store the result.
305313
auto *resultBuffer = params.claimNext();
314+
// `swift.type*` that holds the type fo the result.
315+
auto *resultType = params.claimNext();
306316
// Reference to a `self` of the actor to be called.
307317
auto *actorSelf = params.claimNext();
308318

@@ -333,7 +343,7 @@ void DistributedAccessor::emit() {
333343

334344
// Step one is to load all of the data from argument buffer,
335345
// so it could be forwarded to the distributed method.
336-
computeArguments(argBuffer, arguments);
346+
computeArguments(argBuffer, argTypes, arguments);
337347

338348
// Step two, let's form and emit a call to the distributed method
339349
// using computed argument explosion.

stdlib/public/Concurrency/Actor.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,9 @@ using TargetExecutorSignature =
17271727
AsyncSignature<void(/*on=*/DefaultActor *,
17281728
/*targetName=*/const char *, /*targetNameSize=*/size_t,
17291729
/*argumentBuffer=*/void *,
1730+
/*argumentTypes=*/const Metadata *const *,
17301731
/*resultBuffer=*/void *,
1732+
/*resultType=*/const Metadata *,
17311733
/*resumeFunc=*/TaskContinuationFunction *,
17321734
/*callContext=*/AsyncContext *),
17331735
/*throws=*/true>;
@@ -1742,7 +1744,11 @@ TargetExecutorSignature::FunctionType swift_distributed_execute_target;
17421744
/// - a result buffer as a raw pointer
17431745
/// - a reference to an actor to execute method on.
17441746
using DistributedAccessorSignature =
1745-
AsyncSignature<void(void *, void *, HeapObject *),
1747+
AsyncSignature<void(/*argumentBuffer=*/void *,
1748+
/*argumentTypes=*/const Metadata *const *,
1749+
/*resultBuffer=*/void *,
1750+
/*resultType=*/const Metadata *,
1751+
/*actor=*/HeapObject *),
17461752
/*throws=*/true>;
17471753

17481754
SWIFT_CC(swiftasync)
@@ -1769,7 +1775,9 @@ void ::swift_distributed_execute_target(
17691775
DefaultActor *actor,
17701776
const char *targetNameStart, size_t targetNameLength,
17711777
void *argumentBuffer,
1778+
const Metadata *const *argumentTypes,
17721779
void *resultBuffer,
1780+
const Metadata *resultType,
17731781
TaskContinuationFunction *resumeFunc,
17741782
AsyncContext *callContext) {
17751783
auto *accessor = findDistributedAccessor(targetNameStart, targetNameLength);
@@ -1803,5 +1811,8 @@ void ::swift_distributed_execute_target(
18031811
calleeContext->ResumeParent = reinterpret_cast<TaskContinuationFunction *>(
18041812
swift_distributed_execute_target_resume);
18051813

1806-
accessorEntry(calleeContext, argumentBuffer, resultBuffer, actor);
1814+
accessorEntry(calleeContext,
1815+
argumentBuffer, argumentTypes,
1816+
resultBuffer, resultType,
1817+
actor);
18071818
}

stdlib/public/Distributed/DistributedActorSystem.swift

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,9 @@ extension DistributedActorSystem {
207207
}
208208

209209
// Prepare buffer for the parameter types to be decoded into:
210-
let paramTypesBuffer = UnsafeMutableRawBufferPointer
211-
.allocate(byteCount: MemoryLayout<Any.Type>.size * Int(paramCount),
212-
alignment: MemoryLayout<Any.Type>.alignment)
210+
let argumentTypesBuffer = UnsafeMutableBufferPointer<Any.Type>.allocate(capacity: Int(paramCount))
213211
defer {
214-
paramTypesBuffer.deallocate()
212+
argumentTypesBuffer.deallocate()
215213
}
216214

217215
// Demangle and write all parameter types into the prepared buffer
@@ -220,7 +218,7 @@ extension DistributedActorSystem {
220218
nameUTF8.baseAddress!, UInt(nameUTF8.endIndex),
221219
genericEnv,
222220
substitutionsBuffer,
223-
paramTypesBuffer.baseAddress!._rawValue, Int(paramCount))
221+
argumentTypesBuffer.baseAddress!._rawValue, Int(paramCount))
224222
}
225223

226224
// Fail if the decoded parameter types count seems off and fishy
@@ -234,11 +232,11 @@ extension DistributedActorSystem {
234232
}
235233

236234
// Copy the types from the buffer into a Swift Array
237-
var paramTypes: [Any.Type] = []
235+
var argumentTypes: [Any.Type] = []
238236
do {
239-
paramTypes.reserveCapacity(Int(decodedNum))
240-
for paramType in paramTypesBuffer.bindMemory(to: Any.Type.self) {
241-
paramTypes.append(paramType)
237+
argumentTypes.reserveCapacity(Int(decodedNum))
238+
for argumentType in argumentTypesBuffer {
239+
argumentTypes.append(argumentType)
242240
}
243241
}
244242

@@ -268,7 +266,7 @@ extension DistributedActorSystem {
268266
}
269267

270268
// Prepare the buffer to decode the argument values into
271-
let hargs = HeterogeneousBuffer.allocate(forTypes: paramTypes)
269+
let hargs = HeterogeneousBuffer.allocate(forTypes: argumentTypes)
272270
defer {
273271
hargs.deinitialize()
274272
hargs.deallocate()
@@ -279,14 +277,14 @@ extension DistributedActorSystem {
279277
// TODO(distributed): decode the generics info
280278
// TODO(distributed): move this into the IRGen synthesized funcs, so we don't need hargs at all and can specialize the decodeNextArgument calls
281279
do {
282-
var paramIdx = 0
280+
var argumentIdx = 0
283281
for unsafeRawArgPointer in hargs {
284-
guard paramIdx < paramCount else {
282+
guard argumentIdx < paramCount else {
285283
throw ExecuteDistributedTargetError(
286-
message: "Unexpected attempt to decode more parameters than expected: \(paramIdx + 1)")
284+
message: "Unexpected attempt to decode more parameters than expected: \(argumentIdx + 1)")
287285
}
288-
let paramType = paramTypes[paramIdx]
289-
paramIdx += 1
286+
let argumentType = argumentTypes[argumentIdx]
287+
argumentIdx += 1
290288

291289
// FIXME(distributed): func doDecode<Arg: SerializationRequirement>(_: Arg.Type) throws {
292290
// FIXME: but how would we call this...?
@@ -296,7 +294,7 @@ extension DistributedActorSystem {
296294
.bindMemory(to: Arg.self, capacity: 1)
297295
try invocationDecoder.decodeNextArgument(Arg.self, into: unsafeArgPointer)
298296
}
299-
try _openExistential(paramType, do: doDecodeArgument)
297+
try _openExistential(argumentType, do: doDecodeArgument)
300298
}
301299
}
302300

@@ -308,7 +306,9 @@ extension DistributedActorSystem {
308306
on: actor,
309307
mangledTargetName, UInt(mangledTargetName.count),
310308
argumentBuffer: hargs.buffer._rawValue, // TODO(distributed): pass the invocationDecoder instead, so we can decode inside IRGen directly into the argument explosion
311-
resultBuffer: resultBuffer._rawValue
309+
argumentTypes: argumentTypesBuffer.baseAddress!._rawValue,
310+
resultBuffer: resultBuffer._rawValue,
311+
resultType: returnTypeFromTypeInfo
312312
)
313313

314314
func onReturn<R>(_ resultTy: R.Type) async throws {
@@ -327,7 +327,9 @@ func _executeDistributedTarget(
327327
on actor: AnyObject, // DistributedActor
328328
_ targetName: UnsafePointer<UInt8>, _ targetNameLength: UInt,
329329
argumentBuffer: Builtin.RawPointer, // HeterogeneousBuffer of arguments
330-
resultBuffer: Builtin.RawPointer
330+
argumentTypes: Builtin.RawPointer,
331+
resultBuffer: Builtin.RawPointer,
332+
resultType: Any.Type
331333
) async throws
332334

333335
// ==== ----------------------------------------------------------------------------------------------------------------

test/Distributed/distributed_actor_accessor_thunks_64bit.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public distributed actor MyOtherActor {
9090

9191
// CHECK: define hidden swifttailcc void @"$s27distributed_actor_accessors7MyActorC7simple1yySiFTE"
9292

93-
// CHECK: define internal swifttailcc void @"$s27distributed_actor_accessors7MyActorC7simple1yySiFTETF"(%swift.context* swiftasync %0, i8* %1, i8* %2, %T27distributed_actor_accessors7MyActorC* %3)
93+
// CHECK: define internal swifttailcc void @"$s27distributed_actor_accessors7MyActorC7simple1yySiFTETF"(%swift.context* swiftasync %0, i8* %1, i8* %2, i8* %3, i8* %4, %T27distributed_actor_accessors7MyActorC* %5)
9494

9595
/// Read the current offset and cast an element to `Int`
9696

@@ -113,7 +113,7 @@ public distributed actor MyOtherActor {
113113
// CHECK-SAME: i8* bitcast (void (%swift.context*, i64, %T27distributed_actor_accessors7MyActorC*)* @"$s27distributed_actor_accessors7MyActorC7simple1yySiFTE" to i8*),
114114
// CHECK-SAME: %swift.context* [[THUNK_CONTEXT_PTR]],
115115
// CHECK-SAME: i64 [[ARG_VAL]],
116-
// CHECK-SAME: %T27distributed_actor_accessors7MyActorC* %3)
116+
// CHECK-SAME: %T27distributed_actor_accessors7MyActorC* %5)
117117

118118
// CHECK-NEXT: [[TASK_REF:%.*]] = extractvalue { i8*, %swift.error* } [[THUNK_RESULT]], 0
119119
// CHECK-NEXT: {{.*}} = call i8* @__swift_async_resume_project_context(i8* [[TASK_REF]])
@@ -141,7 +141,7 @@ public distributed actor MyOtherActor {
141141
// CHECK-SAME: i8* bitcast (void (%swift.context*, i64, %T27distributed_actor_accessors7MyActorC*)* @"$s27distributed_actor_accessors7MyActorC7simple2ySSSiFTE" to i8*),
142142
// CHECK-SAME: %swift.context* [[THUNK_CONTEXT_PTR]],
143143
// CHECK-SAME: i64 [[NATIVE_ARG_VAL]],
144-
// CHECK-SAME: %T27distributed_actor_accessors7MyActorC* %3)
144+
// CHECK-SAME: %T27distributed_actor_accessors7MyActorC* %5)
145145

146146
// CHECK-NEXT: [[TASK_REF:%.*]] = extractvalue { i8*, i64, %swift.bridge*, %swift.error* } [[THUNK_RESULT]], 0
147147
// CHECK-NEXT: {{.*}} = call i8* @__swift_async_resume_project_context(i8* [[TASK_REF]])
@@ -164,7 +164,7 @@ public distributed actor MyOtherActor {
164164
// CHECK: define hidden swifttailcc void @"$s27distributed_actor_accessors7MyActorC7simple3ySiSSFTE"
165165

166166
/// !!! in `simple3` interesting bits are: argument value extraction (because string is exploded into N arguments) and call to distributed thunk
167-
// CHECK: define internal swifttailcc void @"$s27distributed_actor_accessors7MyActorC7simple3ySiSSFTETF"(%swift.context* swiftasync {{.*}}, i8* [[ARG_BUFF:%.*]], i8* [[RESULT_BUFF:%.*]], %T27distributed_actor_accessors7MyActorC* {{.*}})
167+
// CHECK: define internal swifttailcc void @"$s27distributed_actor_accessors7MyActorC7simple3ySiSSFTETF"(%swift.context* swiftasync {{.*}}, i8* [[ARG_BUFF:%.*]], i8* [[ARG_TYPES:%.*]], i8* [[RESULT_BUFF:%.*]], {{.*}}, %T27distributed_actor_accessors7MyActorC* {{.*}})
168168

169169
// CHECK: [[TYPED_RESULT_BUFF:%.*]] = bitcast i8* [[RESULT_BUFF]] to %TSi*
170170

@@ -188,7 +188,7 @@ public distributed actor MyOtherActor {
188188
// CHECK-SAME: %swift.context* [[THUNK_CONTEXT_PTR]],
189189
// CHECK-SAME: i64 [[STR_SIZE]],
190190
// CHECK-SAME: %swift.bridge* [[STR_VAL]],
191-
// CHECK-SAME: %T27distributed_actor_accessors7MyActorC* %3)
191+
// CHECK-SAME: %T27distributed_actor_accessors7MyActorC* %5)
192192

193193
// CHECK-NEXT: [[TASK_REF:%.*]] = extractvalue { i8*, i64, %swift.error* } [[THUNK_RESULT]], 0
194194
// CHECK-NEXT: {{.*}} = call i8* @__swift_async_resume_project_context(i8* [[TASK_REF]])
@@ -201,15 +201,14 @@ public distributed actor MyOtherActor {
201201

202202
// CHECK: define hidden swifttailcc void @"$s27distributed_actor_accessors7MyActorC16single_case_enumyAA7SimpleEOAFFTE"
203203

204-
// CHECK: define internal swifttailcc void @"$s27distributed_actor_accessors7MyActorC16single_case_enumyAA7SimpleEOAFFTETF"(%swift.context* swiftasync %0, i8* [[BUFFER:%.*]], i8* [[RESULT_BUFF:%.*]], %T27distributed_actor_accessors7MyActorC* {{.*}})
204+
// CHECK: define internal swifttailcc void @"$s27distributed_actor_accessors7MyActorC16single_case_enumyAA7SimpleEOAFFTETF"(%swift.context* swiftasync %0, i8* [[BUFFER:%.*]], i8* [[ARG_TYPES:%.*]], i8* [[RESULT_BUFF:%.*]], {{.*}}, %T27distributed_actor_accessors7MyActorC* {{.*}})
205205

206206
/// First, let's check that there were no loads from the argument buffer and no stores to "current offset".
207207

208208
// CHECK: [[OFFSET:%.*]] = bitcast i8** %offset to i8*
209209
// CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 8, i8* [[OFFSET]])
210210
// CHECK-NEXT: store i8* [[BUFFER]], i8** %offset
211211
// CHECK-NEXT: %elt_offset = load i8*, i8** %offset
212-
// CHECK-NEXT: [[ELT_PTR:.*]] = bitcast i8* %elt_offset to %T27distributed_actor_accessors7SimpleEO*
213212
// CHECK-NEXT: [[OFFSET:%.*]] = bitcast i8** %offset to i8*
214213
// CHECK-NEXT call void @llvm.lifetime.end.p0i8(i64 8, i8* [[OFFSET]])
215214

@@ -226,7 +225,7 @@ public distributed actor MyOtherActor {
226225

227226
/// First, Load both arguments from the buffer.
228227

229-
// CHECK: [[TYPED_RESULT_BUFF:%.*]] = bitcast i8* %2 to %T27distributed_actor_accessors9IndirectEO*
228+
// CHECK: [[TYPED_RESULT_BUFF:%.*]] = bitcast i8* [[RESULT_BUFF]] to %T27distributed_actor_accessors9IndirectEO*
230229
// CHECK: store i8* %1, i8** %offset
231230
// CHECK-NEXT: %elt_offset = load i8*, i8** %offset
232231

@@ -257,7 +256,7 @@ public distributed actor MyOtherActor {
257256

258257
// CHECK: define hidden swifttailcc void @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtFTE"
259258

260-
// CHECK: define internal swifttailcc void @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtFTETF"(%swift.context* swiftasync {{.*}}, i8* [[ARG_BUFF:%.*]], i8* [[RESULT_BUFF:%.*]], %T27distributed_actor_accessors7MyActorC* {{.*}})
259+
// CHECK: define internal swifttailcc void @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtFTETF"(%swift.context* swiftasync {{.*}}, i8* [[ARG_BUFF:%.*]], i8* [[ARG_TYPES:%.*]], i8* [[RESULT_BUFF:%.*]], {{.*}}, %T27distributed_actor_accessors7MyActorC* {{.*}})
261260

262261
/// First, let's check that all of the different argument types here are loaded correctly.
263262

@@ -318,7 +317,7 @@ public distributed actor MyOtherActor {
318317

319318
/// Let's check that there is no offset allocation here since parameter list is empty
320319

321-
// CHECK: define internal swifttailcc void @"$s27distributed_actor_accessors12MyOtherActorC5emptyyyFTETF"(%swift.context* swiftasync {{.*}}, i8* [[ARG_BUFF:%.*]], i8* [[RESULT_BUFF:%.*]], %T27distributed_actor_accessors12MyOtherActorC* {{.*}})
320+
// CHECK: define internal swifttailcc void @"$s27distributed_actor_accessors12MyOtherActorC5emptyyyFTETF"(%swift.context* swiftasync {{.*}}, i8* [[ARG_BUFF:%.*]], i8* [[ARG_TYPES:%.*]], i8* [[RESULT_BUFF:%.*]], {{.*}}, %T27distributed_actor_accessors12MyOtherActorC* {{.*}})
322321
// CHECK-NEXT: entry:
323322
// CHECK-NEXT: {{.*}} = alloca %swift.context*
324323
// CHECK-NEXT: %swifterror = alloca swifterror %swift.error*

0 commit comments

Comments
 (0)