Skip to content

Commit 7da2333

Browse files
Merge pull request #80167 from aschwaighofer/print_cond_fail_message_strings
SIL: Add an option to print all the cond_fail message strings encountered during IRGenPrepare
2 parents 35c8ba8 + e11e026 commit 7da2333

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

lib/SILOptimizer/Mandatory/IRGenPrepare.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333

3434
using namespace swift;
3535

36+
// Print the message string of encountered `cond_fail` instructions the first
37+
// time the message string is encountered.
38+
static llvm::cl::opt<bool> PrintCondFailMessages(
39+
"print-cond-fail-messages", llvm::cl::init(false),
40+
llvm::cl::desc("print cond_fail messages"));
41+
static llvm::DenseSet<StringRef> CondFailMessages;
42+
3643
static bool cleanFunction(SILFunction &fn) {
3744
bool madeChange = false;
3845

@@ -43,6 +50,26 @@ static bool cleanFunction(SILFunction &fn) {
4350
SILInstruction *inst = &*i;
4451
++i;
4552

53+
// Print cond_fail messages the first time a specific cond_fail message
54+
// string is encountered.
55+
// Run the swift-frontend in a mode that will generate LLVM IR adding
56+
// the option `-print-cond-fail-messages` will dump all cond_fail
57+
// message strings encountered in the SIL.
58+
// ```
59+
// % swift-frontend -Xllvm -print-cond-fail-messages -emit-ir/-c ...
60+
// ...
61+
// cond_fail message encountered: Range out of bounds
62+
// cond_fail message encountered: Array index is out of range
63+
// ...
64+
// ```
65+
if (PrintCondFailMessages) {
66+
if (auto CFI = dyn_cast<CondFailInst>(inst)) {
67+
auto msg = CFI->getMessage();
68+
if (CondFailMessages.insert(msg).second)
69+
llvm::dbgs() << "cond_fail message encountered: " << msg << "\n";
70+
}
71+
}
72+
4673
// Remove calls to Builtin.poundAssert() and Builtin.staticReport().
4774
auto *bi = dyn_cast<BuiltinInst>(inst);
4875
if (!bi) {

0 commit comments

Comments
 (0)