Repository: opencv/opencv
File: modules/dnn/src/net_impl2.cpp (C++)

Code before:
                Arg inp = inputs[i];
                traceArg(strm_, "Input", i, inp, false);
            }
        }
        bool dynamicOutShapes = layer->dynamicOutputShapes();
        if (!dynamicOutShapes) {
            allocateLayerOutputs(layer, inpTypes, inpShapes, outTypes, outShapes, outOrigData, outMats,
                                 tempTypes, tempShapes, tempMats, scratchBufs, true);
        } else {
            outMats.resize(noutputs);
            for (i = 0; i < noutputs; i++) {
                Arg out = outputs[i];
                outMats[i] = argTensor(out);
            }
            tempMats = scratchBufs;
        }

        timestamp = getTickCount();

        // [TODO] handle If/Loop/...
        Ptr<IfLayer> iflayer = layer.dynamicCast<IfLayer>();
        if (iflayer){ //i.e if cast is successful then
            Mat inp0 = inpMats[0];
            CV_Assert(inp0.dims == 0);
            bool flag = inp0.at<bool>(0);
            // forwardGraph(iflayer->then_else(flag));
            auto subgraph = iflayer->then_else(flag);
            forwardGraph(subgraph, inputs_, outputs_, mainGraph);
        } else {
            CV_Assert(!layer->subgraphs());
            if (finalizeLayers)
                layer->finalize(inpMats, outMats);
        }
        layer->forward(inpMats, outMats, tempMats);
        CV_Assert(outMats.size() == noutputs);

        for (i = 0; i < noutputs; i++) {
            Arg out = outputs[i];
            ArgData& adata = args[out.idx];
            const Mat& m = outMats[i];
            //checkRange(m, false);
            adata.type = m.type();
            adata.shape = m.shape();
            if (adata.kind == DNN_ARG_TEMP) {
                int bufidx = bufidxs.at(out.idx);
                Mat& buf = buffers.at(bufidx);

                if (!dynamicOutShapes) {
                    // a sanity check: make sure that the data was not reallocated during Layer::forward()
                    // if the layer claims it does not produce dynamic-shape outputs.
                    CV_Assert_N(buf.u == m.u,

Review comment (suggestion):
I suggest to have check for empty subgraphs first.

```
std::vector<Ptr<Graph>>* subgraphs = layer->subgraphs();
if (!subgraphs) { // main branch, true for 99.9% of layers
   if (finalizeLayers)
       layer->finalize(inpMats, outMats);
   layer->forward(inpMats, outMats, tempMats);
} else {
   Ptr<IfLayer> iflayer = layer.dynamicCast<IfLayer>();
   if (iflayer) {
       // subgraphs already contains pointer to vector where, subgraphs[0] is then, subgraphs[1] is else.
       Mat inp0 = inpMats[0];
       CV_Assert(inp0.total() == 1u);
       CV_Assert(inp0.type() == CV_Bool);
       bool flag = inp0.at<bool>(0);
       auto subgraph = subgraphs->at((int)(!flag));
       forwardGraph(subgraph, inpMats, outMats, false);
    } else {
       CV_Error_(Error::StsNotImplemented, "unknown layer type '%s' with subgraphs", layer->type.c_str()));
    }   
}
```



Code after:
            }
        }
        bool dynamicOutShapes = layer->dynamicOutputShapes();
        if (!dynamicOutShapes) {
            allocateLayerOutputs(layer, inpTypes, inpShapes, outTypes, outShapes, outOrigData, outMats,
                                 tempTypes, tempShapes, tempMats, scratchBufs, true);
        } else {
            outMats.resize(noutputs);
            for (i = 0; i < noutputs; i++) {
                Arg out = outputs[i];
                outMats[i] = argTensor(out);
            }
            tempMats = scratchBufs;
        }

        timestamp = getTickCount();

        std::vector<Ptr<Graph> >* subgraphs = layer->subgraphs();
        if (!subgraphs) {
            if (finalizeLayers)
                layer->finalize(inpMats, outMats);
            layer->forward(inpMats, outMats, tempMats);
        }
        else {
            Ptr<IfLayer> iflayer = layer.dynamicCast<IfLayer>();
            if (iflayer) {
                int branch = iflayer->branch(inpMats[0]);
                Ptr<Graph> subgraph = subgraphs->at(branch);
                std::vector<Mat> branchInputs;
                if (inpMats.size() > 1)
                    branchInputs.assign(inpMats.begin() + 1, inpMats.end());
                forwardGraph(subgraph, branchInputs, outMats, false);
            }
            else {
                CV_Error_(Error::StsNotImplemented,
                          ("unknown layer type '%s' with subgraphs", layer->type.c_str()));
            }
        }
        CV_Assert(outMats.size() == noutputs);

        for (i = 0; i < noutputs; i++) {
            Arg out = outputs[i];
            ArgData& adata = args[out.idx];
            const Mat& m = outMats[i];
            //checkRange(m, false);
            adata.type = m.type();
            adata.shape = m.shape();
            if (adata.kind == DNN_ARG_TEMP) {
                int bufidx = bufidxs.at(out.idx);
                Mat& buf = buffers.at(bufidx);