@@ -1143,8 +1143,8 @@ be passed by value.
11431143
11441144.. _DEBUG :
11451145
1146- The ``LLVM_DEBUG() `` macro and ``-debug `` option
1147- ------------------------------------------------
1146+ The ``LDBG `` and `` LLVM_DEBUG() `` macros and ``-debug `` option
1147+ --------------------------------------------------------------
11481148
11491149Often when working on your pass you will put a bunch of debugging printouts and
11501150other code into your pass. After you get it working, you want to remove it, but
@@ -1154,36 +1154,65 @@ Naturally, because of this, you don't want to delete the debug printouts, but
11541154you don't want them to always be noisy. A standard compromise is to comment
11551155them out, allowing you to enable them if you need them in the future.
11561156
1157- The ``llvm/Support/Debug.h `` (`doxygen
1158- <https://llvm.org/doxygen/Debug_8h_source.html> `__) file provides a macro named
1159- ``LLVM_DEBUG() `` that is a much nicer solution to this problem. Basically, you can
1160- put arbitrary code into the argument of the ``LLVM_DEBUG `` macro, and it is only
1161- executed if '``opt ``' (or any other tool) is run with the '``-debug ``' command
1162- line argument:
1157+ The ``llvm/Support/DebugLog.h `` file provides a macro named ``LDBG `` that is a
1158+ more convenient way to add debug output to your code. It is a macro that
1159+ provides a raw_ostream that is used to write the debug output.
1160+
1161+ .. code-block :: c++
1162+
1163+ LDBG() << "I am here!";
1164+
1165+ It'll only print the output if the debug output is enabled.
1166+ It also supports a `level ` argument to control the verbosity of the output.
11631167
11641168.. code-block :: c++
11651169
1166- LLVM_DEBUG(dbgs() << "I am here!\n ");
1170+ LDBG(2) << "I am here!";
1171+
1172+ A ``DEBUG_TYPE `` macro should be defined in the file before using ``LDBG() ``.
1173+ The file name and line number are automatically added to the output, as well as
1174+ a terminating newline.
11671175
1168- Then you can run your pass like this:
1176+ The debug output can be enabled by passing the `` -debug `` command line argument.
11691177
11701178.. code-block :: none
11711179
11721180 $ opt < a.bc > /dev/null -mypass
11731181 <no output>
11741182 $ opt < a.bc > /dev/null -mypass -debug
1175- I am here!
1183+ [my-pass:2] MyPass.cpp:123 I am here!
11761184
1177- Using the ``LLVM_DEBUG() `` macro instead of a home-brewed solution allows you to not
1178- have to create "yet another" command-line option for the debug output for your
1179- pass. Note that ``LLVM_DEBUG() `` macros are disabled for non-asserts builds, so they
1180- do not cause a performance impact at all (for the same reason, they should also
1181- not contain side-effects!).
1185+ While `LDBG() ` is useful to add debug output to your code, there are cases
1186+ where you may need to guard a block of code with a debug check. The
1187+ ``llvm/Support/Debug.h `` (`doxygen
1188+ <https://llvm.org/doxygen/Debug_8h_source.html> `__) file provides a macro named
1189+ ``LLVM_DEBUG() `` that offers a solution to this problem. You can put arbitrary
1190+ code into the argument of the ``LLVM_DEBUG `` macro, and it is only executed if
1191+ '``opt ``' (or any other tool) is run with the '``-debug ``' command
1192+ line argument.
11821193
1183- One additional nice thing about the ``LLVM_DEBUG() `` macro is that you can enable or
1184- disable it directly in gdb. Just use "``set DebugFlag=0 ``" or "``set
1185- DebugFlag=1 ``" from the gdb if the program is running. If the program hasn't
1186- been started yet, you can always just run it with ``-debug ``.
1194+ .. code-block :: c++
1195+
1196+ LLVM_DEBUG({
1197+ llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> logBuffer =
1198+ llvm::MemoryBuffer: :getFile(logFile->first);
1199+ if (logBuffer && !(*logBuffer)->getBuffer().empty()) {
1200+ LDBG() << "Output:\n " << (*logBuffer)->getBuffer();
1201+ }
1202+ });
1203+
1204+
1205+ Using these macros instead of a home-brewed solution allows you to not have to
1206+ create "yet another" command-line option for the debug output for your pass.
1207+ Note that ``LDBG() `` and ``LLVM_DEBUG() `` macros are disabled for non-asserts
1208+ builds, so they do not cause a performance impact at all (for the same reason,
1209+ they should also not contain side-effects!).
1210+
1211+ One additional nice thing about the ``LDBG() `` and ``LLVM_DEBUG() `` macros is
1212+ that you can enable or disable it directly in gdb. Just use
1213+ "``set DebugFlag=0 ``" or "``set DebugFlag=1 ``" from the gdb if the program is
1214+ running. If the program hasn't been started yet, you can always just run it
1215+ with ``-debug ``.
11871216
11881217.. _DEBUG_TYPE :
11891218
@@ -1199,52 +1228,74 @@ follows:
11991228.. code-block :: c++
12001229
12011230 #define DEBUG_TYPE "foo"
1202- LLVM_DEBUG(dbgs() << "'foo' debug type\n ");
1203- #undef DEBUG_TYPE
1204- #define DEBUG_TYPE "bar"
1205- LLVM_DEBUG(dbgs() << "'bar' debug type\n ");
1206- #undef DEBUG_TYPE
1231+ LDBG(2) << "Hello,";
1232+ // DEBUG_TYPE can be overridden locally, here with "bar"
1233+ LDBG("bar", 3) << "'bar' debug type";
1234+
12071235
1208- Then you can run your pass like this:
1236+ A more fine-grained control can be achieved by passing the ``-debug-only ``
1237+ command line argument:
12091238
12101239.. code-block :: none
12111240
1212- $ opt < a.bc > /dev/null -mypass
1213- <no output>
1214- $ opt < a.bc > /dev/null -mypass -debug
1215- 'foo' debug type
1216- 'bar' debug type
12171241 $ opt < a.bc > /dev/null -mypass -debug-only=foo
1218- 'foo' debug type
1219- $ opt < a.bc > /dev/null -mypass -debug-only=bar
1220- 'bar' debug type
1242+ [foo:2] MyPass.cpp:123 Hello,
12211243 $ opt < a.bc > /dev/null -mypass -debug-only=foo,bar
1222- 'foo' debug type
1223- 'bar' debug type
1224-
1225- Of course, in practice, you should only set ``DEBUG_TYPE `` at the top of a file,
1226- to specify the debug type for the entire module. Be careful that you only do
1227- this after including ``Debug.h `` and not around any #include of headers. Also, you
1228- should use names more meaningful than "foo" and "bar", because there is no
1229- system in place to ensure that names do not conflict. If two different modules
1230- use the same string, they will all be turned on when the name is specified.
1244+ [foo:2] MyPass.cpp:123 Hello,
1245+ [bar:3] MyPass.cpp:124 World!
1246+ $ opt < a.bc > /dev/null -mypass -debug-only=bar
1247+ [bar:3] MyPass.cpp:124 World!
1248+
1249+ The debug-only argument is a comma separated list of debug types and levels.
1250+ The level is an optional integer setting the maximum debug level to enable:
1251+
1252+ .. code-block :: none
1253+
1254+ $ opt < a.bc > /dev/null -mypass -debug-only=foo:2,bar:2
1255+ [foo:2] MyPass.cpp:123 Hello,
1256+ $ opt < a.bc > /dev/null -mypass -debug-only=foo:1,bar:3
1257+ [bar:3] MyPass.cpp:124 World!
1258+
1259+ Instead of opting in specific debug types, the ``-debug-only `` option also
1260+ works to filter out debug output for specific debug types, by omitting the
1261+ level (or setting it to 0):
1262+
1263+ .. code-block :: none
1264+
1265+ $ opt < a.bc > /dev/null -mypass -debug-only=foo:
1266+ [bar:3] MyPass.cpp:124 World!
1267+ $ opt < a.bc > /dev/null -mypass -debug-only=bar:0,foo:
1268+
1269+
1270+ In practice, you should only set ``DEBUG_TYPE `` at the top of a file, to
1271+ specify the debug type for the entire module. Be careful that you only do
1272+ this after you're done including headers (in particular ``Debug.h ``/``DebugLog.h ``).
1273+ Also, you should use names more meaningful than "foo" and "bar", because there
1274+ is no system in place to ensure that names do not conflict. If two different
1275+ modules use the same string, they will all be turned on when the name is specified.
12311276This allows, for example, all debug information for instruction scheduling to be
12321277enabled with ``-debug-only=InstrSched ``, even if the source lives in multiple
12331278files. The name must not include a comma (,) as that is used to separate the
12341279arguments of the ``-debug-only `` option.
12351280
1236- For performance reasons, -debug-only is not available in optimized build
1237- (`` --enable-optimized ``) of LLVM.
1281+ For performance reasons, -debug-only is not available in non-asserts build
1282+ of LLVM.
12381283
1239- The ``DEBUG_WITH_TYPE `` macro is also available for situations where you would
1240- like to set ``DEBUG_TYPE ``, but only for one specific ``DEBUG `` statement. It
1241- takes an additional first parameter, which is the type to use. For example, the
1242- preceding example could be written as:
1284+ The ``DEBUG_WITH_TYPE `` macro is an alternative to the ``LLVM_DEBUG() `` macro
1285+ for situations where you would like to set ``DEBUG_TYPE ``, but only for one
1286+ specific ``LLVM_DEBUG `` statement. It takes an additional first parameter,
1287+ which is the type to use. The example from the previous section could be
1288+ written as:
12431289
12441290.. code-block :: c++
12451291
1246- DEBUG_WITH_TYPE("foo", dbgs() << "'foo' debug type\n ");
1247- DEBUG_WITH_TYPE("bar", dbgs() << "'bar' debug type\n ");
1292+ DEBUG_WITH_TYPE("special-type", {
1293+ llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> logBuffer =
1294+ llvm::MemoryBuffer: :getFile(logFile->first);
1295+ if (logBuffer && !(*logBuffer)->getBuffer().empty()) {
1296+ LDBG("special-type") << "Output:\n " << (*logBuffer)->getBuffer();
1297+ }
1298+ });
12481299
12491300.. _Statistic :
12501301
0 commit comments