Skip to content

Commit b68c55c

Browse files
authored
Merge pull request #74685 from eeckstein/assertion-improvements
Some assertion improvements
2 parents f0d9420 + 1f29622 commit b68c55c

File tree

9 files changed

+59
-74
lines changed

9 files changed

+59
-74
lines changed

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,36 @@
2121
/// case for `precondition`.
2222
@_transparent
2323
public func assert(_ condition: Bool, _ message: @autoclosure () -> String,
24-
file: StaticString = #fileID, line: UInt = #line) {
25-
if !condition {
26-
fatalError(message(), file: file, line: line)
27-
}
24+
file: StaticString = #fileID, line: UInt = #line, function: StaticString = #function) {
25+
precondition(condition, message(), file: file, line: line, function: function)
2826
}
2927

3028
/// The assert function (without a message) to be used in the compiler.
3129
///
3230
/// Unforuntately it's not possible to just add a default argument to `message` in the
3331
/// other `assert` function. We need to defined this overload.
32+
/// TODO: For some reason the compiler is not happy when adding a `function` argument.
3433
@_transparent
3534
public func assert(_ condition: Bool, file: StaticString = #fileID, line: UInt = #line) {
36-
if !condition {
37-
fatalError("", file: file, line: line)
35+
precondition(condition, "", file: file, line: line, function: "")
36+
}
37+
38+
/// The assert function to be used in the compiler.
39+
///
40+
/// This overrides the standard Swift precondition and forwards an assertion failure
41+
/// to the assertion-handling in the C++ code base.
42+
@_transparent
43+
public func precondition(_ condition: Bool, _ message: @autoclosure () -> String,
44+
file: StaticString = #fileID, line: UInt = #line, function: StaticString = #function) {
45+
if !_fastPath(condition) {
46+
let msg = message()
47+
msg.withCString { msgStr in
48+
file.withUTF8Buffer { fileBytes in
49+
function.withUTF8Buffer { functionBytes in
50+
assertFail(msgStr, fileBytes.baseAddress!, line, functionBytes.baseAddress!)
51+
}
52+
}
53+
}
3854
}
3955
}
4056

include/swift/Basic/BasicBridging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ typedef uintptr_t SwiftUInt;
107107
#define BRIDGING_WRAPPER_NULLABLE(Node, Name) \
108108
BRIDGING_WRAPPER_IMPL(Node, Nullable##Name, _Nullable)
109109

110+
void assertFail(const char * _Nonnull msg, const char * _Nonnull file,
111+
SwiftUInt line, const char * _Nonnull function);
112+
110113
//===----------------------------------------------------------------------===//
111114
// MARK: ArrayRef
112115
//===----------------------------------------------------------------------===//

include/swift/Basic/Require.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

include/swift/SIL/SILBitfield.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#ifndef SWIFT_SIL_SILBITFIELD_H
1818
#define SWIFT_SIL_SILBITFIELD_H
1919

20-
#include "swift/Basic/Require.h"
20+
#include "swift/Basic/Assertions.h"
2121
#include "swift/SIL/SILFunction.h"
2222

2323
namespace swift {
@@ -56,12 +56,12 @@ template <class Impl, class T> class SILBitfield {
5656
parent(parent),
5757
function(function) {
5858
assert(size > 0 && "bit field size must be > 0");
59-
require(endBit <= T::numCustomBits,
60-
"too many/large bit fields allocated in function");
61-
assert((!parent || bitfieldID > parent->bitfieldID) &&
59+
ASSERT(endBit <= T::numCustomBits &&
60+
"too many/large bit fields allocated in function");
61+
ASSERT((!parent || bitfieldID > parent->bitfieldID) &&
6262
"BasicBlockBitfield indices are not in order");
63-
require(function->currentBitfieldID < T::maxBitfieldID,
64-
"currentBitfieldID overflow");
63+
ASSERT(function->currentBitfieldID < T::maxBitfieldID &&
64+
"currentBitfieldID overflow");
6565
++function->currentBitfieldID;
6666
}
6767

lib/Basic/Assertions.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ int CONDITIONAL_ASSERT_Global_enable_flag =
3737
#endif
3838

3939
void ASSERT_failure(const char *expr, const char *filename, int line, const char *func) {
40-
if (AssertHelp) {
41-
ASSERT_help();
42-
} else {
43-
llvm::errs() << "Assertion help: -Xllvm -assert-help\n";
44-
}
45-
46-
4740
// Format here matches that used by `assert` on macOS:
4841
llvm::errs()
4942
<< "Assertion failed: "
@@ -52,6 +45,8 @@ void ASSERT_failure(const char *expr, const char *filename, int line, const char
5245
<< filename << ":"
5346
<< line << ".\n";
5447

48+
ASSERT_help();
49+
5550
if (AssertContinue) {
5651
llvm::errs() << "Continuing after failed assertion (-Xllvm -assert-continue)\n";
5752
return;
@@ -67,6 +62,11 @@ void ASSERT_help() {
6762
}
6863
ASSERT_help_shown = 1;
6964

65+
if (!AssertHelp) {
66+
llvm::errs() << "(to display assertion configuration options: -Xllvm -assert-help)\n";
67+
return;
68+
}
69+
7070
llvm::errs() << "\n";
7171
llvm::errs() << "Control assertion behavior with one or more of the following options:\n\n";
7272
llvm::errs() << " -Xllvm -assert-continue\n";

lib/Basic/BasicBridging.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "swift/Basic/Assertions.h"
1314
#include "swift/Basic/BasicBridging.h"
1415
#include "llvm/Support/JSON.h"
1516
#include "llvm/Support/raw_ostream.h"
@@ -22,6 +23,11 @@
2223

2324
using namespace swift;
2425

26+
void assertFail(const char * _Nonnull msg, const char * _Nonnull file,
27+
SwiftUInt line, const char * _Nonnull function) {
28+
ASSERT_failure(msg, file, line, function);
29+
}
30+
2531
//===----------------------------------------------------------------------===//
2632
// MARK: BridgedStringRef
2733
//===----------------------------------------------------------------------===//

lib/SIL/IR/Linker.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
#include "swift/AST/ProtocolConformance.h"
6161
#include "swift/AST/SubstitutionMap.h"
6262
#include "swift/Basic/Assertions.h"
63-
#include "swift/Basic/Require.h"
6463
#include "swift/ClangImporter/ClangModule.h"
6564
#include "swift/SIL/FormalLinkage.h"
6665
#include "swift/Serialization/SerializedSILLoader.h"
@@ -76,20 +75,20 @@ STATISTIC(NumFuncLinked, "Number of SIL functions linked");
7675
//===----------------------------------------------------------------------===//
7776

7877
void SILLinkerVisitor::deserializeAndPushToWorklist(SILFunction *F) {
79-
assert(F->isExternalDeclaration());
78+
ASSERT(F->isExternalDeclaration());
8079

8180
LLVM_DEBUG(llvm::dbgs() << "Imported function: "
8281
<< F->getName() << "\n");
8382
SILFunction *NewF =
8483
Mod.getSILLoader()->lookupSILFunction(F, /*onlyUpdateLinkage*/ false);
85-
assert(!NewF || NewF == F);
84+
ASSERT(!NewF || NewF == F);
8685
if (!NewF || F->isExternalDeclaration()) {
87-
assert((!hasSharedVisibility(F->getLinkage()) || F->hasForeignBody()) &&
86+
ASSERT((!hasSharedVisibility(F->getLinkage()) || F->hasForeignBody()) &&
8887
"cannot deserialize shared function");
8988
return;
9089
}
9190

92-
assert(!F->isAnySerialized() == Mod.isSerialized() &&
91+
ASSERT(!F->isAnySerialized() == Mod.isSerialized() &&
9392
"the de-serializer did set the wrong serialized flag");
9493

9594
F->setBare(IsBare);
@@ -103,9 +102,9 @@ void SILLinkerVisitor::deserializeAndPushToWorklist(SILFunction *F) {
103102
void SILLinkerVisitor::maybeAddFunctionToWorklist(
104103
SILFunction *F, SerializedKind_t callerSerializedKind) {
105104
SILLinkage linkage = F->getLinkage();
106-
require(callerSerializedKind == IsNotSerialized ||
105+
ASSERT((callerSerializedKind == IsNotSerialized ||
107106
F->hasValidLinkageForFragileRef(callerSerializedKind) ||
108-
hasSharedVisibility(linkage) || F->isExternForwardDeclaration(),
107+
hasSharedVisibility(linkage) || F->isExternForwardDeclaration()) &&
109108
"called function has wrong linkage for serialized function");
110109
if (!F->isExternalDeclaration()) {
111110
// The function is already in the module, so no need to de-serialized it.
@@ -175,7 +174,7 @@ void SILLinkerVisitor::linkInVTable(ClassDecl *D) {
175174
// vtables that might have shared linkage yet, so this is only needed in
176175
// the performance pipeline to deserialize more functions early, and expose
177176
// optimization opportunities.
178-
assert(isLinkAll());
177+
ASSERT(isLinkAll());
179178

180179
// Attempt to lookup the Vtbl from the SILModule.
181180
SILVTable *Vtbl = Mod.lookUpVTable(D);

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,8 +1422,8 @@ FixedSizeSlab *SwiftPassInvocation::freeSlab(FixedSizeSlab *slab) {
14221422
}
14231423

14241424
BasicBlockSet *SwiftPassInvocation::allocBlockSet() {
1425-
require(numBlockSetsAllocated < BlockSetCapacity,
1426-
"too many BasicBlockSets allocated");
1425+
ASSERT(numBlockSetsAllocated < BlockSetCapacity &&
1426+
"too many BasicBlockSets allocated");
14271427

14281428
auto *storage = (BasicBlockSet *)blockSetStorage + numBlockSetsAllocated;
14291429
BasicBlockSet *set = new (storage) BasicBlockSet(function);
@@ -1446,8 +1446,8 @@ void SwiftPassInvocation::freeBlockSet(BasicBlockSet *set) {
14461446
}
14471447

14481448
NodeSet *SwiftPassInvocation::allocNodeSet() {
1449-
require(numNodeSetsAllocated < NodeSetCapacity,
1450-
"too many NodeSets allocated");
1449+
ASSERT(numNodeSetsAllocated < NodeSetCapacity &&
1450+
"too many NodeSets allocated");
14511451

14521452
auto *storage = (NodeSet *)nodeSetStorage + numNodeSetsAllocated;
14531453
NodeSet *set = new (storage) NodeSet(function);
@@ -1470,8 +1470,8 @@ void SwiftPassInvocation::freeNodeSet(NodeSet *set) {
14701470
}
14711471

14721472
OperandSet *SwiftPassInvocation::allocOperandSet() {
1473-
require(numOperandSetsAllocated < OperandSetCapacity,
1474-
"too many OperandSets allocated");
1473+
ASSERT(numOperandSetsAllocated < OperandSetCapacity &&
1474+
"too many OperandSets allocated");
14751475

14761476
auto *storage = (OperandSet *)operandSetStorage + numOperandSetsAllocated;
14771477
OperandSet *set = new (storage) OperandSet(function);

lib/SILOptimizer/Utils/PerformanceInlinerUtils.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "swift/SILOptimizer/Utils/PerformanceInlinerUtils.h"
1616
#include "swift/AST/Module.h"
1717
#include "swift/Basic/Assertions.h"
18-
#include "swift/Basic/Require.h"
1918
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
2019
#include "llvm/Support/CommandLine.h"
2120

@@ -853,8 +852,8 @@ SILFunction *swift::getEligibleFunction(FullApplySite AI,
853852
!Callee->hasValidLinkageForFragileRef(Caller->getSerializedKind())) {
854853
llvm::errs() << "caller: " << Caller->getName() << "\n";
855854
llvm::errs() << "callee: " << Callee->getName() << "\n";
856-
require(false, "Should never be inlining a resilient function into "
857-
"a fragile function");
855+
ASSERT(false && "Should never be inlining a resilient function into "
856+
"a fragile function");
858857
}
859858
return nullptr;
860859
}

0 commit comments

Comments
 (0)