Skip to content

Commit

Permalink
Re-add #30710
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
texasmichelle committed Nov 3, 2020
1 parent f8a8d08 commit 1e86c04
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 22 deletions.
31 changes: 19 additions & 12 deletions include/swift/SILOptimizer/Analysis/FunctionOrder.h
Expand Up @@ -31,7 +31,6 @@ class BottomUpFunctionOrder {
typedef TinyPtrVector<SILFunction *> SCC;

private:
SILModule &M;
llvm::SmallVector<SCC, 32> TheSCCs;
llvm::SmallVector<SILFunction *, 32> TheFunctions;

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

public:
BottomUpFunctionOrder(SILModule &M, BasicCalleeAnalysis *BCA)
: M(M), BCA(BCA), NextDFSNum(0) {}
// SWIFT_ENABLE_TENSORFLOW
BottomUpFunctionOrder(BasicCalleeAnalysis *BCA)
: BCA(BCA), NextDFSNum(0) {}

/// DFS on 'F' to compute bottom up order
void computeBottomUpOrder(SILFunction *F) {
DFS(F);
}

/// DFS on all functions in the module to compute bottom up order
void computeBottomUpOrder(SILModule *M) {
for (auto &F : *M)
DFS(&F);
}
// SWIFT_ENABLE_TENSORFLOW END

/// Get the SCCs in bottom-up order.
ArrayRef<SCC> getSCCs() {
if (!TheSCCs.empty())
return TheSCCs;

FindSCCs(M);
return TheSCCs;
}

/// Get a flattened view of all functions in all the SCCs in
/// bottom-up order
ArrayRef<SILFunction *> getFunctions() {
// SWIFT_ENABLE_TENSORFLOW
/// Get a flattened view of all functions in all the SCCs in bottom-up order
ArrayRef<SILFunction *> getBottomUpOrder() {
// SWIFT_ENABLE_TENSORFLOW END
if (!TheFunctions.empty())
return TheFunctions;

for (auto SCC : getSCCs())
for (auto *F : SCC)
TheFunctions.push_back(F);
Expand All @@ -71,7 +79,6 @@ class BottomUpFunctionOrder {

private:
void DFS(SILFunction *F);
void FindSCCs(SILModule &M);
};

} // end namespace swift
Expand Down
5 changes: 0 additions & 5 deletions lib/SILOptimizer/Analysis/FunctionOrder.cpp
Expand Up @@ -73,8 +73,3 @@ void BottomUpFunctionOrder::DFS(SILFunction *Start) {
TheSCCs.push_back(CurrentSCC);
}
}

void BottomUpFunctionOrder::FindSCCs(SILModule &M) {
for (auto &F : M)
DFS(&F);
}
50 changes: 48 additions & 2 deletions lib/SILOptimizer/PassManager/PassManager.cpp
Expand Up @@ -533,8 +533,11 @@ runFunctionPasses(unsigned FromTransIdx, unsigned ToTransIdx) {
return;

BasicCalleeAnalysis *BCA = getAnalysis<BasicCalleeAnalysis>();
BottomUpFunctionOrder BottomUpOrder(*Mod, BCA);
auto BottomUpFunctions = BottomUpOrder.getFunctions();
// SWIFT_ENABLE_TENSORFLOW
BottomUpFunctionOrder BottomUpOrder(BCA);
BottomUpOrder.computeBottomUpOrder(Mod);
auto BottomUpFunctions = BottomUpOrder.getBottomUpOrder();
// SWIFT_ENABLE_TENSORFLOW END

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

Expand Down Expand Up @@ -587,6 +590,49 @@ runFunctionPasses(unsigned FromTransIdx, unsigned ToTransIdx) {
++Entry.PipelineIdx;
}
clearRestartPipeline();

// SWIFT_ENABLE_TENSORFLOW
if (TailIdx == (FunctionWorklist.size() - 1)) {
// No new functions to process
continue;
}

// Compute the bottom up order of the new functions and the callees in it
BottomUpFunctionOrder SubBottomUpOrder(BCA);
// Initialize BottomUpFunctionOrder with new functions
for (auto It = FunctionWorklist.begin() + TailIdx + 1;
It != FunctionWorklist.end(); It++) {
SubBottomUpOrder.computeBottomUpOrder(It->F);
}
auto NewFunctionsBottomUp = SubBottomUpOrder.getBottomUpOrder();
SmallPtrSet<SILFunction *, 8> NewBottomUpSet(NewFunctionsBottomUp.begin(),
NewFunctionsBottomUp.end());

// Remove all the functions in the new bottom up order from FunctionWorklist
llvm::DenseMap<SILFunction *, WorklistEntry> FunctionsToReorder;
auto RemoveFn = [&FunctionsToReorder,
&NewBottomUpSet](WorklistEntry Entry) {
if (NewBottomUpSet.find(Entry.F) == NewBottomUpSet.end()) {
return false;
}
FunctionsToReorder.insert(std::make_pair(Entry.F, Entry));
return true;
};
std::remove_if(FunctionWorklist.begin(), FunctionWorklist.end(), RemoveFn);
FunctionWorklist.erase((FunctionWorklist.begin() + FunctionWorklist.size() -
FunctionsToReorder.size()),
FunctionWorklist.end());

// Add back the functions in the new bottom up order to the FunctionWorklist
for (auto it = NewFunctionsBottomUp.rbegin();
it != NewFunctionsBottomUp.rend(); it++) {
auto Entry = FunctionsToReorder.find(*it);
if (Entry == FunctionsToReorder.end()) {
continue;
}
FunctionWorklist.push_back((*Entry).second);
}
// SWIFT_ENABLE_TENSORFLOW END
}
}

Expand Down
6 changes: 4 additions & 2 deletions lib/SILOptimizer/UtilityPasses/FunctionOrderPrinter.cpp
Expand Up @@ -35,8 +35,10 @@ class FunctionOrderPrinterPass : public SILModuleTransform {
/// The entry point to the transformation.
void run() override {
BCA = getAnalysis<BasicCalleeAnalysis>();
auto &M = *getModule();
BottomUpFunctionOrder Orderer(M, BCA);
// SWIFT_ENABLE_TENSORFLOW
BottomUpFunctionOrder Orderer(BCA);
Orderer.computeBottomUpOrder(getModule());
// SWIFT_ENABLE_TENSORFLOW END

llvm::outs() << "Bottom up function order:\n";
auto SCCs = Orderer.getSCCs();
Expand Down
4 changes: 3 additions & 1 deletion test/DebugInfo/inlined-generics-basic.swift
Expand Up @@ -91,9 +91,11 @@ public class C<R> {
// IR-LABEL: ret void

// IR: ![[BOOL:[0-9]+]] = !DICompositeType({{.*}}name: "Bool"
// IR: ![[LET_BOOL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[BOOL]])
// IR: ![[INT:[0-9]+]] = !DICompositeType({{.*}}name: "Int"
// IR: ![[LET_INT:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[INT]])
// SWIFT_ENABLE_TENSORFLOW
// IR: ![[LET_BOOL:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[BOOL]])
// SWIFT_ENABLE_TENSORFLOW END
// IR: ![[TAU_0_0:[0-9]+]] = {{.*}}DW_TAG_structure_type, name: "$sxD",
// IR: ![[LET_TAU_0_0:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[TAU_0_0]])
// IR: ![[TAU_1_0:[0-9]+]] = {{.*}}DW_TAG_structure_type, name: "$sqd__D",
Expand Down

0 comments on commit 1e86c04

Please sign in to comment.