Skip to content

Commit 57dc5a6

Browse files
authored
[IRGen] Add flags to enable or disable generation of layout string value witnesses (#64819)
rdar://107477762
1 parent 4288ba9 commit 57dc5a6

File tree

6 files changed

+77
-18
lines changed

6 files changed

+77
-18
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,5 +535,9 @@ WARNING(enable_interop_flag_deprecated,none,
535535
"the -enable-experimental-cxx-interop flag is deprecated. Please pass "
536536
"'-cxx-interoperability-mode=' instead.", ())
537537

538+
ERROR(layout_string_instantiation_without_layout_strings,none,
539+
"-enable-layout-string-value-witnesses-instantiation can not be enabled "
540+
"without -enable-layout-string-value-witnesses.", ())
541+
538542
#define UNDEFINE_DIAGNOSTIC_MACROS
539543
#include "DefineDiagnosticMacros.h"

include/swift/AST/IRGenOptions.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ class IRGenOptions {
383383
/// using TypeInfo entries.
384384
unsigned ForceStructTypeLayouts : 1;
385385

386+
/// Enable generation and use of layout string based value witnesses
387+
unsigned EnableLayoutStringValueWitnesses : 1;
388+
389+
/// Enable runtime instantiation of value witness strings for generic types
390+
unsigned EnableLayoutStringValueWitnessesInstantiation : 1;
391+
386392
/// Instrument code to generate profiling information.
387393
unsigned GenerateProfile : 1;
388394

@@ -496,6 +502,8 @@ class IRGenOptions {
496502
CompactAbsoluteFunctionPointer(false), DisableLegacyTypeInfo(false),
497503
PrespecializeGenericMetadata(false), UseIncrementalLLVMCodeGen(true),
498504
UseTypeLayoutValueHandling(true), ForceStructTypeLayouts(false),
505+
EnableLayoutStringValueWitnesses(false),
506+
EnableLayoutStringValueWitnessesInstantiation(false),
499507
GenerateProfile(false), EnableDynamicReplacementChaining(false),
500508
DisableDebuggerShadowCopies(false),
501509
DisableConcreteTypeMetadataMangledNameAccessors(false),

include/swift/Option/FrontendOptions.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,18 @@ def disable_type_layouts : Flag<["-"], "disable-type-layout">,
10211021
def force_struct_type_layouts : Flag<["-"], "force-struct-type-layouts">,
10221022
HelpText<"Force type layout based lowering for structs">;
10231023

1024+
def enable_layout_string_value_witnesses : Flag<["-"], "enable-layout-string-value-witnesses">,
1025+
HelpText<"Enable layout string based value witnesses">;
1026+
1027+
def disable_layout_string_value_witnesses : Flag<["-"], "disable-layout-string-value-witnesses">,
1028+
HelpText<"Disable layout string based value witnesses">;
1029+
1030+
def enable_layout_string_value_witnesses_instantiation : Flag<["-"], "enable-layout-string-value-witnesses-instantiation">,
1031+
HelpText<"Enable runtime instantiation of layout string value witnesses for generic types">;
1032+
1033+
def disable_layout_string_value_witnesses_instantiation : Flag<["-"], "disable-layout-string-value-witnesses-instantiation">,
1034+
HelpText<"Disable runtime instantiation of layout string value witnesses for generic types">;
1035+
10241036
def disable_interface_lockfile : Flag<["-"], "disable-interface-lock">,
10251037
HelpText<"Don't lock interface file when building module">;
10261038

lib/Frontend/CompilerInvocation.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2675,6 +2675,21 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
26752675
Args.hasFlag(OPT_enable_relative_protocol_witness_tables,
26762676
OPT_disable_relative_protocol_witness_tables,
26772677
Opts.UseRelativeProtocolWitnessTables);
2678+
2679+
Opts.EnableLayoutStringValueWitnesses = Args.hasFlag(OPT_enable_layout_string_value_witnesses,
2680+
OPT_disable_layout_string_value_witnesses,
2681+
Opts.EnableLayoutStringValueWitnesses);
2682+
2683+
Opts.EnableLayoutStringValueWitnessesInstantiation = Args.hasFlag(OPT_enable_layout_string_value_witnesses_instantiation,
2684+
OPT_disable_layout_string_value_witnesses_instantiation,
2685+
Opts.EnableLayoutStringValueWitnessesInstantiation);
2686+
2687+
if (Opts.EnableLayoutStringValueWitnessesInstantiation &&
2688+
!Opts.EnableLayoutStringValueWitnesses) {
2689+
Diags.diagnose(SourceLoc(), diag::layout_string_instantiation_without_layout_strings);
2690+
return true;
2691+
}
2692+
26782693
return false;
26792694
}
26802695

lib/IRGen/GenMeta.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,15 +2591,17 @@ void irgen::emitLazyTypeContextDescriptor(IRGenModule &IGM,
25912591
auto &ti = IGM.getTypeInfo(lowered);
25922592
auto *typeLayoutEntry =
25932593
ti.buildTypeLayoutEntry(IGM, lowered, /*useStructLayouts*/ true);
2594-
if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses)) {
2594+
if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
2595+
IGM.getOptions().EnableLayoutStringValueWitnesses) {
25952596

25962597
auto genericSig =
25972598
lowered.getNominalOrBoundGenericNominal()->getGenericSignature();
25982599
hasLayoutString = !!typeLayoutEntry->layoutString(IGM, genericSig);
25992600
}
26002601

26012602
if (auto sd = dyn_cast<StructDecl>(type)) {
2602-
if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnessesInstantiation)) {
2603+
if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnessesInstantiation) &&
2604+
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation) {
26032605
hasLayoutString |= requiresForeignTypeMetadata(type) ||
26042606
needsSingletonMetadataInitialization(IGM, type) ||
26052607
(type->isGenericContext() && !isa<FixedTypeInfo>(ti));
@@ -2896,7 +2898,8 @@ static void emitInitializeFieldOffsetVectorWithLayoutString(
28962898
bool isVWTMutable, MetadataDependencyCollector *collector) {
28972899
auto &IGM = IGF.IGM;
28982900
assert(IGM.Context.LangOpts.hasFeature(
2899-
Feature::LayoutStringValueWitnessesInstantiation));
2901+
Feature::LayoutStringValueWitnessesInstantiation) &&
2902+
IGM.getOptions().EnableLayoutStringValueWitnesses);
29002903

29012904
auto *target = T.getStructOrBoundGenericStruct();
29022905

@@ -3008,7 +3011,9 @@ static void emitInitializeValueMetadata(IRGenFunction &IGF,
30083011

30093012
if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
30103013
IGM.Context.LangOpts.hasFeature(
3011-
Feature::LayoutStringValueWitnessesInstantiation)) {
3014+
Feature::LayoutStringValueWitnessesInstantiation) &&
3015+
IGM.getOptions().EnableLayoutStringValueWitnesses &&
3016+
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation) {
30123017
emitInitializeFieldOffsetVectorWithLayoutString(IGF, loweredTy, metadata,
30133018
isVWTMutable, collector);
30143019
} else {
@@ -3123,7 +3128,8 @@ namespace {
31233128
Impl &asImpl() { return *static_cast<Impl*>(this); }
31243129

31253130
llvm::Constant *emitLayoutString() {
3126-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses))
3131+
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
3132+
!IGM.getOptions().EnableLayoutStringValueWitnesses)
31273133
return nullptr;
31283134
auto lowered = getLoweredTypeInPrimaryContext(
31293135
IGM, Target->getDeclaredType()->getCanonicalType());
@@ -3206,7 +3212,8 @@ namespace {
32063212
asImpl().emitInitializeMetadata(IGF, metadata, false, collector);
32073213

32083214
if (IGM.Context.LangOpts.hasFeature(
3209-
Feature::LayoutStringValueWitnesses)) {
3215+
Feature::LayoutStringValueWitnesses) &&
3216+
IGM.getOptions().EnableLayoutStringValueWitnesses) {
32103217
if (auto *layoutString = getLayoutString()) {
32113218
auto layoutStringCast = IGF.Builder.CreateBitCast(layoutString,
32123219
IGM.Int8PtrTy);
@@ -3782,7 +3789,8 @@ namespace {
37823789
}
37833790

37843791
llvm::Constant *emitLayoutString() {
3785-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses))
3792+
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
3793+
!IGM.getOptions().EnableLayoutStringValueWitnesses)
37863794
return nullptr;
37873795
auto lowered = getLoweredTypeInPrimaryContext(
37883796
IGM, Target->getDeclaredType()->getCanonicalType());
@@ -4986,11 +4994,13 @@ namespace {
49864994
}
49874995

49884996
bool hasLayoutString() {
4989-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses)) {
4997+
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
4998+
!IGM.getOptions().EnableLayoutStringValueWitnesses) {
49904999
return false;
49915000
}
49925001

4993-
if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnessesInstantiation)) {
5002+
if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnessesInstantiation) &&
5003+
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation) {
49945004
return !!getLayoutString() || needsSingletonMetadataInitialization(IGM, Target);
49955005
}
49965006

@@ -5029,7 +5039,8 @@ namespace {
50295039
}
50305040

50315041
llvm::Constant *emitLayoutString() {
5032-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses))
5042+
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
5043+
!IGM.getOptions().EnableLayoutStringValueWitnesses)
50335044
return nullptr;
50345045
auto lowered = getLoweredTypeInPrimaryContext(
50355046
IGM, Target->getDeclaredType()->getCanonicalType());
@@ -5180,12 +5191,14 @@ namespace {
51805191

51815192
bool hasLayoutString() {
51825193
if (!IGM.Context.LangOpts.hasFeature(
5183-
Feature::LayoutStringValueWitnesses)) {
5194+
Feature::LayoutStringValueWitnesses) ||
5195+
!IGM.getOptions().EnableLayoutStringValueWitnesses) {
51845196
return false;
51855197
}
51865198
return !!getLayoutString() ||
51875199
(IGM.Context.LangOpts.hasFeature(
51885200
Feature::LayoutStringValueWitnessesInstantiation) &&
5201+
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation &&
51895202
(HasDependentVWT || HasDependentMetadata) &&
51905203
!isa<FixedTypeInfo>(IGM.getTypeInfo(getLoweredType())));
51915204
}
@@ -5445,7 +5458,8 @@ namespace {
54455458
}
54465459

54475460
llvm::Constant *emitLayoutString() {
5448-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses))
5461+
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
5462+
!IGM.getOptions().EnableLayoutStringValueWitnesses)
54495463
return nullptr;
54505464
auto lowered = getLoweredTypeInPrimaryContext(
54515465
IGM, Target->getDeclaredType()->getCanonicalType());

lib/IRGen/GenValueWitness.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,8 @@ bool isRuntimeInstatiatedLayoutString(IRGenModule &IGM,
921921
const TypeLayoutEntry *typeLayoutEntry) {
922922
if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
923923
IGM.Context.LangOpts.hasFeature(
924-
Feature::LayoutStringValueWitnessesInstantiation)) {
924+
Feature::LayoutStringValueWitnessesInstantiation) &&
925+
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation) {
925926
return (typeLayoutEntry->isAlignedGroup() &&
926927
!typeLayoutEntry->isFixedSize(IGM));
927928
}
@@ -986,7 +987,8 @@ static void addValueWitness(IRGenModule &IGM, ConstantStructBuilder &B,
986987
return addFunction(getNoOpVoidFunction(IGM));
987988
} else if (concreteTI.isSingleSwiftRetainablePointer(ResilienceExpansion::Maximal)) {
988989
return addFunction(getDestroyStrongFunction(IGM));
989-
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses)) {
990+
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
991+
IGM.getOptions().EnableLayoutStringValueWitnesses) {
990992
if (auto *typeLayoutEntry =
991993
concreteTI.buildTypeLayoutEntry(IGM, concreteType,
992994
/*useStructLayouts*/true)) {
@@ -1013,7 +1015,8 @@ static void addValueWitness(IRGenModule &IGM, ConstantStructBuilder &B,
10131015
case ValueWitness::InitializeWithTake:
10141016
if (concreteTI.isBitwiseTakable(ResilienceExpansion::Maximal)) {
10151017
return addFunction(getMemCpyFunction(IGM, concreteTI));
1016-
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses)) {
1018+
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
1019+
IGM.getOptions().EnableLayoutStringValueWitnesses) {
10171020
if (auto *typeLayoutEntry =
10181021
concreteTI.buildTypeLayoutEntry(IGM, concreteType,
10191022
/*useStructLayouts*/true)) {
@@ -1032,7 +1035,8 @@ static void addValueWitness(IRGenModule &IGM, ConstantStructBuilder &B,
10321035
return addFunction(getMemCpyFunction(IGM, concreteTI));
10331036
} else if (concreteTI.isSingleSwiftRetainablePointer(ResilienceExpansion::Maximal)) {
10341037
return addFunction(getAssignWithCopyStrongFunction(IGM));
1035-
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses)) {
1038+
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
1039+
IGM.getOptions().EnableLayoutStringValueWitnesses) {
10361040
if (auto *typeLayoutEntry =
10371041
concreteTI.buildTypeLayoutEntry(IGM, concreteType,
10381042
/*useStructLayouts*/true)) {
@@ -1051,7 +1055,8 @@ static void addValueWitness(IRGenModule &IGM, ConstantStructBuilder &B,
10511055
return addFunction(getMemCpyFunction(IGM, concreteTI));
10521056
} else if (concreteTI.isSingleSwiftRetainablePointer(ResilienceExpansion::Maximal)) {
10531057
return addFunction(getAssignWithTakeStrongFunction(IGM));
1054-
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses)) {
1058+
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
1059+
IGM.getOptions().EnableLayoutStringValueWitnesses) {
10551060
if (auto *typeLayoutEntry =
10561061
concreteTI.buildTypeLayoutEntry(IGM, concreteType,
10571062
/*useStructLayouts*/true)) {
@@ -1070,7 +1075,8 @@ static void addValueWitness(IRGenModule &IGM, ConstantStructBuilder &B,
10701075
return addFunction(getMemCpyFunction(IGM, concreteTI));
10711076
} else if (concreteTI.isSingleSwiftRetainablePointer(ResilienceExpansion::Maximal)) {
10721077
return addFunction(getInitWithCopyStrongFunction(IGM));
1073-
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses)) {
1078+
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
1079+
IGM.getOptions().EnableLayoutStringValueWitnesses) {
10741080
if (auto *typeLayoutEntry =
10751081
concreteTI.buildTypeLayoutEntry(IGM, concreteType,
10761082
/*useStructLayouts*/true)) {

0 commit comments

Comments
 (0)