Skip to content

Commit cc76ede

Browse files
committed
Add a -print-cond-fail-messages-include-function-name option
Additionally, also consider the function name as the key to base cond_fail removal on under `-cond-fail-config-file`.
1 parent 815c3e8 commit cc76ede

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/SILOptimizer/Mandatory/IRGenPrepare.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ using namespace swift;
3838
static llvm::cl::opt<bool> PrintCondFailMessages(
3939
"print-cond-fail-messages", llvm::cl::init(false),
4040
llvm::cl::desc("print cond_fail messages"));
41+
static llvm::cl::opt<bool> IncludeCondFailMessagesFunction(
42+
"print-cond-fail-messages-include-function-name", llvm::cl::init(false),
43+
llvm::cl::desc("when printing cond_fail messages include"
44+
"the current SIL function name"));
45+
4146
static llvm::DenseSet<StringRef> CondFailMessages;
4247

4348
static bool cleanFunction(SILFunction &fn) {
@@ -64,7 +69,11 @@ static bool cleanFunction(SILFunction &fn) {
6469
// ```
6570
if (PrintCondFailMessages) {
6671
if (auto CFI = dyn_cast<CondFailInst>(inst)) {
67-
auto msg = CFI->getMessage();
72+
auto msg = CFI->getMessage().str();
73+
if (IncludeCondFailMessagesFunction) {
74+
msg.append(" in ");
75+
msg.append(fn.getName().str());
76+
}
6877
if (CondFailMessages.insert(msg).second)
6978
llvm::dbgs() << "cond_fail message encountered: " << msg << "\n";
7079
}

lib/SILOptimizer/SILCombiner/SILCombine.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,8 @@ void SwiftPassInvocation::eraseInstruction(SILInstruction *inst) {
707707
}
708708
}
709709

710+
// cond_fail removal based on cond_fail message and containing function name.
711+
//
710712
// The standard library uses _precondition calls which have a message argument.
711713
//
712714
// Allow disabling the generated cond_fail by these message arguments.
@@ -728,6 +730,10 @@ void SwiftPassInvocation::eraseInstruction(SILInstruction *inst) {
728730
//
729731
// The optimizer will remove these cond_fails if the swift frontend is invoked
730732
// with -Xllvm -cond-fail-config-file=/path/to/disable_cond_fails.
733+
//
734+
// Additionally, also interpret the lines as function names and check whether
735+
// the current cond_fail is contained in a listed function when considering
736+
// whether to remove it.
731737
static llvm::cl::opt<std::string> CondFailConfigFile(
732738
"cond-fail-config-file", llvm::cl::init(""),
733739
llvm::cl::desc("read the cond_fail message strings to elimimate from file"));
@@ -750,6 +756,13 @@ bool SILCombiner::shouldRemoveCondFail(CondFailInst &CFI) {
750756
}
751757
fs.close();
752758
}
759+
// Check whether the cond_fail's containing function was listed in the config
760+
// file.
761+
if (CondFailsToRemove.find(CFI.getFunction()->getName().str()) !=
762+
CondFailsToRemove.end())
763+
return true;
764+
765+
// Check whether the cond_fail's message was listed in the config file.
753766
auto message = CFI.getMessage();
754767
return CondFailsToRemove.find(message.str()) != CondFailsToRemove.end();
755768
}

0 commit comments

Comments
 (0)