Skip to content

Commit 1fd53d2

Browse files
committed
SIL: Allow to selectively disabled cond_fails by cond_fail message
The standard library uses `_precondition` calls which have a message argument. Allow disabling the generated cond_fail by these message arguments. For example: _precondition(source >= (0 as T), "Negative value is not representable") Results in a `cond_fail "Negative value is not representable"`. This commit allows for specifying a file that contains these messages on each line. /path/to/disable_cond_fails: ``` Negative value is not representable Array index is out of range ``` The optimizer will remove these cond_fails if the swift frontend is invoked with `-Xllvm -cond-fail-config-file=/path/to/disable_cond_fails`.
1 parent 92ead52 commit 1fd53d2

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

lib/SILOptimizer/SILCombiner/SILCombine.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@
4343
#include "llvm/ADT/SmallPtrSet.h"
4444
#include "llvm/ADT/SmallVector.h"
4545
#include "llvm/ADT/Statistic.h"
46+
#include "llvm/Support/CommandLine.h"
4647
#include "llvm/Support/Debug.h"
48+
#include <fstream>
49+
#include <set>
4750

4851
using namespace swift;
4952

@@ -690,3 +693,29 @@ void SwiftPassInvocation::eraseInstruction(SILInstruction *inst) {
690693
}
691694
}
692695
}
696+
697+
static llvm::cl::opt<std::string> CondFailConfigFile(
698+
"cond-fail-config-file", llvm::cl::init(""),
699+
llvm::cl::desc("read the cond_fail message strings to elimimate from file"));
700+
701+
static std::set<std::string> CondFailsToRemove;
702+
703+
bool SILCombiner::shouldRemoveCondFail(CondFailInst &CFI) {
704+
if (CondFailConfigFile.empty())
705+
return false;
706+
707+
std::fstream fs(CondFailConfigFile);
708+
if (!fs) {
709+
llvm::errs() << "cannot cond_fail disablement config file\n";
710+
exit(1);
711+
}
712+
if (CondFailsToRemove.empty()) {
713+
std::string line;
714+
while (std::getline(fs, line)) {
715+
CondFailsToRemove.insert(line);
716+
}
717+
fs.close();
718+
}
719+
auto message = CFI.getMessage();
720+
return CondFailsToRemove.find(message.str()) != CondFailsToRemove.end();
721+
}

lib/SILOptimizer/SILCombiner/SILCombiner.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ class SILCombiner :
136136

137137
bool runOnFunction(SILFunction &F);
138138

139+
bool shouldRemoveCondFail(CondFailInst &);
140+
139141
void clear() {
140142
Iteration = 0;
141143
Worklist.resetChecked();

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,9 @@ SILInstruction *SILCombiner::visitCondFailInst(CondFailInst *CFI) {
803803
if (RemoveCondFails)
804804
return eraseInstFromFunction(*CFI);
805805

806+
if (shouldRemoveCondFail(*CFI))
807+
return eraseInstFromFunction(*CFI);
808+
806809
auto *I = dyn_cast<IntegerLiteralInst>(CFI->getOperand());
807810
if (!I)
808811
return nullptr;

0 commit comments

Comments
 (0)