Skip to content

Commit 1e86c04

Browse files
committed
Re-add #30710
Reverts the reversion in #34188. Bisection points to #34188 as the cause of a crasher that looks to be the result of a miscompile. Adding this back resolves the crasher. Details of the crash: The `DiscontiguousSliceSlicing` test in validation-test/StdlibUnittest/RangeSet.swift crashes in the `every()` function on line 15. When the contents of this function are replaced with a functional equivalent that uses different syntax, the crash resolves.
1 parent f8a8d08 commit 1e86c04

File tree

5 files changed

+74
-22
lines changed

5 files changed

+74
-22
lines changed

include/swift/SILOptimizer/Analysis/FunctionOrder.h

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class BottomUpFunctionOrder {
3131
typedef TinyPtrVector<SILFunction *> SCC;
3232

3333
private:
34-
SILModule &M;
3534
llvm::SmallVector<SCC, 32> TheSCCs;
3635
llvm::SmallVector<SILFunction *, 32> TheFunctions;
3736

@@ -44,24 +43,33 @@ class BottomUpFunctionOrder {
4443
llvm::SmallSetVector<SILFunction *, 4> DFSStack;
4544

4645
public:
47-
BottomUpFunctionOrder(SILModule &M, BasicCalleeAnalysis *BCA)
48-
: M(M), BCA(BCA), NextDFSNum(0) {}
46+
// SWIFT_ENABLE_TENSORFLOW
47+
BottomUpFunctionOrder(BasicCalleeAnalysis *BCA)
48+
: BCA(BCA), NextDFSNum(0) {}
49+
50+
/// DFS on 'F' to compute bottom up order
51+
void computeBottomUpOrder(SILFunction *F) {
52+
DFS(F);
53+
}
54+
55+
/// DFS on all functions in the module to compute bottom up order
56+
void computeBottomUpOrder(SILModule *M) {
57+
for (auto &F : *M)
58+
DFS(&F);
59+
}
60+
// SWIFT_ENABLE_TENSORFLOW END
4961

5062
/// Get the SCCs in bottom-up order.
5163
ArrayRef<SCC> getSCCs() {
52-
if (!TheSCCs.empty())
53-
return TheSCCs;
54-
55-
FindSCCs(M);
5664
return TheSCCs;
5765
}
5866

59-
/// Get a flattened view of all functions in all the SCCs in
60-
/// bottom-up order
61-
ArrayRef<SILFunction *> getFunctions() {
67+
// SWIFT_ENABLE_TENSORFLOW
68+
/// Get a flattened view of all functions in all the SCCs in bottom-up order
69+
ArrayRef<SILFunction *> getBottomUpOrder() {
70+
// SWIFT_ENABLE_TENSORFLOW END
6271
if (!TheFunctions.empty())
6372
return TheFunctions;
64-
6573
for (auto SCC : getSCCs())
6674
for (auto *F : SCC)
6775
TheFunctions.push_back(F);
@@ -71,7 +79,6 @@ class BottomUpFunctionOrder {
7179

7280
private:
7381
void DFS(SILFunction *F);
74-
void FindSCCs(SILModule &M);
7582
};
7683

7784
} // end namespace swift

lib/SILOptimizer/Analysis/FunctionOrder.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,3 @@ void BottomUpFunctionOrder::DFS(SILFunction *Start) {
7373
TheSCCs.push_back(CurrentSCC);
7474
}
7575
}
76-
77-
void BottomUpFunctionOrder::FindSCCs(SILModule &M) {
78-
for (auto &F : M)
79-
DFS(&F);
80-
}

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,11 @@ runFunctionPasses(unsigned FromTransIdx, unsigned ToTransIdx) {
533533
return;
534534

535535
BasicCalleeAnalysis *BCA = getAnalysis<BasicCalleeAnalysis>();
536-
BottomUpFunctionOrder BottomUpOrder(*Mod, BCA);
537-
auto BottomUpFunctions = BottomUpOrder.getFunctions();
536+
// SWIFT_ENABLE_TENSORFLOW
537+
BottomUpFunctionOrder BottomUpOrder(BCA);
538+
BottomUpOrder.computeBottomUpOrder(Mod);
539+
auto BottomUpFunctions = BottomUpOrder.getBottomUpOrder();
540+
// SWIFT_ENABLE_TENSORFLOW END
538541

539542
assert(FunctionWorklist.empty() && "Expected empty function worklist!");
540543

@@ -587,6 +590,49 @@ runFunctionPasses(unsigned FromTransIdx, unsigned ToTransIdx) {
587590
++Entry.PipelineIdx;
588591
}
589592
clearRestartPipeline();
593+
594+
// SWIFT_ENABLE_TENSORFLOW
595+
if (TailIdx == (FunctionWorklist.size() - 1)) {
596+
// No new functions to process
597+
continue;
598+
}
599+
600+
// Compute the bottom up order of the new functions and the callees in it
601+
BottomUpFunctionOrder SubBottomUpOrder(BCA);
602+
// Initialize BottomUpFunctionOrder with new functions
603+
for (auto It = FunctionWorklist.begin() + TailIdx + 1;
604+
It != FunctionWorklist.end(); It++) {
605+
SubBottomUpOrder.computeBottomUpOrder(It->F);
606+
}
607+
auto NewFunctionsBottomUp = SubBottomUpOrder.getBottomUpOrder();
608+
SmallPtrSet<SILFunction *, 8> NewBottomUpSet(NewFunctionsBottomUp.begin(),
609+
NewFunctionsBottomUp.end());
610+
611+
// Remove all the functions in the new bottom up order from FunctionWorklist
612+
llvm::DenseMap<SILFunction *, WorklistEntry> FunctionsToReorder;
613+
auto RemoveFn = [&FunctionsToReorder,
614+
&NewBottomUpSet](WorklistEntry Entry) {
615+
if (NewBottomUpSet.find(Entry.F) == NewBottomUpSet.end()) {
616+
return false;
617+
}
618+
FunctionsToReorder.insert(std::make_pair(Entry.F, Entry));
619+
return true;
620+
};
621+
std::remove_if(FunctionWorklist.begin(), FunctionWorklist.end(), RemoveFn);
622+
FunctionWorklist.erase((FunctionWorklist.begin() + FunctionWorklist.size() -
623+
FunctionsToReorder.size()),
624+
FunctionWorklist.end());
625+
626+
// Add back the functions in the new bottom up order to the FunctionWorklist
627+
for (auto it = NewFunctionsBottomUp.rbegin();
628+
it != NewFunctionsBottomUp.rend(); it++) {
629+
auto Entry = FunctionsToReorder.find(*it);
630+
if (Entry == FunctionsToReorder.end()) {
631+
continue;
632+
}
633+
FunctionWorklist.push_back((*Entry).second);
634+
}
635+
// SWIFT_ENABLE_TENSORFLOW END
590636
}
591637
}
592638

lib/SILOptimizer/UtilityPasses/FunctionOrderPrinter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ class FunctionOrderPrinterPass : public SILModuleTransform {
3535
/// The entry point to the transformation.
3636
void run() override {
3737
BCA = getAnalysis<BasicCalleeAnalysis>();
38-
auto &M = *getModule();
39-
BottomUpFunctionOrder Orderer(M, BCA);
38+
// SWIFT_ENABLE_TENSORFLOW
39+
BottomUpFunctionOrder Orderer(BCA);
40+
Orderer.computeBottomUpOrder(getModule());
41+
// SWIFT_ENABLE_TENSORFLOW END
4042

4143
llvm::outs() << "Bottom up function order:\n";
4244
auto SCCs = Orderer.getSCCs();

test/DebugInfo/inlined-generics-basic.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ public class C<R> {
9191
// IR-LABEL: ret void
9292

9393
// IR: ![[BOOL:[0-9]+]] = !DICompositeType({{.*}}name: "Bool"
94-
// IR: ![[LET_BOOL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[BOOL]])
9594
// IR: ![[INT:[0-9]+]] = !DICompositeType({{.*}}name: "Int"
9695
// IR: ![[LET_INT:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[INT]])
96+
// SWIFT_ENABLE_TENSORFLOW
97+
// IR: ![[LET_BOOL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[BOOL]])
98+
// SWIFT_ENABLE_TENSORFLOW END
9799
// IR: ![[TAU_0_0:[0-9]+]] = {{.*}}DW_TAG_structure_type, name: "$sxD",
98100
// IR: ![[LET_TAU_0_0:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[TAU_0_0]])
99101
// IR: ![[TAU_1_0:[0-9]+]] = {{.*}}DW_TAG_structure_type, name: "$sqd__D",

0 commit comments

Comments
 (0)