Skip to content

Commit 0d34bc2

Browse files
committed
IRGen: Emit reflection metadata for certain builtin types when -enable-reflection-builtins flag is passed in
These types are not directly referenced as fields of aggregate types, but are needed for reflection type lowering. Also, use a SetVector to collect referenced builtin types, instead of a SmallPtrSet, to ensure compiler output is deterministic.
1 parent 02a6be6 commit 0d34bc2

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ function(_compile_swift_files dependency_target_out_var_name)
406406
if(SWIFTFILE_IS_STDLIB_CORE)
407407
list(APPEND swift_flags
408408
"-nostdimport" "-parse-stdlib" "-module-name" "Swift")
409+
list(APPEND swift_flags
410+
"-Xfrontend" "-enable-reflection-builtins")
409411
list(APPEND swift_flags "-Xfrontend" "-group-info-path"
410412
"-Xfrontend" "${GROUP_INFO_JSON_FILE}")
411413
if (NOT SWIFT_STDLIB_ENABLE_RESILIENCE)

include/swift/AST/IRGenOptions.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ class IRGenOptions {
146146
/// Emit names of struct stored properties and enum cases.
147147
unsigned EnableReflectionNames : 1;
148148

149+
/// Emit metadata for certain builtin types. Only for use by
150+
/// standard library.
151+
unsigned EnableReflectionBuiltins : 1;
152+
149153
/// List of backend command-line options for -embed-bitcode.
150154
std::vector<uint8_t> CmdArgs;
151155

@@ -166,7 +170,8 @@ class IRGenOptions {
166170
PrintInlineTree(false), EmbedMode(IRGenEmbedMode::None),
167171
HasValueNamesSetting(false), ValueNames(false),
168172
EnableReflectionMetadata(false), EnableReflectionNames(false),
169-
CmdArgs(), UseIncrementalLLVMCodeGen(true)
173+
EnableReflectionBuiltins(false), CmdArgs(),
174+
UseIncrementalLLVMCodeGen(true)
170175
{}
171176

172177
/// Gets the name of the specified output filename.

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ def enable_reflection_names : Flag<["-"], "enable-reflection-names">,
197197
HelpText<"Enable emission of names of stored properties and enum cases in"
198198
"reflection metadata">;
199199

200+
def enable_reflection_builtins : Flag<["-"], "enable-reflection-builtins">,
201+
HelpText<"Enable emission of reflection metadata for builtin types"
202+
" (standard library only)">;
203+
200204
def stack_promotion_checks : Flag<["-"], "emit-stack-promotion-checks">,
201205
HelpText<"Emit runtime checks for correct stack promotion of objects.">;
202206

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,9 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
12821282
if (Args.hasArg(OPT_enable_reflection_names)) {
12831283
Opts.EnableReflectionNames = true;
12841284
}
1285+
if (Args.hasArg(OPT_enable_reflection_builtins)) {
1286+
Opts.EnableReflectionBuiltins = true;
1287+
}
12851288
}
12861289

12871290
return false;

lib/IRGen/GenReflection.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using namespace irgen;
3131

3232
class ReflectionMetadataBuilder : public ConstantBuilder<> {
3333
protected:
34-
SmallPtrSetImpl<CanType> &BuiltinTypes;
34+
llvm::SetVector<CanType> &BuiltinTypes;
3535

3636
// Collect any builtin types referenced from this type.
3737
void addBuiltinTypeRefs(CanType type) {
@@ -54,7 +54,7 @@ class ReflectionMetadataBuilder : public ConstantBuilder<> {
5454

5555
public:
5656
ReflectionMetadataBuilder(IRGenModule &IGM,
57-
SmallPtrSetImpl<CanType> &BuiltinTypes)
57+
llvm::SetVector<CanType> &BuiltinTypes)
5858
: ConstantBuilder(IGM), BuiltinTypes(BuiltinTypes) {}
5959
};
6060

@@ -127,7 +127,7 @@ class AssociatedTypeMetadataBuilder : public ReflectionMetadataBuilder {
127127
AssociatedTypeMetadataBuilder(IRGenModule &IGM,
128128
ArrayRef<const NominalTypeDecl *> NominalTypeDecls,
129129
ArrayRef<const ExtensionDecl *> ExtensionDecls,
130-
SmallPtrSetImpl<CanType> &BuiltinTypes)
130+
llvm::SetVector<CanType> &BuiltinTypes)
131131
: ReflectionMetadataBuilder(IGM, BuiltinTypes),
132132
NominalTypeDecls(NominalTypeDecls),
133133
ExtensionDecls(ExtensionDecls) {}
@@ -237,7 +237,7 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
237237
public:
238238
FieldTypeMetadataBuilder(IRGenModule &IGM,
239239
ArrayRef<const NominalTypeDecl *> NominalTypeDecls,
240-
SmallPtrSetImpl<CanType> &BuiltinTypes)
240+
llvm::SetVector<CanType> &BuiltinTypes)
241241
: ReflectionMetadataBuilder(IGM, BuiltinTypes),
242242
NominalTypeDecls(NominalTypeDecls) {}
243243

@@ -273,7 +273,7 @@ class BuiltinTypeMetadataBuilder : public ReflectionMetadataBuilder {
273273
void addBuiltinType(CanType builtinType) {
274274
addTypeRef(builtinType->getASTContext().TheBuiltinModule, builtinType);
275275

276-
auto &ti = cast<LoadableTypeInfo>(IGM.getTypeInfoForUnlowered(builtinType));
276+
auto &ti = cast<FixedTypeInfo>(IGM.getTypeInfoForUnlowered(builtinType));
277277
addConstantInt32(ti.getFixedSize().getValue());
278278
addConstantInt32(ti.getFixedAlignment().getValue());
279279
addConstantInt32(ti.getFixedStride().getValue());
@@ -288,7 +288,7 @@ class BuiltinTypeMetadataBuilder : public ReflectionMetadataBuilder {
288288

289289
public:
290290
BuiltinTypeMetadataBuilder(IRGenModule &IGM,
291-
SmallPtrSetImpl<CanType> &BuiltinTypes)
291+
llvm::SetVector<CanType> &BuiltinTypes)
292292
: ReflectionMetadataBuilder(IGM, BuiltinTypes) {}
293293

294294
llvm::GlobalVariable *emit() {
@@ -381,15 +381,16 @@ llvm::Constant *IRGenModule::getAddrOfStringForTypeRef(StringRef Str) {
381381

382382
void IRGenModule::emitReflectionMetadataRecords() {
383383
auto DoNotHaveDecls = NominalTypeDecls.empty() && ExtensionDecls.empty();
384-
if (!Opts.EnableReflectionMetadata || DoNotHaveDecls)
384+
if (!Opts.EnableReflectionMetadata ||
385+
(!Opts.EnableReflectionBuiltins && DoNotHaveDecls))
385386
return;
386387

387388
// We collect all referenced builtin types and emit records for them.
388389
// In practice only the standard library should directly reference
389390
// builtin types.
390391
//
391392
// FIXME: This metadata should be in the runtime instead.
392-
SmallPtrSet<CanType, 4> BuiltinTypes;
393+
llvm::SetVector<CanType> BuiltinTypes;
393394

394395
{
395396
FieldTypeMetadataBuilder builder(*this, NominalTypeDecls, BuiltinTypes);
@@ -408,7 +409,13 @@ void IRGenModule::emitReflectionMetadataRecords() {
408409
addUsedGlobal(var);
409410
}
410411

411-
{
412+
if (Opts.EnableReflectionBuiltins) {
413+
BuiltinTypes.insert(Context.TheNativeObjectType);
414+
BuiltinTypes.insert(Context.TheUnknownObjectType);
415+
BuiltinTypes.insert(Context.TheBridgeObjectType);
416+
BuiltinTypes.insert(Context.TheRawPointerType);
417+
BuiltinTypes.insert(Context.TheUnsafeValueBufferType);
418+
412419
BuiltinTypeMetadataBuilder builder(*this, BuiltinTypes);
413420
auto var = builder.emit();
414421
if (var)

0 commit comments

Comments
 (0)