Skip to content

Commit dd9ce40

Browse files
committed
add the allocVector builtin
1 parent df9f480 commit dd9ce40

File tree

11 files changed

+42
-1
lines changed

11 files changed

+42
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBuiltin.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extension BuiltinInst : OnoneSimplifyable {
4141
.AssignCopyArrayFrontToBack,
4242
.AssignCopyArrayBackToFront,
4343
.AssignTakeArray,
44+
.AllocVector,
4445
.IsPOD:
4546
optimizeArgumentToThinMetatype(argument: 0, context)
4647
case .CreateAsyncTask:

include/swift/AST/Builtins.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,9 @@ BUILTIN_MISC_OPERATION(UnprotectedStackAlloc, "unprotectedStackAlloc", "", Speci
646646
/// Builtin.stackAlloc(), is deallocated from the stack.
647647
BUILTIN_MISC_OPERATION(StackDealloc, "stackDealloc", "", Special)
648648

649+
/// allocVector<Element>(Element.Type, Builtin.Word) -> Builtin.RawPointer
650+
BUILTIN_MISC_OPERATION(AllocVector, "allocVector", "", Special)
651+
649652
/// Fence has type () -> ().
650653
BUILTIN_MISC_OPERATION(Fence, "fence", "", None)
651654

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ LANGUAGE_FEATURE(BuiltinCreateAsyncTaskInGroup, 0, "MainActor executor building
8989
LANGUAGE_FEATURE(BuiltinCopy, 0, "Builtin.copy()", true)
9090
LANGUAGE_FEATURE(BuiltinStackAlloc, 0, "Builtin.stackAlloc", true)
9191
LANGUAGE_FEATURE(BuiltinUnprotectedStackAlloc, 0, "Builtin.unprotectedStackAlloc", true)
92+
LANGUAGE_FEATURE(BuiltinAllocVector, 0, "Builtin.allocVector", true)
9293
LANGUAGE_FEATURE(BuiltinTaskRunInline, 0, "Builtin.taskRunInline", true)
9394
LANGUAGE_FEATURE(BuiltinUnprotectedAddressOf, 0, "Builtin.unprotectedAddressOf", true)
9495
LANGUAGE_FEATURE(NewCxxMethodSafetyHeuristics, 0, "Only import C++ methods that return pointers (projections) on owned types as unsafe", true)

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3212,6 +3212,10 @@ static bool usesFeatureBuiltinUnprotectedStackAlloc(Decl *decl) {
32123212
return false;
32133213
}
32143214

3215+
static bool usesFeatureBuiltinAllocVector(Decl *decl) {
3216+
return false;
3217+
}
3218+
32153219
static bool usesFeatureBuiltinAssumeAlignment(Decl *decl) {
32163220
return false;
32173221
}

lib/AST/Builtins.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,13 @@ static ValueDecl *getStackDeallocOperation(ASTContext &ctx, Identifier id) {
10731073
_void);
10741074
}
10751075

1076+
static ValueDecl *getAllocVectorOperation(ASTContext &ctx, Identifier id) {
1077+
return getBuiltinFunction(ctx, id, _thin,
1078+
_generics(_unrestricted),
1079+
_parameters(_metatype(_typeparam(0)), _word),
1080+
_rawPointer);
1081+
}
1082+
10761083
static ValueDecl *getFenceOperation(ASTContext &ctx, Identifier id) {
10771084
return getBuiltinFunction(ctx, id, _thin, _parameters(), _void);
10781085
}
@@ -2743,6 +2750,9 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
27432750
case BuiltinValueKind::StackDealloc:
27442751
return getStackDeallocOperation(Context, Id);
27452752

2753+
case BuiltinValueKind::AllocVector:
2754+
return getAllocVectorOperation(Context, Id);
2755+
27462756
case BuiltinValueKind::CastToNativeObject:
27472757
case BuiltinValueKind::UnsafeCastToNativeObject:
27482758
case BuiltinValueKind::CastFromNativeObject:

lib/IRGen/GenBuiltin.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,13 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
14901490
out.add(pointerSrc);
14911491
return;
14921492
}
1493-
1493+
if (Builtin.ID == BuiltinValueKind::AllocVector) {
1494+
(void)args.claimAll();
1495+
IGF.emitTrap("escaped vector allocation", /*EmitUnreachable=*/true);
1496+
out.add(llvm::UndefValue::get(IGF.IGM.Int8PtrTy));
1497+
llvm::BasicBlock *contBB = llvm::BasicBlock::Create(IGF.IGM.getLLVMContext());
1498+
IGF.Builder.emitBlock(contBB);
1499+
return;
1500+
}
14941501
llvm_unreachable("IRGen unimplemented for this builtin!");
14951502
}

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, SSubOver)
836836
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, StackAlloc)
837837
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, UnprotectedStackAlloc)
838838
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, StackDealloc)
839+
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, AllocVector)
839840
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, SToSCheckedTrunc)
840841
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, SToUCheckedTrunc)
841842
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, Expect)

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ CONSTANT_OWNERSHIP_BUILTIN(None, UToSCheckedTrunc)
540540
CONSTANT_OWNERSHIP_BUILTIN(None, StackAlloc)
541541
CONSTANT_OWNERSHIP_BUILTIN(None, UnprotectedStackAlloc)
542542
CONSTANT_OWNERSHIP_BUILTIN(None, StackDealloc)
543+
CONSTANT_OWNERSHIP_BUILTIN(None, AllocVector)
543544
CONSTANT_OWNERSHIP_BUILTIN(None, SToSCheckedTrunc)
544545
CONSTANT_OWNERSHIP_BUILTIN(None, SToUCheckedTrunc)
545546
CONSTANT_OWNERSHIP_BUILTIN(None, UToUCheckedTrunc)

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,6 +2597,7 @@ static void visitBuiltinAddress(BuiltinInst *builtin,
25972597
case BuiltinValueKind::DeallocRaw:
25982598
case BuiltinValueKind::StackAlloc:
25992599
case BuiltinValueKind::UnprotectedStackAlloc:
2600+
case BuiltinValueKind::AllocVector:
26002601
case BuiltinValueKind::StackDealloc:
26012602
case BuiltinValueKind::Fence:
26022603
case BuiltinValueKind::StaticReport:

lib/SILOptimizer/Transforms/AccessEnforcementReleaseSinking.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ static bool isBarrier(SILInstruction *inst) {
155155
case BuiltinValueKind::StackAlloc:
156156
case BuiltinValueKind::UnprotectedStackAlloc:
157157
case BuiltinValueKind::StackDealloc:
158+
case BuiltinValueKind::AllocVector:
158159
case BuiltinValueKind::AssumeAlignment:
159160
return false;
160161

0 commit comments

Comments
 (0)