Skip to content

Commit ac7a478

Browse files
authored
Merge pull request #65354 from eeckstein/fix-sizeof-builtin
fix `Builtin.sizeof` et al for metatypes
2 parents 8f99736 + f5a0519 commit ac7a478

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

lib/IRGen/GenBuiltin.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ getLoweredTypeAndTypeInfo(IRGenModule &IGM, Type unloweredType) {
120120
return {lowered, IGM.getTypeInfo(lowered)};
121121
}
122122

123+
static std::pair<SILType, const TypeInfo &>
124+
getMaximallyAbstractedLoweredTypeAndTypeInfo(IRGenModule &IGM, Type unloweredType) {
125+
auto lowered = IGM.getLoweredType(AbstractionPattern::getOpaque(), unloweredType);
126+
return {lowered, IGM.getTypeInfo(lowered)};
127+
}
128+
123129
/// emitBuiltinCall - Emit a call to a builtin function.
124130
void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
125131
BuiltinInst *Inst, ArrayRef<SILType> argTypes,
@@ -144,23 +150,23 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
144150
// These builtins don't care about their argument:
145151
if (Builtin.ID == BuiltinValueKind::Sizeof) {
146152
(void)args.claimAll();
147-
auto valueTy = getLoweredTypeAndTypeInfo(IGF.IGM,
153+
auto valueTy = getMaximallyAbstractedLoweredTypeAndTypeInfo(IGF.IGM,
148154
substitutions.getReplacementTypes()[0]);
149155
out.add(valueTy.second.getSize(IGF, valueTy.first));
150156
return;
151157
}
152158

153159
if (Builtin.ID == BuiltinValueKind::Strideof) {
154160
(void)args.claimAll();
155-
auto valueTy = getLoweredTypeAndTypeInfo(IGF.IGM,
161+
auto valueTy = getMaximallyAbstractedLoweredTypeAndTypeInfo(IGF.IGM,
156162
substitutions.getReplacementTypes()[0]);
157163
out.add(valueTy.second.getStride(IGF, valueTy.first));
158164
return;
159165
}
160166

161167
if (Builtin.ID == BuiltinValueKind::Alignof) {
162168
(void)args.claimAll();
163-
auto valueTy = getLoweredTypeAndTypeInfo(IGF.IGM,
169+
auto valueTy = getMaximallyAbstractedLoweredTypeAndTypeInfo(IGF.IGM,
164170
substitutions.getReplacementTypes()[0]);
165171
// The alignof value is one greater than the alignment mask.
166172
out.add(IGF.Builder.CreateAdd(

lib/SILOptimizer/IRGenTransforms/TargetConstantFolding.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ class TargetConstantFolding : public SILModuleTransform {
141141

142142
const TypeInfo &getTypeInfoOfBuiltin(BuiltinInst *bi, IRGenModule &IGM) {
143143
SubstitutionMap subs = bi->getSubstitutions();
144-
SILType lowered = IGM.getLoweredType(subs.getReplacementTypes()[0]);
144+
SILType lowered = IGM.getLoweredType(AbstractionPattern::getOpaque(),
145+
subs.getReplacementTypes()[0]);
145146
return IGM.getTypeInfo(lowered);
146147
}
147148
};

test/IRGen/builtins.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
// RUN: %target-swift-frontend -module-name builtins -parse-stdlib -disable-access-control -primary-file %s -emit-ir -o - -disable-objc-attr-requires-foundation-module | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%target-runtime
2+
// RUN: %target-swift-frontend -module-name builtins -parse-stdlib -Xllvm -sil-disable-pass=target-constant-folding -disable-access-control -primary-file %s -emit-ir -o - -disable-objc-attr-requires-foundation-module | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%target-runtime
33

4-
// REQUIRES: CPU=x86_64
4+
// REQUIRES: CPU=x86_64 || CPU=arm64 || CPU=arm64e
55

66
import Swift
77

@@ -210,6 +210,16 @@ func sizeof_alignof_test() {
210210

211211
}
212212

213+
// CHECK: define hidden {{.*}}void @"$s8builtins28sizeof_alignof_metatype_testyyF"()
214+
func sizeof_alignof_metatype_test() {
215+
// CHECK: store i64 8, i64*
216+
var xs = Builtin.sizeof(Int.Type.self)
217+
// CHECK: store i64 8, i64*
218+
var xa = Builtin.alignof(Int.Type.self)
219+
// CHECK: store i64 8, i64*
220+
var xt = Builtin.strideof(Int.Type.self)
221+
}
222+
213223
// CHECK: define hidden {{.*}}void @"$s8builtins27generic_sizeof_alignof_testyyxlF"
214224
func generic_sizeof_alignof_test<T>(_: T) {
215225
// CHECK: [[T0:%.*]] = getelementptr inbounds %swift.vwtable, %swift.vwtable* [[T:%.*]], i32 0, i32 8

test/SILOptimizer/target-const-prop.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ func testit() {
7474

7575
// CHECK-OUTPUT: doubleSize: true
7676
print("doubleSize: \(S.doubleSize == getSize(S.self) * 2)")
77+
78+
// CHECK-OUTPUT: metatype-size-1: true
79+
print("metatype-size-1: \(MemoryLayout<S.Type>.size == MemoryLayout<UnsafeRawPointer>.size)")
80+
// CHECK-OUTPUT: metatype-size-2: true
81+
print("metatype-size-2: \(getSize(S.Type.self) == MemoryLayout<UnsafeRawPointer>.size)")
82+
// CHECK-OUTPUT: metatype-alignment: true
83+
print("metatype-alignment: \(getAlignment(S.Type.self) == MemoryLayout<UnsafeRawPointer>.alignment)")
84+
// CHECK-OUTPUT: metatype-stride: true
85+
print("metatype-stride: \(getStride(S.Type.self) == MemoryLayout<UnsafeRawPointer>.stride)")
7786
}
7887

7988
testit()

0 commit comments

Comments
 (0)