Skip to content
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
4 changes: 2 additions & 2 deletions src/cmake/testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ macro (osl_add_all_tests)
oslinfo-arrayparams oslinfo-colorctrfloat
oslinfo-metadata oslinfo-noparams
osl-imageio
oso-bad-argderivs oso-bad-jumps oso-bad-maincode
oso-bad-structfields
oso-bad-argderivs oso-bad-arity oso-bad-jumps
oso-bad-maincode oso-bad-structfields
oso-excess-defaults
paramval-floatpromotion
pragma-nowarn
Expand Down
2 changes: 2 additions & 0 deletions src/liboslexec/backendcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,8 @@ BackendCpp::build_cpp_code(int opbegin, int opend, bool do_indent_block)

auto* opdesc = shadingsys().op_descriptor(op.opname());
if (opdesc && opdesc->cppgen) {
// The loader checked arg counts; ops made since must comply too.
OSL_DASSERT(opdesc->nargs_ok(op.nargs()));
// If the opcode has a C++ generator, call it
if (!opdesc->cppgen(*this, opnum))
outputfmtln("// Cpp {} FAILED", op.opname());
Expand Down
2 changes: 2 additions & 0 deletions src/liboslexec/batched_llvm_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,8 @@ BatchedBackendLLVM::build_llvm_code(int beginop, int endop,
const Opcode& op = inst()->ops()[opnum];
const OpDescriptor* opd = shadingsys().op_descriptor(op.opname());
if (opd && opd->llvmgenwide) {
// The loader checked arg counts; ops made since must comply too.
OSL_DASSERT(opd->nargs_ok(op.nargs()));
if (shadingsys().debug_uninit() /* debug uninitialized vals */)
llvm_generate_debug_uninit(op);
if (shadingsys().llvm_debug_ops())
Expand Down
2 changes: 2 additions & 0 deletions src/liboslexec/llvm_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,8 @@ BackendLLVM::build_llvm_code(int beginop, int endop, llvm::BasicBlock* bb)
const Opcode& op = inst()->ops()[opnum];
const OpDescriptor* opd = shadingsys().op_descriptor(op.opname());
if (opd && opd->llvmgen) {
// The loader checked arg counts; ops made since must comply too.
OSL_DASSERT(opd->nargs_ok(op.nargs()));
if (shadingsys().debug_uninit() /* debug uninitialized vals */)
llvm_generate_debug_uninit(op);
if (shadingsys().llvm_debug_ops())
Expand Down
29 changes: 26 additions & 3 deletions src/liboslexec/loadshader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class OSOReaderToMaster final : public OSOReader {
int m_sym_default_index; ///< Next sym default value to fill in
bool m_errors; ///< Did we hit any errors?
typedef std::unordered_map<ustring, int> UstringIntMap;
UstringIntMap m_symmap; ///< map sym name to index
UstringIntMap m_symmap; ///< map sym name to index
const OpDescriptor* m_opdesc = nullptr; ///< Descriptor of current op
};


Expand Down Expand Up @@ -556,6 +557,7 @@ OSOReaderToMaster::instruction(int /*label*/, const char* opcode)
m_nargs = 0;
m_reading_instruction = true;
const OpDescriptor* od = m_shadingsys.op_descriptor(uopcode);
m_opdesc = od;
if (od) {
// Replace the name in case it was aliased for compatibility
uopcode = od->name;
Expand Down Expand Up @@ -648,9 +650,30 @@ OSOReaderToMaster::check_jumps()
void
OSOReaderToMaster::instruction_end()
{
m_master->m_ops.back().set_args(m_firstarg, m_nargs);
m_master->m_ops.back().source(m_sourcefile, m_sourceline);
Opcode& op(m_master->m_ops.back());
op.set_args(m_firstarg, m_nargs);
op.source(m_sourcefile, m_sourceline);
m_reading_instruction = false;
// Reject an op with the wrong number of args, so that code generators
// and constant folders can count on the args they need being present.
if (m_opdesc && !m_opdesc->nargs_ok(int(m_nargs))) {
if (m_opdesc->minargs == m_opdesc->maxargs)
m_shadingsys.errorfmt(
"Parsing shader \"{}\": op {} ({}) has {} args (expected {})",
m_master->shadername(), m_master->m_ops.size() - 1, op.opname(),
m_nargs, m_opdesc->minargs);
else if (m_opdesc->maxargs == OpDescriptor::AnyNumArgs)
m_shadingsys.errorfmt(
"Parsing shader \"{}\": op {} ({}) has {} args (expected at least {})",
m_master->shadername(), m_master->m_ops.size() - 1, op.opname(),
m_nargs, m_opdesc->minargs);
else
m_shadingsys.errorfmt(
"Parsing shader \"{}\": op {} ({}) has {} args (expected {}..{})",
m_master->shadername(), m_master->m_ops.size() - 1, op.opname(),
m_nargs, m_opdesc->minargs, m_opdesc->maxargs);
m_errors = true;
}
}


Expand Down
19 changes: 18 additions & 1 deletion src/liboslexec/oslexec_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <limits>
#include <list>
#include <map>
#include <memory>
Expand Down Expand Up @@ -149,14 +150,22 @@ struct OpDescriptor {
// simple if wholly overwrites arg0, no other writes, no side effects
bool simple_assign { false };
int flags { 0 }; // other flags
// Range of the number of args the op may have. The .oso loader rejects
// any op outside its range, so code generators and constant folders
// may count on at least minargs args being present.
int minargs { 0 };
int maxargs { AnyNumArgs };

static constexpr int AnyNumArgs = std::numeric_limits<int>::max();

OpDescriptor() = default;

OpDescriptor(const char* n, OpLLVMGen ll,
#if OSL_USE_BATCHED
OpLLVMGenWide llw,
#endif
OpFolder fold = NULL, bool simple = false, int flags = 0)
OpFolder fold = NULL, bool simple = false, int flags = 0,
int minargs = 0, int maxargs = AnyNumArgs)
: name(n)
, llvmgen(ll)
,
Expand All @@ -167,7 +176,15 @@ struct OpDescriptor {
folder(fold)
, simple_assign(simple)
, flags(flags)
, minargs(minargs)
, maxargs(maxargs)
{
}

// Is `nargs` an allowed number of args for this op?
bool nargs_ok(int nargs) const
{
return nargs >= minargs && nargs <= maxargs;
}

enum FlagValues {
Expand Down
2 changes: 2 additions & 0 deletions src/liboslexec/runtimeoptimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,8 @@ RuntimeOptimizer::optimize_ops(int beginop, int endop,
// constant-fold, dispatch to the appropriate routine.
if (optimize() >= 2 && m_opt_constant_fold) {
if (opd && opd->folder) {
// The loader checked arg counts; new ops must comply too.
OSL_DASSERT(opd->nargs_ok(op->nargs()));
int c = (*opd->folder)(*this, opnum);
if (c) {
changed += c;
Expand Down
Loading
Loading