3333
3434using 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+
3643static 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