Skip to content

Commit 5fe0c5b

Browse files
authored
Some bitcode and symbol debugging aids (#831)
* Add a new shadingsys option "llvm_optput_bitcode" that will dump the bitcode for each group, even if the other debug options aren't turned all the way on. * bitcode now dumps in text as well as binary. (It saves a step of running "llvm-dis" if your goal is just ot see the text in the first place, and also it helps the llvm bug where sometimes it refuses to work because of mismatched version strings.) * Make sure that both in the bitcode output and other debug symbols, we are more careful about being sure the shader group name is included, not just the layer name, and also if a layer name is not specified to ss->Shader(), make one up based on the shader master name and the layer number.
1 parent 5ea1cce commit 5fe0c5b

14 files changed

Lines changed: 106 additions & 34 deletions

File tree

src/include/OSL/llvm_util.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,10 @@ class OSLEXECPUBLIC LLVM_Util {
474474
/// file. If err is not NULL, errors will be deposited there.
475475
void write_bitcode_file (const char *filename, std::string *err=NULL);
476476

477-
/// Convert a function's bitcode to a string.
477+
/// Convert a whole module's bitcode to a string.
478+
std::string bitcode_string (llvm::Module *module);
479+
480+
/// Convert one function's bitcode to a string.
478481
std::string bitcode_string (llvm::Function *func);
479482

480483
/// Delete the IR for the body of the given function to reclaim its

src/include/OSL/oslexec.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ class OSLEXECPUBLIC ShadingSystem
157157
/// layer functions.
158158
/// int llvm_debug_ops Extra printfs for each OSL op (helpful
159159
/// for devs to find crashes)
160+
/// int llvm_output_bitcode Output the full bitcode for each group,
161+
/// for debugging. (0)
160162
/// int max_local_mem_KB Error if shader group needs more than this
161163
/// much local storage to execute (1024K)
162164
/// string debug_groupname Name of shader group -- debug only this one

src/liboslexec/llvm_gen.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,8 @@ BackendLLVM::llvm_call_layer (int layer, bool unconditional)
157157
// insert point is now then_block
158158
}
159159

160-
std::string name = Strutil::format ("%s_%d", parent->layername().c_str(),
161-
parent->id());
162160
// Mark the call as a fast call
163-
llvm::Value *funccall = ll.call_function (name.c_str(), args, 2);
161+
llvm::Value *funccall = ll.call_function (layer_function_name(group(), *parent).c_str(), args, 2);
164162
if (!parent->entry_layer())
165163
ll.mark_fast_func_call (funccall);
166164

src/liboslexec/llvm_instance.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727
*/
2828

2929
#include <cmath>
30+
#include <iostream>
3031
#include <unordered_map>
3132

3233
#include <OpenImageIO/timer.h>
@@ -761,8 +762,7 @@ BackendLLVM::build_llvm_instance (bool groupentry)
761762
{
762763
// Make a layer function: void layer_func(ShaderGlobals*, GroupData*)
763764
// Note that the GroupData* is passed as a void*.
764-
std::string unique_layer_name = Strutil::format ("%s_%d", inst()->layername(), inst()->id());
765-
765+
std::string unique_layer_name = layer_function_name();
766766
bool is_entry_layer = group().is_entry_layer(layer());
767767
ll.current_function (
768768
ll.make_function (unique_layer_name,
@@ -1103,6 +1103,20 @@ BackendLLVM::run ()
11031103
}
11041104
ll.internalize_module_functions ("osl_", external_function_names, entry_function_names);
11051105

1106+
// Debug code to dump the pre-optimized bitcode to a file
1107+
if (llvm_debug() >= 2 || shadingsys().llvm_output_bitcode()) {
1108+
std::string name = Strutil::format ("%s_%s_%d.bc", group().name(),
1109+
inst()->layername(), inst()->id());
1110+
ll.write_bitcode_file (name.c_str());
1111+
name = Strutil::format ("%s_%s_%d.ll", group().name(),
1112+
inst()->layername(), inst()->id());
1113+
std::ofstream out (name, std::ios_base::out | std::ios_base::trunc);
1114+
if (out.good()) {
1115+
out << ll.bitcode_string (ll.module());
1116+
out.close ();
1117+
}
1118+
}
1119+
11061120
// Optimize the LLVM IR unless it's a do-nothing group.
11071121
if (! group().does_nothing())
11081122
ll.do_optimize();
@@ -1116,11 +1130,18 @@ BackendLLVM::run ()
11161130
std::cout.flush();
11171131
}
11181132

1119-
// Debug code to dump the resulting bitcode to a file
1120-
if (llvm_debug() >= 2) {
1121-
std::string name = Strutil::format ("%s_%d.bc", inst()->layername(),
1122-
inst()->id());
1133+
// Debug code to dump the post-optimized bitcode to a file
1134+
if (llvm_debug() >= 2 || shadingsys().llvm_output_bitcode()) {
1135+
std::string name = Strutil::format ("%s_%s_%d_opt.bc", group().name(),
1136+
inst()->layername(), inst()->id());
11231137
ll.write_bitcode_file (name.c_str());
1138+
name = Strutil::format ("%s_%s_%d_opt.ll", group().name(),
1139+
inst()->layername(), inst()->id());
1140+
std::ofstream out (name, std::ios_base::out | std::ios_base::trunc);
1141+
if (out.good()) {
1142+
out << ll.bitcode_string (ll.module());
1143+
out.close ();
1144+
}
11241145
}
11251146

11261147
// Force the JIT to happen now and retrieve the JITed function pointers

src/liboslexec/llvm_util.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,30 @@ LLVM_Util::bitcode_string (llvm::Function *func)
14951495

14961496

14971497

1498+
std::string
1499+
LLVM_Util::bitcode_string (llvm::Module *module)
1500+
{
1501+
std::string s;
1502+
llvm::raw_string_ostream stream (s);
1503+
1504+
#if OSL_LLVM_VERSION < 40
1505+
for (llvm::Module::iterator iter = module->begin(); iter != module->end(); iter++) {
1506+
llvm::Function *funcptr = static_cast<llvm::Function*>(iter);
1507+
stream << (*funcptr);
1508+
stream << "\n";
1509+
}
1510+
#else
1511+
for (const llvm::Function& func : module->getFunctionList()) {
1512+
stream << func;
1513+
stream << "\n";
1514+
}
1515+
#endif
1516+
1517+
return stream.str();
1518+
}
1519+
1520+
1521+
14981522
void
14991523
LLVM_Util::delete_func_body (llvm::Function *func)
15001524
{

src/liboslexec/oslexec_pvt.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ class ShadingSystemImpl
609609
int llvm_debug () const { return m_llvm_debug; }
610610
int llvm_debug_layers () const { return m_llvm_debug_layers; }
611611
int llvm_debug_ops () const { return m_llvm_debug_ops; }
612+
int llvm_output_bitcode () const { return m_llvm_output_bitcode; }
612613
bool fold_getattribute () const { return m_opt_fold_getattribute; }
613614
bool opt_texture_handle () const { return m_opt_texture_handle; }
614615
int opt_passes() const { return m_opt_passes; }
@@ -802,6 +803,7 @@ class ShadingSystemImpl
802803
int m_llvm_debug; ///< More LLVM debugging output
803804
int m_llvm_debug_layers; ///< Add layer enter/exit printfs
804805
int m_llvm_debug_ops; ///< Add printfs to every op
806+
int m_llvm_output_bitcode; ///< Output bitcode for each group
805807
ustring m_debug_groupname; ///< Name of sole group to debug
806808
ustring m_debug_layername; ///< Name of sole layer to debug
807809
ustring m_opt_layername; ///< Name of sole layer to optimize
@@ -1997,6 +1999,16 @@ class OSOProcessorBase {
19971999
return m_bblockids[opnum];
19982000
}
19992001

2002+
// Mangle the group and layer into a unique function name
2003+
std::string layer_function_name (const ShaderGroup &group,
2004+
const ShaderInstance &inst) {
2005+
return Strutil::format ("%s_%s_%d", group.name(),
2006+
inst.layername(), inst.id());
2007+
}
2008+
std::string layer_function_name () {
2009+
return layer_function_name (group(), *inst());
2010+
}
2011+
20002012
protected:
20012013
ShadingSystemImpl &m_shadingsys; ///< Backpointer to shading system
20022014
ShaderGroup &m_group; ///< Group we're processing

src/liboslexec/shadingsys.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ ShadingSystemImpl::ShadingSystemImpl (RendererServices *renderer,
669669
m_llvm_optimize(0),
670670
m_debug(0), m_llvm_debug(0),
671671
m_llvm_debug_layers(0), m_llvm_debug_ops(0),
672+
m_llvm_output_bitcode(0),
672673
m_commonspace_synonym("world"),
673674
m_colorspace("Rec709"),
674675
m_max_local_mem_KB(2048),
@@ -1092,6 +1093,7 @@ ShadingSystemImpl::attribute (string_view name, TypeDesc type,
10921093
ATTR_SET ("llvm_debug", int, m_llvm_debug);
10931094
ATTR_SET ("llvm_debug_layers", int, m_llvm_debug_layers);
10941095
ATTR_SET ("llvm_debug_ops", int, m_llvm_debug_ops);
1096+
ATTR_SET ("llvm_output_bitcode", int, m_llvm_output_bitcode);
10951097
ATTR_SET ("strict_messages", int, m_strict_messages);
10961098
ATTR_SET ("range_checking", int, m_range_checking);
10971099
ATTR_SET ("unknown_coordsys_error", int, m_unknown_coordsys_error);
@@ -1203,6 +1205,7 @@ ShadingSystemImpl::getattribute (string_view name, TypeDesc type,
12031205
ATTR_DECODE ("llvm_debug", int, m_llvm_debug);
12041206
ATTR_DECODE ("llvm_debug_layers", int, m_llvm_debug_layers);
12051207
ATTR_DECODE ("llvm_debug_ops", int, m_llvm_debug_ops);
1208+
ATTR_DECODE ("llvm_output_bitcode", int, m_llvm_output_bitcode);
12061209
ATTR_DECODE ("strict_messages", int, m_strict_messages);
12071210
ATTR_DECODE ("range_checking", int, m_range_checking);
12081211
ATTR_DECODE ("unknown_coordsys_error", int, m_unknown_coordsys_error);
@@ -1619,6 +1622,7 @@ ShadingSystemImpl::getstats (int level) const
16191622
INTOPT (llvm_debug);
16201623
BOOLOPT (llvm_debug_layers);
16211624
BOOLOPT (llvm_debug_ops);
1625+
BOOLOPT (llvm_output_bitcode);
16221626
BOOLOPT (lazylayers);
16231627
BOOLOPT (lazyglobals);
16241628
BOOLOPT (lazyunconnected);
@@ -1957,6 +1961,14 @@ ShadingSystemImpl::Shader (string_view shaderusage,
19571961
return false;
19581962
}
19591963

1964+
// If a layer name was not supplied, make one up.
1965+
std::string local_layername;
1966+
if (layername.empty()) {
1967+
local_layername = OIIO::Strutil::format ("%s_%d", master->shadername(),
1968+
m_curgroup->nlayers());
1969+
layername = string_view (local_layername);
1970+
}
1971+
19601972
ShaderInstanceRef instance (new ShaderInstance (master, layername));
19611973
instance->parameters (m_pending_params);
19621974
m_pending_params.clear ();

testsuite/array-range/ref/out-alt.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ Compiled test.osl -> test.oso
22
constant index:
33
reading:
44
array[1] = 1
5-
ERROR: Index [10] out of range $const1[0..4]: test.osl:8 (group <unnamed group>, layer 0 <unnamed layer>, shader test)
5+
ERROR: Index [10] out of range $const1[0..4]: test.osl:8 (group <unnamed group>, layer 0 test_0, shader test)
66
array[10] = 4
77
writing:
8-
ERROR: Index [10] out of range array[0..4]: test.osl:10 (group <unnamed group>, layer 0 <unnamed layer>, shader test)
8+
ERROR: Index [10] out of range array[0..4]: test.osl:10 (group <unnamed group>, layer 0 test_0, shader test)
99
variable index:
1010
reading:
1111
array[0] = 0
1212
array[1] = 1
1313
array[2] = 2
1414
array[3] = 3
1515
array[4] = 42
16-
ERROR: Index [5] out of range array[0..4]: test.osl:15 (group <unnamed group>, layer 0 <unnamed layer>, shader test)
16+
ERROR: Index [5] out of range array[0..4]: test.osl:15 (group <unnamed group>, layer 0 test_0, shader test)
1717
array[5] = 42
1818
writing:
19-
ERROR: Index [5] out of range array[0..4]: test.osl:19 (group <unnamed group>, layer 0 <unnamed layer>, shader test)
19+
ERROR: Index [5] out of range array[0..4]: test.osl:19 (group <unnamed group>, layer 0 test_0, shader test)
2020

testsuite/array-range/ref/out.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ Compiled test.osl -> test.oso
22
constant index:
33
reading:
44
array[1] = 1
5-
ERROR: Index [10] out of range array[0..4]: test.osl:8 (group <unnamed group>, layer 0 <unnamed layer>, shader test)
5+
ERROR: Index [10] out of range array[0..4]: test.osl:8 (group <unnamed group>, layer 0 test_0, shader test)
66
array[10] = 4
77
writing:
8-
ERROR: Index [10] out of range array[0..4]: test.osl:10 (group <unnamed group>, layer 0 <unnamed layer>, shader test)
8+
ERROR: Index [10] out of range array[0..4]: test.osl:10 (group <unnamed group>, layer 0 test_0, shader test)
99
variable index:
1010
reading:
1111
array[0] = 0
1212
array[1] = 1
1313
array[2] = 2
1414
array[3] = 3
1515
array[4] = 42
16-
ERROR: Index [5] out of range array[0..4]: test.osl:15 (group <unnamed group>, layer 0 <unnamed layer>, shader test)
16+
ERROR: Index [5] out of range array[0..4]: test.osl:15 (group <unnamed group>, layer 0 test_0, shader test)
1717
array[5] = 42
1818
writing:
19-
ERROR: Index [5] out of range array[0..4]: test.osl:19 (group <unnamed group>, layer 0 <unnamed layer>, shader test)
19+
ERROR: Index [5] out of range array[0..4]: test.osl:19 (group <unnamed group>, layer 0 test_0, shader test)
2020

testsuite/component-range/ref/out-alt.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ Compiled test.osl -> test.oso
22
constant index:
33
reading:
44
V[1] = 1
5-
ERROR: Index [10] out of range $const1[0..2]: test.osl:8 (group <unnamed group>, layer 0 <unnamed layer>, shader test)
5+
ERROR: Index [10] out of range $const1[0..2]: test.osl:8 (group <unnamed group>, layer 0 test_0, shader test)
66
V[10] = 2
77
writing:
8-
ERROR: Index [10] out of range V[0..2]: test.osl:10 (group <unnamed group>, layer 0 <unnamed layer>, shader test)
8+
ERROR: Index [10] out of range V[0..2]: test.osl:10 (group <unnamed group>, layer 0 test_0, shader test)
99
variable index:
1010
reading:
1111
V[0] = 0
1212
V[1] = 1
1313
V[2] = 42
14-
ERROR: Index [3] out of range V[0..2]: test.osl:15 (group <unnamed group>, layer 0 <unnamed layer>, shader test)
14+
ERROR: Index [3] out of range V[0..2]: test.osl:15 (group <unnamed group>, layer 0 test_0, shader test)
1515
V[3] = 42
1616
writing:
17-
ERROR: Index [3] out of range V[0..2]: test.osl:19 (group <unnamed group>, layer 0 <unnamed layer>, shader test)
17+
ERROR: Index [3] out of range V[0..2]: test.osl:19 (group <unnamed group>, layer 0 test_0, shader test)
1818

0 commit comments

Comments
 (0)