Files
swift-mirror/SwiftCompilerSources/Sources/Optimizer/TestPasses/SILPrinter.swift
Andrew Trick a5d8aafb23 SwiftCompilerSources: Replace BlockArgument with Phi and TermResult.
All SILArgument types are "block arguments". There are three kinds:
1. Function arguments
2. Phis
3. Terminator results

In every situation where the source of the block argument matters, we
need to distinguish between these three. Accidentally failing to
handle one of the cases is an perpetual source of compiler
bugs. Attempting to handle both phis and terminator results uniformly
is *always* a bug, especially once OSSA has phi flags. Even when all
cases are handled correctly, the code that deals with data flow across
blocks is incomprehensible without giving each case a type. This
continues to be a massive waste of time literally every time I review
code that involves cross-block control flow.

Unfortunately, we don't have these C++ types yet (nothing big is
blocking that, it just wasn't done). That's manageable because we can
use wrapper types on the Swift side for now. Wrapper types don't
create any more complexity than protocols, but they do sacrifice some
usability in switch cases.

There is no reason for a BlockArgument type. First, a function
argument is a block argument just as much as any other. BlockArgument
provides no useful information beyond Argument. And it is nearly
always a mistake to care about whether a value is a function argument
and not care whether it is a phi or terminator result.
2023-09-27 18:47:46 -07:00

54 lines
1.6 KiB
Swift

//===--- SILPrinter.swift - Example swift function pass -------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SIL
let silPrinterPass = FunctionPass(name: "sil-printer", runSILPrinter)
func runSILPrinter(function: Function, context: FunctionPassContext) {
print("run SILPrinter on function: \(function.name)")
for (bbIdx, block) in function.blocks.enumerated() {
print("bb\(bbIdx):")
print(" predecessors: \(block.predecessors)")
print(" successors: \(block.successors)")
print(" arguments:")
for arg in block.arguments {
print(" arg: \(arg)")
for use in arg.uses {
print(" user: \(use.instruction)")
}
if let phi = Phi(arg) {
for incoming in phi.incomingValues {
print(" incoming: \(incoming)")
}
}
}
print(" instructions:")
for inst in block.instructions {
print(" \(inst)")
for op in inst.operands {
print(" op: \(op.value)")
}
for (resultIdx, result) in inst.results.enumerated() {
for use in result.uses {
print(" user of result \(resultIdx): \(use.instruction)")
}
}
}
print()
}
}