Skip to content

Commit 3ded3e6

Browse files
authored
Merge pull request #2888 from swiftwasm/main
[pull] swiftwasm from main
2 parents 6beb6a9 + a2cb5a8 commit 3ded3e6

File tree

62 files changed

+1043
-1467
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1043
-1467
lines changed

docs/DebuggingTheCompiler.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,157 @@ the pipeline. Here is a quick summary of the various options:
537537
When printing IR for functions for print-[before|after]-all options, Only
538538
print the IR for functions whose name is in this comma separated list.
539539

540+
## Debugging assembly and object code
541+
542+
Understanding layout of compiler-generated metadata
543+
can sometimes involve looking at assembly and object code.
544+
545+
### Working with a single file
546+
547+
Here's how to generate assembly or object code:
548+
549+
```
550+
# Emit assembly in Intel syntax (AT&T syntax is the default)
551+
swiftc tmp.swift -emit-assembly -Xllvm -x86-asm-syntax=intel -o tmp.S
552+
553+
# Emit object code
554+
swiftc tmp.swift -emit-object -o tmp.o
555+
```
556+
557+
Understanding mangled names can be hard though: `swift demangle` to the rescue!
558+
559+
```
560+
swiftc tmp.swift -emit-assembly -Xllvm -x86-asm-syntax=intel -o - \
561+
| swift demangle > tmp-demangled.S
562+
563+
swiftc tmp.swift -emit-object -o tmp.o
564+
565+
# Look at where different symbols are located, sorting by address (-n)
566+
# and displaying section names (-m)
567+
nm -n -m tmp.o | swift demangle > tmp.txt
568+
569+
# Inspect disassembly of an existing dylib (AT&T syntax is the default)
570+
objdump -d -macho --x86-asm-syntax=intel /path/to/libcake.dylib \
571+
| swift demangle > libcake.S
572+
```
573+
574+
### Working with multiple files
575+
576+
Some bugs only manifest in WMO, and may involve complicated Xcode projects.
577+
Moreover, Xcode may be passing arguments via `-filelist`
578+
and expecting outputs via `-output-filelist`, and those file lists
579+
may be in temporary directories.
580+
581+
If you want to inspect assembly or object code for individual files when
582+
compiling under WMO, you can mimic this by doing the following:
583+
584+
```
585+
# Assuming all .swift files from the MyProject/Sources directory
586+
# need to be included
587+
find MyProject/Sources -name '*.swift' -type f > input-files.txt
588+
589+
# In some cases, projects may use multiple files with the same
590+
# name but in different directories (for different schemes),
591+
# which can be a problem. Having a file list makes working around
592+
# this convenient as you can manually manually edit out the files
593+
# that are not of interest at this stage.
594+
595+
mkdir Output
596+
597+
# 1. -output-filelist doesn't recreate a subdirectory structure,
598+
# so first strip out directories
599+
# 2. map .swift files to assembly files
600+
sed -e 's|.*/|Output/|;s|\.swift|.S|' input-files.txt > output-files.txt
601+
602+
# Save command-line arguments from Xcode's 'CompileSwiftSources' phase in
603+
# the build log to a file for convenience, say args.txt.
604+
#
605+
# -sdk /path/to/sdk <... other args ...>
606+
607+
xcrun swift-frontend @args.txt \
608+
-filelist input-files.txt \
609+
-output-filelist output-files.txt \
610+
-O -whole-module-optimization \
611+
-emit-assembly
612+
```
613+
614+
If you are manually calling `swift-frontend` without an Xcode invocation to
615+
use as a template, you will need to at least add
616+
`-sdk "$(xcrun --show-sdk-path macosx)"` (if compiling for macOS),
617+
and `-I /path/to/includedir` to include necessary swift modules and interfaces.
618+
619+
### Working with multi-architecture binaries
620+
621+
On macOS, one might be interested in debugging multi-architecture binaries
622+
such as [universal binaries][]. By default `nm` will show symbols from all
623+
architectures, so a universal binary might look funny due to two copies of
624+
everything. Use `nm -arch` to look at a specific architecture:
625+
626+
```
627+
nm -n -m -arch x86_64 path/to/libcake.dylib | swift demangle
628+
```
629+
630+
[universal binaries]: https://en.wikipedia.org/wiki/Universal_binary
631+
632+
### Other helpful tools
633+
634+
TODO: This section should mention information about non-macOS platforms:
635+
maybe we can have a table with rows for use cases and columns for
636+
platforms (macOS, Linux, Windows), and the cells would be tool names.
637+
We could also mention platforms next to the tool names.
638+
639+
In the previous sub-sections, we've seen how using different tools can
640+
make working with assembly and object code much nicer. Here is a short
641+
listing of commonly used tools on macOS, along with some example use cases:
642+
643+
- Miscellaneous:
644+
- `strings`: Find printable strings in a binary file.
645+
- Potential use cases: If you're building a binary in multiple configurations,
646+
and forgot which binary corresponds to which configuration, you can look
647+
through the output of `strings` to identify differences.
648+
- `c++filt`: The C++ equivalent of `swift-demangle`.
649+
- Potential use cases: Looking at the generated code for the
650+
Swift runtime, investigating C++ interop issues.
651+
652+
- Linking:
653+
- `libtool`: A tool to create static and dynamic libraries. Generally, it's
654+
easier to instead ask `swiftc` to link files, but potentially handy as
655+
a higher-level alternative to `ld`, `ar` and `lipo`.
656+
657+
- Debug info:
658+
- `dwarfdump`: Extract debug info in human-readable form.
659+
- Potential use cases: If you want to quickly check if two binaries
660+
are identical, you can compare their UUIDs. For on-disk binaries,
661+
you can obtain the UUID using `dwarfdump --uuid` For binaries
662+
loaded by a running application, you can obtain the UUID using
663+
`image list` in LLDB.
664+
- `objdump`: Dump object files.
665+
Some examples of using `objdump` are documented in the previous subsection.
666+
If you have a Swift compiler build, you can use `llvm-objdump` from
667+
`$LLVM_BUILD_DIR/bin` instead of using the system `objdump`.
668+
669+
Compared to other tools on this list, `objdump` packs a LOT of
670+
functionality; it can show information about sections, relocations
671+
and more. It also supports many flags to format and filter the output.
672+
673+
- Linker information (symbol table, sections, binding):
674+
- `nm`: Display symbol tables.
675+
Some examples of using `nm` are documented in the previous subsection.
676+
- `size`: Get high-level information about sections in a binary,
677+
such as the sizes of sections and where they are located.
678+
- `dyldinfo`: Display information used by dyld, such as which dylibs
679+
an image depends on.
680+
- `install_name_tool`: Change the name for a dynamic shared library,
681+
and query or modify the runpath search paths (aka 'rpaths') it uses.
682+
683+
- Multi-architecture binaries:
684+
- `lipo`: A tool that can be used to create, inspect and dissect
685+
[universal binaries][universal binaries].
686+
- Potential use cases: If you have a universal binary on an
687+
Apple Silicon Mac, but want to quickly test if the issue would reproduce
688+
on `x86_64`, you can extract the `x86_64` slice by using `lipo`.
689+
The `x86_64` binary will automatically run under Rosetta 2.
690+
540691
## Bisecting Compiler Errors
541692

542693
### Bisecting on SIL optimizer pass counts to identify optimizer bugs

docs/HowToGuides/FirstPullRequest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ If you had an active discussion with someone on how to implement your change,
124124
you can `@` mention them in the PR description and ask for code review.
125125
If you directly implemented the change without any guidance from anyone else,
126126
`@` mention someone from GitHub's suggested reviewers. If GitHub doesn't
127-
make any suggestions, ask the [Code Owner](/CODE_OWNERS.txt) based on the
127+
make any suggestions, ask the [Code Owner](https://github.com/apple/swift/blob/main/CODE_OWNERS.TXT) based on the
128128
component for your change. Please ask someone though! We don't want you to get
129129
stuck because you were not sure who to ask for code review.
130130

include/swift/AST/DiagnosticEngine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,10 @@ namespace swift {
740740
/// Path to diagnostic documentation directory.
741741
std::string diagnosticDocumentationPath = "";
742742

743+
/// Whether we are actively pretty-printing a declaration as part of
744+
/// diagnostics.
745+
bool IsPrettyPrintingDecl = false;
746+
743747
friend class InFlightDiagnostic;
744748
friend class DiagnosticTransaction;
745749
friend class CompoundDiagnosticTransaction;
@@ -796,6 +800,8 @@ namespace swift {
796800
return diagnosticDocumentationPath;
797801
}
798802

803+
bool isPrettyPrintingDecl() const { return IsPrettyPrintingDecl; }
804+
799805
void setLocalization(std::string locale, std::string path) {
800806
assert(!locale.empty());
801807
assert(!path.empty());

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ ERROR(cannot_apply_lvalue_binop_to_subelement,none,
225225
ERROR(cannot_apply_lvalue_binop_to_rvalue,none,
226226
"left side of mutating operator has immutable type %0", (Type))
227227

228+
ERROR(cannot_subscript_base,none,
229+
"cannot subscript a value of type %0",
230+
(Type))
231+
232+
ERROR(cannot_subscript_ambiguous_base,none,
233+
"cannot subscript a value of incorrect or ambiguous type", ())
234+
228235
ERROR(cannot_subscript_nil_literal,none,
229236
"cannot subscript a nil literal value", ())
230237
ERROR(conditional_cast_from_nil,none,

include/swift/AST/Expr.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,9 +3148,24 @@ class LoadExpr : public ImplicitConversionExpr {
31483148
static bool classof(const Expr *E) { return E->getKind() == ExprKind::Load; }
31493149
};
31503150

3151+
/// This is a conversion from an expression of UnresolvedType to an arbitrary
3152+
/// other type, and from an arbitrary type to UnresolvedType. This node does
3153+
/// not appear in valid code, only in code involving diagnostics.
3154+
class UnresolvedTypeConversionExpr : public ImplicitConversionExpr {
3155+
public:
3156+
UnresolvedTypeConversionExpr(Expr *subExpr, Type type)
3157+
: ImplicitConversionExpr(ExprKind::UnresolvedTypeConversion, subExpr, type) {}
3158+
3159+
static bool classof(const Expr *E) {
3160+
return E->getKind() == ExprKind::UnresolvedTypeConversion;
3161+
}
3162+
};
3163+
31513164
/// FunctionConversionExpr - Convert a function to another function type,
31523165
/// which might involve renaming the parameters or handling substitutions
31533166
/// of subtypes (in the return) or supertypes (in the input).
3167+
///
3168+
/// FIXME: This should be a CapturingExpr.
31543169
class FunctionConversionExpr : public ImplicitConversionExpr {
31553170
public:
31563171
FunctionConversionExpr(Expr *subExpr, Type type)

include/swift/AST/ExprNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ ABSTRACT_EXPR(Apply, Expr)
151151
ABSTRACT_EXPR(ImplicitConversion, Expr)
152152
EXPR(Load, ImplicitConversionExpr)
153153
EXPR(DestructureTuple, ImplicitConversionExpr)
154+
EXPR(UnresolvedTypeConversion, ImplicitConversionExpr)
154155
EXPR(FunctionConversion, ImplicitConversionExpr)
155156
EXPR(CovariantFunctionConversion, ImplicitConversionExpr)
156157
EXPR(CovariantReturnConversion, ImplicitConversionExpr)

include/swift/Basic/LangOptions.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,6 @@ namespace swift {
290290
/// [TODO: Clang-type-plumbing] Turn on for feature rollout.
291291
bool UseClangFunctionTypes = false;
292292

293-
/// Whether to use the import as member inference system
294-
///
295-
/// When importing a global, try to infer whether we can import it as a
296-
/// member of some type instead. This includes inits, computed properties,
297-
/// and methods.
298-
bool InferImportAsMember = false;
299-
300293
/// If set to true, compile with the SIL Opaque Values enabled.
301294
/// This is for bootstrapping. It can't be in SILOptions because the
302295
/// TypeChecker uses it to set resolve the ParameterConvention.
@@ -644,13 +637,6 @@ namespace swift {
644637
/// instead of dropped altogether when possible.
645638
bool ImportForwardDeclarations = false;
646639

647-
/// Whether to use the import as member inference system
648-
///
649-
/// When importing a global, try to infer whether we can import it as a
650-
/// member of some type instead. This includes inits, computed properties,
651-
/// and methods.
652-
bool InferImportAsMember = false;
653-
654640
/// If true ignore the swift bridged attribute.
655641
bool DisableSwiftBridgeAttr = false;
656642

@@ -683,7 +669,6 @@ namespace swift {
683669
static_cast<uint8_t>(Mode),
684670
DetailedPreprocessingRecord,
685671
ImportForwardDeclarations,
686-
InferImportAsMember,
687672
DisableSwiftBridgeAttr,
688673
DisableOverlayModules);
689674
}

include/swift/Option/FrontendOptions.td

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,8 @@ def disable_conformance_availability_errors : Flag<["-"],
417417
"disable-conformance-availability-errors">,
418418
HelpText<"Diagnose conformance availability violations as warnings">;
419419

420-
def enable_infer_import_as_member :
421-
Flag<["-"], "enable-infer-import-as-member">,
422-
HelpText<"Infer when a global could be imported as a member">;
420+
def report_errors_to_debugger : Flag<["-"], "report-errors-to-debugger">,
421+
HelpText<"Deprecated, will be removed in future versions.">;
423422

424423
def enable_swift3_objc_inference : Flag<["-"], "enable-swift3-objc-inference">,
425424
Flags<[FrontendOption, HelpHidden]>,

lib/AST/ASTDumper.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,6 +2270,11 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
22702270
printRec(E->getResultExpr());
22712271
PrintWithColorRAII(OS, ParenthesisColor) << ')';
22722272
}
2273+
void visitUnresolvedTypeConversionExpr(UnresolvedTypeConversionExpr *E) {
2274+
printCommon(E, "unresolvedtype_conversion_expr") << '\n';
2275+
printRec(E->getSubExpr());
2276+
PrintWithColorRAII(OS, ParenthesisColor) << ')';
2277+
}
22732278
void visitFunctionConversionExpr(FunctionConversionExpr *E) {
22742279
printCommon(E, "function_conversion_expr") << '\n';
22752280
printRec(E->getSubExpr());

lib/AST/DiagnosticEngine.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,8 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {
10391039
// Pretty-print the declaration we've picked.
10401040
llvm::raw_svector_ostream out(buffer);
10411041
TrackingPrinter printer(entries, out, bufferAccessLevel);
1042+
llvm::SaveAndRestore<bool> isPrettyPrinting(
1043+
IsPrettyPrintingDecl, true);
10421044
ppDecl->print(
10431045
printer,
10441046
PrintOptions::printForDiagnostics(

lib/AST/Expr.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ ConcreteDeclRef Expr::getReferencedDecl(bool stopAtParenExpr) const {
338338

339339
PASS_THROUGH_REFERENCE(Load, getSubExpr);
340340
NO_REFERENCE(DestructureTuple);
341+
NO_REFERENCE(UnresolvedTypeConversion);
341342
PASS_THROUGH_REFERENCE(FunctionConversion, getSubExpr);
342343
PASS_THROUGH_REFERENCE(CovariantFunctionConversion, getSubExpr);
343344
PASS_THROUGH_REFERENCE(CovariantReturnConversion, getSubExpr);
@@ -662,6 +663,7 @@ bool Expr::canAppendPostfixExpression(bool appendingPostfixOperator) const {
662663

663664
case ExprKind::Load:
664665
case ExprKind::DestructureTuple:
666+
case ExprKind::UnresolvedTypeConversion:
665667
case ExprKind::FunctionConversion:
666668
case ExprKind::CovariantFunctionConversion:
667669
case ExprKind::CovariantReturnConversion:

lib/ClangImporter/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ add_swift_host_library(swiftClangImporter STATIC
1414
ClangModuleDependencyScanner.cpp
1515
ClangSourceBufferImporter.cpp
1616
DWARFImporter.cpp
17-
IAMInference.cpp
1817
ImportDecl.cpp
1918
ImportEnumInfo.cpp
2019
ImportMacro.cpp

0 commit comments

Comments
 (0)