Skip to content

Commit 9dad3af

Browse files
Merge pull request #34746 from aschwaighofer/reinstate_task_allocator_size_alignment_requirement
Reinstate task allocator size alignment requirement
2 parents a0dccc5 + e7e6490 commit 9dad3af

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

lib/IRGen/GenOpaque.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,9 @@ StackAddress IRGenFunction::emitDynamicAlloca(llvm::Type *eltTy,
545545
} else {
546546
byteCount = Builder.CreateMul(arraySize, IGM.getSize(Size(eltSize)));
547547
}
548+
// The task allocator wants size increments in the mulitple of
549+
// MaximumAlignment.
550+
byteCount = alignUpToMaximumAlignment(IGM.SizeTy, byteCount);
548551
auto address = emitTaskAlloc(byteCount, align);
549552
return {address, address.getAddress()};
550553
// In coroutines, call llvm.coro.alloca.alloc.

lib/IRGen/IRGenFunction.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// performs IR generation for function bodies.
1515
//
1616
//===----------------------------------------------------------------------===//
17-
17+
#include "swift/ABI/MetadataValues.h"
1818
#include "swift/AST/IRGenOptions.h"
1919
#include "swift/Basic/SourceLoc.h"
2020
#include "swift/IRGen/Linking.h"
@@ -507,3 +507,9 @@ void IRGenFunction::emitTaskDealloc(Address address) {
507507
call->setDoesNotThrow();
508508
call->setCallingConv(IGM.SwiftCC);
509509
}
510+
511+
llvm::Value *IRGenFunction::alignUpToMaximumAlignment(llvm::Type *sizeTy, llvm::Value *val) {
512+
auto *alignMask = llvm::ConstantInt::get(sizeTy, MaximumAlignment - 1);
513+
auto *invertedMask = Builder.CreateNot(alignMask);
514+
return Builder.CreateAnd(Builder.CreateAdd(val, alignMask), invertedMask);
515+
}

lib/IRGen/IRGenFunction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ class IRGenFunction {
460460
Alignment alignment);
461461
void emitTaskDealloc(Address address);
462462

463+
llvm::Value *alignUpToMaximumAlignment(llvm::Type *sizeTy, llvm::Value *val);
464+
463465
//--- Expression emission
464466
//------------------------------------------------------
465467
public:

stdlib/public/Concurrency/Task.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,7 @@ AsyncTaskAndContext swift::swift_task_create_future_f(
213213
// This means that we never get rid of this allocation.
214214
size_t amountToAllocate = headerSize + initialContextSize;
215215

216-
// TODO: if this is necessary we need to teach LLVM lowering to request async
217-
// context sizes that are mulitple of that maximum alignment.
218-
// For now disable this assert.
219-
// assert(amountToAllocate % MaximumAlignment == 0);
216+
assert(amountToAllocate % MaximumAlignment == 0);
220217

221218
void *allocation = malloc(amountToAllocate);
222219

test/Interpreter/async.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -Xfrontend -enable-experimental-concurrency %s -module-name main -o %t/main
3+
// RUN: %target-codesign %t/main
4+
// RUN: %target-run %t/main | %FileCheck %s
5+
6+
// REQUIRES: concurrency
7+
// UNSUPPORTED: use_os_stdlib
8+
// UNSUPPORTED: CPU=arm64e
9+
10+
11+
func sayHello() async {
12+
print("hello")
13+
}
14+
15+
func sayGeneric<T>(_ msg: T) async {
16+
await sayHello()
17+
print(msg)
18+
}
19+
20+
func sayWithClosure(_ action: () async -> ()) async {
21+
await action()
22+
print("hallo welt")
23+
}
24+
25+
runAsync {
26+
// CHECK: hello
27+
await sayHello()
28+
29+
// CHECK: hello
30+
// CHECK: world
31+
await sayGeneric("world")
32+
33+
34+
// CHECK: hello
35+
// CHECK: and now in german
36+
// CHECK: hallo welt
37+
await sayWithClosure {
38+
await sayHello()
39+
print("and now in german")
40+
}
41+
}

0 commit comments

Comments
 (0)