Commit Graph

141 Commits

Author SHA1 Message Date
Anthony Latsis
fec049e5e4 Address llvm::PointerUnion::{is,get} deprecations
These were deprecated in
https://github.com/llvm/llvm-project/pull/122623.
2025-07-29 18:37:48 +01:00
Erik Eckstein
63cb683cb7 SIL: improve some Location APIs
* rename `var autoGenerated` -> `var asAutoGenerated`
* add `var asCleanup`
* add `func withScope`
2025-06-20 08:15:00 +02:00
Anthony Latsis
08d71a46eb Basic, SIL: Include <swift/bridging> when avaiable instead of defining our own macros 2025-06-13 23:13:26 +01:00
Usama Hameed
203f906364 Serialize/Deserialize source locations for instructions (#77281)
This commit adds support for serializing and deserializing source locations for instructions.
2024-12-12 16:15:44 +05:00
Egor Zhdan
0efa7f90ef [cxx-interop][SwiftCompilerSources] Do not use SWIFT_IMPORT_UNSAFE with SILDebugLocation
`SWIFT_IMPORT_UNSAFE` is an escape hatch that can be used to make the Swift compiler ignore its usual safety heuristics for C++ types.

`SILDebugLocation` is better described as a self-contained Swift type.

Similar to 35263ef9.
2024-09-17 13:07:57 +01:00
Ben Barham
d72f5b12c4 Update StringRef::equals references to operator==
`equals` has been deprecated upstream, use `operator==` instead.
2024-06-27 19:14:06 -07:00
Emil Pedersen
bc3bf84872 Fix crash in hash_value for SILLocation
We should not dereference the file position if this SILLocation is not a position.
If it's a pointer, we compare the pointers.
2024-05-09 14:57:00 -07:00
Emil Pedersen
584d1db307 [IRGen] [DebugInfo] Ignore autogenerated flag for variables 2024-04-24 18:25:43 -07:00
Emil Pedersen
fed03ccc2b [SILVerifier] Add detection of conflicting debug variables 2024-03-20 15:55:52 -07:00
Michael Gottesman
c929995fdf [sil] Add the ability to grab an ASTNode out of a SILLocation.
Previously if one wanted to get an ASTNode, one needed to use the
getAsASTNode<T>() type. If one just wants to get out the type and use it in a
generic way using ASTNode there wasn't any way to do this... so I did it. We
actually have to do the marshalling here since ASTNodeTy and ASTNode have
different layouts despite them both being PointerUnions. So one can't just cast
in between them =---(.
2023-11-15 16:31:32 -08:00
Erik Eckstein
2dbd6cc56b SwiftCompilerSources: rework bridging
Introduce two modes of bridging:
* inline mode: this is basically how it worked so far. Using full C++ interop which allows bridging functions to be inlined.
* pure mode: bridging functions are not inlined but compiled in a cpp file. This allows to reduce the C++ interop requirements to a minimum. No std/llvm/swift headers are imported.

This change requires a major refactoring of bridging sources. The implementation of bridging functions go to two separate files: SILBridgingImpl.h and OptimizerBridgingImpl.h.
Depending on the mode, those files are either included in the corresponding header files (inline mode), or included in the c++ file (pure mode).

The mode can be selected with the BRIDGING_MODE cmake variable. By default it is set to the inline mode (= existing behavior). The pure mode is only selected in certain configurations to work around C++ interop issues:
* In debug builds, to workaround a problem with LLDB's `po` command (rdar://115770255).
* On windows to workaround a build problem.
2023-10-09 09:52:52 +02:00
Adrian Prantl
958a1577b5 Ensure calls to getters have a source location.
Calls to getters are implicit because the compiler inserts them on a property
access, but the location is useful in backtraces so it should be preserved.

rdar://109123395
2023-05-31 17:41:26 -07:00
Erik Eckstein
ee1c52bc77 Swift SIL: add some APIs 2023-05-22 15:34:26 +02:00
Adrian Prantl
158772c2ab Rebase SILScope generation on top of ASTScope.
This patch replaces the stateful generation of SILScope information in
SILGenFunction with data derived from the ASTScope hierarchy, which should be
100% in sync with the scopes needed for local variables. The goal is to
eliminate the surprising effects that the stack of cleanup operations can have
on the current state of SILBuilder leading to a fully deterministic (in the
sense of: predictible by a human) association of SILDebugScopes with
SILInstructions. The patch also eliminates the need to many workarounds. There
are still some accomodations for several Sema transformation passes such as
ResultBuilders, which don't correctly update the source locations when moving
around nodes. If these were implemented as macros, this problem would disappear.

This necessary rewrite of the macro scope handling included in this patch also
adds proper support nested macro expansions.

This fixes

rdar://88274783

and either fixes or at least partially addresses the following:

rdar://89252827
rdar://105186946
rdar://105757810
rdar://105997826
rdar://105102288
2023-04-04 15:20:11 -07:00
Adrian Prantl
af3b73a8ac Merge pull request #64590 from adrian-prantl/implicit-sillocation
Preserve the Implicit attribute in SILLocation. (NFC)
2023-03-30 13:46:51 -07:00
Adrian Prantl
c5efebe784 Preserve the Implicit attribute in SILLocation. (NFC)
This change aims at reducing the need for SIL passes to check into the AST
storage of SILLocations. The end goal is to eventually merge this with the
autogenerated flag, but at the moment the emergent semantics of both properties
are not identical.
2023-03-23 14:59:05 -07:00
Adrian Prantl
63304d0787 SILLocation: Move pointsToEnd flag into ASTNodeTy. NFC
The pointsToEnd flag is only meaningful when combined with an AST node, and
moving it into the AST pointer frees up a bit in the flags bitfield for later
use.
2023-03-22 14:04:28 -07:00
Erik Eckstein
7905a9ac64 Swift Bridging: remove C bridging functions for SILDebugLocation 2023-03-21 15:33:09 +01:00
Erik Eckstein
e92f27c01a Swift SIL: add some APIs to Location
* `hasValidLineNumber`
* `isAutoGenerated`
* `isDebugSteppable`
2023-02-09 06:50:05 +01:00
Erik Eckstein
0ff8663f53 SILLocation: fix location comparison for "FilenameAndLocation" locations
Those are locations which are parsed form SIL files
2023-02-09 06:50:05 +01:00
Michael Gottesman
a5d7a095d2 [sil] Add the ability to create an ExtendedASTNodeLoc that is only used for diagnostics and not for debugging.
We already have entry points for creating an ExtendedASTNodeLoc with an empty
location for diagnostics and a location for debugging. This does the inverse so
one can emit diagnostics that point at a terminator sourceloc without needing to
add a ReturnInst to the instruction which is illegal and would cause the
SILVerifier to assert.
2023-01-24 16:55:54 -08:00
Doug Gregor
456fbf84f5 [Debug info] Dump macro expansion buffers to disk for debug info to reference
When emitting debug info that references into a macro expansion, dump
the macro expansion buffer into a file on disk (in the temporary
directory) so that one can see the macro expansion buffers in the
source files themselves.

The test here validates that the generated LLVM IR looks correct, but
I'm having a problem getting LLDB to actually locate the source code
correctly.
2023-01-19 21:30:25 -08:00
Erik Eckstein
d96ef3bedd Swift SIL: add some APIs to Location
* `var description`
* `func ==`
* `func hasSameSourceLocation(as:)`
2023-01-16 19:00:09 +01:00
Erik Eckstein
64ba6fdf5d SIL: simplify the SILNode inline bitfields.
The use of the SWIFT_INLINE_BITFIELD macros in SILNode were a constant source of confusion and bugs.
With this refactoring I tried to simplify the definition of "shared fields" in SILNode, SILValue and SILInstruction classes:

* Move `kind`, `locationKindAndFlags` and the 32-bit fields out of the 64-bitfield into their own member variables. This avoids _a lot_ of manual bit position computations.
* Now we have two separate "shared fields": an 8-bit field (e.g. for boolean flags) and a 32-bit field (e.g. for indices, which can potentially get large). Both fields can be used independently. Also, they are not "bit fields" per se. Instructions can use the field e.g. as a `bool`, `uint32_t`, or  - if multiple flags are to be stored - as a packed bit field.
* With these two separate fields, we don't have the need for defining bitfields both in a base class _and_ in a derived value/instruction class. We can get rid of the complex logic which handles such cases. Just keep a check to catch accidental overlaps of fields in base and derived classes.
* Still use preprocessor macros for the implementation, but much simpler ones than before.
* Add documentation.
2022-07-11 17:27:29 +02:00
Adrian Prantl
7c1c7b6249 Emit debug locations for hop_to_executor instructions.
The function prologue of async funclets inherits its source location
from the hop_to_executor instruction. This makes it easier to produce
logical backtraces, since the PC in logical frames will always point
to the start if the function.

rdar://89776340
2022-06-23 09:40:25 -07:00
Michael Gottesman
056132cca0 [move-function] Add a new pass that propagates debug_value [moved] into coroutine func-lets.
NOTE: debug_value [moved] appearing in the source code implies a _move was
used. So this will not effect current stable swift code.

This is just a first version of this that I am using to commit/bring up tests
for IRGen supporting a full dataflow version of this patch.

Big picture is that there is a bunch of work that is done in the LLVM level in
the coroutine splitter to work around communicating live variables in the
various coroutine func-lets. This logic is all done with debug.declare and we
would need to update that logic in the coroutine splitter to handle
debug.addr. Rather than do this, after some conversation, AdrianP and I realized
that we could get the same effect of a debug.declare by just redeclaring the
current live set of debug_value after each possible coroutine funclet start. To
do this in full generality, we need a full dataflow but just to bring this up we
initially perform a dominance propagation algorithm of the following sort:

1. We walk the CFG along successors. By doing this we guarantee that we visit
   blocks after their dominators.

2. When we visit a block, we walk the block from start->end. During this walk:

   a. We grab a new block state from the centralized block->blockState map. This
      state is a [SILDebugVariable : DebugValueInst].

   b. If we see a debug_value, we map blockState[debug_value.getDbgVar()] =
      debug_value. This ensures that when we get to the bottom of the block, we
      have pairs of SILDebugVariable + last debug_value on it.

   c. If we see any coroutine funclet boundaries, we clone the current tracked
      set of our block state and then walk up the dom tree dumping in each block
      any debug_value with a SILDebugVariable that we have not already
      dumped. This is maintained by using a visited set of SILDebugVariable for
      each funclet boundary.

The end result is that at the beginning of each funclet we will basically
declare the debug info for an addr.

This is insufficient of course for moves that are in conditional control flow,
e.x.:

```
let x = Klass()
if boolValue {
  await asyncCall()
  let _ = _move(x)
}
```

but this at least lets me begin to write tests for this in lldb using straight
line code and work out the rest of the issues in CodeGen using those tests.
2022-03-21 14:20:06 -07:00
Erik Eckstein
ce7358d35d SIL: create hop_to_executor instructions with "auto-generated" debug locations.
Those instructions should not be "visible" for debugging and for diagnostic messages.

Fixes a wrong "unreachable code" warning.

https://bugs.swift.org/browse/SR-14873
rdar://80237870
2021-08-26 19:14:16 +02:00
Min-Yih Hsu
d00a6cc0e3 [SIL][Frontend] Simplify debug info generation flow for SIL files
- If any of the `-g<kind>` flag is given -- except `-gnone`, debug
   info will be printed into every generated SIL files.
 - The `-gsil` is deprecated in favor of `-sil-based-debuginfo`. The
   SILDebugInfoGenerator Pass now generates intermediate SIL file with
   name "<output file>.sil_dbg_<n>.sil". Other functionalities of that
   Pass remain the same.
2021-06-30 17:21:58 -07:00
Saleem Abdulrasool
25f437e17d mark some switches as covered (NFCI)
Unfortunately, MSVC does not detect covered switches as clang.  Mark
some of the switches as covered to avoid an unnecessary warning from
MSVC.
2021-06-05 15:30:25 -07:00
Erik Eckstein
acd9e6354c SILLocation: fix a compiler error in no-assert builds 2021-03-10 19:17:13 +01:00
Erik Eckstein
65208c0642 SIL: efficiently store SILLocation in SILInstruction
Store the 1-byte kindAndFlags of SILLocation in the instruction's SILNode bitfield and only store SILLocation::storage in SILInstruction directly.
This reduces the space for the location from 2 to 1 word in SILInstruction.
2021-01-29 20:28:21 +01:00
Erik Eckstein
ec64f2a255 SILLocation: replace CleanupLocation::get(loc) with CleanupLocation(loc)
No need to have a static get function - the constructor can be used directly.
NFC
2021-01-29 20:28:21 +01:00
Erik Eckstein
462e58d3cb SILLocation: a big refactoring and reducing its size from 3 to 2 words
My goal was to reduce the size of SILLocation. It now contains only of a storage union, which is basically a pointer and a bitfield containing the Kind, StorageKind and flags. By far, most locations are only single pointers to an AST node. For the few cases where more data needs to be stored, this data is allocated separately: with the SILModule's bump pointer allocator.

While working on this, I couldn't resist to do a major refactoring to simplify the code:

* removed unused stuff
* The term "DebugLoc" was used for 3 completely different things:
    - for `struct SILLocation::DebugLoc` -> renamed it to `FilePosition`
    - for `hasDebugLoc()`/`getDebugSourceLoc()` -> renamed it to `hasASTNodeForDebugging()`/`getSourceLocForDebugging()`
    - for `class SILDebugLocation` -> kept it as it is (though, `SILScopedLocation` would be a better name, IMO)
* made SILLocation more "functional", i.e. replaced some setters with corresponding constructors
* replaced the hand-written bitfield `KindData` with C bitfields
* updated and improved comments
2021-01-29 20:28:21 +01:00
Erik Eckstein
fd85e0e1e0 SILLocation: remove unused flag bits.
PointsToStartBit: was never set.
IsInTopLevel: was never checked (except for SIL printing)
2021-01-29 20:28:21 +01:00
swift-ci
2ccecb09e0 Merge remote-tracking branch 'origin/master' into master-rebranch 2020-07-28 18:43:55 -07:00
Michael Gottesman
158eff19fd Merge pull request #33166 from gottesmm/pr-824f03205dd43d5614f7605c3f3ae53ac78be8cb
[silgen] Mark a bunch of functions that SILGen open-codeds as "auto-generated" and thus not user generated code.
2020-07-28 18:38:37 -07:00
Michael Gottesman
e3fa1376f6 [silgen] Mark a bunch of functions that SILGen open-codeds as "auto-generated" and thus not user generated code.
This ensures that we do not emit diagnostics onto these functions. I noticed we
were doing this with my work on opt-remarks when I was hitting diagnostics in
these functions in a set of subsequent commits.

The way that I found these is that I went through this file and looked for any
cases where we were never calling emitStmt on a user provided function body.

I also eliminated two places where we were setting a new DebugScope even though
preEmitFunction already does that for us.
2020-07-28 14:11:52 -07:00
Michael Gottesman
94cafc34ef [sil] Add an implicit operator bool conversion to SILDebugLocation.
The reason to do this is that one can now do:

```
if (auto debugLoc = i.getDebugLocation()) {
  // do something with debug loc.
}
```

I also cleaned up the style a little bit by renaming the internal ivars to be
camelCase.
2020-07-28 11:43:18 -07:00
swift_jenkins
14f2bbc9ec Merge remote-tracking branch 'origin/master' into master-next 2020-06-11 11:58:47 -07:00
David Zarzycki
d9073c83fa Merge pull request #32135 from davezarzycki/pr32135
[SIL] NFC: Shrink SILGlobalVariable by 16 bytes
2020-06-11 14:51:05 -04:00
swift_jenkins
d5a58018fc Merge remote-tracking branch 'origin/master' into master-next 2020-06-10 13:39:43 -07:00
Vedant Kumar
60ec3f1b90 Fix debug description for cases with multiple items (#32282)
* [SILGenFunction] Don't create redundant nested debug scopes

Instead of emitting:

```
sil_scope 4 { loc "main.swift":6:19 parent 3 }
sil_scope 5 { loc "main.swift":7:3 parent 4 }
sil_scope 6 { loc "main.swift":7:3 parent 5 }
sil_scope 7 { loc "main.swift":7:3 parent 5 }
sil_scope 8 { loc "main.swift":9:5 parent 4 }
```

Emit:

```
sil_scope 4 { loc "main.swift":6:19 parent 3 }
sil_scope 5 { loc "main.swift":7:3 parent 4 }
sil_scope 6 { loc "main.swift":9:5 parent 5 }
```

* [IRGenSIL] Diagnose conflicting shadow copies

If we attempt to store a value with the wrong type into a slot reserved
for a shadow copy, diagnose what went wrong.

* [SILGenPattern] Defer debug description of case variables

Create unique nested debug scopes for a switch, each of its case labels,
and each of its case bodies. This looks like:

```
  switch ... { // Enter scope 1.
    case ... : // Enter scope 2, nested within scope 1.
      <body-1> // Enter scope 3, nested within scope 2.

    case ... : // Enter scope 4, nested within scope 1.
      <body-2> // Enter scope 5, nested within scope 4.
  }
```

Use the new scope structure to defer emitting debug descriptions of case
bindings. Specifically, defer the work until we can nest the scope for a
case body under the scope for a pattern match.

This fixes SR-7973, a problem where it was impossible to inspect a case
binding in lldb when stopped at a case with multiple items.

Previously, we would emit the debug descriptions too early (in the
pattern match), leading to duplicate/conflicting descriptions. The only
reason that the ambiguous description was allowed to compile was because
the debug scopes were nested incorrectly.

rdar://41048339

* Update tests
2020-06-10 13:31:10 -07:00
Karoy Lorentey
68351d2110 Revert "Merge remote-tracking branch 'origin/master-next'"
This reverts commit 1c9b0908e6, reversing
changes made to 3eb82c1836.
2020-06-08 16:48:38 -07:00
David Zarzycki
5f162eb1fa [SIL] NFC: Shrink SILGlobalVariable by 16 bytes 2020-06-05 07:24:25 -04:00
Raphael Isemann
18b03171c4 Fix build after removal of PointerUnion3/4
Commit 2948ec5ca98f8593584f2117bc92fe8d75f6f098 removed the
PointerUnion3 and PointerUnion4 aliases and migrated everything
to the variadic template. Let's do the same here to get the
build running.
2020-01-16 10:14:37 +01:00
Adrian Prantl
5b92814a0e Debug Info: Add missing debug info propagation to SILCloner.
While tightening the requirements of the debug info generator in
IRGenSIL I noticed that SILCloner didn't correctly transfer variable
debug info on alloc_box and alloc_stack instructions. In order to make
these mistakes easier to find I added an assertion to SILBuilder and
fixed all issues uncovered by that assertion, too.

The result is a moderate increase in debug info coverage in optimized code.

On stdlib/public/core/OSX/x86_64/Swift.o "variables with location"
increases from 60134 to 60299.
2019-09-24 14:10:25 -07:00
Davide Italiano
b163c3ead0 [SISLLocation] Remove NoneKind, it's been unused for a while. 2019-08-22 14:08:40 -07:00
Erik Eckstein
bee1d94003 SIL: fix a crash when constructing a debug location in mandatory inlining
There was one case which was not handled and that's inlining of a compiler intrinsic.
In this case there is a different location type on the function call.

rdar://problem/49651421
2019-04-09 10:46:40 -07:00
Jordan Rose
146d907f60 [SIL] Reorganize the fields in SILLocation to shrink it to three words (#19095)
Since SIL instructions get used a /lot/, this may result in a
compile-time speedup and/or lowered peak memory usage.
(It's not a lot in practice, though.)
2018-09-05 14:33:12 -07:00
Ellis Hoag
a9765e9b68 Reduce breaks in CodeView linetables
Summary:
CodeView does not recognize zero as an artificial line location
and so a line location of zero causes unexpected behavior when
stepping through user code. If we find a line location of zero
and our scope has not changed, we use the most recent debug
location. That is expected to be the user code that most likely
relates to the current instruction and is similar to the behavior
in LLDB.

Test Plan:
test/DebugInfo/linetable-codeview.swift
2018-08-01 10:40:33 -07:00