Skip to content

Commit 9c363fb

Browse files
committed
libswift: add an example SILPrinter pass.
SILPrinter prints the SIL of a function. It's and example pass which demonstrates the basic SIL API of libswift.
1 parent 14422cd commit 9c363fb

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ PASS(SILDebugInfoGenerator, "sil-debuginfo-gen",
339339
"Generate Debug Information with Source Locations into Textual SIL")
340340
PASS(EarlySROA, "early-sroa",
341341
"Scalar Replacement of Aggregate Stack Objects on high-level SIL")
342+
SWIFT_FUNCTION_PASS(SILPrinter, "sil-printer",
343+
"Test pass which prints the SIL of a function")
342344
PASS(SROA, "sroa",
343345
"Scalar Replacement of Aggregate Stack Objects")
344346
PASS(SROABBArgs, "sroa-bb-args",

libswift/Sources/Optimizer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ add_libswift_module(Optimizer
1010
DEPENDS SIL)
1111

1212
add_subdirectory(PassManager)
13+
add_subdirectory(FunctionPasses)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
libswift_sources(Optimizer
10+
SILPrinter.swift
11+
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===--- SILPrinter.swift - Example swift function pass -------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 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+
import SIL
14+
15+
let silPrinterPass = FunctionPass(name: "sil-printer", runSILPrinter)
16+
17+
func runSILPrinter(function: Function, context: FunctionPassContext) {
18+
print("run SILPrinter on function: \(function.name)")
19+
20+
for (bbIdx, block) in function.blocks.enumerated() {
21+
print("bb\(bbIdx):")
22+
23+
print(" arguments:")
24+
for arg in block.arguments {
25+
print(" arg: \(arg)")
26+
for use in arg.uses {
27+
print(" user: \(use.instruction)")
28+
}
29+
}
30+
31+
print(" instructions:")
32+
for inst in block.instructions {
33+
print(" \(inst)")
34+
for op in inst.operands {
35+
print(" op: \(op.value)")
36+
}
37+
for (resultIdx, result) in inst.results.enumerated() {
38+
for use in result.uses {
39+
print(" user of result \(resultIdx): \(use.instruction)")
40+
}
41+
}
42+
}
43+
print()
44+
}
45+
}

libswift/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ private func registerPass(
2828
}
2929

3030
private func registerSwiftPasses() {
31+
registerPass(silPrinterPass, { silPrinterPass.run($0) })
3132
}

0 commit comments

Comments
 (0)