Skip to content

[IRGen][interop] do not add 'nocapture' to not bitwise takable types #68481

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
Sep 26, 2023
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
34 changes: 23 additions & 11 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ static void addIndirectValueParameterAttributes(IRGenModule &IGM,
llvm::AttrBuilder b(IGM.getLLVMContext());
// Value parameter pointers can't alias or be captured.
b.addAttribute(llvm::Attribute::NoAlias);
b.addAttribute(llvm::Attribute::NoCapture);
// Bitwise takable value types are guaranteed not to capture
// a pointer into itself.
if (ti.isBitwiseTakable(ResilienceExpansion::Maximal))
b.addAttribute(llvm::Attribute::NoCapture);
// The parameter must reference dereferenceable memory of the type.
addDereferenceableAttributeToBuilder(IGM, b, ti);

Expand All @@ -278,9 +281,11 @@ static void addPackParameterAttributes(IRGenModule &IGM,
llvm::AttributeList &attrs,
unsigned argIndex) {
llvm::AttrBuilder b(IGM.getLLVMContext());
// Pack parameter pointers can't alias or be captured.
// Pack parameter pointers can't alias.
// Note: they are not marked `nocapture` as one
// pack parameter could be a value type (e.g. a C++ type)
// that captures its own pointer in itself.
b.addAttribute(llvm::Attribute::NoAlias);
b.addAttribute(llvm::Attribute::NoCapture);
// TODO: we could mark this dereferenceable when the pack has fixed
// components.
// TODO: add an alignment attribute
Expand All @@ -301,8 +306,10 @@ static void addInoutParameterAttributes(IRGenModule &IGM, SILType paramSILType,
// attribute if it's a pointer being passed inout.
b.addAttribute(llvm::Attribute::NoAlias);
}
// Aliasing inouts can't be captured without doing unsafe stuff.
b.addAttribute(llvm::Attribute::NoCapture);
// Bitwise takable value types are guaranteed not to capture
// a pointer into itself.
if (ti.isBitwiseTakable(ResilienceExpansion::Maximal))
b.addAttribute(llvm::Attribute::NoCapture);
// The inout must reference dereferenceable memory of the type.
addDereferenceableAttributeToBuilder(IGM, b, ti);

Expand Down Expand Up @@ -341,10 +348,14 @@ llvm::CallingConv::ID irgen::expandCallingConv(IRGenModule &IGM,
static void addIndirectResultAttributes(IRGenModule &IGM,
llvm::AttributeList &attrs,
unsigned paramIndex, bool allowSRet,
llvm::Type *storageType) {
llvm::Type *storageType,
const TypeInfo &typeInfo) {
llvm::AttrBuilder b(IGM.getLLVMContext());
b.addAttribute(llvm::Attribute::NoAlias);
b.addAttribute(llvm::Attribute::NoCapture);
// Bitwise takable value types are guaranteed not to capture
// a pointer into itself.
if (typeInfo.isBitwiseTakable(ResilienceExpansion::Maximal))
b.addAttribute(llvm::Attribute::NoCapture);
if (allowSRet) {
assert(storageType);
b.addStructRetAttr(storageType);
Expand Down Expand Up @@ -509,7 +520,7 @@ llvm::Type *SignatureExpansion::addIndirectResult() {
const TypeInfo &resultTI = IGM.getTypeInfo(resultType);
auto storageTy = resultTI.getStorageType();
addIndirectResultAttributes(IGM, Attrs, ParamIRTypes.size(), claimSRet(),
storageTy);
storageTy, resultTI);
addPointerParameter(storageTy);
return IGM.VoidTy;
}
Expand Down Expand Up @@ -568,11 +579,12 @@ void SignatureExpansion::expandIndirectResults() {
auto useSRet = claimSRet();
// We need to use opaque types or non fixed size storage types because llvm
// does type based analysis based on the type of sret arguments.
if (useSRet && !isa<FixedTypeInfo>(IGM.getTypeInfo(indirectResultType))) {
const TypeInfo &typeInfo = IGM.getTypeInfo(indirectResultType);
if (useSRet && !isa<FixedTypeInfo>(typeInfo)) {
storageTy = IGM.OpaqueTy;
}
addIndirectResultAttributes(IGM, Attrs, ParamIRTypes.size(), useSRet,
storageTy);
storageTy, typeInfo);
addPointerParameter(storageTy);
}
}
Expand Down Expand Up @@ -1459,7 +1471,7 @@ void SignatureExpansion::expandExternalSignatureTypes() {
param, IGM.getMaximalTypeExpansionContext());
auto &paramTI = cast<FixedTypeInfo>(IGM.getTypeInfo(paramTy));
addIndirectResultAttributes(IGM, Attrs, getCurParamIndex(), claimSRet(),
paramTI.getStorageType());
paramTI.getStorageType(), paramTI);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/AutoDiff/IRGen/runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func test_context_builtins_with_type<T>(t: T) {
UnsafeMutableRawPointer(newBuffer).storeBytes(of: t, as: T.self)
}

// CHECK-LABEL: define{{.*}}@test_context_builtins_with_type(ptr noalias nocapture %0, ptr %T)
// CHECK-LABEL: define{{.*}}@test_context_builtins_with_type(ptr noalias %0, ptr %T)
// CHECK: entry:
// CHECK: [[CTX:%.*]] = call swiftcc ptr @swift_autoDiffCreateLinearMapContextWithType(ptr %T)
// CHECK: call swiftcc ptr @swift_autoDiffProjectTopLevelSubcontext(ptr [[CTX]])
Expand Down
2 changes: 1 addition & 1 deletion test/DebugInfo/inlined-generics-basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class C<R> {

// SIL-LABEL: // C.f<A>(_:)
// IR-LABEL: define {{.*}} @"$s1A1CC1fyyqd__lF"
// IR-SAME: nocapture %[[ARG_0:.*]], {{.*}} %[[ARG_S:.*]],
// IR-SAME: %[[ARG_0:.*]], {{.*}} %[[ARG_S:.*]],
#sourceLocation(file: "f.swift", line: 1)
public func f<S>(_ s: S) {
// SIL: debug_value %0 : $*S, let, name "s", argno 1, expr op_deref, {{.*}} scope [[F]]
Expand Down
4 changes: 2 additions & 2 deletions test/DebugInfo/move_function_dbginfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public func copyableVarArgTest(_ k: inout Klass) {
k.doSomething()
}

// CHECK-LABEL: define swiftcc void @"$s21move_function_dbginfo20addressOnlyValueTestyyxAA1PRzlF"(ptr noalias nocapture %0, ptr %T, ptr %T.P)
// CHECK-LABEL: define swiftcc void @"$s21move_function_dbginfo20addressOnlyValueTestyyxAA1PRzlF"(ptr noalias %0, ptr %T, ptr %T.P)
// CHECK: @llvm.dbg.addr(metadata ptr %{{.*}}, metadata ![[K_ADDR_LET_METADATA:[0-9]+]], metadata !DIExpression()), !dbg ![[ADDR_LOC:[0-9]*]]
// CHECK-NEXT: br
// CHECK: @llvm.dbg.value(metadata ptr undef, metadata ![[K_ADDR_LET_METADATA]], metadata !DIExpression()), !dbg ![[ADDR_LOC]]
Expand Down Expand Up @@ -308,7 +308,7 @@ public func addressOnlyValueArgTest<T : P>(_ k: __owned T) {
m.doSomething()
}

// CHECK-LABEL: define swiftcc void @"$s21move_function_dbginfo18addressOnlyVarTestyyxAA1PRzlF"(ptr noalias nocapture %0, ptr %T, ptr %T.P)
// CHECK-LABEL: define swiftcc void @"$s21move_function_dbginfo18addressOnlyVarTestyyxAA1PRzlF"(ptr noalias %0, ptr %T, ptr %T.P)
// CHECK: @llvm.dbg.addr(metadata ptr %{{.*}}, metadata ![[K_ADDRONLY_VAR_METADATA:[0-9]+]], metadata !DIExpression()), !dbg ![[ADDR_LOC:[0-9]*]]
// CHECK-NEXT: br
// CHECK: @llvm.dbg.value(metadata ptr undef, metadata ![[K_ADDRONLY_VAR_METADATA]], metadata !DIExpression()), !dbg ![[ADDR_LOC]]
Expand Down
4 changes: 2 additions & 2 deletions test/DebugInfo/struct_resilience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
//
import resilient_struct

// CHECK-LABEL: define{{.*}} swiftcc void @"$s17struct_resilience9takesSizeyy010resilient_A00D0VF"(ptr noalias nocapture %0)
// CHECK-LLDB-LABEL: define{{.*}} swiftcc void @"$s17struct_resilience9takesSizeyy010resilient_A00D0VF"(ptr noalias nocapture dereferenceable({{8|16}}) %0)
// CHECK-LABEL: define{{.*}} swiftcc void @"$s17struct_resilience9takesSizeyy010resilient_A00D0VF"(ptr noalias %0)
// CHECK-LLDB-LABEL: define{{.*}} swiftcc void @"$s17struct_resilience9takesSizeyy010resilient_A00D0VF"(ptr noalias dereferenceable({{8|16}}) %0)
public func takesSize(_ s: Size) {}


Expand Down
14 changes: 7 additions & 7 deletions test/Distributed/distributed_actor_accessor_thunks_64bit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public distributed actor MyOtherActor {

// CHECK: define hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7simple1yySiYaKFTE"

// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7simple1yySiYaKFTETF"(ptr swiftasync %0, ptr nocapture %1, ptr %2, ptr %3, {{.*}}, ptr [[ACTOR:%.*]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7simple1yySiYaKFTETF"(ptr swiftasync %0, ptr %1, ptr %2, ptr %3, {{.*}}, ptr [[ACTOR:%.*]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])

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

Expand All @@ -116,7 +116,7 @@ public distributed actor MyOtherActor {
// CHECK: missing-witness1:
// CHECK-NEXT: call void @llvm.trap()
// CHECK-NEXT: unreachable
// CHECK: call swiftcc void @"$s27FakeDistributedActorSystems0A17InvocationDecoderC18decodeNextArgumentxyKSeRzSERzlF"(ptr noalias nocapture sret(%swift.opaque) [[ARG_0_VALUE_BUF]], ptr %arg_type, ptr [[ENCODABLE_WITNESS]], ptr [[DECODABLE_WITNESS]], ptr swiftself [[DECODER]], ptr noalias nocapture swifterror dereferenceable(8) %swifterror)
// CHECK: call swiftcc void @"$s27FakeDistributedActorSystems0A17InvocationDecoderC18decodeNextArgumentxyKSeRzSERzlF"(ptr noalias sret(%swift.opaque) [[ARG_0_VALUE_BUF]], ptr %arg_type, ptr [[ENCODABLE_WITNESS]], ptr [[DECODABLE_WITNESS]], ptr swiftself [[DECODER]], ptr noalias nocapture swifterror dereferenceable(8) %swifterror)

// CHECK: store ptr null, ptr %swifterror
// CHECK-NEXT: %._value = getelementptr inbounds %TSi, ptr [[ARG_0_VALUE_BUF]], i32 0, i32 0
Expand Down Expand Up @@ -192,7 +192,7 @@ public distributed actor MyOtherActor {
// CHECK: define hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7simple3ySiSSYaKFTE"

/// !!! in `simple3` interesting bits are: argument value extraction (because string is exploded into N arguments) and call to distributed thunk
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7simple3ySiSSYaKFTETF"(ptr swiftasync %0, ptr nocapture [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr [[ACTOR]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7simple3ySiSSYaKFTETF"(ptr swiftasync %0, ptr [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr [[ACTOR]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])


// CHECK: [[ARG_SIZE:%.*]] = and i64 {{.*}}, -16
Expand Down Expand Up @@ -232,7 +232,7 @@ public distributed actor MyOtherActor {

// CHECK: define hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC16single_case_enumyAA7SimpleEOAFYaKFTE"

// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC16single_case_enumyAA7SimpleEOAFYaKFTETF"(ptr swiftasync %0, ptr nocapture [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr [[ACTOR]], ptr [[DECODER_TYPE]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC16single_case_enumyAA7SimpleEOAFYaKFTETF"(ptr swiftasync %0, ptr [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr [[ACTOR]], ptr [[DECODER_TYPE]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])

/// Let's check that the call doesn't have any arguments and returns nothing.

Expand Down Expand Up @@ -272,7 +272,7 @@ public distributed actor MyOtherActor {

// CHECK: define hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtYaKFTE"

// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtYaKFTETF"(ptr swiftasync {{.*}}, ptr nocapture [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr [[ACTOR]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtYaKFTETF"(ptr swiftasync {{.*}}, ptr [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr [[ACTOR]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])

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

Expand Down Expand Up @@ -319,7 +319,7 @@ public distributed actor MyOtherActor {

/// ---> Accessor for `genericArgs`

// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC11genericArgsyyx_Sayq_GtYaKSeRzSERzSeR_SER_r0_lFTETF"(ptr swiftasync %0, ptr nocapture [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUF:%.*]], ptr [[GENERIC_SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr [[ACTOR:%.*]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC11genericArgsyyx_Sayq_GtYaKSeRzSERzSeR_SER_r0_lFTETF"(ptr swiftasync %0, ptr [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUF:%.*]], ptr [[GENERIC_SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr [[ACTOR:%.*]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])

/// ---> Load `T`

Expand Down Expand Up @@ -368,7 +368,7 @@ public distributed actor MyOtherActor {

/// Let's check that there is argument decoding since parameter list is empty

// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors12MyOtherActorC5emptyyyYaKFTETF"(ptr swiftasync {{.*}}, ptr nocapture [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr {{.*}}, ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors12MyOtherActorC5emptyyyYaKFTETF"(ptr swiftasync {{.*}}, ptr [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr {{.*}}, ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])
// CHECK-NEXT: entry:
// CHECK-NEXT: {{.*}} = alloca ptr
// CHECK-NEXT: %swifterror = alloca swifterror ptr
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/TestABIInaccessible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public struct AnotherType<T> {
}

// Don't pass the metadata of Private<T> to AnotherType<T>'s outlined destroy.
// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc void @"$s4main4copyyAA11AnotherTypeVyxGAElF"(ptr noalias nocapture sret({{.*}}) %0, ptr noalias nocapture %1, ptr %T)
// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} swiftcc void @"$s4main4copyyAA11AnotherTypeVyxGAElF"(ptr noalias sret({{.*}}) %0, ptr noalias %1, ptr %T)
// CHECK: [[MD:%.*]] = call swiftcc %swift.metadata_response @"$s4main11AnotherTypeVMa"(i{{.*}} 0, ptr %T)
// CHECK: [[MD1:%.*]] = extractvalue %swift.metadata_response [[MD]], 0
// CHECK: [[MD2:%.*]] = call swiftcc %swift.metadata_response @"$s4main6PublicVMa"(i{{.*}} 0, ptr %T)
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/archetype_resilience.sil
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum EnumWithClassArchetypeAndDynamicSize<T : AnyObject> {
case B(Size)
}

// CHECK-LABEL: define swiftcc void @copyDynamicMultiEnum(ptr %"EnumWithClassArchetypeAndDynamicSize<T>", ptr %U, ptr noalias nocapture swiftself %0)
// CHECK-LABEL: define swiftcc void @copyDynamicMultiEnum(ptr %"EnumWithClassArchetypeAndDynamicSize<T>", ptr %U, ptr noalias swiftself %0)
// CHECK: call ptr @"$s20archetype_resilience36EnumWithClassArchetypeAndDynamicSizeOyxGRlzCr0_lWOc"(ptr %0, ptr {{.*}}, ptr %"EnumWithClassArchetypeAndDynamicSize<T>")
// CHECK: ret void
sil [ossa] @copyDynamicMultiEnum : $@convention(method) <T, U where T: AnyObject> (@in_guaranteed EnumWithClassArchetypeAndDynamicSize<T>) -> () {
Expand Down
Loading