Skip to content

Commit

Permalink
Apply recent run layer call optimization to batched execution
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Hellmuth <chellmuth@gmail.com>
  • Loading branch information
chellmuth committed Apr 11, 2023
1 parent e402988 commit 2ef8e0d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
10 changes: 4 additions & 6 deletions src/liboslexec/batched_backendllvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,9 @@ class BatchedBackendLLVM : public OSOProcessorBase {

/// Execute the upstream connection (if any, and if not yet run) that
/// establishes the value of symbol sym, which has index 'symindex'
/// within the current layer rop.inst(). If already_run is not NULL,
/// it points to a vector of layer indices that are known to have been
/// run -- those can be skipped without dynamically checking their
/// execution status.
/// within the current layer rop.inst().
void llvm_run_connected_layers(const Symbol& sym, int symindex,
int opnum = -1,
std::set<int>* already_run = NULL);
int opnum = -1);



Expand Down Expand Up @@ -705,6 +701,8 @@ class BatchedBackendLLVM : public OSOProcessorBase {
shadingsys().m_stat_tex_calls_as_handles += 1;
}

void increment_useparam_ops() { shadingsys().m_stat_useparam_ops++; }

void llvm_print_mask(const char* title, llvm::Value* mask = nullptr);

/// Return the userdata index for the given Symbol. Return -1 if the Symbol
Expand Down
34 changes: 17 additions & 17 deletions src/liboslexec/batched_llvm_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,15 @@ BatchedBackendLLVM::llvm_call_layer(int layer, bool unconditional)

if (!unconditional)
ll.op_branch(after_block); // also moves insert point

shadingsys().m_stat_call_layers_inserted++;
}



void
BatchedBackendLLVM::llvm_run_connected_layers(const Symbol& sym, int symindex,
int opnum,
std::set<int>* already_run)
int opnum)
{
if (sym.valuesource() != Symbol::ConnectedVal)
return; // Nothing to do
Expand All @@ -171,16 +172,6 @@ BatchedBackendLLVM::llvm_run_connected_layers(const Symbol& sym, int symindex,
const Connection& con(inst()->connection(c));
// If the connection gives a value to this param
if (con.dst.param == symindex) {
// already_run is a set of layers run for this particular op.
// Just so we don't stupidly do several consecutive checks on
// whether we ran this same layer. It's JUST for this op.
if (already_run) {
if (already_run->count(con.srclayer))
continue; // already ran that one on this op
else
already_run->insert(con.srclayer); // mark it
}

if (inmain) {
// There is an instance-wide m_layers_already_run that tries
// to remember which earlier layers have unconditionally
Expand All @@ -199,6 +190,16 @@ BatchedBackendLLVM::llvm_run_connected_layers(const Symbol& sym, int symindex,
}
}

// m_call_layers_inserted tracks if we've already run this layer either:
// * inside the current basic block (opnum >= 0)
// * while writing output parameters (opnum == -1)
const CallLayerKey key = { opnum >= 0 ? m_bblockids[opnum] : opnum,
con.srclayer };
if (m_call_layers_inserted.count(key)) {
continue;
}
m_call_layers_inserted.insert(key);

// If the earlier layer it comes from has not yet been
// executed, do so now.
llvm_call_layer(con.srclayer);
Expand All @@ -223,15 +224,11 @@ LLVMGEN(llvm_gen_useparam)
std::cout << ">>>>>>>>>>>>>>>>>>>>>llvm_gen_useparam <<<<<<<<<<<<<<<<<<<"
<< std::endl);

// If we have multiple params needed on this statement, don't waste
// time checking the same upstream layer more than once.
std::set<int> already_run;

Opcode& op(rop.inst()->ops()[opnum]);
for (int i = 0; i < op.nargs(); ++i) {
Symbol& sym = *rop.opargsym(op, i);
int symindex = rop.inst()->arg(op.firstarg() + i);
rop.llvm_run_connected_layers(sym, symindex, opnum, &already_run);
rop.llvm_run_connected_layers(sym, symindex, opnum);
// If it's an interpolated (userdata) parameter and we're
// initializing them lazily, now we have to do it.
if ((sym.symtype() == SymTypeParam
Expand All @@ -243,6 +240,9 @@ LLVMGEN(llvm_gen_useparam)
rop.ll.current_mask()));
}
}

rop.increment_useparam_ops();

return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/liboslexec/batched_llvm_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2096,6 +2096,7 @@ BatchedBackendLLVM::build_llvm_instance(bool groupentry)
// records for each.
find_basic_blocks();
find_conditionals();
m_call_layers_inserted.clear();

build_llvm_code(inst()->maincodebegin(), inst()->maincodeend());

Expand Down
5 changes: 2 additions & 3 deletions src/liboslexec/llvm_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,8 @@ BackendLLVM::llvm_run_connected_layers(Symbol& sym, int symindex, int opnum)
// m_call_layers_inserted tracks if we've already run this layer either:
// * inside the current basic block (opnum >= 0)
// * while writing output parameters (opnum == -1)
const auto key = std::make_pair(opnum >= 0 ? m_bblockids[opnum]
: opnum,
con.srclayer);
const CallLayerKey key = { opnum >= 0 ? m_bblockids[opnum] : opnum,
con.srclayer };
if (m_call_layers_inserted.count(key)) {
continue;
}
Expand Down
15 changes: 13 additions & 2 deletions src/liboslexec/oslexec_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -2626,8 +2626,19 @@ class OSOProcessorBase {
std::vector<char> m_in_conditional; ///< Whether each op is in a cond
std::vector<char> m_in_loop; ///< Whether each op is in a loop
int m_first_return; ///< Op number of first return or exit
std::set<std::pair<int, int>>
m_call_layers_inserted; ///< Lookup for (bblockid, layerid)

struct CallLayerKey {
int bblockid;
int layerid;

bool operator<(const CallLayerKey& other) const
{
return bblockid < other.bblockid
|| (bblockid == other.bblockid && layerid <= other.layerid);
}
};
std::set<CallLayerKey>
m_call_layers_inserted; ///< Lookup used during llvm gen
};

}; // namespace pvt
Expand Down

0 comments on commit 2ef8e0d

Please sign in to comment.