@@ -370,8 +370,9 @@ void copyModuleContents(Module& input, Name inputName) {
370370 merged.start = input.start ;
371371 } else {
372372 // Merge them, keeping the order. We copy both functions to avoid issues
373- // with other references to them, and just call the second one, leaving
374- // inlining to the optimizer if that makes sense to do.
373+ // with other references to them, and then add a new function that calls
374+ // the two (we leave proper inlining, including handling of control flow
375+ // etc., to the optimizer).
375376 auto copiedOldName =
376377 Names::getValidFunctionName (merged, " merged.start.old" );
377378 auto copiedNewName =
@@ -380,10 +381,18 @@ void copyModuleContents(Module& input, Name inputName) {
380381 merged.getFunction (merged.start ), merged, copiedOldName);
381382 ModuleUtils::copyFunction (
382383 merged.getFunction (input.start ), merged, copiedNewName);
384+
385+ auto combinedName =
386+ Names::getValidFunctionName (merged, " merged.start.combined" );
383387 Builder builder (merged);
384- copiedOld->body = builder.makeSequence (
385- copiedOld->body , builder.makeCall (copiedNewName, {}, Type::none));
386- merged.start = copiedOldName;
388+ auto * callOld = builder.makeCall (copiedOldName, {}, Type::none);
389+ auto * callNew = builder.makeCall (copiedNewName, {}, Type::none);
390+ auto * body = builder.makeSequence (callOld, callNew);
391+ auto combinedStart = builder.makeFunction (combinedName,
392+ Signature (Type::none, Type::none),
393+ {},
394+ calls);
395+ merged.start = combinedName;
387396 }
388397 }
389398
0 commit comments