Skip to content

Commit 4ff34aa

Browse files
committed
[SIL] Enabled printing canonical module.
To print the module, use the new llvm flag -sil-print-canonical-module which parallels the existing flag -sil-view-canonical-cfg. When that flag is passed, the new pass ModulePrinter is added to the diagnostic pass pipeline after mandatory diagnostics have run. The new pass just prints the module to stdout.
1 parent 11f2650 commit 4ff34aa

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

include/swift/SILOptimizer/PassManager/Passes.def

+2
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ PASS(Outliner, "outliner",
308308
"Function Outlining Optimization")
309309
PASS(OwnershipModelEliminator, "ownership-model-eliminator",
310310
"Eliminate Ownership Annotation of SIL")
311+
PASS(ModulePrinter, "module-printer",
312+
"Print the module")
311313
PASS(NestedSemanticFunctionCheck, "nested-semantic-function-check",
312314
"Diagnose improperly nested '@_semantics' functions")
313315
PASS(NonTransparentFunctionOwnershipModelEliminator,

lib/SILOptimizer/PassManager/PassPipeline.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ static llvm::cl::opt<bool> SILViewCanonicalCFG(
4747
"sil-view-canonical-cfg", llvm::cl::init(false),
4848
llvm::cl::desc("Enable the sil cfg viewer pass after diagnostics"));
4949

50+
static llvm::cl::opt<bool> SILPrintCanonicalModule(
51+
"sil-print-canonical-module", llvm::cl::init(false),
52+
llvm::cl::desc("Print the textual SIL module after diagnostics"));
53+
5054
static llvm::cl::opt<bool> SILViewSILGenCFG(
5155
"sil-view-silgen-cfg", llvm::cl::init(false),
5256
llvm::cl::desc("Enable the sil cfg viewer pass before diagnostics"));
@@ -64,6 +68,12 @@ static void addCFGPrinterPipeline(SILPassPipelinePlan &P, StringRef Name) {
6468
P.addCFGPrinter();
6569
}
6670

71+
static void addModulePrinterPipeline(SILPassPipelinePlan &plan,
72+
StringRef name) {
73+
plan.startPipeline(name);
74+
plan.addModulePrinter();
75+
}
76+
6777
static void addMandatoryDebugSerialization(SILPassPipelinePlan &P) {
6878
P.startPipeline("Mandatory Debug Serialization");
6979
P.addOwnershipModelEliminator();
@@ -190,6 +200,9 @@ SILPassPipelinePlan::getDiagnosticPassPipeline(const SILOptions &Options) {
190200
if (SILViewCanonicalCFG) {
191201
addCFGPrinterPipeline(P, "SIL View Canonical CFG");
192202
}
203+
if (SILPrintCanonicalModule) {
204+
addModulePrinterPipeline(P, "SIL Print Canonical Module");
205+
}
193206
return P;
194207
}
195208

lib/SILOptimizer/UtilityPasses/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ target_sources(swiftSILOptimizer PRIVATE
2525
LoopInfoPrinter.cpp
2626
LoopRegionPrinter.cpp
2727
MemBehaviorDumper.cpp
28+
ModulePrinter.cpp
2829
RCIdentityDumper.cpp
2930
SerializeSILPass.cpp
3031
SILDebugInfoGenerator.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===--- ModulePrinter.cpp - Module printer 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+
// A utility module pass to print the module as textual SIL.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#include "swift/SIL/SILPrintContext.h"
18+
#include "swift/SILOptimizer/PassManager/Passes.h"
19+
#include "swift/SILOptimizer/PassManager/Transforms.h"
20+
21+
using namespace swift;
22+
23+
namespace {
24+
25+
class SILModulePrinter : public SILModuleTransform {
26+
27+
/// The entry point.
28+
void run() override {
29+
auto *module = getModule();
30+
SILPrintContext context(llvm::outs(), /*Verbose*/ true, /*SortedSIL*/ true,
31+
/*PrintFullConvention*/ true);
32+
module->print(context);
33+
}
34+
};
35+
36+
} // end anonymous namespace
37+
38+
SILTransform *swift::createModulePrinter() { return new SILModulePrinter(); }

0 commit comments

Comments
 (0)