Skip to content

Commit a27c5f0

Browse files
[Printer] Conditionally print Clang types in emitted SIL.
Hopefully, this helps us debug Clang type mismatches better.
1 parent 5f45820 commit a27c5f0

File tree

15 files changed

+86
-37
lines changed

15 files changed

+86
-37
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ struct PrintOptions {
564564
static PrintOptions printDocInterface();
565565

566566
/// Retrieve the set of options suitable for printing SIL functions.
567-
static PrintOptions printSIL() {
567+
static PrintOptions printSIL(bool printFullConvention = false) {
568568
PrintOptions result;
569569
result.PrintLongAttrsOnSeparateLines = true;
570570
result.PrintStorageRepresentationAttrs = true;
@@ -575,6 +575,9 @@ struct PrintOptions {
575575
result.PrintIfConfig = false;
576576
result.OpaqueReturnTypePrinting =
577577
OpaqueReturnTypePrintingMode::StableReference;
578+
if (printFullConvention)
579+
result.PrintFunctionRepresentationAttrs =
580+
PrintOptions::FunctionRepresentationMode::Full;
578581
return result;
579582
}
580583

include/swift/AST/SILOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class SILOptions {
7878
/// variables by name when we print it out. This eases diffing of SIL files.
7979
bool EmitSortedSIL = false;
8080

81+
/// See \ref FrontendOptions.PrintFullConvention
82+
bool PrintFullConvention = false;
83+
8184
/// Whether to stop the optimization pipeline after serializing SIL.
8285
bool StopOptimizationAfterSerialization = false;
8386

include/swift/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ class FrontendOptions {
253253
/// See the \ref SILOptions.EmitSortedSIL flag.
254254
bool EmitSortedSIL = false;
255255

256+
/// Should we emit the cType when printing @convention(c) or no?
257+
bool PrintFullConvention = false;
258+
256259
/// Indicates whether the dependency tracker should track system
257260
/// dependencies as well.
258261
bool TrackSystemDeps = false;

include/swift/Frontend/ModuleInterfaceSupport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ struct ModuleInterfaceOptions {
3131
/// interface, or should we fully-qualify them?
3232
bool PreserveTypesAsWritten = false;
3333

34-
/// Should we emit the cType when printing @convention(c) or no?
35-
/// FIXME: [clang-function-type-serialization] This check should go away.
34+
/// See \ref FrontendOptions.PrintFullConvention.
35+
/// FIXME: [clang-function-type-serialization] This flag should go away.
3636
bool PrintFullConvention = false;
3737

3838
/// Copy of all the command-line flags passed at .swiftinterface

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,12 @@ def module_interface_preserve_types_as_written :
608608
HelpText<"When emitting a module interface, preserve types as they were "
609609
"written in the source">;
610610

611+
// FIXME: [clang-function-type-serialization] Make this a SIL-only option once we
612+
// start unconditionally emitting non-canonical Clang types in swiftinterfaces.
611613
def experimental_print_full_convention :
612614
Flag<["-"], "experimental-print-full-convention">,
613-
HelpText<"When emitting a module interface, emit additional @convention "
614-
"arguments, regardless of whether they were written in the source">;
615+
HelpText<"When emitting a module interface or SIL, emit additional @convention"
616+
" arguments, regardless of whether they were written in the source">;
615617

616618
def prebuilt_module_cache_path :
617619
Separate<["-"], "prebuilt-module-cache-path">,

include/swift/SIL/SILModule.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,10 +647,11 @@ class SILModule {
647647
/// \param ShouldSort If set to true sorts functions, vtables, sil global
648648
/// variables, and witness tables by name to ease diffing.
649649
/// \param PrintASTDecls If set to true print AST decls.
650-
void print(raw_ostream &OS, bool Verbose = false,
651-
ModuleDecl *M = nullptr, bool ShouldSort = false,
650+
void print(raw_ostream &OS,
651+
ModuleDecl *M = nullptr,
652+
const SILOptions &Opts = SILOptions(),
652653
bool PrintASTDecls = true) const {
653-
SILPrintContext PrintCtx(OS, Verbose, ShouldSort);
654+
SILPrintContext PrintCtx(OS, Opts);
654655
print(PrintCtx, M, PrintASTDecls);
655656
}
656657

include/swift/SIL/SILPrintContext.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_SIL_PRINTCONTEXT_H
1414
#define SWIFT_SIL_PRINTCONTEXT_H
1515

16+
#include "swift/AST/SILOptions.h"
1617
#include "swift/SIL/SILDebugScope.h"
1718
#include "swift/SIL/SILValue.h"
1819
#include "llvm/ADT/DenseMap.h"
@@ -65,13 +66,21 @@ class SILPrintContext {
6566
/// Print debug locations and scopes.
6667
bool DebugInfo;
6768

69+
/// See \ref FrontendOptions.PrintFullConvention.
70+
bool PrintFullConvention;
71+
6872
public:
6973
/// Constructor with default values for options.
7074
///
7175
/// DebugInfo will be set according to the -sil-print-debuginfo option.
7276
SILPrintContext(llvm::raw_ostream &OS, bool Verbose = false,
7377
bool SortedSIL = false);
7478

79+
/// Constructor based on SILOptions.
80+
///
81+
/// DebugInfo will be set according to the -sil-print-debuginfo option.
82+
SILPrintContext(llvm::raw_ostream &OS, const SILOptions &Opts);
83+
7584
SILPrintContext(llvm::raw_ostream &OS, bool Verbose,
7685
bool SortedSIL, bool DebugInfo);
7786

@@ -94,6 +103,9 @@ class SILPrintContext {
94103
/// Returns true if debug locations and scopes should be printed.
95104
bool printDebugInfo() const { return DebugInfo; }
96105

106+
/// Returns true if the entire @convention(c, cType: ..) should be printed.
107+
bool printFullConvention() const { return PrintFullConvention; }
108+
97109
SILPrintContext::ID getID(const SILBasicBlock *Block);
98110

99111
SILPrintContext::ID getID(const SILNode *node);

include/swift/SIL/SILType.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,8 @@ class SILType {
581581

582582
std::string getAsString() const;
583583
void dump() const;
584-
void print(raw_ostream &OS) const;
584+
void print(raw_ostream &OS,
585+
const PrintOptions &PO = PrintOptions::printSIL()) const;
585586
};
586587

587588
// Statically prevent SILTypes from being directly cast to a type

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ bool ArgsToFrontendOptionsConverter::convert(
6868

6969
Opts.EmitVerboseSIL |= Args.hasArg(OPT_emit_verbose_sil);
7070
Opts.EmitSortedSIL |= Args.hasArg(OPT_emit_sorted_sil);
71+
Opts.PrintFullConvention |=
72+
Args.hasArg(OPT_experimental_print_full_convention);
7173

7274
Opts.EnableTesting |= Args.hasArg(OPT_enable_testing);
7375
Opts.EnablePrivateImports |= Args.hasArg(OPT_enable_private_imports);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,8 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
942942
Opts.DebugSerialization |= Args.hasArg(OPT_sil_debug_serialization);
943943
Opts.EmitVerboseSIL |= Args.hasArg(OPT_emit_verbose_sil);
944944
Opts.EmitSortedSIL |= Args.hasArg(OPT_emit_sorted_sil);
945+
Opts.PrintFullConvention |=
946+
Args.hasArg(OPT_experimental_print_full_convention);
945947
Opts.PrintInstCounts |= Args.hasArg(OPT_print_inst_counts);
946948
if (const Arg *A = Args.getLastArg(OPT_external_pass_pipeline_filename))
947949
Opts.ExternalPassPipelineFilename = A->getValue();

0 commit comments

Comments
 (0)