Skip to content

Commit 95bf159

Browse files
authored
Make sure the shader execution heap is cache aligned (#1159)
1 parent af601c1 commit 95bf159

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

src/liboslexec/context.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,10 @@ ShadingContext::execute_init (ShaderGroup &sgroup, ShaderGlobals &ssg, bool run)
7171

7272
// Allocate enough space on the heap
7373
size_t heap_size_needed = sgroup.llvm_groupdata_size();
74-
if (heap_size_needed > m_heap.size()) {
75-
if (shadingsys().debug())
76-
infof(" ShadingContext %p growing heap to %d",
77-
static_cast<void*>(this), heap_size_needed);
78-
m_heap.resize (heap_size_needed);
79-
}
74+
reserve_heap(heap_size_needed);
8075
// Zero out the heap memory we will be using
8176
if (shadingsys().m_clearmemory)
82-
memset (&m_heap[0], 0, heap_size_needed);
77+
memset (m_heap.get(), 0, heap_size_needed);
8378

8479
// Set up closure storage
8580
m_closure_pool.clear();
@@ -100,7 +95,7 @@ ShadingContext::execute_init (ShaderGroup &sgroup, ShaderGlobals &ssg, bool run)
10095
ssg.context = this;
10196
ssg.renderer = renderer();
10297
ssg.Ci = NULL;
103-
run_func (&ssg, &m_heap[0]);
98+
run_func (&ssg, m_heap.get());
10499
}
105100

106101
if (profile)
@@ -124,7 +119,7 @@ ShadingContext::execute_layer (ShaderGlobals &ssg, int layernumber)
124119
if (! run_func)
125120
return false;
126121

127-
run_func (&ssg, &m_heap[0]);
122+
run_func (&ssg, m_heap.get());
128123

129124
if (profile)
130125
m_ticks += timer.ticks();
@@ -257,9 +252,9 @@ ShadingContext::symbol_data (const Symbol &sym) const
257252
if (! sgroup.optimized())
258253
return NULL; // can't retrieve symbol if we didn't optimize it
259254

260-
if (sym.dataoffset() >= 0 && (int)m_heap.size() > sym.dataoffset()) {
255+
if (sym.dataoffset() >= 0 && (int)m_heapsize > sym.dataoffset()) {
261256
// lives on the heap
262-
return &m_heap[sym.dataoffset()];
257+
return m_heap.get() + sym.dataoffset();
263258
}
264259

265260
// doesn't live on the heap

src/liboslexec/oslexec_pvt.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,13 @@ class OSLEXECPUBLIC ShadingContext {
17411741
record_error(ErrorHandler::EH_MESSAGE, Strutil::sprintf (fmt, args...));
17421742
}
17431743

1744+
void reserve_heap(size_t size) {
1745+
if (size > m_heapsize) {
1746+
m_heap.reset((char *)OIIO::aligned_malloc(size, OIIO_CACHE_LINE_SIZE));
1747+
m_heapsize = size;
1748+
}
1749+
}
1750+
17441751
private:
17451752

17461753
void free_dict_resources ();
@@ -1750,7 +1757,9 @@ class OSLEXECPUBLIC ShadingContext {
17501757
PerThreadInfo *m_threadinfo; ///< Ptr to our thread's info
17511758
mutable TextureSystem::Perthread *m_texture_thread_info; ///< Ptr to texture thread info
17521759
ShaderGroup *m_group; ///< Ptr to shader group
1753-
std::vector<char> m_heap; ///< Heap memory
1760+
// Heap memory
1761+
std::unique_ptr<char, decltype(&OIIO::aligned_free)> m_heap { nullptr, &OIIO::aligned_free };
1762+
size_t m_heapsize = 0;
17541763
typedef std::unordered_map<ustring, std::unique_ptr<regex>, ustringHash> RegexMap;
17551764
RegexMap m_regex_map; ///< Compiled regex's
17561765
MessageList m_messages; ///< Message blackboard

0 commit comments

Comments
 (0)