Skip to content

Commit 3f729c9

Browse files
committed
[SILGen] Add Builtin.storeRaw.
The new builtin performs a store to unaligned memory.
1 parent 1a2af60 commit 3f729c9

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

include/swift/AST/Builtins.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,12 @@ BUILTIN_SIL_OPERATION(Assign, "assign", Special)
261261
/// Init has type (T, Builtin.RawPointer) -> ()
262262
BUILTIN_SIL_OPERATION(Init, "initialize", Special)
263263

264+
/// StoreRaw has type (T, Builtin.RawPointer) -> ()
265+
/// Stores a T to raw memory.
266+
/// Its address does not adhere to strict aliasing.
267+
/// See also LoadRaw.
268+
BUILTIN_SIL_OPERATION(StoreRaw, "storeRaw", Special)
269+
264270
/// CastToNativeObject has type (T) -> Builtin.NativeObject.
265271
///
266272
/// This builtin asserts if the underlying type /could/ be objc.

lib/AST/Builtins.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,6 +2778,7 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
27782778

27792779
case BuiltinValueKind::Assign:
27802780
case BuiltinValueKind::Init:
2781+
case BuiltinValueKind::StoreRaw:
27812782
if (!Types.empty()) return nullptr;
27822783
return getStoreOperation(Context, Id);
27832784

lib/SILGen/SILGenBuiltin.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ static ManagedValue emitBuiltinAssign(SILGenFunction &SGF, SILLocation loc,
240240
/*isInvariant=*/false, llvm::MaybeAlign());
241241
}
242242

243+
static ManagedValue emitBuiltinStoreRaw(SILGenFunction &SGF, SILLocation loc,
244+
SubstitutionMap substitutions,
245+
ArrayRef<ManagedValue> args,
246+
SGFContext C) {
247+
return emitBuiltinStore(SGF, loc, substitutions, args, C, /*isStrict=*/false,
248+
/*isInvariant=*/false, llvm::MaybeAlign(1));
249+
}
250+
243251
/// Emit Builtin.initialize by evaluating the operand directly into
244252
/// the address.
245253
static ManagedValue emitBuiltinInit(SILGenFunction &SGF,

test/SILOptimizer/builtins.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-swift-frontend -enable-builtin-module -O -emit-sil %s | %FileCheck %s
2+
3+
import Builtin
4+
5+
// CHECK-LABEL: sil @storeBytes_BitwiseCopyable : {{.*}} {
6+
// CHECK: bb0(
7+
// CHECK-SAME: [[SELF:%[^,]+]] : $UnsafeMutableRawPointer,
8+
// CHECK-SAME: [[VALUE:%[^,]+]] : $*T,
9+
// CHECK-SAME: [[OFFSET:%[^,]+]] : $Int,
10+
// CHECK-SAME: {{%[^,]+}} : $@thick T.Type):
11+
// CHECK: [[SELF_RAW_VALUE:%[^,]+]] = struct_extract [[SELF]] : {{.*}} #UnsafeMutableRawPointer._rawValue
12+
// CHECK: [[OFFSET_VALUE:%[^,]+]] = struct_extract [[OFFSET]] : {{.*}} #Int._value
13+
// CHECK: [[OFFSET_WORD:%[^,]+]] = builtin "truncOrBitCast_Int64_Word"([[OFFSET_VALUE]] : $Builtin.Int64)
14+
// CHECK: [[DESTINATION_POINTER:%[^,]+]] = index_raw_pointer [[SELF_RAW_VALUE]] : $Builtin.RawPointer, [[OFFSET_WORD]]
15+
// CHECK: [[DESTINATION:%[^,]+]] = pointer_to_address [[DESTINATION_POINTER]] : $Builtin.RawPointer to [align=1]
16+
// CHECK: copy_addr [[VALUE]] to [[DESTINATION]]
17+
// CHECK-LABEL: } // end sil function 'storeBytes_BitwiseCopyable'
18+
@_silgen_name("storeBytes_BitwiseCopyable")
19+
public func storeBytes<T : _BitwiseCopyable>(
20+
_ self: UnsafeMutableRawPointer,
21+
of value: T, toByteOffset offset: Int = 0, as type: T.Type
22+
) {
23+
Builtin.storeRaw(value, (self + offset)._rawValue)
24+
}

0 commit comments

Comments
 (0)