Skip to content

Commit 80c89fc

Browse files
authored
Merge pull request swiftlang#69144 from atrick/bridge-diagnostics
SwiftCompilerSources: diagnostics bridging
2 parents 0fe9c92 + 3f6751a commit 80c89fc

File tree

7 files changed

+225
-24
lines changed

7 files changed

+225
-24
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import AST
1314
import SIL
1415
import OptimizerBridging
1516

@@ -40,6 +41,12 @@ extension Context {
4041
}
4142
}
4243

44+
extension Context {
45+
var diagnosticEngine: DiagnosticEngine {
46+
return DiagnosticEngine(bridged: _bridged.getDiagnosticEngine())
47+
}
48+
}
49+
4350
/// A context which allows mutation of a function's SIL.
4451
protocol MutatingContext : Context {
4552
// Called by all instruction mutations, including inserted new instructions.

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,38 @@ final public class HopToExecutorInst : Instruction, UnaryInstruction {}
353353

354354
final public class FixLifetimeInst : Instruction, UnaryInstruction {}
355355

356-
final public class DebugValueInst : Instruction, UnaryInstruction {}
356+
// VarDecl is a struct wrapper around a C++ VarDecl pointer. This insulates the Swift interface from the C++ interface and avoids the need to fake a Swift class. When the AST exposes VarDecl, then this can be replaced whenever it is convenient.
357+
public struct VarDecl {
358+
var bridged: BridgedVarDecl
359+
360+
public init?(bridged: OptionalBridgedVarDecl) {
361+
guard let decl = bridged.decl else { return nil }
362+
self.bridged = BridgedVarDecl(decl: decl)
363+
}
364+
365+
public var userFacingName: String { String(bridged.getUserFacingName()) }
366+
}
367+
368+
// See C++ VarDeclCarryingInst
369+
public protocol VarDeclInstruction {
370+
var varDecl: VarDecl? { get }
371+
}
372+
373+
public protocol DebugVariableInstruction : VarDeclInstruction {
374+
typealias DebugVariable = OptionalBridgedSILDebugVariable
375+
376+
var debugVariable: DebugVariable { get }
377+
}
378+
379+
final public class DebugValueInst : Instruction, UnaryInstruction, DebugVariableInstruction {
380+
public var varDecl: VarDecl? {
381+
VarDecl(bridged: bridged.DebugValue_getDecl())
382+
}
383+
384+
public var debugVariable: DebugVariable {
385+
return bridged.DebugValue_getVarInfo()
386+
}
387+
}
357388

358389
final public class DebugStepInst : Instruction {}
359390

@@ -608,7 +639,11 @@ final public class DynamicFunctionRefInst : FunctionRefBaseInst {
608639
final public class PreviousDynamicFunctionRefInst : FunctionRefBaseInst {
609640
}
610641

611-
final public class GlobalAddrInst : GlobalAccessInst {}
642+
final public class GlobalAddrInst : GlobalAccessInst, VarDeclInstruction {
643+
public var varDecl: VarDecl? {
644+
VarDecl(bridged: bridged.GlobalAddr_getDecl())
645+
}
646+
}
612647

613648
final public class GlobalValueInst : GlobalAccessInst {
614649
public var isBare: Bool { bridged.GlobalValueInst_isBare() }
@@ -711,13 +746,17 @@ final public class SelectEnumInst : SingleValueInstruction {
711746
public var enumOperand: Operand { operands[0] }
712747
}
713748

714-
final public class RefElementAddrInst : SingleValueInstruction, UnaryInstruction {
749+
final public class RefElementAddrInst : SingleValueInstruction, UnaryInstruction, VarDeclInstruction {
715750
public var instance: Value { operand.value }
716751
public var fieldIndex: Int { bridged.RefElementAddrInst_fieldIndex() }
717752

718753
public var fieldIsLet: Bool { bridged.RefElementAddrInst_fieldIsLet() }
719754

720755
public var isImmutable: Bool { bridged.RefElementAddrInst_isImmutable() }
756+
757+
public var varDecl: VarDecl? {
758+
VarDecl(bridged: bridged.RefElementAddr_getDecl())
759+
}
721760
}
722761

723762
final public class RefTailAddrInst : SingleValueInstruction, UnaryInstruction {
@@ -942,8 +981,16 @@ final public class LinearFunctionInst: SingleValueInstruction, ForwardingInstruc
942981

943982
public protocol Allocation : SingleValueInstruction { }
944983

945-
final public class AllocStackInst : SingleValueInstruction, Allocation {
984+
final public class AllocStackInst : SingleValueInstruction, Allocation, DebugVariableInstruction {
946985
public var hasDynamicLifetime: Bool { bridged.AllocStackInst_hasDynamicLifetime() }
986+
987+
public var varDecl: VarDecl? {
988+
VarDecl(bridged: bridged.AllocStack_getDecl())
989+
}
990+
991+
public var debugVariable: DebugVariable {
992+
return bridged.AllocStack_getVarInfo()
993+
}
947994
}
948995

949996
public class AllocRefInstBase : SingleValueInstruction, Allocation {
@@ -973,7 +1020,15 @@ final public class AllocRefDynamicInst : AllocRefInstBase {
9731020
}
9741021
}
9751022

976-
final public class AllocBoxInst : SingleValueInstruction, Allocation {
1023+
final public class AllocBoxInst : SingleValueInstruction, Allocation, DebugVariableInstruction {
1024+
1025+
public var varDecl: VarDecl? {
1026+
VarDecl(bridged: bridged.AllocBox_getDecl())
1027+
}
1028+
1029+
public var debugVariable: DebugVariable {
1030+
return bridged.AllocBox_getVarInfo()
1031+
}
9771032
}
9781033

9791034
final public class AllocExistentialBoxInst : SingleValueInstruction, Allocation {
@@ -1002,6 +1057,10 @@ final public class BeginApplyInst : MultipleValueInstruction, FullApplySite {
10021057
public var numArguments: Int { bridged.BeginApplyInst_numArguments() }
10031058

10041059
public var singleDirectResult: Value? { nil }
1060+
1061+
public var yieldedValues: Results {
1062+
Results(inst: self, numResults: resultCount - 1)
1063+
}
10051064
}
10061065

10071066
//===----------------------------------------------------------------------===//

SwiftCompilerSources/Sources/SIL/Location.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public struct Location: Equatable, CustomStringConvertible {
1818
public var description: String {
1919
return String(taking: bridged.getDebugDescription())
2020
}
21+
22+
public var sourceLoc: SourceLoc? {
23+
return SourceLoc(bridged: bridged.getSourceLocation())
24+
}
2125

2226
/// Keeps the debug scope but marks it as auto-generated.
2327
public var autoGenerated: Location {

include/swift/SIL/SILBridging.h

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#ifdef USED_IN_CPP_SOURCE
2323
#include "llvm/ADT/ArrayRef.h"
2424
#include "swift/SIL/SILBuilder.h"
25+
#include "swift/SIL/SILDebugVariable.h"
26+
#include "swift/SIL/SILDefaultWitnessTable.h"
27+
#include "swift/SIL/SILFunctionConventions.h"
2528
#include "swift/SIL/SILInstruction.h"
2629
#include "swift/SIL/SILWitnessTable.h"
2730
#endif
@@ -53,6 +56,7 @@ class SILVTable;
5356
class SILWitnessTable;
5457
class SILDefaultWitnessTable;
5558
class NominalTypeDecl;
59+
class VarDecl;
5660
class SwiftPassInvocation;
5761
class GenericSpecializationInformation;
5862
}
@@ -126,6 +130,28 @@ struct BridgedType {
126130
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getTupleElementType(SwiftInt idx) const;
127131
};
128132

133+
// AST bridging
134+
135+
struct BridgedNominalTypeDecl {
136+
swift::NominalTypeDecl * _Nonnull decl;
137+
138+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getName() const;
139+
bool isStructWithUnreferenceableStorage() const;
140+
BRIDGED_INLINE bool isGlobalActor() const;
141+
};
142+
143+
struct BridgedVarDecl {
144+
const swift::VarDecl * _Nonnull decl;
145+
146+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getUserFacingName() const;
147+
};
148+
149+
struct OptionalBridgedVarDecl {
150+
const swift::VarDecl * _Nullable decl;
151+
};
152+
153+
// SIL Bridging
154+
129155
struct BridgedValue {
130156
SwiftObject obj;
131157

@@ -438,6 +464,7 @@ struct BridgedLocation {
438464
BRIDGED_INLINE bool hasValidLineNumber() const;
439465
BRIDGED_INLINE bool isAutoGenerated() const;
440466
BRIDGED_INLINE bool isEqualTo(BridgedLocation rhs) const;
467+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSourceLoc getSourceLocation() const;
441468
BRIDGED_INLINE bool hasSameSourceLocation(BridgedLocation rhs) const;
442469
static BRIDGED_INLINE BridgedLocation getArtificialUnreachableLocation();
443470
};
@@ -446,6 +473,23 @@ struct BridgedGenericSpecializationInformation {
446473
const swift::GenericSpecializationInformation * _Nullable data = nullptr;
447474
};
448475

476+
struct OptionalBridgedSILDebugVariable {
477+
uint64_t storage[16];
478+
479+
#ifdef USED_IN_CPP_SOURCE
480+
using OptionalSILDebugVariable = llvm::Optional<swift::SILDebugVariable>;
481+
482+
OptionalBridgedSILDebugVariable(
483+
OptionalSILDebugVariable &&debugVariable) {
484+
static_assert(sizeof(OptionalSILDebugVariable) <= 16*8);
485+
*reinterpret_cast<OptionalSILDebugVariable *>(&storage) = debugVariable;
486+
}
487+
const OptionalSILDebugVariable &getDebugVar() const {
488+
return *reinterpret_cast<const OptionalSILDebugVariable *>(&storage);
489+
}
490+
#endif
491+
};
492+
449493
struct BridgedInstruction {
450494
SwiftObject obj;
451495

@@ -602,6 +646,31 @@ struct BridgedInstruction {
602646
BRIDGED_INLINE BridgedArgumentConvention ApplySite_getArgumentConvention(SwiftInt calleeArgIdx) const;
603647
BRIDGED_INLINE SwiftInt ApplySite_getNumArguments() const;
604648
BRIDGED_INLINE SwiftInt FullApplySite_numIndirectResultArguments() const;
649+
650+
// =========================================================================//
651+
// VarDeclInst and DebugVariableInst
652+
// =========================================================================//
653+
654+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
655+
DebugValue_getDecl() const;
656+
657+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
658+
AllocStack_getDecl() const;
659+
660+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
661+
AllocBox_getDecl() const;
662+
663+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
664+
GlobalAddr_getDecl() const;
665+
666+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
667+
RefElementAddr_getDecl() const;
668+
669+
BRIDGED_INLINE OptionalBridgedSILDebugVariable DebugValue_getVarInfo() const;
670+
671+
BRIDGED_INLINE OptionalBridgedSILDebugVariable AllocStack_getVarInfo() const;
672+
673+
BRIDGED_INLINE OptionalBridgedSILDebugVariable AllocBox_getVarInfo() const;
605674
};
606675

607676
struct OptionalBridgedInstruction {
@@ -861,15 +930,11 @@ struct BridgedBuilder{
861930
bool keepUnique) const;
862931
};
863932

864-
// AST bridging
865-
866-
struct BridgedNominalTypeDecl {
867-
swift::NominalTypeDecl * _Nonnull decl;
933+
// Passmanager and Context
868934

869-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getName() const;
870-
bool isStructWithUnreferenceableStorage() const;
871-
BRIDGED_INLINE bool isGlobalActor() const;
872-
};
935+
namespace swift {
936+
class SwiftPassInvocation;
937+
}
873938

874939
struct BridgedChangeNotificationHandler {
875940
swift::SwiftPassInvocation * _Nonnull invocation;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,24 @@ BridgedType BridgedType::getTupleElementType(SwiftInt idx) const {
213213
return get().getTupleElementType(idx);
214214
}
215215

216+
//===----------------------------------------------------------------------===//
217+
// BridgedNominalTypeDecl
218+
//===----------------------------------------------------------------------===//
219+
220+
BridgedStringRef BridgedNominalTypeDecl::getName() const {
221+
return decl->getName().str();
222+
}
223+
224+
bool BridgedNominalTypeDecl::isGlobalActor() const { return decl->isGlobalActor(); }
225+
226+
//===----------------------------------------------------------------------===//
227+
// BridgedVarDecl
228+
//===----------------------------------------------------------------------===//
229+
230+
BridgedStringRef BridgedVarDecl::getUserFacingName() const {
231+
return decl->getBaseName().userFacingName();
232+
}
233+
216234
//===----------------------------------------------------------------------===//
217235
// BridgedValue
218236
//===----------------------------------------------------------------------===//
@@ -359,6 +377,12 @@ bool BridgedLocation::isAutoGenerated() const {
359377
bool BridgedLocation::isEqualTo(BridgedLocation rhs) const {
360378
return getLoc().isEqualTo(rhs.getLoc());
361379
}
380+
BridgedSourceLoc BridgedLocation::getSourceLocation() const {
381+
swift::SILDebugLocation debugLoc = getLoc();
382+
swift::SILLocation silLoc = debugLoc.getLocation();
383+
swift::SourceLoc sourceLoc = silLoc.getSourceLoc();
384+
return BridgedSourceLoc(sourceLoc.getOpaquePointerValue());
385+
}
362386
bool BridgedLocation::hasSameSourceLocation(BridgedLocation rhs) const {
363387
return getLoc().hasSameSourceLocation(rhs.getLoc());
364388
}
@@ -991,6 +1015,45 @@ SwiftInt BridgedInstruction::FullApplySite_numIndirectResultArguments() const {
9911015
return fas.getNumIndirectSILResults();
9921016
}
9931017

1018+
//===----------------------------------------------------------------------===//
1019+
// VarDeclInst and DebugVariableInst
1020+
//===----------------------------------------------------------------------===//
1021+
1022+
OptionalBridgedVarDecl BridgedInstruction::DebugValue_getDecl() const {
1023+
return {getAs<swift::DebugValueInst>()->getDecl()};
1024+
}
1025+
1026+
OptionalBridgedVarDecl BridgedInstruction::AllocStack_getDecl() const {
1027+
return {getAs<swift::AllocStackInst>()->getDecl()};
1028+
}
1029+
1030+
OptionalBridgedVarDecl BridgedInstruction::AllocBox_getDecl() const {
1031+
return {getAs<swift::AllocBoxInst>()->getDecl()};
1032+
}
1033+
1034+
OptionalBridgedVarDecl BridgedInstruction::GlobalAddr_getDecl() const {
1035+
return {getAs<swift::DebugValueInst>()->getDecl()};
1036+
}
1037+
1038+
OptionalBridgedVarDecl BridgedInstruction::RefElementAddr_getDecl() const {
1039+
return {getAs<swift::DebugValueInst>()->getDecl()};
1040+
}
1041+
1042+
OptionalBridgedSILDebugVariable
1043+
BridgedInstruction::DebugValue_getVarInfo() const {
1044+
return getAs<swift::DebugValueInst>()->getVarInfo();
1045+
}
1046+
1047+
OptionalBridgedSILDebugVariable
1048+
BridgedInstruction::AllocStack_getVarInfo() const {
1049+
return getAs<swift::AllocStackInst>()->getVarInfo();
1050+
}
1051+
1052+
OptionalBridgedSILDebugVariable
1053+
BridgedInstruction::AllocBox_getVarInfo() const {
1054+
return getAs<swift::AllocBoxInst>()->getVarInfo();
1055+
}
1056+
9941057
//===----------------------------------------------------------------------===//
9951058
// BridgedBasicBlock
9961059
//===----------------------------------------------------------------------===//
@@ -1371,17 +1434,6 @@ BridgedInstruction BridgedBuilder::createEndCOWMutation(BridgedValue instance, b
13711434
return {get().createEndCOWMutation(regularLoc(), instance.getSILValue(), keepUnique)};
13721435
}
13731436

1374-
//===----------------------------------------------------------------------===//
1375-
// BridgedNominalTypeDecl
1376-
//===----------------------------------------------------------------------===//
1377-
1378-
BridgedStringRef BridgedNominalTypeDecl::getName() const {
1379-
return decl->getName().str();
1380-
}
1381-
1382-
bool BridgedNominalTypeDecl::isGlobalActor() const { return decl->isGlobalActor(); }
1383-
1384-
13851437
SWIFT_END_NULLABILITY_ANNOTATIONS
13861438

13871439
#endif

0 commit comments

Comments
 (0)