Skip to content

Commit 4355cad

Browse files
committed
Add a pass to eliminate access markers.
1 parent ecfff06 commit 4355cad

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

include/swift/SIL/SILFunction.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,11 +642,14 @@ class SILFunction
642642
const BlockListType &getBlocks() const { return BlockList; }
643643

644644
typedef BlockListType::iterator iterator;
645+
typedef BlockListType::reverse_iterator reverse_iterator;
645646
typedef BlockListType::const_iterator const_iterator;
646647

647648
bool empty() const { return BlockList.empty(); }
648649
iterator begin() { return BlockList.begin(); }
649650
iterator end() { return BlockList.end(); }
651+
reverse_iterator rbegin() { return BlockList.rbegin(); }
652+
reverse_iterator rend() { return BlockList.rend(); }
650653
const_iterator begin() const { return BlockList.begin(); }
651654
const_iterator end() const { return BlockList.end(); }
652655
unsigned size() const { return BlockList.size(); }

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ PASS(AADumper, "aa-dump",
5454
"Dump Alias Analysis over all Pairs")
5555
PASS(ABCOpt, "abcopts",
5656
"Array Bounds Check Optimization")
57+
PASS(AccessMarkerElimination, "access-marker-elim",
58+
"Access Marker Elimination.")
5759
PASS(AddressLowering, "address-lowering",
5860
"SIL Address Lowering")
5961
PASS(AllocBoxToStack, "allocbox-to-stack",
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//===--- AccessMarkerElimination.cpp - Eliminate access markers. ----------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
///
13+
/// This pass eliminates the instructions that demarcate memory access regions.
14+
/// If no memory access markers exist, then the pass does nothing. Otherwise, it
15+
/// unconditionally eliminates the markers.
16+
///
17+
/// This is an always-on pass for temporary bootstrapping. It allows running
18+
/// test cases through the pipeline and exercising SIL verification before all
19+
/// passes support access markers.
20+
///
21+
/// TODO: DefiniteInitialization needs to be fixed to handle begin/end_access.
22+
/// The immediate goal is to run this pass only at -O. Access markers should not
23+
/// interfere with any -Onone passes.
24+
///
25+
//===----------------------------------------------------------------------===//
26+
27+
#define DEBUG_TYPE "access-marker-elim"
28+
#include "swift/Basic/Range.h"
29+
#include "swift/SIL/SILFunction.h"
30+
#include "swift/SILOptimizer/PassManager/Transforms.h"
31+
32+
using namespace swift;
33+
34+
namespace {
35+
36+
struct AccessMarkerElimination : SILModuleTransform {
37+
void run() override {
38+
for (auto &F : *getModule()) {
39+
// If the function has no access markers, don't invalidate any analyses.
40+
if (!F.hasAccessMarkers())
41+
continue;
42+
43+
F.disableAccessMarkers();
44+
45+
// iterating in reverse eliminates more begin_access users before they
46+
// need to be replaced.
47+
for (auto &BB : reversed(F)) {
48+
for (auto II = BB.rbegin(), IE = BB.rend(); II != IE;) {
49+
SILInstruction *inst = &*II;
50+
++II;
51+
52+
if (auto beginAccess = dyn_cast<BeginAccessInst>(inst)) {
53+
beginAccess->replaceAllUsesWith(beginAccess->getSource());
54+
beginAccess->eraseFromParent();
55+
}
56+
if (auto endAccess = dyn_cast<EndAccessInst>(inst)) {
57+
assert(endAccess->use_empty());
58+
endAccess->eraseFromParent();
59+
}
60+
}
61+
}
62+
63+
auto InvalidKind = SILAnalysis::InvalidationKind::Instructions;
64+
invalidateAnalysis(&F, InvalidKind);
65+
}
66+
}
67+
};
68+
69+
} // end anonymous namespace
70+
71+
SILTransform *swift::createAccessMarkerElimination() {
72+
return new AccessMarkerElimination();
73+
}

lib/SILOptimizer/Mandatory/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(MANDATORY_SOURCES
2+
Mandatory/AccessMarkerElimination.cpp
23
Mandatory/AddressLowering.cpp
34
Mandatory/DefiniteInitialization.cpp
45
Mandatory/DIMemoryUseCollector.cpp

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ static void addMandatoryOptPipeline(SILPassPipelinePlan &P,
8282

8383
P.addOwnershipModelEliminator();
8484
P.addNoReturnFolding();
85+
P.addAccessMarkerElimination();
8586
P.addDefiniteInitialization();
8687

8788
P.addMandatoryInlining();

0 commit comments

Comments
 (0)