|
| 1 | +//===--- AssumeSingleThreaded.cpp - Assume single-threaded execution -----===// |
| 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 | +// Assume that user code is single-thread. |
| 14 | +// |
| 15 | +// Convert all reference counting operations into non-atomic ones. |
| 16 | +// |
| 17 | +// To get read of most atomic reference counting operations, the standard |
| 18 | +// library should be compiled in this mode as well |
| 19 | +// |
| 20 | +// This pass affects only reference counting operations resulting from SIL |
| 21 | +// instructions. It wouldn't affect places in the runtime C++ code which |
| 22 | +// hard-code calls to retain/release. We could take advantage of the Instruments |
| 23 | +// instrumentation stubs to redirect calls from the runtime if it was |
| 24 | +// significant, or else just build a single-threaded variant of the runtime. |
| 25 | +// |
| 26 | +//===----------------------------------------------------------------------===// |
| 27 | + |
| 28 | +#include "swift/SIL/SILFunction.h" |
| 29 | +#include "swift/SIL/SILInstruction.h" |
| 30 | +#include "swift/SIL/SILModule.h" |
| 31 | +#include "swift/SILOptimizer/PassManager/Passes.h" |
| 32 | +#include "swift/SILOptimizer/PassManager/Transforms.h" |
| 33 | +#include "swift/SILOptimizer/Utils/InstOptUtils.h" |
| 34 | +#include "llvm/Support/CommandLine.h" |
| 35 | + |
| 36 | +using namespace swift; |
| 37 | + |
| 38 | +namespace { |
| 39 | +class AssumeSingleThreaded : public swift::SILFunctionTransform { |
| 40 | + /// The entry point to the transformation. |
| 41 | + void run() override { |
| 42 | + if (!getOptions().AssumeSingleThreaded) |
| 43 | + return; |
| 44 | + for (auto &BB : *getFunction()) { |
| 45 | + for (auto &I : BB) { |
| 46 | + if (auto RCInst = dyn_cast<RefCountingInst>(&I)) |
| 47 | + RCInst->setNonAtomic(); |
| 48 | + } |
| 49 | + } |
| 50 | + invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions); |
| 51 | + } |
| 52 | +}; |
| 53 | +} // end anonymous namespace |
| 54 | + |
| 55 | +SILTransform *swift::createLegacyAssumeSingleThreaded() { |
| 56 | + return new AssumeSingleThreaded(); |
| 57 | +} |
0 commit comments