Add Print helpers to reactor
New tools for debugging JITed code.
rr::Print() is a new function that emits a call to printf() using the provided message and optional values. There is also an overload that accepts additional file, line, and function parameters to help identify the call site.
Format values passed to rr::Print() are implicity cast to a new rr::PrintValues type which does the work of dispatching the value type to the correct printf() format specifier(s) and argument value(s). A single rr::Print() format value can automatically be expanded into multiple printf values - for example an rr::Float4 will expand to "%f %f %f %f" and four scalar float values.
rr::Print() format values can be any of the following types:
* Reactor LValues, RValues, Pointers.
* Standard Plain-Old-Value types (int, float, bool, etc)
* Custom types that specialize the PrintValue::Ty template struct.
* Static arrays in the form T[N] where T can be any of the above.
The sw::Vector4f and sw::Vector4s types have already had custom formatters written.
These new functions and types described above are typically not called directly. Instead there are two helper macros which simplifies usage:
RR_LOG() is a new macro that wraps rr::Print(), automatically populating the function, file and line parameters and appending a newline to the string.
RR_WATCH() is a new helper macro that prints the name and value of all the supplied arguments. For example, if you had the Int and bool variables 'foo' and 'bar' that you want to print, you can simply write:
RR_WATCH(foo, bar)
When this JIT compiled code is executed, it will print:
"foo: 1, bar: true"
All of this code is disabled in non-debug builds, or if the reactor backend is not LLVM 7+.
Change-Id: Ia39b1e507b6afaa3bb1d33e40b1333017f4b4f21
Reviewed-on: https://swiftshader-review.googlesource.com/c/24768
Tested-by: Chris Forbes <chrisforbes@google.com>
Reviewed-by: Chris Forbes <chrisforbes@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
diff --git a/src/Shader/ShaderCore.hpp b/src/Shader/ShaderCore.hpp
index fd12bf0..6b54428 100644
--- a/src/Shader/ShaderCore.hpp
+++ b/src/Shader/ShaderCore.hpp
@@ -381,4 +381,35 @@
};
}
+#ifdef ENABLE_RR_PRINT
+namespace rr {
+ template <> struct PrintValue::Ty<sw::Vector4f>
+ {
+ static constexpr const char* fmt =
+ "[x: <%f, %f, %f, %f>,"
+ " y: <%f, %f, %f, %f>,"
+ " z: <%f, %f, %f, %f>,"
+ " w: <%f, %f, %f, %f>]";
+
+ static std::vector<rr::Value*> val(const sw::Vector4f& v)
+ {
+ return PrintValue::vals(v.x, v.y, v.z, v.w);
+ }
+ };
+ template <> struct PrintValue::Ty<sw::Vector4s>
+ {
+ static constexpr const char* fmt =
+ "[x: <%d, %d, %d, %d>,"
+ " y: <%d, %d, %d, %d>,"
+ " z: <%d, %d, %d, %d>,"
+ " w: <%d, %d, %d, %d>]";
+
+ static std::vector<rr::Value*> val(const sw::Vector4s& v)
+ {
+ return PrintValue::vals(v.x, v.y, v.z, v.w);
+ }
+ };
+}
+#endif // ENABLE_RR_PRINT
+
#endif // sw_ShaderCore_hpp