Skip to content

Commit 35153d8

Browse files
authored
[6.0][IRGen] Add ability to disable compact value witnesses from block list (swiftlang#72697)
* [IRGen] Add ability to disable compact value witnesses from block list rdar://124629183 Allows usage of compact value witnesses to be disabled using a block list. * [IRGen] Fix diagnostic for CVW disablement through block list rdar://125486631
1 parent 9c4adb7 commit 35153d8

File tree

7 files changed

+80
-46
lines changed

7 files changed

+80
-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
@@ -2750,8 +2750,7 @@ void irgen::emitLazyTypeContextDescriptor(IRGenModule &IGM,
27502750
auto &ti = IGM.getTypeInfo(lowered);
27512751
auto *typeLayoutEntry =
27522752
ti.buildTypeLayoutEntry(IGM, lowered, /*useStructLayouts*/ true);
2753-
if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
2754-
IGM.getOptions().EnableLayoutStringValueWitnesses) {
2753+
if (layoutStringsEnabled(IGM)) {
27552754

27562755
auto genericSig =
27572756
lowered.getNominalOrBoundGenericNominal()->getGenericSignature();
@@ -3278,11 +3277,11 @@ static void emitInitializeValueMetadata(IRGenFunction &IGF,
32783277
MetadataDependencyCollector *collector) {
32793278
auto &IGM = IGF.IGM;
32803279
auto loweredTy = IGM.getLoweredType(nominalDecl->getDeclaredTypeInContext());
3281-
bool useLayoutStrings = IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) &&
3282-
IGM.Context.LangOpts.hasFeature(
3283-
Feature::LayoutStringValueWitnessesInstantiation) &&
3284-
IGM.getOptions().EnableLayoutStringValueWitnesses &&
3285-
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation;
3280+
bool useLayoutStrings =
3281+
layoutStringsEnabled(IGM) &&
3282+
IGM.Context.LangOpts.hasFeature(
3283+
Feature::LayoutStringValueWitnessesInstantiation) &&
3284+
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation;
32863285

32873286
if (auto sd = dyn_cast<StructDecl>(nominalDecl)) {
32883287
auto &fixedTI = IGM.getTypeInfo(loweredTy);
@@ -3433,8 +3432,7 @@ namespace {
34333432
Impl &asImpl() { return *static_cast<Impl*>(this); }
34343433

34353434
llvm::Constant *emitLayoutString() {
3436-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
3437-
!IGM.getOptions().EnableLayoutStringValueWitnesses)
3435+
if (!layoutStringsEnabled(IGM))
34383436
return nullptr;
34393437
auto lowered = getLoweredTypeInPrimaryContext(IGM, Target);
34403438
auto &ti = IGM.getTypeInfo(lowered);
@@ -3515,9 +3513,7 @@ namespace {
35153513
if (HasDependentMetadata)
35163514
asImpl().emitInitializeMetadata(IGF, metadata, false, collector);
35173515

3518-
if (IGM.Context.LangOpts.hasFeature(
3519-
Feature::LayoutStringValueWitnesses) &&
3520-
IGM.getOptions().EnableLayoutStringValueWitnesses) {
3516+
if (layoutStringsEnabled(IGM)) {
35213517
if (auto *layoutString = getLayoutString()) {
35223518
auto layoutStringCast = IGF.Builder.CreateBitCast(layoutString,
35233519
IGM.Int8PtrTy);
@@ -4112,8 +4108,7 @@ namespace {
41124108
}
41134109

41144110
llvm::Constant *emitLayoutString() {
4115-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
4116-
!IGM.getOptions().EnableLayoutStringValueWitnesses)
4111+
if (!layoutStringsEnabled(IGM))
41174112
return nullptr;
41184113
auto lowered = getLoweredTypeInPrimaryContext(IGM, Target);
41194114
auto &ti = IGM.getTypeInfo(lowered);
@@ -4850,7 +4845,7 @@ namespace {
48504845
SILType getLoweredType() { return SILType::getPrimitiveObjectType(type); }
48514846

48524847
llvm::Constant *emitLayoutString() {
4853-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses))
4848+
if (!layoutStringsEnabled(IGM))
48544849
return nullptr;
48554850
auto lowered = getLoweredType();
48564851
auto &ti = IGM.getTypeInfo(lowered);
@@ -4877,7 +4872,7 @@ namespace {
48774872
}
48784873

48794874
bool hasLayoutString() {
4880-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses)) {
4875+
if (!layoutStringsEnabled(IGM)) {
48814876
return false;
48824877
}
48834878

@@ -5460,8 +5455,7 @@ namespace {
54605455
}
54615456

54625457
bool hasLayoutString() {
5463-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
5464-
!IGM.getOptions().EnableLayoutStringValueWitnesses) {
5458+
if (!layoutStringsEnabled(IGM)) {
54655459
return false;
54665460
}
54675461

@@ -5505,8 +5499,7 @@ namespace {
55055499
}
55065500

55075501
llvm::Constant *emitLayoutString() {
5508-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
5509-
!IGM.getOptions().EnableLayoutStringValueWitnesses)
5502+
if (!layoutStringsEnabled(IGM))
55105503
return nullptr;
55115504
auto lowered = getLoweredTypeInPrimaryContext(IGM, Target);
55125505
auto &ti = IGM.getTypeInfo(lowered);
@@ -5653,9 +5646,7 @@ namespace {
56535646
}
56545647

56555648
bool hasLayoutString() {
5656-
if (!IGM.Context.LangOpts.hasFeature(
5657-
Feature::LayoutStringValueWitnesses) ||
5658-
!IGM.getOptions().EnableLayoutStringValueWitnesses) {
5649+
if (!layoutStringsEnabled(IGM)) {
56595650
return false;
56605651
}
56615652
return !!getLayoutString() ||
@@ -5937,18 +5928,15 @@ namespace {
59375928
}
59385929

59395930
bool hasLayoutString() {
5940-
if (!IGM.Context.LangOpts.hasFeature(
5941-
Feature::LayoutStringValueWitnesses) ||
5942-
!IGM.getOptions().EnableLayoutStringValueWitnesses) {
5931+
if (!layoutStringsEnabled(IGM)) {
59435932
return false;
59445933
}
59455934

59465935
return hasInstantiatedLayoutString() || !!getLayoutString();
59475936
}
59485937

59495938
llvm::Constant *emitLayoutString() {
5950-
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
5951-
!IGM.getOptions().EnableLayoutStringValueWitnesses)
5939+
if (!layoutStringsEnabled(IGM))
59525940
return nullptr;
59535941
auto lowered = getLoweredTypeInPrimaryContext(IGM, Target);
59545942
auto &ti = IGM.getTypeInfo(lowered);
@@ -6174,9 +6162,7 @@ namespace {
61746162
}
61756163

61766164
bool hasLayoutString() {
6177-
if (!IGM.Context.LangOpts.hasFeature(
6178-
Feature::LayoutStringValueWitnesses) ||
6179-
!IGM.getOptions().EnableLayoutStringValueWitnesses) {
6165+
if (!layoutStringsEnabled(IGM)) {
61806166
return false;
61816167
}
61826168

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: 7 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,8 @@ static void performParallelIRGeneration(IRGenDescriptor desc) {
14041407
runIRGenPreparePasses(*SILMod, *IGM);
14051408
DidRunSILCodeGenPreparePasses = true;
14061409
}
1410+
1411+
(void)layoutStringsEnabled(*IGM, /*diagnose*/ true);
14071412
}
14081413

14091414
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)