Skip to content

Commit 8e72a01

Browse files
authored
[IRGen] Add ability to disable compact value witnesses from block list (#72335)
rdar://124629183 Allows usage of compact value witnesses to be disabled using a block list.
1 parent e424ede commit 8e72a01

File tree

7 files changed

+84
-46
lines changed

7 files changed

+84
-46
lines changed

include/swift/AST/DiagnosticsIRGen.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,9 @@ ERROR(temporary_allocation_alignment_not_power_of_2,none,
6565
ERROR(explosion_size_oveflow,none,
6666
"explosion size too large", ())
6767

68+
NOTE(layout_strings_blocked,none,
69+
"Layout string value witnesses have been disabled for module '%0' "
70+
"through block list entry", (StringRef))
71+
6872
#define UNDEFINE_DIAGNOSTIC_MACROS
6973
#include "DefineDiagnosticMacros.h"

include/swift/Basic/BlockListAction.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
BLOCKLIST_ACTION(ShouldUseBinaryModule)
2323
BLOCKLIST_ACTION(ShouldUseTextualModule)
2424
BLOCKLIST_ACTION(DowngradeInterfaceVerificationFailure)
25+
BLOCKLIST_ACTION(ShouldUseLayoutStringValueWitnesses)
2526

2627
#undef BLOCKLIST_ACTION

lib/IRGen/GenMeta.cpp

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,8 +2598,7 @@ void irgen::emitLazyTypeContextDescriptor(IRGenModule &IGM,
25982598
auto &ti = IGM.getTypeInfo(lowered);
25992599
auto *typeLayoutEntry =
26002600
ti.buildTypeLayoutEntry(IGM, lowered, /*useStructLayouts*/ true);
2601-
if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
2602-
IGM.getOptions().EnableLayoutStringValueWitnesses) {
2601+
if (layoutStringsEnabled(IGM)) {
26032602

26042603
auto genericSig =
26052604
lowered.getNominalOrBoundGenericNominal()->getGenericSignature();
@@ -3126,11 +3125,11 @@ static void emitInitializeValueMetadata(IRGenFunction &IGF,
31263125
MetadataDependencyCollector *collector) {
31273126
auto &IGM = IGF.IGM;
31283127
auto loweredTy = IGM.getLoweredType(nominalDecl->getDeclaredTypeInContext());
3129-
bool useLayoutStrings = IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
3130-
IGM.Context.LangOpts.hasFeature(
3131-
Feature::LayoutStringValueWitnessesInstantiation) &&
3132-
IGM.getOptions().EnableLayoutStringValueWitnesses &&
3133-
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation;
3128+
bool useLayoutStrings =
3129+
layoutStringsEnabled(IGM) &&
3130+
IGM.Context.LangOpts.hasFeature(
3131+
Feature::LayoutStringValueWitnessesInstantiation) &&
3132+
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation;
31343133

31353134
if (auto sd = dyn_cast<StructDecl>(nominalDecl)) {
31363135
auto &fixedTI = IGM.getTypeInfo(loweredTy);
@@ -3281,8 +3280,7 @@ namespace {
32813280
Impl &asImpl() { return *static_cast<Impl*>(this); }
32823281

32833282
llvm::Constant *emitLayoutString() {
3284-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
3285-
!IGM.getOptions().EnableLayoutStringValueWitnesses)
3283+
if (!layoutStringsEnabled(IGM))
32863284
return nullptr;
32873285
auto lowered = getLoweredTypeInPrimaryContext(IGM, Target);
32883286
auto &ti = IGM.getTypeInfo(lowered);
@@ -3363,9 +3361,7 @@ namespace {
33633361
if (HasDependentMetadata)
33643362
asImpl().emitInitializeMetadata(IGF, metadata, false, collector);
33653363

3366-
if (IGM.Context.LangOpts.hasFeature(
3367-
Feature::LayoutStringValueWitnesses) &&
3368-
IGM.getOptions().EnableLayoutStringValueWitnesses) {
3364+
if (layoutStringsEnabled(IGM)) {
33693365
if (auto *layoutString = getLayoutString()) {
33703366
auto layoutStringCast = IGF.Builder.CreateBitCast(layoutString,
33713367
IGM.Int8PtrTy);
@@ -3960,8 +3956,7 @@ namespace {
39603956
}
39613957

39623958
llvm::Constant *emitLayoutString() {
3963-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
3964-
!IGM.getOptions().EnableLayoutStringValueWitnesses)
3959+
if (!layoutStringsEnabled(IGM))
39653960
return nullptr;
39663961
auto lowered = getLoweredTypeInPrimaryContext(IGM, Target);
39673962
auto &ti = IGM.getTypeInfo(lowered);
@@ -4681,7 +4676,7 @@ namespace {
46814676
SILType getLoweredType() { return SILType::getPrimitiveObjectType(type); }
46824677

46834678
llvm::Constant *emitLayoutString() {
4684-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses))
4679+
if (!layoutStringsEnabled(IGM))
46854680
return nullptr;
46864681
auto lowered = getLoweredType();
46874682
auto &ti = IGM.getTypeInfo(lowered);
@@ -4708,7 +4703,7 @@ namespace {
47084703
}
47094704

47104705
bool hasLayoutString() {
4711-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses)) {
4706+
if (!layoutStringsEnabled(IGM)) {
47124707
return false;
47134708
}
47144709

@@ -5291,8 +5286,7 @@ namespace {
52915286
}
52925287

52935288
bool hasLayoutString() {
5294-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
5295-
!IGM.getOptions().EnableLayoutStringValueWitnesses) {
5289+
if (!layoutStringsEnabled(IGM)) {
52965290
return false;
52975291
}
52985292

@@ -5336,8 +5330,7 @@ namespace {
53365330
}
53375331

53385332
llvm::Constant *emitLayoutString() {
5339-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
5340-
!IGM.getOptions().EnableLayoutStringValueWitnesses)
5333+
if (!layoutStringsEnabled(IGM))
53415334
return nullptr;
53425335
auto lowered = getLoweredTypeInPrimaryContext(IGM, Target);
53435336
auto &ti = IGM.getTypeInfo(lowered);
@@ -5484,9 +5477,7 @@ namespace {
54845477
}
54855478

54865479
bool hasLayoutString() {
5487-
if (!IGM.Context.LangOpts.hasFeature(
5488-
Feature::LayoutStringValueWitnesses) ||
5489-
!IGM.getOptions().EnableLayoutStringValueWitnesses) {
5480+
if (!layoutStringsEnabled(IGM)) {
54905481
return false;
54915482
}
54925483
return !!getLayoutString() ||
@@ -5768,18 +5759,15 @@ namespace {
57685759
}
57695760

57705761
bool hasLayoutString() {
5771-
if (!IGM.Context.LangOpts.hasFeature(
5772-
Feature::LayoutStringValueWitnesses) ||
5773-
!IGM.getOptions().EnableLayoutStringValueWitnesses) {
5762+
if (!layoutStringsEnabled(IGM)) {
57745763
return false;
57755764
}
57765765

57775766
return hasInstantiatedLayoutString() || !!getLayoutString();
57785767
}
57795768

57805769
llvm::Constant *emitLayoutString() {
5781-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
5782-
!IGM.getOptions().EnableLayoutStringValueWitnesses)
5770+
if (!layoutStringsEnabled(IGM))
57835771
return nullptr;
57845772
auto lowered = getLoweredTypeInPrimaryContext(IGM, Target);
57855773
auto &ti = IGM.getTypeInfo(lowered);
@@ -6005,9 +5993,7 @@ namespace {
60055993
}
60065994

60075995
bool hasLayoutString() {
6008-
if (!IGM.Context.LangOpts.hasFeature(
6009-
Feature::LayoutStringValueWitnesses) ||
6010-
!IGM.getOptions().EnableLayoutStringValueWitnesses) {
5996+
if (!layoutStringsEnabled(IGM)) {
60115997
return false;
60125998
}
60135999

lib/IRGen/GenValueWitness.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323

2424
#include "swift/AST/ASTContext.h"
2525
#include "swift/AST/Attr.h"
26+
#include "swift/AST/DiagnosticsIRGen.h"
2627
#include "swift/AST/IRGenOptions.h"
2728
#include "swift/AST/Types.h"
29+
#include "swift/Basic/BlockList.h"
2830
#include "swift/IRGen/Linking.h"
2931
#include "swift/SIL/TypeLowering.h"
3032
#include "llvm/ADT/SmallString.h"
@@ -885,9 +887,26 @@ void addStride(ConstantStructBuilder &B, const TypeInfo *TI, IRGenModule &IGM) {
885887
}
886888
} // end anonymous namespace
887889

890+
bool irgen::layoutStringsEnabled(IRGenModule &IGM, bool diagnose) {
891+
auto moduleName = IGM.getSwiftModule()->getRealName().str();
892+
if (IGM.Context.blockListConfig.hasBlockListAction(
893+
moduleName, BlockListKeyKind::ModuleName,
894+
BlockListAction::ShouldUseLayoutStringValueWitnesses)) {
895+
if (diagnose) {
896+
IGM.Context.Diags.diagnose(SourceLoc(), diag::layout_strings_blocked,
897+
moduleName);
898+
}
899+
return false;
900+
}
901+
902+
return IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
903+
IGM.getOptions().EnableLayoutStringValueWitnesses;
904+
}
905+
888906
static bool isRuntimeInstatiatedLayoutString(IRGenModule &IGM,
889907
const TypeLayoutEntry *typeLayoutEntry) {
890-
if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
908+
909+
if (layoutStringsEnabled(IGM) &&
891910
IGM.Context.LangOpts.hasFeature(
892911
Feature::LayoutStringValueWitnessesInstantiation) &&
893912
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation) {
@@ -1028,8 +1047,7 @@ static void addValueWitness(IRGenModule &IGM, ConstantStructBuilder &B,
10281047
return addFunction(getNoOpVoidFunction(IGM));
10291048
} else if (concreteTI.isSingleSwiftRetainablePointer(ResilienceExpansion::Maximal)) {
10301049
return addFunction(getDestroyStrongFunction(IGM));
1031-
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
1032-
IGM.getOptions().EnableLayoutStringValueWitnesses) {
1050+
} else if (layoutStringsEnabled(IGM)) {
10331051
auto ty = boundGenericCharacteristics ? boundGenericCharacteristics->concreteType : concreteType;
10341052
auto &typeInfo = boundGenericCharacteristics ? *boundGenericCharacteristics->TI : concreteTI;
10351053
if (auto *typeLayoutEntry =
@@ -1053,8 +1071,7 @@ static void addValueWitness(IRGenModule &IGM, ConstantStructBuilder &B,
10531071
}
10541072
}
10551073

1056-
if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
1057-
IGM.getOptions().EnableLayoutStringValueWitnesses) {
1074+
if (layoutStringsEnabled(IGM)) {
10581075
auto ty = boundGenericCharacteristics
10591076
? boundGenericCharacteristics->concreteType
10601077
: concreteType;
@@ -1077,8 +1094,7 @@ static void addValueWitness(IRGenModule &IGM, ConstantStructBuilder &B,
10771094
case ValueWitness::InitializeWithTake:
10781095
if (concreteTI.isBitwiseTakable(ResilienceExpansion::Maximal)) {
10791096
return addFunction(getMemCpyFunction(IGM, concreteTI));
1080-
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
1081-
IGM.getOptions().EnableLayoutStringValueWitnesses) {
1097+
} else if (layoutStringsEnabled(IGM)) {
10821098
auto ty = boundGenericCharacteristics ? boundGenericCharacteristics->concreteType : concreteType;
10831099
auto &typeInfo = boundGenericCharacteristics ? *boundGenericCharacteristics->TI : concreteTI;
10841100
if (auto *typeLayoutEntry =
@@ -1098,8 +1114,7 @@ static void addValueWitness(IRGenModule &IGM, ConstantStructBuilder &B,
10981114
return addFunction(getMemCpyFunction(IGM, concreteTI));
10991115
} else if (concreteTI.isSingleSwiftRetainablePointer(ResilienceExpansion::Maximal)) {
11001116
return addFunction(getAssignWithCopyStrongFunction(IGM));
1101-
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
1102-
IGM.getOptions().EnableLayoutStringValueWitnesses) {
1117+
} else if (layoutStringsEnabled(IGM)) {
11031118
auto ty = boundGenericCharacteristics ? boundGenericCharacteristics->concreteType : concreteType;
11041119
auto &typeInfo = boundGenericCharacteristics ? *boundGenericCharacteristics->TI : concreteTI;
11051120
if (auto *typeLayoutEntry =
@@ -1119,8 +1134,7 @@ static void addValueWitness(IRGenModule &IGM, ConstantStructBuilder &B,
11191134
return addFunction(getMemCpyFunction(IGM, concreteTI));
11201135
} else if (concreteTI.isSingleSwiftRetainablePointer(ResilienceExpansion::Maximal)) {
11211136
return addFunction(getAssignWithTakeStrongFunction(IGM));
1122-
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
1123-
IGM.getOptions().EnableLayoutStringValueWitnesses) {
1137+
} else if (layoutStringsEnabled(IGM)) {
11241138
auto ty = boundGenericCharacteristics ? boundGenericCharacteristics->concreteType : concreteType;
11251139
auto &typeInfo = boundGenericCharacteristics ? *boundGenericCharacteristics->TI : concreteTI;
11261140
if (auto *typeLayoutEntry =
@@ -1140,8 +1154,7 @@ static void addValueWitness(IRGenModule &IGM, ConstantStructBuilder &B,
11401154
return addFunction(getMemCpyFunction(IGM, concreteTI));
11411155
} else if (concreteTI.isSingleSwiftRetainablePointer(ResilienceExpansion::Maximal)) {
11421156
return addFunction(getInitWithCopyStrongFunction(IGM));
1143-
} else if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
1144-
IGM.getOptions().EnableLayoutStringValueWitnesses) {
1157+
} else if (layoutStringsEnabled(IGM)) {
11451158
auto ty = boundGenericCharacteristics ? boundGenericCharacteristics->concreteType : concreteType;
11461159
auto &typeInfo = boundGenericCharacteristics ? *boundGenericCharacteristics->TI : concreteTI;
11471160
if (auto *typeLayoutEntry =

lib/IRGen/GenValueWitness.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ namespace irgen {
4949

5050
SILType getLoweredTypeInPrimaryContext(IRGenModule &IGM,
5151
NominalTypeDecl *type);
52+
53+
bool layoutStringsEnabled(IRGenModule &IGM, bool diagnose = false);
5254
}
5355
}
5456

lib/IRGen/IRGen.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "../Serialization/ModuleFormat.h"
18+
#include "GenValueWitness.h"
1819
#include "IRGenModule.h"
1920
#include "swift/ABI/MetadataValues.h"
2021
#include "swift/ABI/ObjectFile.h"
@@ -56,15 +57,14 @@
5657
#include "llvm/CodeGen/TargetSubtargetInfo.h"
5758
#include "llvm/IR/Constants.h"
5859
#include "llvm/IR/DataLayout.h"
59-
#include "llvm/IRPrinter/IRPrintingPasses.h"
6060
#include "llvm/IR/LLVMContext.h"
6161
#include "llvm/IR/LegacyPassManager.h"
6262
#include "llvm/IR/Module.h"
6363
#include "llvm/IR/PassManager.h"
6464
#include "llvm/IR/ValueSymbolTable.h"
6565
#include "llvm/IR/Verifier.h"
66+
#include "llvm/IRPrinter/IRPrintingPasses.h"
6667
#include "llvm/Linker/Linker.h"
67-
#include "llvm/TargetParser/SubtargetFeature.h"
6868
#include "llvm/MC/TargetRegistry.h"
6969
#include "llvm/Object/ObjectFile.h"
7070
#include "llvm/Passes/PassBuilder.h"
@@ -79,6 +79,7 @@
7979
#include "llvm/Support/VirtualOutputBackend.h"
8080
#include "llvm/Support/VirtualOutputConfig.h"
8181
#include "llvm/Target/TargetMachine.h"
82+
#include "llvm/TargetParser/SubtargetFeature.h"
8283
#include "llvm/Transforms/IPO.h"
8384
#include "llvm/Transforms/IPO/AlwaysInliner.h"
8485
#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
@@ -1162,6 +1163,8 @@ GeneratedModule IRGenRequest::evaluate(Evaluator &evaluator,
11621163
// Run SIL level IRGen preparation passes.
11631164
runIRGenPreparePasses(*SILMod, IGM);
11641165

1166+
(void)layoutStringsEnabled(IGM, /*diagnose*/ true);
1167+
11651168
{
11661169
FrontendStatsTracer tracer(Ctx.Stats, "IRGen");
11671170

@@ -1404,6 +1407,12 @@ static void performParallelIRGeneration(IRGenDescriptor desc) {
14041407
runIRGenPreparePasses(*SILMod, *IGM);
14051408
DidRunSILCodeGenPreparePasses = true;
14061409
}
1410+
1411+
if (!layoutStringsEnabled(*IGM)) {
1412+
auto moduleName = IGM->getSwiftModule()->getRealName().str();
1413+
IGM->Context.Diags.diagnose(SourceLoc(), diag::layout_strings_blocked,
1414+
moduleName);
1415+
}
14071416
}
14081417

14091418
if (!IGMcreated) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -enable-experimental-feature LayoutStringValueWitnesses -enable-layout-string-value-witnesses -emit-ir -module-name Foo %s | %FileCheck %s
4+
// RUN: %target-swift-frontend -emit-ir -module-name Foo %s | %FileCheck %s --check-prefix=CHECK-DISABLED
5+
6+
// RUN: echo "---" > %t/blocklist.yml
7+
// RUN: echo "ShouldUseLayoutStringValueWitnesses:" >> %t/blocklist.yml
8+
// RUN: echo " ModuleName:" >> %t/blocklist.yml
9+
// RUN: echo " - Foo" >> %t/blocklist.yml
10+
11+
// RUN: %target-swift-frontend -enable-experimental-feature LayoutStringValueWitnesses -enable-layout-string-value-witnesses -emit-ir -blocklist-file %t/blocklist.yml -module-name Foo %s 2>&1 | %FileCheck %s --check-prefix=CHECK-BLOCKED
12+
13+
// CHECK: type_layout_string
14+
15+
// CHECK-BLOCKED: note: Layout string value witnesses have been disabled for module 'Foo' through block list entry
16+
// CHECK-BLOCKED-NOT: type_layout_string
17+
18+
// CHECK-DISABLED-NOT: note: Layout string value witnesses have been disabled for module 'Foo' through block list entry
19+
// CHECK-DISABLED-NOT: type_layout_string
20+
public struct Bar {
21+
let x: Int
22+
let y: AnyObject
23+
}

0 commit comments

Comments
 (0)