Skip to content

Commit f5e10e2

Browse files
committed
Get rid of sil instruction and use a builtin for addressOfRawLayout
1 parent 937c85c commit f5e10e2

File tree

11 files changed

+26
-8
lines changed

11 files changed

+26
-8
lines changed

include/swift/AST/Builtins.def

+1-1
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ BUILTIN_MISC_OPERATION_WITH_SILGEN(DistributedActorAsAnyActor, "distributedActor
11121112
/// Returns a raw pointer to the address of the raw layout type. This address is
11131113
/// only valid during a borrow access of the raw layout type or until the value
11141114
/// is either moved or consumed.
1115-
BUILTIN_SIL_OPERATION(AddressOfRawLayout, "addressOfRawLayout", Special)
1115+
BUILTIN_MISC_OPERATION_WITH_SILGEN(AddressOfRawLayout, "addressOfRawLayout", "n", Special)
11161116

11171117
/// Builtins for instrumentation added by sanitizers during SILGen.
11181118
#ifndef BUILTIN_SANITIZER_OPERATION

include/swift/SIL/AddressWalker.h

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ TransitiveAddressWalker<Impl>::walk(SILValue projectedAddress) && {
276276
case BuiltinValueKind::ZeroInitializer:
277277
case BuiltinValueKind::GetEnumTag:
278278
case BuiltinValueKind::InjectEnumTag:
279+
case BuiltinValueKind::AddressOfRawLayout:
279280
callVisitUse(op);
280281
continue;
281282
default:

lib/IRGen/GenBuiltin.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1477,5 +1477,12 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
14771477
return;
14781478
}
14791479

1480+
if (Builtin.ID == BuiltinValueKind::AddressOfRawLayout) {
1481+
auto addr = args.claimNext();
1482+
auto value = IGF.Builder.CreateBitCast(addr, IGF.IGM.Int8PtrTy);
1483+
out.add(value);
1484+
return;
1485+
}
1486+
14801487
llvm_unreachable("IRGen unimplemented for this builtin!");
14811488
}

lib/SIL/IR/OperandOwnership.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, TargetOSVersionAtLeast)
892892
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, GetEnumTag)
893893
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, InjectEnumTag)
894894
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, DistributedActorAsAnyActor)
895+
BUILTIN_OPERAND_OWNERSHIP(InstantaneousUse, AddressOfRawLayout)
895896
OperandOwnership OperandOwnershipBuiltinClassifier::visitCopy(BuiltinInst *bi,
896897
StringRef) {
897898
if (bi->getFunction()->getConventions().useLoweredAddresses()) {

lib/SIL/IR/ValueOwnership.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ CONSTANT_OWNERSHIP_BUILTIN(None, GetEnumTag)
620620
CONSTANT_OWNERSHIP_BUILTIN(None, InjectEnumTag)
621621
CONSTANT_OWNERSHIP_BUILTIN(Owned, DistributedActorAsAnyActor)
622622
CONSTANT_OWNERSHIP_BUILTIN(Guaranteed, ExtractFunctionIsolation) // unreachable
623+
CONSTANT_OWNERSHIP_BUILTIN(None, AddressOfRawLayout)
623624

624625
#undef CONSTANT_OWNERSHIP_BUILTIN
625626

lib/SIL/Utils/MemAccessUtils.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -2667,6 +2667,7 @@ static void visitBuiltinAddress(BuiltinInst *builtin,
26672667
// These builtins take a generic 'T' as their operand.
26682668
case BuiltinValueKind::GetEnumTag:
26692669
case BuiltinValueKind::InjectEnumTag:
2670+
case BuiltinValueKind::AddressOfRawLayout:
26702671
visitor(&builtin->getAllOperands()[0]);
26712672
return;
26722673

lib/SIL/Verifier/SILVerifier.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,12 @@ struct ImmutableAddressUseVerifier {
600600
if (builtinKind == BuiltinValueKind::GetEnumTag) {
601601
return false;
602602
}
603+
604+
// The optimizer cannot reason about a raw layout type's address due
605+
// to it not respecting formal access scopes.
606+
if (builtinKind == BuiltinValueKind::AddressOfRawLayout) {
607+
return false;
608+
}
603609
}
604610

605611
// Otherwise this is a builtin that we are not expecting to see, so bail

lib/SILOptimizer/Transforms/AccessEnforcementReleaseSinking.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ static bool isBarrier(SILInstruction *inst) {
159159
case BuiltinValueKind::GetEnumTag:
160160
case BuiltinValueKind::InjectEnumTag:
161161
case BuiltinValueKind::ExtractFunctionIsolation:
162+
case BuiltinValueKind::AddressOfRawLayout:
162163
return false;
163164

164165
// Handle some rare builtins that may be sensitive to object lifetime

test/IRGen/raw_layout.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ entry(%K: $*Keymaster):
160160
return undef : $()
161161
}
162162

163-
// CHECK: define swiftcc ptr @get_cell_addr(ptr %"Cell<T>", ptr {{.*}} swiftself [[SELF:%.*]])
163+
// CHECK: define {{.*}}swiftcc ptr @get_cell_addr(ptr %"Cell<T>", ptr {{.*}} swiftself [[SELF:%.*]])
164164
// CHECK-NEXT: entry:
165165
// CHECK-NEXT: ret ptr [[SELF]]
166166
sil @get_cell_addr : $@convention(method) <T> (@in_guaranteed Cell<T>) -> UnsafeMutablePointer<T> {
167167
entry(%0 : $*Cell<T>):
168-
%1 = raw_layout_address_to_pointer %0 : $*Cell<T> to $Builtin.RawPointer
168+
%1 = builtin "addressOfRawLayout"(%0 : $*Cell<T>) : $Builtin.RawPointer
169169
%2 = struct $UnsafeMutablePointer<T> (%1 : $Builtin.RawPointer)
170170
return %2 : $UnsafeMutablePointer<T>
171171
}

test/SILGen/raw_layout.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct Lock: ~Copyable {
2626
@_rawLayout(like: T)
2727
struct Cell<T>: ~Copyable {
2828
// CHECK-LABEL: sil {{.*}} @$s10raw_layout4CellV7addressSpyxGvg : $@convention(method) <T> (@in_guaranteed Cell<T>) -> UnsafeMutablePointer<T> {
29-
// CHECK: [[RAW_LAYOUT_ADDR:%.*]] = raw_layout_address_to_pointer {{%.*}} : $*Cell<T> to $Builtin.RawPointer
29+
// CHECK: {{%.*}} = builtin "addressOfRawLayout"<Cell<T>>({{%.*}} : $*Cell<T>) : $Builtin.RawPointer
3030
// CHECK-LABEL: } // end sil function '$s10raw_layout4CellV7addressSpyxGvg'
3131
var address: UnsafeMutablePointer<T> {
3232
.init(Builtin.addressOfRawLayout(self))

test/SILOptimizer/stdlib/Cell.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Builtin
77
public struct Cell<T: ~Copyable>: ~Copyable {
88
// CHECK-LABEL: sil {{.*}} @$s4CellAAVAARiczrlE7addressSpyxGvg : $@convention(method) <T where T : ~Copyable> (@in_guaranteed Cell<T>) -> UnsafeMutablePointer<T> {
99
// CHECK: bb0([[SELF:%.*]] : $*Cell<T>):
10-
// CHECK: [[RAW_LAYOUT_ADDR:%.*]] = raw_layout_address_to_pointer [[SELF]] : $*Cell<T> to $Builtin.RawPointer
10+
// CHECK: [[RAW_LAYOUT_ADDR:%.*]] = builtin "addressOfRawLayout"<Cell<T>>([[SELF]] : $*Cell<T>) : $Builtin.RawPointer
1111
// CHECK-NEXT: [[POINTER:%.*]] = struct $UnsafeMutablePointer<T> ([[RAW_LAYOUT_ADDR]] : $Builtin.RawPointer)
1212
// CHECK-NEXT: return [[POINTER]] : $UnsafeMutablePointer<T>
1313
// CHECK-LABEL: } // end sil function '$s4CellAAVAARiczrlE7addressSpyxGvg'
@@ -17,9 +17,9 @@ public struct Cell<T: ~Copyable>: ~Copyable {
1717
}
1818

1919
// CHECK-LABEL: sil {{.*}} @$s4CellAAVAARiczrlEyAByxGxcfC : $@convention(method) <T where T : ~Copyable> (@in T, @thin Cell<T>.Type) -> @out Cell<T> {
20-
// CHECK: bb0([[SELF:%.*]] : $*Cell<T>, [[VALUE:%.*]] : $*T, {{%.*}} : $@thin Cell<T>.Type):
21-
// CHECK-NEXT: {{%.*}} = builtin "zeroInitializer"<Cell<T>>([[SELF]] : $*Cell<T>) : $()
22-
// CHECK-NEXT: [[RAW_LAYOUT_ADDR:%.*]] = raw_layout_address_to_pointer [[SELF]] : $*Cell<T> to $Builtin.RawPointer
20+
// CHECK: bb0({{%.*}} : $*Cell<T>, [[VALUE:%.*]] : $*T, {{%.*}} : $@thin Cell<T>.Type):
21+
// CHECK: {{%.*}} = builtin "zeroInitializer"<Cell<T>>([[SELF:%.*]] : $*Cell<T>) : $()
22+
// CHECK-NEXT: [[RAW_LAYOUT_ADDR:%.*]] = builtin "addressOfRawLayout"<Cell<T>>([[SELF]] : $*Cell<T>) : $Builtin.RawPointer
2323
// CHECK-NEXT: [[POINTER:%.*]] = struct $UnsafeMutablePointer<T> ([[RAW_LAYOUT_ADDR]] : $Builtin.RawPointer)
2424
// Calling 'UnsafeMutablePointer<T>.initialize(to:)'
2525
// CHECK: {{%.*}} = apply {{%.*}}<T>([[VALUE]], [[POINTER]])

0 commit comments

Comments
 (0)