Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSL_ALLOCA #1589

Merged
merged 2 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/include/OSL/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,19 @@
# define OSL_DASSERT_MSG(x, ...) ((void)sizeof(x)) /*NOLINT*/
#endif

#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#define OSL_STACK_ARRAY(TYPE, NAME, DYNAMIC_COUNT) TYPE * NAME = reinterpret_cast<TYPE *>(alloca(sizeof(TYPE)*(DYNAMIC_COUNT)))

/// OSL_ALLOCA is used to allocate smallish amount of memory on the stack,
/// equivalent of C99 type var_name[size].
///
/// NOTE: in a debug build, this will assert for allocations >= 1MB, which is
/// much too big. Hopefully this will keep us from abusing alloca and having
/// stack overflows. The rule of thumb is that it's ok to use alloca for small
/// things of bounded size, but not for anything that could be arbitrarily
/// big, which should instead use heap allocation.
#if defined(__GNUC__)
# define OSL_ALLOCA(type, size) (assert(size < (1<<20)), (size) != 0 ? (reinterpret_cast<type*>(__builtin_alloca((size) * sizeof(type)))) : nullptr)
#else
// Utilize variable length array compiler extension when available
#define OSL_STACK_ARRAY(TYPE, NAME, DYNAMIC_COUNT) TYPE NAME[DYNAMIC_COUNT]
# define OSL_ALLOCA(type, size) (assert(size < (1<<20)), (size) != 0 ? (reinterpret_cast<type*>(alloca((size) * sizeof(type)))) : nullptr)
#endif

OSL_NAMESPACE_ENTER
Expand Down
8 changes: 4 additions & 4 deletions src/liboslexec/batched_analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,8 @@ class DependencyTreeTracker {
auto write_iter = begin_at(write_pos);

const int read_count = read_iter.depth() + 1;
OSL_STACK_ARRAY(Position, read_path, read_count);
int read_depth = 0;
Position* read_path = OSL_ALLOCA(Position, read_count);
int read_depth = 0;
{
// Loop structure is to allow
// writing the end position into the read path
Expand All @@ -658,8 +658,8 @@ class DependencyTreeTracker {
}

const int writeCount = write_iter.depth() + 1;
OSL_STACK_ARRAY(Position, write_path, writeCount);
int write_depth = 0;
Position* write_path = OSL_ALLOCA(Position, writeCount);
int write_depth = 0;
{
// Loop structure is to allow
// writing the end position into the write path
Expand Down
4 changes: 2 additions & 2 deletions src/liboslexec/constfold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2410,7 +2410,7 @@ DECLFOLDER(constfold_gettextureinfo)
ustring filename = Filename.get_string();
ustring dataname = Dataname.get_string();
TypeDesc t = Data.typespec().simpletype();
void* mydata = OIIO_ALLOCA(char, t.size());
void* mydata = OSL_ALLOCA(char, t.size());
// FIXME(ptex) -- exclude folding of ptex, since these things
// can vary per face.
ustring errormessage;
Expand Down Expand Up @@ -2776,7 +2776,7 @@ DECLFOLDER(constfold_pointcloud_get)
return 0;

// Must transfer to size_t array
size_t* indices = OIIO_ALLOCA(size_t, count);
size_t* indices = OSL_ALLOCA(size_t, count);
for (int i = 0; i < count; ++i)
indices[i] = Indices.get_int(i);

Expand Down
6 changes: 3 additions & 3 deletions src/liboslexec/pointcloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ RendererServices::pointcloud_get(ShaderGlobals* sg, ustring filename,
// Actual data query
if (partio_type == TypeString) {
// strings are special cases because they are stored as int index
int* strindices = OIIO_ALLOCA(int, count);
int* strindices = OSL_ALLOCA(int, count);
const_cast<Partio::ParticlesData*>(cloud)->data(
*attr, count, (const Partio::ParticleIndex*)indices,
/*sorted=*/false, (void*)strindices);
Expand Down Expand Up @@ -385,7 +385,7 @@ osl_pointcloud_search(ShaderGlobals* sg, const char* filename, void* center,
if (sizeof(int) == sizeof(size_t) && out_indices)
indices = (size_t*)out_indices;
else
indices = OIIO_ALLOCA(size_t, max_points);
indices = OSL_ALLOCA(size_t, max_points);

int count
= sg->renderer->pointcloud_search(sg, USTR(filename), *((Vec3*)center),
Expand Down Expand Up @@ -425,7 +425,7 @@ osl_pointcloud_get(ShaderGlobals* sg, const char* filename, void* in_indices,
if (shadingsys.no_pointcloud()) // Debug mode to skip pointcloud expense
return 0;

size_t* indices = OIIO_ALLOCA(size_t, count);
size_t* indices = OSL_ALLOCA(size_t, count);
for (int i = 0; i < count; ++i)
indices[i] = ((int*)in_indices)[i];

Expand Down
8 changes: 4 additions & 4 deletions src/liboslexec/shadeimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ shade_image(ShadingSystem& shadingsys, ShaderGroup& group,

// Gather some information about the outputs once, rather than for
// each pixel.
const ShaderSymbol** output_sym = OIIO_ALLOCA(const ShaderSymbol*,
outputs.size());
TypeDesc* output_type = OIIO_ALLOCA(TypeDesc, outputs.size());
int* output_nchans = OIIO_ALLOCA(int, outputs.size());
const ShaderSymbol** output_sym = OSL_ALLOCA(const ShaderSymbol*,
outputs.size());
TypeDesc* output_type = OSL_ALLOCA(TypeDesc, outputs.size());
int* output_nchans = OSL_ALLOCA(int, outputs.size());
for (int i = 0; i < int(outputs.size()); ++i) {
output_sym[i] = shadingsys.find_symbol(group, outputs[i]);
output_type[i] = shadingsys.symbol_typedesc(output_sym[i]);
Expand Down
16 changes: 8 additions & 8 deletions src/liboslexec/wide/wide_oppointcloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,16 @@ default_pointcloud_search(BatchedShaderGlobals* bsg, ustring filename,
// indices array
// TODO: evaluate if sg->context->alloc_scratch should be used instead?
Partio::ParticleIndex* indices
= (Partio::ParticleIndex*)OIIO_ALLOCA(size_t, max_points);
= (Partio::ParticleIndex*)OSL_ALLOCA(size_t, max_points);

Wide<const OSL::Vec3> wcenter(wcenter_);

// Partio uses array of structure data layout for distances,
// and our batched representation is
// structure of arrays (wide) so we need a scalar temporary
// distances array
float* dist2 = OIIO_ALLOCA(float, max_points);
SortedPointRecord* sorted = OIIO_ALLOCA(SortedPointRecord, max_points);
float* dist2 = OSL_ALLOCA(float, max_points);
SortedPointRecord* sorted = OSL_ALLOCA(SortedPointRecord, max_points);
auto windices = results.windices();
auto wnum_points = results.wnum_points();
results.mask().foreach ([=](ActiveLane lane) -> void {
Expand All @@ -118,7 +118,7 @@ default_pointcloud_search(BatchedShaderGlobals* bsg, ustring filename,
// indices at the same time.
if (sort && count > 1) {
//SortedPointRecord *sorted = (SortedPointRecord *) sg->context->alloc_scratch (count * sizeof(SortedPointRecord), sizeof(SortedPointRecord));
//SortedPointRecord *sorted = OIIO_ALLOCA(SortedPointRecord, count);
//SortedPointRecord *sorted = OSL_ALLOCA(SortedPointRecord, count);
for (int i = 0; i < count; ++i)
sorted[i] = SortedPointRecord(dist2[i], indices[i]);
std::sort(sorted, sorted + count, SortedPointCompare());
Expand Down Expand Up @@ -154,7 +154,7 @@ default_pointcloud_search(BatchedShaderGlobals* bsg, ustring filename,
// We are going to need the positions if we need to compute
// distance derivs
//OSL::Vec3 *positions = (OSL::Vec3 *) sg->context->alloc_scratch (sizeof(OSL::Vec3) * count, sizeof(float));
OSL::Vec3* positions = OIIO_ALLOCA(OSL::Vec3, count);
OSL::Vec3* positions = OSL_ALLOCA(OSL::Vec3, count);
// FIXME(Partio): this function really should be marked as const because it is just a wrapper of a private const method
const_cast<Partio::ParticlesData*>(cloud)->data(
*pos_attr, count, indices, true, (void*)positions);
Expand Down Expand Up @@ -255,14 +255,14 @@ default_pointcloud_get(BatchedShaderGlobals* bsg, ustring filename,
if (partio_type == OIIO::TypeString) {
// strings are special cases because they are stored as int index
// Ensure alloca's happen outside loops
strindices = OIIO_ALLOCA(int, attr_type.numelements());
strindices = OSL_ALLOCA(int, attr_type.numelements());
} else {
aos_buffer = OIIO_ALLOCA(char, attr_type.size());
aos_buffer = OSL_ALLOCA(char, attr_type.size());
}
}

Partio::ParticleIndex* indices
= (Partio::ParticleIndex*)OIIO_ALLOCA(size_t, windices.length());
= (Partio::ParticleIndex*)OSL_ALLOCA(size_t, windices.length());

wout_data.mask().foreach ([=, &success](ActiveLane lane) -> void {
int count = wnum_points[lane];
Expand Down
6 changes: 3 additions & 3 deletions src/testshade/testshade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ save_outputs(SimpleRenderer* rend, ShadingSystem* shadingsys,
} else if (t.basetype == TypeDesc::INT) {
// We are outputting an integer variable, so we need to
// convert it to floating point.
float* pixel = OIIO_ALLOCA(float, nchans);
float* pixel = OSL_ALLOCA(float, nchans);
OIIO::convert_types(TypeDesc::BASETYPE(t.basetype), data,
TypeDesc::FLOAT, pixel, nchans);
outputimg->setpixel(x, y, &pixel[0]);
Expand Down Expand Up @@ -1347,8 +1347,8 @@ batched_save_outputs(SimpleRenderer* rend, ShadingSystem* shadingsys,
Wide<const int[], WidthT> batchResults(
shadingsys->symbol_address(*ctx, out_symbol), nchans);
// TODO: Try not to do alloca's inside a loop
int* intPixel = OIIO_ALLOCA(int, nchans);
float* floatPixel = OIIO_ALLOCA(float, nchans);
int* intPixel = OSL_ALLOCA(int, nchans);
float* floatPixel = OSL_ALLOCA(float, nchans);
for (int batchIndex = 0; batchIndex < batchSize; ++batchIndex) {
int x = bx[batchIndex];
int y = by[batchIndex];
Expand Down