Skip to content

Commit 29f636e

Browse files
authored
Merge pull request swiftlang#32896 from gottesmm/pr-08bb8306e9a10dba9416a852f7508cd1f6fb46e3
[opt-remark] Add a pass called Opt Remark Generator that implements small opt remarks that do not result from large dataflow checks.
2 parents d2f131f + 4559db0 commit 29f636e

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ PASS(MandatoryCombine, "mandatory-combine",
345345
"Perform mandatory peephole combines")
346346
PASS(BugReducerTester, "bug-reducer-tester",
347347
"sil-bug-reducer Tool Testing by Asserting on a Sentinel Function")
348+
PASS(OptRemarkGenerator, "sil-opt-remark-generator",
349+
"Emit small peephole opt remarks that do not use large analyses")
348350
PASS(PruneVTables, "prune-vtables",
349351
"Mark class methods that do not require vtable dispatch")
350352
PASS_RANGE(AllPasses, AADumper, PruneVTables)

lib/SILOptimizer/Transforms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ target_sources(swiftSILOptimizer PRIVATE
2323
MergeCondFail.cpp
2424
Outliner.cpp
2525
ObjectOutliner.cpp
26+
OptRemarkGenerator.cpp
2627
PerformanceInliner.cpp
2728
PhiArgumentOptimizations.cpp
2829
PruneVTables.cpp
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//===--- OptRemarkGenerator.cpp -------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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+
#define DEBUG_TYPE "sil-opt-remark-gen"
14+
15+
#include "swift/SIL/OptimizationRemark.h"
16+
#include "swift/SIL/SILFunction.h"
17+
#include "swift/SIL/SILInstruction.h"
18+
#include "swift/SIL/SILModule.h"
19+
#include "swift/SIL/SILVisitor.h"
20+
#include "swift/SILOptimizer/PassManager/Passes.h"
21+
#include "swift/SILOptimizer/PassManager/Transforms.h"
22+
23+
using namespace swift;
24+
25+
//===----------------------------------------------------------------------===//
26+
// Opt Remark Generator Visitor
27+
//===----------------------------------------------------------------------===//
28+
29+
namespace {
30+
31+
struct OptRemarkGeneratorInstructionVisitor
32+
: public SILInstructionVisitor<OptRemarkGeneratorInstructionVisitor> {
33+
SILModule &mod;
34+
OptRemark::Emitter ORE;
35+
36+
OptRemarkGeneratorInstructionVisitor(SILModule &mod)
37+
: mod(mod), ORE(DEBUG_TYPE, mod) {}
38+
39+
void visitStrongRetainInst(StrongRetainInst *sri);
40+
void visitStrongReleaseInst(StrongReleaseInst *sri);
41+
void visitRetainValueInst(RetainValueInst *rvi);
42+
void visitReleaseValueInst(ReleaseValueInst *rvi);
43+
void visitSILInstruction(SILInstruction *) {}
44+
};
45+
46+
} // anonymous namespace
47+
48+
void OptRemarkGeneratorInstructionVisitor::visitStrongRetainInst(
49+
StrongRetainInst *sri) {
50+
ORE.emit([&]() {
51+
using namespace OptRemark;
52+
return RemarkMissed("memory-management", *sri)
53+
<< "Unable to remove ARC operation";
54+
});
55+
}
56+
57+
void OptRemarkGeneratorInstructionVisitor::visitStrongReleaseInst(
58+
StrongReleaseInst *sri) {
59+
ORE.emit([&]() {
60+
using namespace OptRemark;
61+
return RemarkMissed("memory-management", *sri)
62+
<< "Unable to remove ARC operation";
63+
});
64+
}
65+
66+
void OptRemarkGeneratorInstructionVisitor::visitRetainValueInst(
67+
RetainValueInst *rvi) {
68+
ORE.emit([&]() {
69+
using namespace OptRemark;
70+
return RemarkMissed("memory-management", *rvi)
71+
<< "Unable to remove ARC operation";
72+
});
73+
}
74+
75+
void OptRemarkGeneratorInstructionVisitor::visitReleaseValueInst(
76+
ReleaseValueInst *rvi) {
77+
ORE.emit([&]() {
78+
using namespace OptRemark;
79+
return RemarkMissed("memory-management", *rvi)
80+
<< "Unable to remove ARC operation";
81+
});
82+
}
83+
84+
//===----------------------------------------------------------------------===//
85+
// Top Level Entrypoint
86+
//===----------------------------------------------------------------------===//
87+
88+
namespace {
89+
90+
class OptRemarkGenerator : public SILFunctionTransform {
91+
~OptRemarkGenerator() override {}
92+
93+
/// The entry point to the transformation.
94+
void run() override {
95+
OptRemarkGeneratorInstructionVisitor visitor(getFunction()->getModule());
96+
for (auto &block : *getFunction()) {
97+
for (auto &inst : block) {
98+
visitor.visit(&inst);
99+
}
100+
}
101+
}
102+
};
103+
104+
} // end anonymous namespace
105+
106+
SILTransform *swift::createOptRemarkGenerator() {
107+
return new OptRemarkGenerator();
108+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-sil-opt -sil-opt-remark-generator -sil-remarks-missed=sil-opt-remark-gen %s -o /dev/null 2>&1 | %FileCheck %s
2+
3+
// CHECK: {{.*}}opt-remark-generator.sil:18:3: remark: Unable to remove ARC operation
4+
// CHECK-NEXT: strong_retain
5+
// CHECK: {{.*}}opt-remark-generator.sil:19:3: remark: Unable to remove ARC operation
6+
// CHECK-NEXT: retain_value
7+
// CHECK: {{.*}}opt-remark-generator.sil:20:3: remark: Unable to remove ARC operation
8+
// CHECK-NEXT: strong_release
9+
// CHECK: {{.*}}opt-remark-generator.sil:21:3: remark: Unable to remove ARC operation
10+
// CHECK-NEXT: release_value
11+
12+
sil_stage canonical
13+
14+
import Builtin
15+
16+
sil @foo : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
17+
bb0(%0 : $Builtin.NativeObject):
18+
strong_retain %0 : $Builtin.NativeObject
19+
retain_value %0 : $Builtin.NativeObject
20+
strong_release %0 : $Builtin.NativeObject
21+
release_value %0 : $Builtin.NativeObject
22+
%9999 = tuple()
23+
return %9999 : $()
24+
}

0 commit comments

Comments
 (0)