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.
This commit is contained in:
Slava Pestov
2016-04-22 22:02:03 -07:00
parent 02a6be6d01
commit 0d34bc21ef
5 changed files with 31 additions and 10 deletions

View File

@@ -406,6 +406,8 @@ function(_compile_swift_files dependency_target_out_var_name)
if(SWIFTFILE_IS_STDLIB_CORE)
list(APPEND swift_flags
"-nostdimport" "-parse-stdlib" "-module-name" "Swift")
list(APPEND swift_flags
"-Xfrontend" "-enable-reflection-builtins")
list(APPEND swift_flags "-Xfrontend" "-group-info-path"
"-Xfrontend" "${GROUP_INFO_JSON_FILE}")
if (NOT SWIFT_STDLIB_ENABLE_RESILIENCE)

View File

@@ -146,6 +146,10 @@ public:
/// Emit names of struct stored properties and enum cases.
unsigned EnableReflectionNames : 1;
/// Emit metadata for certain builtin types. Only for use by
/// standard library.
unsigned EnableReflectionBuiltins : 1;
/// List of backend command-line options for -embed-bitcode.
std::vector<uint8_t> CmdArgs;
@@ -166,7 +170,8 @@ public:
PrintInlineTree(false), EmbedMode(IRGenEmbedMode::None),
HasValueNamesSetting(false), ValueNames(false),
EnableReflectionMetadata(false), EnableReflectionNames(false),
CmdArgs(), UseIncrementalLLVMCodeGen(true)
EnableReflectionBuiltins(false), CmdArgs(),
UseIncrementalLLVMCodeGen(true)
{}
/// Gets the name of the specified output filename.

View File

@@ -197,6 +197,10 @@ def enable_reflection_names : Flag<["-"], "enable-reflection-names">,
HelpText<"Enable emission of names of stored properties and enum cases in"
"reflection metadata">;
def enable_reflection_builtins : Flag<["-"], "enable-reflection-builtins">,
HelpText<"Enable emission of reflection metadata for builtin types"
" (standard library only)">;
def stack_promotion_checks : Flag<["-"], "emit-stack-promotion-checks">,
HelpText<"Emit runtime checks for correct stack promotion of objects.">;

View File

@@ -1282,6 +1282,9 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
if (Args.hasArg(OPT_enable_reflection_names)) {
Opts.EnableReflectionNames = true;
}
if (Args.hasArg(OPT_enable_reflection_builtins)) {
Opts.EnableReflectionBuiltins = true;
}
}
return false;

View File

@@ -31,7 +31,7 @@ using namespace irgen;
class ReflectionMetadataBuilder : public ConstantBuilder<> {
protected:
SmallPtrSetImpl<CanType> &BuiltinTypes;
llvm::SetVector<CanType> &BuiltinTypes;
// Collect any builtin types referenced from this type.
void addBuiltinTypeRefs(CanType type) {
@@ -54,7 +54,7 @@ protected:
public:
ReflectionMetadataBuilder(IRGenModule &IGM,
SmallPtrSetImpl<CanType> &BuiltinTypes)
llvm::SetVector<CanType> &BuiltinTypes)
: ConstantBuilder(IGM), BuiltinTypes(BuiltinTypes) {}
};
@@ -127,7 +127,7 @@ public:
AssociatedTypeMetadataBuilder(IRGenModule &IGM,
ArrayRef<const NominalTypeDecl *> NominalTypeDecls,
ArrayRef<const ExtensionDecl *> ExtensionDecls,
SmallPtrSetImpl<CanType> &BuiltinTypes)
llvm::SetVector<CanType> &BuiltinTypes)
: ReflectionMetadataBuilder(IGM, BuiltinTypes),
NominalTypeDecls(NominalTypeDecls),
ExtensionDecls(ExtensionDecls) {}
@@ -237,7 +237,7 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
public:
FieldTypeMetadataBuilder(IRGenModule &IGM,
ArrayRef<const NominalTypeDecl *> NominalTypeDecls,
SmallPtrSetImpl<CanType> &BuiltinTypes)
llvm::SetVector<CanType> &BuiltinTypes)
: ReflectionMetadataBuilder(IGM, BuiltinTypes),
NominalTypeDecls(NominalTypeDecls) {}
@@ -273,7 +273,7 @@ class BuiltinTypeMetadataBuilder : public ReflectionMetadataBuilder {
void addBuiltinType(CanType builtinType) {
addTypeRef(builtinType->getASTContext().TheBuiltinModule, builtinType);
auto &ti = cast<LoadableTypeInfo>(IGM.getTypeInfoForUnlowered(builtinType));
auto &ti = cast<FixedTypeInfo>(IGM.getTypeInfoForUnlowered(builtinType));
addConstantInt32(ti.getFixedSize().getValue());
addConstantInt32(ti.getFixedAlignment().getValue());
addConstantInt32(ti.getFixedStride().getValue());
@@ -288,7 +288,7 @@ class BuiltinTypeMetadataBuilder : public ReflectionMetadataBuilder {
public:
BuiltinTypeMetadataBuilder(IRGenModule &IGM,
SmallPtrSetImpl<CanType> &BuiltinTypes)
llvm::SetVector<CanType> &BuiltinTypes)
: ReflectionMetadataBuilder(IGM, BuiltinTypes) {}
llvm::GlobalVariable *emit() {
@@ -381,7 +381,8 @@ llvm::Constant *IRGenModule::getAddrOfStringForTypeRef(StringRef Str) {
void IRGenModule::emitReflectionMetadataRecords() {
auto DoNotHaveDecls = NominalTypeDecls.empty() && ExtensionDecls.empty();
if (!Opts.EnableReflectionMetadata || DoNotHaveDecls)
if (!Opts.EnableReflectionMetadata ||
(!Opts.EnableReflectionBuiltins && DoNotHaveDecls))
return;
// We collect all referenced builtin types and emit records for them.
@@ -389,7 +390,7 @@ void IRGenModule::emitReflectionMetadataRecords() {
// builtin types.
//
// FIXME: This metadata should be in the runtime instead.
SmallPtrSet<CanType, 4> BuiltinTypes;
llvm::SetVector<CanType> BuiltinTypes;
{
FieldTypeMetadataBuilder builder(*this, NominalTypeDecls, BuiltinTypes);
@@ -408,7 +409,13 @@ void IRGenModule::emitReflectionMetadataRecords() {
addUsedGlobal(var);
}
{
if (Opts.EnableReflectionBuiltins) {
BuiltinTypes.insert(Context.TheNativeObjectType);
BuiltinTypes.insert(Context.TheUnknownObjectType);
BuiltinTypes.insert(Context.TheBridgeObjectType);
BuiltinTypes.insert(Context.TheRawPointerType);
BuiltinTypes.insert(Context.TheUnsafeValueBufferType);
BuiltinTypeMetadataBuilder builder(*this, BuiltinTypes);
auto var = builder.emit();
if (var)