Hi Hagen,
Before getting to the problem, I noticed that the attached model uses some incompatible model parameters and solver settings for HDL code generation. This causes compilation and/or simulation failures in the generated model and validation model due to discrepancies in sample time. You can see these warnings in the HDL Check Report once code generation is successful. To resolve this problem, before generating HDL code, run hdlsetup on the model to set the appropriate model parameters and solver settings. Now getting back to the problem, the issue has to do with how resource sharing works in both cases. In case 1, the DUT has a Delay block preceding the Add block inside each of the four Atomic Subsystems. In case 2, it's the same DUT but each Atomic Subsystem contains only the Add block. When you apply resource sharing on a Subsystem that has Delay blocks, the optimization tries to insert pipelines with a delay length equal to N, where N is the SharingFactor that you specify.
In case 1, where you have a Unit Delay inside the Atomic Subsystem, resource sharing tries to introduce an Integer delay of length 4, i.e Z^-4, in the generated model (since the SharingFactor is 4), which becomes part of a feedback loop. HDL Coder is unable to insert the delay on the feedback path since they cannot be path balanced resulting in a delay balancing failure. You can see a similar error message in the Delay Balancing section of the Code Generation report. However, in case 2, when you have only the Add blocks in your design, resource sharing does not have to insert pipelines and hence delay balancing works successfully. For the issue in case 1, as a workaround, you can flatten the hierarchy of all four atomic subsystems by setting FlattenHierarchy to ‘inherit’ or ‘on’ for all four subsystems. Setting this option removes the subsystem hierarchy and considers the Delay and Add blocks to be at the same level in the hierarchy. With this optimization, you cannot share the Atomic Subsystems but you can share the Add blocks by enabling the ShareAdders setting on the model. Enabling these settings shares resources and also improves opportunities for clock-rate pipelining which you can see in the generated model. You could also apply these same optimizations for the DUT in case 2 and make clock-rate pipelining work more effectively.
Best Answer