Skip to content

Re-instate the allocVector builtin #79359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/swift/AST/Builtins.def
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,11 @@ BUILTIN_MISC_OPERATION(UnprotectedStackAlloc, "unprotectedStackAlloc", "", Speci
/// Builtin.stackAlloc(), is deallocated from the stack.
BUILTIN_MISC_OPERATION(StackDealloc, "stackDealloc", "", Special)

// Obsolete: only there to be able to read old Swift.interface files which still
// contain the builtin.
/// allocVector<Element>(Element.Type, Builtin.Word) -> Builtin.RawPointer
BUILTIN_MISC_OPERATION(AllocVector, "allocVector", "", Special)

/// Fence has type () -> ().
BUILTIN_MISC_OPERATION(Fence, "fence", "", None)

Expand Down
12 changes: 12 additions & 0 deletions lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,15 @@ static ValueDecl *getStackDeallocOperation(ASTContext &ctx, Identifier id) {
_void);
}

// Obsolete: only there to be able to read old Swift.interface files which still
// contain the builtin.
static ValueDecl *getAllocVectorOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
_parameters(_metatype(_typeparam(0)), _word),
_rawPointer);
}

static ValueDecl *getFenceOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin, _parameters(), _void);
}
Expand Down Expand Up @@ -3042,6 +3051,9 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
case BuiltinValueKind::StackDealloc:
return getStackDeallocOperation(Context, Id);

case BuiltinValueKind::AllocVector:
return getAllocVectorOperation(Context, Id);

case BuiltinValueKind::CastToNativeObject:
case BuiltinValueKind::UnsafeCastToNativeObject:
case BuiltinValueKind::CastFromNativeObject:
Expand Down
11 changes: 11 additions & 0 deletions lib/IRGen/GenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,17 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
return;
}

if (Builtin.ID == BuiltinValueKind::AllocVector) {
// Obsolete: only there to be able to read old Swift.interface files which still
// contain the builtin.
(void)args.claimAll();
IGF.emitTrap("vector allocation not supported anymore", /*EmitUnreachable=*/true);
out.add(llvm::UndefValue::get(IGF.IGM.Int8PtrTy));
llvm::BasicBlock *contBB = llvm::BasicBlock::Create(IGF.IGM.getLLVMContext());
IGF.Builder.emitBlock(contBB);
return;
}

if (Builtin.ID == BuiltinValueKind::GetEnumTag) {
auto arg = args.claimNext();
auto ty = argTypes[0];
Expand Down
1 change: 1 addition & 0 deletions lib/SIL/IR/OperandOwnership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, SSubOver)
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, StackAlloc)
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, UnprotectedStackAlloc)
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, StackDealloc)
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, AllocVector)
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, SToSCheckedTrunc)
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, SToUCheckedTrunc)
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, Expect)
Expand Down
1 change: 1 addition & 0 deletions lib/SIL/IR/ValueOwnership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ CONSTANT_OWNERSHIP_BUILTIN(None, UToSCheckedTrunc)
CONSTANT_OWNERSHIP_BUILTIN(None, StackAlloc)
CONSTANT_OWNERSHIP_BUILTIN(None, UnprotectedStackAlloc)
CONSTANT_OWNERSHIP_BUILTIN(None, StackDealloc)
CONSTANT_OWNERSHIP_BUILTIN(None, AllocVector)
CONSTANT_OWNERSHIP_BUILTIN(None, SToSCheckedTrunc)
CONSTANT_OWNERSHIP_BUILTIN(None, SToUCheckedTrunc)
CONSTANT_OWNERSHIP_BUILTIN(None, UToUCheckedTrunc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ static bool isBarrier(SILInstruction *inst) {
case BuiltinValueKind::StackAlloc:
case BuiltinValueKind::UnprotectedStackAlloc:
case BuiltinValueKind::StackDealloc:
case BuiltinValueKind::AllocVector:
case BuiltinValueKind::AssumeAlignment:
case BuiltinValueKind::GetEnumTag:
case BuiltinValueKind::InjectEnumTag:
Expand Down
9 changes: 9 additions & 0 deletions test/IRGen/builtins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -861,4 +861,13 @@ func injectEnumTag<T>(_ x: inout T, tag: UInt32) {
Builtin.injectEnumTag(&x, tag._value)
}

// Check that we still support the obsolete allocVector builtin in old Swift.interface files.

// CHECK-LABEL: define {{.*}} swiftcc ptr @"$s8builtins14allocateVector11elementType8capacityBpxm_BwtlF"
// CHECK: trap()
// CHECK: unreachable
func allocateVector<Element>(elementType: Element.Type, capacity: Builtin.Word) -> Builtin.RawPointer {
return Builtin.allocVector(elementType, capacity)
}

// CHECK: ![[R]] = !{i64 0, i64 9223372036854775807}