Skip to content

Commit cb9935c

Browse files
authored
Fixes to group names and debug bitcode output (#833)
* Better guaranteed unique naming of missing names. * When dumping ll/bc files for debugging to filenames derived from the group name, beware group names that have slashes in them! Also, sheesh, watch out for group names that are so long that they exceed the longest filename allowed (256 chars for most modern filesystems). * Decided only the ll files are useful for debugging, I don't think we need the binary bc files. * Simplify ShaderGroup ctr by using C++11 data member defaults and delegating constructors.
1 parent 0729cca commit cb9935c

File tree

4 files changed

+42
-41
lines changed

4 files changed

+42
-41
lines changed

src/liboslexec/instance.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -721,28 +721,23 @@ ShaderInstance::mergeable (const ShaderInstance &b, const ShaderGroup &g) const
721721

722722

723723
ShaderGroup::ShaderGroup (string_view name)
724-
: m_optimized(0), m_does_nothing(false),
725-
m_llvm_groupdata_size(0), m_num_entry_layers(0),
726-
m_llvm_compiled_version(NULL),
727-
m_name(name), m_exec_repeat(1), m_raytype_queries(-1), m_raytypes_on(0), m_raytypes_off(0)
728724
{
729-
m_executions = 0;
730-
m_stat_total_shading_time_ticks = 0;
731725
m_id = ++(*(atomic_int *)&next_id);
726+
if (name.size()) {
727+
m_name = name;
728+
} else {
729+
// No name -- make one up using the unique
730+
m_name = ustring::format ("unnamed_group_%d", m_id);
731+
}
732732
}
733733

734734

735735

736736
ShaderGroup::ShaderGroup (const ShaderGroup &g, string_view name)
737-
: m_optimized(0), m_does_nothing(false),
738-
m_llvm_groupdata_size(0), m_num_entry_layers(g.m_num_entry_layers),
739-
m_llvm_compiled_version(NULL),
740-
m_layers(g.m_layers),
741-
m_name(name), m_exec_repeat(1), m_raytype_queries(-1), m_raytypes_on(0), m_raytypes_off(0)
737+
: ShaderGroup(name) // delegate most of the work
742738
{
743-
m_executions = 0;
744-
m_stat_total_shading_time_ticks = 0;
745-
m_id = ++(*(atomic_int *)&next_id);
739+
m_num_entry_layers = g.m_num_entry_layers;
740+
m_layers = g.m_layers;
746741
}
747742

748743

src/liboslexec/llvm_instance.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,15 +1105,17 @@ BackendLLVM::run ()
11051105

11061106
// Debug code to dump the pre-optimized bitcode to a file
11071107
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());
1108+
// Make a safe group name that doesn't have "/" in it! Also beware
1109+
// filename length limits.
1110+
std::string safegroup = Strutil::replace (group().name(), "/", ".", true);
1111+
if (safegroup.size() > 235)
1112+
safegroup = Strutil::format ("TRUNC_%s_%d", safegroup.substr(safegroup.size()-235), group().id());
1113+
std::string name = Strutil::format ("%s.ll", safegroup);
11131114
std::ofstream out (name, std::ios_base::out | std::ios_base::trunc);
11141115
if (out.good()) {
11151116
out << ll.bitcode_string (ll.module());
1116-
out.close ();
1117+
} else {
1118+
shadingcontext()->error ("Could not write to '%s'", name);
11171119
}
11181120
}
11191121

@@ -1132,15 +1134,17 @@ BackendLLVM::run ()
11321134

11331135
// Debug code to dump the post-optimized bitcode to a file
11341136
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());
1137-
ll.write_bitcode_file (name.c_str());
1138-
name = Strutil::format ("%s_%s_%d_opt.ll", group().name(),
1139-
inst()->layername(), inst()->id());
1137+
// Make a safe group name that doesn't have "/" in it! Also beware
1138+
// filename length limits.
1139+
std::string safegroup = Strutil::replace (group().name(), "/", ".", true);
1140+
if (safegroup.size() > 235)
1141+
safegroup = Strutil::format ("TRUNC_%s_%d", safegroup.substr(safegroup.size()-235), group().id());
1142+
std::string name = Strutil::format ("%s_opt.ll", safegroup);
11401143
std::ofstream out (name, std::ios_base::out | std::ios_base::trunc);
11411144
if (out.good()) {
11421145
out << ll.bitcode_string (ll.module());
1143-
out.close ();
1146+
} else {
1147+
shadingcontext()->error ("Could not write to '%s'", name);
11441148
}
11451149
}
11461150

src/liboslexec/llvm_util.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,9 +1489,11 @@ LLVM_Util::write_bitcode_file (const char *filename, std::string *err)
14891489
{
14901490
std::error_code local_error;
14911491
llvm::raw_fd_ostream out (filename, local_error, llvm::sys::fs::F_None);
1492-
llvm::WriteBitcodeToFile (module(), out);
1493-
if (err && local_error)
1494-
*err = local_error.message ();
1492+
if (! out.has_error()) {
1493+
llvm::WriteBitcodeToFile (module(), out);
1494+
if (err && local_error)
1495+
*err = local_error.message ();
1496+
}
14951497
}
14961498

14971499

src/liboslexec/oslexec_pvt.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,20 +1554,20 @@ class ShaderGroup {
15541554
// Put all the things that are read-only (after optimization) and
15551555
// needed on every shade execution at the front of the struct, as much
15561556
// together on one cache line as possible.
1557-
volatile int m_optimized; ///< Is it already optimized?
1558-
bool m_does_nothing; ///< Is the shading group just func() { return; }
1559-
size_t m_llvm_groupdata_size; ///< Heap size needed for its groupdata
1557+
volatile int m_optimized = 0; ///< Is it already optimized?
1558+
bool m_does_nothing = false; ///< Is the shading group just func() { return; }
1559+
size_t m_llvm_groupdata_size = 0;///< Heap size needed for its groupdata
15601560
int m_id; ///< Unique ID for the group
1561-
int m_num_entry_layers; ///< Number of marked entry layers
1562-
RunLLVMGroupFunc m_llvm_compiled_version;
1563-
RunLLVMGroupFunc m_llvm_compiled_init;
1561+
int m_num_entry_layers = 0; ///< Number of marked entry layers
1562+
RunLLVMGroupFunc m_llvm_compiled_version = nullptr;
1563+
RunLLVMGroupFunc m_llvm_compiled_init = nullptr;
15641564
std::vector<RunLLVMGroupFunc> m_llvm_compiled_layers;
15651565
std::vector<ShaderInstanceRef> m_layers;
15661566
ustring m_name;
1567-
int m_exec_repeat; ///< How many times to execute group
1568-
int m_raytype_queries; ///< Bitmask of raytypes queried
1569-
int m_raytypes_on; ///< Bitmask of raytypes we assume to be on
1570-
int m_raytypes_off; ///< Bitmask of raytypes we assume to be off
1567+
int m_exec_repeat = 1; ///< How many times to execute group
1568+
int m_raytype_queries = -1; ///< Bitmask of raytypes queried
1569+
int m_raytypes_on = 0; ///< Bitmask of raytypes we assume to be on
1570+
int m_raytypes_off = 0; ///< Bitmask of raytypes we assume to be off
15711571
mutable mutex m_mutex; ///< Thread-safe optimization
15721572
std::vector<ustring> m_textures_needed;
15731573
std::vector<ustring> m_closures_needed;
@@ -1582,8 +1582,8 @@ class ShaderGroup {
15821582
bool m_unknown_textures_needed;
15831583
bool m_unknown_closures_needed;
15841584
bool m_unknown_attributes_needed;
1585-
atomic_ll m_executions; ///< Number of times the group executed
1586-
atomic_ll m_stat_total_shading_time_ticks; ///< Total shading time (ticks)
1585+
atomic_ll m_executions {0}; ///< Number of times the group executed
1586+
atomic_ll m_stat_total_shading_time_ticks {0}; ///< Total shading time (ticks)
15871587

15881588
friend class OSL::pvt::ShadingSystemImpl;
15891589
friend class OSL::pvt::BackendLLVM;

0 commit comments

Comments
 (0)