20
20
#define SWIFT_SIL_OPTIMIZATIONREMARKEMITTER_H
21
21
22
22
#include " swift/Basic/SourceLoc.h"
23
+ #include " swift/Demangling/Demangler.h"
23
24
#include " swift/SIL/SILBasicBlock.h"
24
25
#include " swift/SIL/SILInstruction.h"
25
26
#include " swift/SIL/SILModule.h"
@@ -34,24 +35,24 @@ namespace OptRemark {
34
35
// / Used in the streaming interface as the general argument type. It
35
36
// / internally converts everything into a key-value pair.
36
37
struct Argument {
37
- std::string Key ;
38
- std::string Val ;
38
+ std::string key ;
39
+ std::string val ;
39
40
// / If set, the debug location corresponding to the value.
40
- SourceLoc Loc ;
41
+ SourceLoc loc ;
41
42
42
- explicit Argument (StringRef Str = " " ) : Key (" String" ), Val (Str) {}
43
- Argument (StringRef Key , StringRef Val ) : Key(Key ), Val(Val ) {}
43
+ explicit Argument (StringRef Str = " " ) : key (" String" ), val (Str) {}
44
+ Argument (StringRef key , StringRef val ) : key(key ), val(val ) {}
44
45
45
- Argument (StringRef Key , int N );
46
- Argument (StringRef Key , long N );
47
- Argument (StringRef Key , long long N );
48
- Argument (StringRef Key , unsigned N );
49
- Argument (StringRef Key , unsigned long N );
50
- Argument (StringRef Key , unsigned long long N );
46
+ Argument (StringRef key , int n );
47
+ Argument (StringRef key , long n );
48
+ Argument (StringRef key , long long n );
49
+ Argument (StringRef key , unsigned n );
50
+ Argument (StringRef key , unsigned long n );
51
+ Argument (StringRef key , unsigned long long n );
51
52
52
- Argument (StringRef Key , SILFunction *F );
53
- Argument (StringRef Key , SILType Ty );
54
- Argument (StringRef Key , CanType Ty );
53
+ Argument (StringRef key , SILFunction *f );
54
+ Argument (StringRef key , SILType ty );
55
+ Argument (StringRef key , CanType ty );
55
56
};
56
57
57
58
// / Shorthand to insert named-value pairs.
@@ -60,132 +61,142 @@ using NV = Argument;
60
61
// / Inserting this into a Remark indents the text when printed as a debug
61
62
// / message.
62
63
struct IndentDebug {
63
- explicit IndentDebug (unsigned Width ) : Width(Width ) {}
64
- unsigned Width ;
64
+ explicit IndentDebug (unsigned width ) : width(width ) {}
65
+ unsigned width ;
65
66
};
66
67
67
68
// / The base class for remarks. This can be created by optimization passed to
68
69
// / report successful and unsuccessful optimizations. CRTP is used to preserve
69
70
// / the underlying type encoding the remark kind in the insertion operator.
70
71
template <typename DerivedT> class Remark {
71
72
// / Arguments collected via the streaming interface.
72
- SmallVector<Argument, 4 > Args ;
73
+ SmallVector<Argument, 4 > args ;
73
74
74
75
// / The name of the pass generating the remark.
75
- StringRef PassName ;
76
+ StringRef passName ;
76
77
77
78
// / Textual identifier for the remark (single-word, camel-case). Can be used
78
- // / by external tools reading the YAML output file for optimization remarks to
79
+ // / by external tools reading the output file for optimization remarks to
79
80
// / identify the remark.
80
- StringRef Identifier ;
81
+ SmallString< 32 > identifier ;
81
82
82
83
// / Source location for the diagnostics.
83
- SourceLoc Location ;
84
+ SourceLoc location ;
84
85
85
86
// / The function for the diagnostics.
86
- SILFunction *Function;
87
+ SILFunction *function;
88
+
89
+ // / The demangled name of \p Function.
90
+ SmallString<64 > demangledFunctionName;
87
91
88
92
// / Indentation used if this remarks is printed as a debug message.
89
- unsigned IndentDebugWidth = 0 ;
93
+ unsigned indentDebugWidth = 0 ;
90
94
91
95
protected:
92
- Remark (StringRef Identifier, SILInstruction &I)
93
- : Identifier(Identifier), Location(I.getLoc().getSourceLoc()),
94
- Function (I.getParent()->getParent()) {}
96
+ Remark (StringRef identifier, SILInstruction &i)
97
+ : identifier((Twine(" sil." ) + identifier).str()),
98
+ location (i.getLoc().getSourceLoc()),
99
+ function(i.getParent()->getParent()),
100
+ demangledFunctionName(Demangle::demangleSymbolAsString(
101
+ function->getName (),
102
+ Demangle::DemangleOptions::SimplifiedUIDemangleOptions())) {}
95
103
96
104
public:
97
- DerivedT &operator <<(StringRef S ) {
98
- Args .emplace_back (S );
105
+ DerivedT &operator <<(StringRef s ) {
106
+ args .emplace_back (s );
99
107
return *static_cast <DerivedT *>(this );
100
108
}
101
109
102
- DerivedT &operator <<(Argument A ) {
103
- Args .push_back (std::move (A ));
110
+ DerivedT &operator <<(Argument a ) {
111
+ args .push_back (std::move (a ));
104
112
return *static_cast <DerivedT *>(this );
105
113
}
106
114
107
- DerivedT &operator <<(IndentDebug ID ) {
108
- IndentDebugWidth = ID. Width ;
115
+ DerivedT &operator <<(IndentDebug indent ) {
116
+ indentDebugWidth = indent. width ;
109
117
return *static_cast <DerivedT *>(this );
110
118
}
111
119
112
- StringRef getPassName () const { return PassName; }
113
- StringRef getIdentifier () const { return Identifier; }
114
- SILFunction *getFunction () const { return Function; }
115
- SourceLoc getLocation () const { return Location; }
120
+ StringRef getPassName () const { return passName; }
121
+ StringRef getIdentifier () const { return identifier; }
122
+ SILFunction *getFunction () const { return function; }
123
+ StringRef getDemangledFunctionName () const { return demangledFunctionName; }
124
+ SourceLoc getLocation () const { return location; }
116
125
std::string getMsg () const ;
117
126
std::string getDebugMsg () const ;
118
127
Remark<DerivedT> &getRemark () { return *this ; }
119
- SmallVector<Argument, 4 > &getArgs () { return Args; }
128
+ SmallVector<Argument, 4 > &getArgs () { return args; }
129
+ ArrayRef<Argument> getArgs () const { return args; }
120
130
121
- void setPassName (StringRef PN ) { PassName = PN ; }
131
+ void setPassName (StringRef name ) { passName = name ; }
122
132
};
123
133
124
134
// / Remark to report a successful optimization.
125
135
struct RemarkPassed : public Remark <RemarkPassed> {
126
- RemarkPassed (StringRef Id , SILInstruction &I ) : Remark(Id, I ) {}
136
+ RemarkPassed (StringRef id , SILInstruction &i ) : Remark(id, i ) {}
127
137
};
128
138
// / Remark to report a unsuccessful optimization.
129
139
struct RemarkMissed : public Remark <RemarkMissed> {
130
- RemarkMissed (StringRef Id , SILInstruction &I ) : Remark(Id, I ) {}
140
+ RemarkMissed (StringRef id , SILInstruction &i ) : Remark(id, i ) {}
131
141
};
132
142
133
143
// / Used to emit the remarks. Passes reporting remarks should create an
134
144
// / instance of this.
135
145
class Emitter {
136
- SILModule &Module ;
137
- std::string PassName ;
138
- bool PassedEnabled ;
139
- bool MissedEnabled ;
146
+ SILModule &module ;
147
+ std::string passName ;
148
+ bool passedEnabled ;
149
+ bool missedEnabled ;
140
150
141
151
// Making these non-generic allows out-of-line definition.
142
- void emit (const RemarkPassed &R );
143
- void emit (const RemarkMissed &R );
144
- static void emitDebug (const RemarkPassed &R );
145
- static void emitDebug (const RemarkMissed &R );
152
+ void emit (const RemarkPassed &remark );
153
+ void emit (const RemarkMissed &remark );
154
+ static void emitDebug (const RemarkPassed &remark );
155
+ static void emitDebug (const RemarkMissed &remark );
146
156
147
157
template <typename RemarkT> bool isEnabled ();
148
158
149
159
public:
150
- Emitter (StringRef PassName , SILModule &M );
160
+ Emitter (StringRef passName , SILModule &m );
151
161
152
162
// / Take a lambda that returns a remark which will be emitted. The
153
163
// / lambda is not evaluated unless remarks are enabled. Second argument is
154
164
// / only used to restrict this to functions.
155
165
template <typename T>
156
- void emit (T RemarkBuilder , decltype (RemarkBuilder ()) * = nullptr) {
157
- using RemarkT = decltype (RemarkBuilder ());
166
+ void emit (T remarkBuilder , decltype (remarkBuilder ()) * = nullptr) {
167
+ using RemarkT = decltype (remarkBuilder ());
158
168
// Avoid building the remark unless remarks are enabled.
159
- if (isEnabled<RemarkT>() || Module. getOptRecordStream ()) {
160
- auto rb = RemarkBuilder ();
161
- rb.setPassName (PassName );
169
+ if (isEnabled<RemarkT>() || module . getSILRemarkStreamer ()) {
170
+ auto rb = remarkBuilder ();
171
+ rb.setPassName (passName );
162
172
emit (rb);
163
173
}
164
174
}
165
175
166
176
// / Emit an optimization remark or debug message.
167
177
template <typename T>
168
- static void emitOrDebug (const char *PassName, Emitter *ORE, T RemarkBuilder,
169
- decltype (RemarkBuilder()) * = nullptr) {
170
- using RemarkT = decltype (RemarkBuilder ());
178
+ static void emitOrDebug (const char *passName, Emitter *emitter,
179
+ T remarkBuilder,
180
+ decltype (remarkBuilder()) * = nullptr) {
181
+ using RemarkT = decltype (remarkBuilder ());
171
182
// Avoid building the remark unless remarks are enabled.
172
- bool EmitRemark =
173
- ORE && (ORE-> isEnabled <RemarkT>() || ORE-> Module . getOptRecordStream ());
183
+ bool emitRemark = emitter && (emitter-> isEnabled <RemarkT>() ||
184
+ emitter-> module . getSILRemarkStreamer ());
174
185
// Same for DEBUG.
175
- bool EmitDebug = false ;
186
+ bool shouldEmitDebug = false ;
176
187
#ifndef NDEBUG
177
- EmitDebug |= llvm::DebugFlag && llvm::isCurrentDebugType (PassName );
188
+ shouldEmitDebug |= llvm::DebugFlag && llvm::isCurrentDebugType (passName );
178
189
#endif // NDEBUG
179
190
180
- if (EmitRemark || EmitDebug ) {
181
- auto R = RemarkBuilder ();
182
- if (EmitDebug )
183
- emitDebug (R );
184
- if (EmitRemark ) {
185
- // If we have ORE use the PassName that was set up with ORE . DEBUG_TYPE
191
+ if (emitRemark || shouldEmitDebug ) {
192
+ auto r = remarkBuilder ();
193
+ if (shouldEmitDebug )
194
+ emitDebug (r );
195
+ if (emitRemark ) {
196
+ // If we have an Emitter use the PassName that was set up. DEBUG_TYPE
186
197
// may be different if a pass is calling other modules.
187
- R .setPassName (ORE-> PassName );
188
- ORE ->emit (R );
198
+ r .setPassName (emitter-> passName );
199
+ emitter ->emit (r );
189
200
}
190
201
}
191
202
}
@@ -195,10 +206,10 @@ class Emitter {
195
206
OptRemark::Emitter::emitOrDebug (DEBUG_TYPE, __VA_ARGS__)
196
207
197
208
template <> inline bool Emitter::isEnabled<RemarkMissed>() {
198
- return MissedEnabled ;
209
+ return missedEnabled ;
199
210
}
200
211
template <> inline bool Emitter::isEnabled<RemarkPassed>() {
201
- return PassedEnabled ;
212
+ return passedEnabled ;
202
213
}
203
214
} // namespace OptRemark
204
215
} // namespace swift
0 commit comments