25
25
#include " llvm/ADT/DenseSet.h"
26
26
#include " llvm/ADT/ImmutableSet.h"
27
27
#include " llvm/ADT/Statistic.h"
28
- // SWIFT_ENABLE_TENSORFLOW
29
- #include " llvm/Support/CommandLine.h"
30
28
#include " llvm/Support/Debug.h"
31
29
32
30
using namespace swift ;
33
31
34
32
using DenseFunctionSet = llvm::DenseSet<SILFunction *>;
35
33
using ImmutableFunctionSet = llvm::ImmutableSet<SILFunction *>;
36
34
37
- // SWIFT_ENABLE_TENSORFLOW
38
- extern llvm::cl::opt<bool > TFLogDeabstractionStats;
39
-
40
35
STATISTIC (NumMandatoryInlines,
41
36
" Number of function application sites inlined by the mandatory "
42
37
" inlining pass" );
@@ -306,9 +301,7 @@ static void collectPartiallyAppliedArguments(
306
301
static SILFunction *getCalleeFunction (
307
302
SILFunction *F, FullApplySite AI, bool &IsThick,
308
303
SmallVectorImpl<std::pair<SILValue, ParameterConvention>> &CaptureArgs,
309
- SmallVectorImpl<SILValue> &FullArgs, PartialApplyInst *&PartialApply,
310
- // SWIFT_ENABLE_TENSORFLOW
311
- const ShouldMandatoryInlineFnPred &shouldInlinePredicate) {
304
+ SmallVectorImpl<SILValue> &FullArgs, PartialApplyInst *&PartialApply) {
312
305
IsThick = false ;
313
306
PartialApply = nullptr ;
314
307
CaptureArgs.clear ();
@@ -454,11 +447,17 @@ static SILFunction *getCalleeFunction(
454
447
return nullptr ;
455
448
}
456
449
457
- // TODO(SR-8015): `shouldInlinePredicate` does too much. Fix this.
458
450
// If the CalleeFunction is a not-transparent definition, we can not process
459
451
// it.
460
- // SWIFT_ENABLE_TENSORFLOW
461
- if (!shouldInlinePredicate (AI, *CalleeFunction))
452
+ if (CalleeFunction->isTransparent () == IsNotTransparent)
453
+ return nullptr ;
454
+
455
+ // If CalleeFunction is a declaration, see if we can load it.
456
+ if (CalleeFunction->empty ())
457
+ AI.getModule ().loadFunction (CalleeFunction);
458
+
459
+ // If we fail to load it, bail.
460
+ if (CalleeFunction->empty ())
462
461
return nullptr ;
463
462
464
463
if (F->isSerialized () &&
@@ -492,20 +491,6 @@ tryDevirtualizeApplyHelper(FullApplySite InnerAI, SILBasicBlock::iterator I,
492
491
newApplyAI->getIterator ());
493
492
}
494
493
495
- // SWIFT_ENABLE_TENSORFLOW
496
- // Event schema:
497
- //
498
- // "S4TF CallEdge", ParentName, ParentSizeBefore, ParentSizeAfter, ChildName,
499
- // ChildSize
500
- static void maybeLogCallEdge (SILFunction *parent, unsigned parentSizeBefore,
501
- SILFunction *child) {
502
- if (!TFLogDeabstractionStats)
503
- return ;
504
- llvm::dbgs () << " S4TF CallEdge," << parent->getName () << " ,"
505
- << parentSizeBefore << " ," << parent->codeSize () << " ,"
506
- << child->getName () << " ," << child->codeSize () << " \n " ;
507
- }
508
-
509
494
// / \brief Inlines all mandatory inlined functions into the body of a function,
510
495
// / first recursively inlining all mandatory apply instructions in those
511
496
// / functions into their bodies if necessary.
@@ -520,14 +505,13 @@ static void maybeLogCallEdge(SILFunction *parent, unsigned parentSizeBefore,
520
505
// / the current call stack of recursive calls
521
506
// /
522
507
// / \returns true if successful, false if failed due to circular inlining.
523
- static bool runOnFunctionRecursively (
524
- SILOptFunctionBuilder &FuncBuilder, SILFunction *F, FullApplySite AI,
525
- DenseFunctionSet &FullyInlinedSet,
526
- ImmutableFunctionSet::Factory &SetFactory,
527
- ImmutableFunctionSet CurrentInliningSet, ClassHierarchyAnalysis *CHA,
528
- // SWIFT_ENABLE_TENSORFLOW
529
- SILInliner::InlineKind inlineKind,
530
- const ShouldMandatoryInlineFnPred &shouldInlinePredicate) {
508
+ static bool
509
+ runOnFunctionRecursively (SILOptFunctionBuilder &FuncBuilder,
510
+ SILFunction *F, FullApplySite AI,
511
+ DenseFunctionSet &FullyInlinedSet,
512
+ ImmutableFunctionSet::Factory &SetFactory,
513
+ ImmutableFunctionSet CurrentInliningSet,
514
+ ClassHierarchyAnalysis *CHA) {
531
515
// Avoid reprocessing functions needlessly.
532
516
if (FullyInlinedSet.count (F))
533
517
return true ;
@@ -574,18 +558,15 @@ static bool runOnFunctionRecursively(
574
558
bool IsThick;
575
559
PartialApplyInst *PAI;
576
560
SILFunction *CalleeFunction = getCalleeFunction (
577
- F, InnerAI, IsThick, CaptureArgs, FullArgs, PAI,
578
- shouldInlinePredicate); // SWIFT_ENABLE_TENSORFLOW
561
+ F, InnerAI, IsThick, CaptureArgs, FullArgs, PAI);
579
562
580
563
if (!CalleeFunction)
581
564
continue ;
582
565
583
566
// Then recursively process it first before trying to inline it.
584
567
if (!runOnFunctionRecursively (FuncBuilder, CalleeFunction, InnerAI,
585
568
FullyInlinedSet, SetFactory,
586
- CurrentInliningSet, CHA,
587
- // SWIFT_ENABLE_TENSORFLOW
588
- inlineKind, shouldInlinePredicate)) {
569
+ CurrentInliningSet, CHA)) {
589
570
// If we failed due to circular inlining, then emit some notes to
590
571
// trace back the failure if we have more information.
591
572
// FIXME: possibly it could be worth recovering and attempting other
@@ -616,10 +597,8 @@ static bool runOnFunctionRecursively(
616
597
OpenedArchetypesTracker.registerUsedOpenedArchetypes (PAI);
617
598
}
618
599
619
- // SWIFT_ENABLE_TENSORFLOW
620
- unsigned parentSizeBefore = F->codeSize ();
621
- SILInliner Inliner (FuncBuilder, inlineKind, Subs,
622
- OpenedArchetypesTracker);
600
+ SILInliner Inliner (FuncBuilder, SILInliner::InlineKind::MandatoryInline,
601
+ Subs, OpenedArchetypesTracker);
623
602
if (!Inliner.canInlineApplySite (InnerAI)) {
624
603
// See comment above about casting when devirtualizing and how this
625
604
// sometimes causes II and InnerAI to be different and even in different
@@ -666,9 +645,6 @@ static bool runOnFunctionRecursively(
666
645
assert (nextI == ApplyBlock->end ()
667
646
|| nextI->getParent () == ApplyBlock
668
647
&& " Mismatch between the instruction and basic block" );
669
-
670
- // SWIFT_ENABLE_TENSORFLOW
671
- maybeLogCallEdge (F, parentSizeBefore, CalleeFunction);
672
648
}
673
649
}
674
650
@@ -678,29 +654,6 @@ static bool runOnFunctionRecursively(
678
654
return true ;
679
655
}
680
656
681
- // SWIFT_ENABLE_TENSORFLOW
682
- // / Scan the given function body, mandatory inlining calls to callees that
683
- // / satisfy the specified predicate, including calls sites exposed by inlining
684
- // / other functions.
685
- // /
686
- // / The deabstraction phase of TensorFlow support needs to mandatory inline
687
- // / certain functions using the standard mandatory inlining algorithm. This
688
- // / function implements support for it, inlining direct calls to callees that
689
- // / satisfy the given predicate.
690
- void swift::inlineForTFDeabstraction (SILOptFunctionBuilder &FB, SILFunction &fn,
691
- const ShouldMandatoryInlineFnPred &predicate) {
692
- DenseFunctionSet FullyInlinedSet;
693
- ImmutableFunctionSet::Factory SetFactory;
694
-
695
- runOnFunctionRecursively (FB, &fn,
696
- FullApplySite (static_cast <ApplyInst*>(nullptr )),
697
- FullyInlinedSet,
698
- SetFactory, SetFactory.getEmptySet (),
699
- /* CHA*/ nullptr ,
700
- SILInliner::InlineKind::PerformanceInline,
701
- predicate);
702
- }
703
-
704
657
// ===----------------------------------------------------------------------===//
705
658
// Top Level Driver
706
659
// ===----------------------------------------------------------------------===//
@@ -726,24 +679,9 @@ class MandatoryInlining : public SILModuleTransform {
726
679
if (F.wasDeserializedCanonical ())
727
680
continue ;
728
681
729
- // SWIFT_ENABLE_TENSORFLOW
730
682
runOnFunctionRecursively (FuncBuilder, &F,
731
- FullApplySite (),
732
- FullyInlinedSet,
733
- SetFactory, SetFactory.getEmptySet (), CHA,
734
- SILInliner::InlineKind::MandatoryInline,
735
- [&](FullApplySite site, SILFunction &callee) -> bool {
736
- if (callee.isTransparent () != IsTransparent)
737
- return false ;
738
-
739
- // If CalleeFunction is a declaration, see if we can load it.
740
- if (callee.empty ())
741
- site.getModule ().loadFunction (&callee);
742
-
743
- // If we failed to load it, bail.
744
- return !callee.empty ();
745
- }
746
- );
683
+ FullApplySite (), FullyInlinedSet, SetFactory,
684
+ SetFactory.getEmptySet (), CHA);
747
685
}
748
686
749
687
if (!ShouldCleanup)
0 commit comments