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();

lib/FrontendTool/FrontendTool.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -499,20 +499,19 @@ static bool emitSyntax(SourceFile *SF, StringRef OutputFilename) {
499499
}
500500

501501
/// Writes SIL out to the given file.
502-
static bool writeSIL(SILModule &SM, ModuleDecl *M, bool EmitVerboseSIL,
503-
StringRef OutputFilename, bool SortSIL) {
502+
static bool writeSIL(SILModule &SM, ModuleDecl *M, const SILOptions &Opts,
503+
StringRef OutputFilename) {
504504
auto OS = getFileOutputStream(OutputFilename, M->getASTContext());
505505
if (!OS) return true;
506-
SM.print(*OS, EmitVerboseSIL, M, SortSIL);
506+
SM.print(*OS, M, Opts);
507507

508508
return M->getASTContext().hadError();
509509
}
510510

511511
static bool writeSIL(SILModule &SM, const PrimarySpecificPaths &PSPs,
512512
const CompilerInstance &Instance,
513-
const SILOptions &opts) {
514-
return writeSIL(SM, Instance.getMainModule(), opts.EmitVerboseSIL,
515-
PSPs.OutputFilename, opts.EmitSortedSIL);
513+
const SILOptions &Opts) {
514+
return writeSIL(SM, Instance.getMainModule(), Opts, PSPs.OutputFilename);
516515
}
517516

518517
/// Prints the Objective-C "generated header" interface for \p M to \p

lib/SIL/SILPrinter.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,11 @@ static void printSILTypeColorAndSigil(raw_ostream &OS, SILType t) {
419419
::print(OS, t.getCategory());
420420
}
421421

422-
void SILType::print(raw_ostream &OS) const {
422+
void SILType::print(raw_ostream &OS, const PrintOptions &PO) const {
423423
printSILTypeColorAndSigil(OS, *this);
424424

425425
// Print other types as their Swift representation.
426-
PrintOptions SubPrinter = PrintOptions::printSIL();
427-
getASTType().print(OS, SubPrinter);
426+
getASTType().print(OS, PO);
428427
}
429428

430429
void SILType::dump() const {
@@ -493,7 +492,8 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
493492
SILPrintContext &PrintCtx,
494493
llvm::DenseMap<CanType, Identifier> *AlternativeTypeNames = nullptr)
495494
: Ctx(PrintCtx),
496-
PrintState{{PrintCtx.OS()}, PrintOptions::printSIL()},
495+
PrintState{{PrintCtx.OS()},
496+
PrintOptions::printSIL(PrintCtx.printFullConvention())},
497497
LastBufferID(0) {
498498
PrintState.ASTOptions.AlternativeTypeNames = AlternativeTypeNames;
499499
PrintState.ASTOptions.PrintForSIL = true;
@@ -2449,7 +2449,8 @@ void SILFunction::print(SILPrintContext &PrintCtx) const {
24492449
}
24502450

24512451
{
2452-
PrintOptions withGenericEnvironment = PrintOptions::printSIL();
2452+
PrintOptions withGenericEnvironment =
2453+
PrintOptions::printSIL(PrintCtx.printFullConvention());
24532454
withGenericEnvironment.GenericEnv = env;
24542455
withGenericEnvironment.AlternativeTypeNames =
24552456
Aliases.empty() ? nullptr : &Aliases;
@@ -2732,7 +2733,8 @@ void SILModule::print(SILPrintContext &PrintCtx, ModuleDecl *M,
27322733
// Print the declarations and types from the associated context (origin module or
27332734
// current file).
27342735
if (M && PrintASTDecls) {
2735-
PrintOptions Options = PrintOptions::printSIL();
2736+
PrintOptions Options =
2737+
PrintOptions::printSIL(PrintCtx.printFullConvention());
27362738
Options.TypeDefinitions = true;
27372739
Options.VarInitializers = true;
27382740
// FIXME: ExplodePatternBindingDecls is incompatible with VarInitializers!
@@ -3116,6 +3118,11 @@ SILPrintContext::SILPrintContext(llvm::raw_ostream &OS, bool Verbose,
31163118
OutStream(OS), Verbose(Verbose), SortedSIL(SortedSIL),
31173119
DebugInfo(SILPrintDebugInfo) { }
31183120

3121+
SILPrintContext::SILPrintContext(llvm::raw_ostream &OS,
3122+
const SILOptions &Opts) :
3123+
OutStream(OS), Verbose(Opts.EmitVerboseSIL), SortedSIL(Opts.EmitSortedSIL),
3124+
DebugInfo(SILPrintDebugInfo), PrintFullConvention(Opts.PrintFullConvention) {}
3125+
31193126
SILPrintContext::SILPrintContext(llvm::raw_ostream &OS, bool Verbose,
31203127
bool SortedSIL, bool DebugInfo) :
31213128
OutStream(OS), Verbose(Verbose), SortedSIL(SortedSIL),

test/SIL/clang-function-types.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %target-swift-frontend %s -emit-sil -swift-version 5 -use-clang-function-types -experimental-print-full-convention -o - | %FileCheck %s
2+
3+
public func f(g: @convention(c) () -> ()) { g() }
4+
5+
// CHECK: sil @$s4main1f1gyyyXC_tF : $@convention(thin) (@convention(c, cType: "void (*)(void)") @noescape () -> ()) -> () {
6+
// CHECK: bb0(%0 : $@convention(c, cType: "void (*)(void)") @noescape () -> ()):
7+
// CHECK: debug_value %0 : $@convention(c, cType: "void (*)(void)") @noescape () -> (), let, name "g", argno 1 // id: %1
8+
// CHECK: %2 = apply %0() : $@convention(c, cType: "void (*)(void)") @noescape () -> ()

tools/sil-func-extractor/SILFunctionExtractor.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ static llvm::cl::opt<std::string> Triple("target",
101101
llvm::cl::desc("target triple"));
102102

103103
static llvm::cl::opt<bool>
104-
EnableSILSortOutput("emit-sorted-sil", llvm::cl::Hidden,
105-
llvm::cl::init(false),
106-
llvm::cl::desc("Sort Functions, VTables, Globals, "
107-
"WitnessTables by name to ease diffing."));
104+
EmitSortedSIL("emit-sorted-sil", llvm::cl::Hidden,
105+
llvm::cl::init(false),
106+
llvm::cl::desc("Sort Functions, VTables, Globals, "
107+
"WitnessTables by name to ease diffing."));
108108

109109
static llvm::cl::opt<bool>
110110
DisableASTDump("sil-disable-ast-dump", llvm::cl::Hidden,
@@ -250,6 +250,10 @@ int main(int argc, char **argv) {
250250
Invocation.getLangOptions().EnableAccessControl = false;
251251
Invocation.getLangOptions().EnableObjCAttrRequiresFoundation = false;
252252

253+
SILOptions &Opts = Invocation.getSILOptions();
254+
Opts.EmitVerboseSIL = EmitVerboseSIL;
255+
Opts.EmitSortedSIL = EmitSortedSIL;
256+
253257
serialization::ExtendedValidationInfo extendedInfo;
254258
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
255259
Invocation.setUpInputForSILTool(InputFilename, ModuleName,
@@ -356,8 +360,8 @@ int main(int argc, char **argv) {
356360
OutputFilename.size() ? StringRef(OutputFilename) : "-";
357361

358362
if (OutputFile == "-") {
359-
CI.getSILModule()->print(llvm::outs(), EmitVerboseSIL, CI.getMainModule(),
360-
EnableSILSortOutput, !DisableASTDump);
363+
CI.getSILModule()->print(llvm::outs(), CI.getMainModule(),
364+
Invocation.getSILOptions(), !DisableASTDump);
361365
} else {
362366
std::error_code EC;
363367
llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_None);
@@ -366,8 +370,8 @@ int main(int argc, char **argv) {
366370
<< '\n';
367371
return 1;
368372
}
369-
CI.getSILModule()->print(OS, EmitVerboseSIL, CI.getMainModule(),
370-
EnableSILSortOutput, !DisableASTDump);
373+
CI.getSILModule()->print(OS, CI.getMainModule(),
374+
Invocation.getSILOptions(), !DisableASTDump);
371375
}
372376
}
373377
}

tools/sil-opt/SILOpt.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ static llvm::cl::opt<std::string>
196196
ModuleCachePath("module-cache-path", llvm::cl::desc("Clang module cache path"));
197197

198198
static llvm::cl::opt<bool>
199-
EnableSILSortOutput("emit-sorted-sil", llvm::cl::Hidden,
200-
llvm::cl::init(false),
201-
llvm::cl::desc("Sort Functions, VTables, Globals, "
202-
"WitnessTables by name to ease diffing."));
199+
EmitSortedSIL("emit-sorted-sil", llvm::cl::Hidden,
200+
llvm::cl::init(false),
201+
llvm::cl::desc("Sort Functions, VTables, Globals, "
202+
"WitnessTables by name to ease diffing."));
203203

204204
static llvm::cl::opt<bool>
205205
DisableASTDump("sil-disable-ast-dump", llvm::cl::Hidden,
@@ -375,6 +375,8 @@ int main(int argc, char **argv) {
375375
break;
376376
}
377377
}
378+
SILOpts.EmitVerboseSIL |= EmitVerboseSIL;
379+
SILOpts.EmitSortedSIL |= EmitSortedSIL;
378380

379381
serialization::ExtendedValidationInfo extendedInfo;
380382
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
@@ -481,8 +483,8 @@ int main(int argc, char **argv) {
481483
StringRef(OutputFilename) : "-";
482484

483485
if (OutputFile == "-") {
484-
CI.getSILModule()->print(llvm::outs(), EmitVerboseSIL, CI.getMainModule(),
485-
EnableSILSortOutput, !DisableASTDump);
486+
CI.getSILModule()->print(llvm::outs(), CI.getMainModule(),
487+
Invocation.getSILOptions(), !DisableASTDump);
486488
} else {
487489
std::error_code EC;
488490
llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_None);
@@ -491,8 +493,8 @@ int main(int argc, char **argv) {
491493
<< EC.message() << '\n';
492494
return 1;
493495
}
494-
CI.getSILModule()->print(OS, EmitVerboseSIL, CI.getMainModule(),
495-
EnableSILSortOutput, !DisableASTDump);
496+
CI.getSILModule()->print(OS, CI.getMainModule(),
497+
Invocation.getSILOptions(), !DisableASTDump);
496498
}
497499
}
498500

0 commit comments

Comments
 (0)