Skip to content

SwiftCompilerSources: diagnostics bridging #69144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

import AST
import SIL
import OptimizerBridging

Expand Down Expand Up @@ -40,6 +41,12 @@ extension Context {
}
}

extension Context {
var diagnosticEngine: DiagnosticEngine {
return DiagnosticEngine(bridged: _bridged.getDiagnosticEngine())
}
}

/// A context which allows mutation of a function's SIL.
protocol MutatingContext : Context {
// Called by all instruction mutations, including inserted new instructions.
Expand Down
69 changes: 64 additions & 5 deletions SwiftCompilerSources/Sources/SIL/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,38 @@ final public class HopToExecutorInst : Instruction, UnaryInstruction {}

final public class FixLifetimeInst : Instruction, UnaryInstruction {}

final public class DebugValueInst : Instruction, UnaryInstruction {}
// 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.
public struct VarDecl {
var bridged: BridgedVarDecl

public init?(bridged: OptionalBridgedVarDecl) {
guard let decl = bridged.decl else { return nil }
self.bridged = BridgedVarDecl(decl: decl)
}

public var userFacingName: String { String(bridged.getUserFacingName()) }
}

// See C++ VarDeclCarryingInst
public protocol VarDeclInstruction {
var varDecl: VarDecl? { get }
}

public protocol DebugVariableInstruction : VarDeclInstruction {
typealias DebugVariable = OptionalBridgedSILDebugVariable

var debugVariable: DebugVariable { get }
}

final public class DebugValueInst : Instruction, UnaryInstruction, DebugVariableInstruction {
public var varDecl: VarDecl? {
VarDecl(bridged: bridged.DebugValue_getDecl())
}

public var debugVariable: DebugVariable {
return bridged.DebugValue_getVarInfo()
}
}

final public class DebugStepInst : Instruction {}

Expand Down Expand Up @@ -608,7 +639,11 @@ final public class DynamicFunctionRefInst : FunctionRefBaseInst {
final public class PreviousDynamicFunctionRefInst : FunctionRefBaseInst {
}

final public class GlobalAddrInst : GlobalAccessInst {}
final public class GlobalAddrInst : GlobalAccessInst, VarDeclInstruction {
public var varDecl: VarDecl? {
VarDecl(bridged: bridged.GlobalAddr_getDecl())
}
}

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

final public class RefElementAddrInst : SingleValueInstruction, UnaryInstruction {
final public class RefElementAddrInst : SingleValueInstruction, UnaryInstruction, VarDeclInstruction {
public var instance: Value { operand.value }
public var fieldIndex: Int { bridged.RefElementAddrInst_fieldIndex() }

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

public var isImmutable: Bool { bridged.RefElementAddrInst_isImmutable() }

public var varDecl: VarDecl? {
VarDecl(bridged: bridged.RefElementAddr_getDecl())
}
}

final public class RefTailAddrInst : SingleValueInstruction, UnaryInstruction {
Expand Down Expand Up @@ -942,8 +981,16 @@ final public class LinearFunctionInst: SingleValueInstruction, ForwardingInstruc

public protocol Allocation : SingleValueInstruction { }

final public class AllocStackInst : SingleValueInstruction, Allocation {
final public class AllocStackInst : SingleValueInstruction, Allocation, DebugVariableInstruction {
public var hasDynamicLifetime: Bool { bridged.AllocStackInst_hasDynamicLifetime() }

public var varDecl: VarDecl? {
VarDecl(bridged: bridged.AllocStack_getDecl())
}

public var debugVariable: DebugVariable {
return bridged.AllocStack_getVarInfo()
}
}

public class AllocRefInstBase : SingleValueInstruction, Allocation {
Expand Down Expand Up @@ -973,7 +1020,15 @@ final public class AllocRefDynamicInst : AllocRefInstBase {
}
}

final public class AllocBoxInst : SingleValueInstruction, Allocation {
final public class AllocBoxInst : SingleValueInstruction, Allocation, DebugVariableInstruction {

public var varDecl: VarDecl? {
VarDecl(bridged: bridged.AllocBox_getDecl())
}

public var debugVariable: DebugVariable {
return bridged.AllocBox_getVarInfo()
}
}

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

public var singleDirectResult: Value? { nil }

public var yieldedValues: Results {
Results(inst: self, numResults: resultCount - 1)
}
}

//===----------------------------------------------------------------------===//
Expand Down
4 changes: 4 additions & 0 deletions SwiftCompilerSources/Sources/SIL/Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public struct Location: Equatable, CustomStringConvertible {
public var description: String {
return String(taking: bridged.getDebugDescription())
}

public var sourceLoc: SourceLoc? {
return SourceLoc(bridged: bridged.getSourceLocation())
}

/// Keeps the debug scope but marks it as auto-generated.
public var autoGenerated: Location {
Expand Down
81 changes: 73 additions & 8 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#ifdef USED_IN_CPP_SOURCE
#include "llvm/ADT/ArrayRef.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILDebugVariable.h"
#include "swift/SIL/SILDefaultWitnessTable.h"
#include "swift/SIL/SILFunctionConventions.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILWitnessTable.h"
#endif
Expand Down Expand Up @@ -53,6 +56,7 @@ class SILVTable;
class SILWitnessTable;
class SILDefaultWitnessTable;
class NominalTypeDecl;
class VarDecl;
class SwiftPassInvocation;
class GenericSpecializationInformation;
}
Expand Down Expand Up @@ -126,6 +130,28 @@ struct BridgedType {
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getTupleElementType(SwiftInt idx) const;
};

// AST bridging

struct BridgedNominalTypeDecl {
swift::NominalTypeDecl * _Nonnull decl;

SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getName() const;
bool isStructWithUnreferenceableStorage() const;
BRIDGED_INLINE bool isGlobalActor() const;
};

struct BridgedVarDecl {
const swift::VarDecl * _Nonnull decl;

SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getUserFacingName() const;
};

struct OptionalBridgedVarDecl {
const swift::VarDecl * _Nullable decl;
};

// SIL Bridging

struct BridgedValue {
SwiftObject obj;

Expand Down Expand Up @@ -438,6 +464,7 @@ struct BridgedLocation {
BRIDGED_INLINE bool hasValidLineNumber() const;
BRIDGED_INLINE bool isAutoGenerated() const;
BRIDGED_INLINE bool isEqualTo(BridgedLocation rhs) const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSourceLoc getSourceLocation() const;
BRIDGED_INLINE bool hasSameSourceLocation(BridgedLocation rhs) const;
static BRIDGED_INLINE BridgedLocation getArtificialUnreachableLocation();
};
Expand All @@ -446,6 +473,23 @@ struct BridgedGenericSpecializationInformation {
const swift::GenericSpecializationInformation * _Nullable data = nullptr;
};

struct OptionalBridgedSILDebugVariable {
uint64_t storage[16];

#ifdef USED_IN_CPP_SOURCE
using OptionalSILDebugVariable = llvm::Optional<swift::SILDebugVariable>;

OptionalBridgedSILDebugVariable(
OptionalSILDebugVariable &&debugVariable) {
static_assert(sizeof(OptionalSILDebugVariable) <= 16*8);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not static_assert(sizeof(OptionalSILDebugVariable) <= sizeof(OptionalBridgedSILDebugVariable))?

*reinterpret_cast<OptionalSILDebugVariable *>(&storage) = debugVariable;
}
const OptionalSILDebugVariable &getDebugVar() const {
return *reinterpret_cast<const OptionalSILDebugVariable *>(&storage);
}
#endif
};

struct BridgedInstruction {
SwiftObject obj;

Expand Down Expand Up @@ -602,6 +646,31 @@ struct BridgedInstruction {
BRIDGED_INLINE BridgedArgumentConvention ApplySite_getArgumentConvention(SwiftInt calleeArgIdx) const;
BRIDGED_INLINE SwiftInt ApplySite_getNumArguments() const;
BRIDGED_INLINE SwiftInt FullApplySite_numIndirectResultArguments() const;

// =========================================================================//
// VarDeclInst and DebugVariableInst
// =========================================================================//

SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
DebugValue_getDecl() const;

SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
AllocStack_getDecl() const;

SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
AllocBox_getDecl() const;

SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
GlobalAddr_getDecl() const;

SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
RefElementAddr_getDecl() const;

BRIDGED_INLINE OptionalBridgedSILDebugVariable DebugValue_getVarInfo() const;

BRIDGED_INLINE OptionalBridgedSILDebugVariable AllocStack_getVarInfo() const;

BRIDGED_INLINE OptionalBridgedSILDebugVariable AllocBox_getVarInfo() const;
};

struct OptionalBridgedInstruction {
Expand Down Expand Up @@ -861,15 +930,11 @@ struct BridgedBuilder{
bool keepUnique) const;
};

// AST bridging

struct BridgedNominalTypeDecl {
swift::NominalTypeDecl * _Nonnull decl;
// Passmanager and Context

SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getName() const;
bool isStructWithUnreferenceableStorage() const;
BRIDGED_INLINE bool isGlobalActor() const;
};
namespace swift {
class SwiftPassInvocation;
}

struct BridgedChangeNotificationHandler {
swift::SwiftPassInvocation * _Nonnull invocation;
Expand Down
74 changes: 63 additions & 11 deletions include/swift/SIL/SILBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,24 @@ BridgedType BridgedType::getTupleElementType(SwiftInt idx) const {
return get().getTupleElementType(idx);
}

//===----------------------------------------------------------------------===//
// BridgedNominalTypeDecl
//===----------------------------------------------------------------------===//

BridgedStringRef BridgedNominalTypeDecl::getName() const {
return decl->getName().str();
}

bool BridgedNominalTypeDecl::isGlobalActor() const { return decl->isGlobalActor(); }

//===----------------------------------------------------------------------===//
// BridgedVarDecl
//===----------------------------------------------------------------------===//

BridgedStringRef BridgedVarDecl::getUserFacingName() const {
return decl->getBaseName().userFacingName();
}

//===----------------------------------------------------------------------===//
// BridgedValue
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -359,6 +377,12 @@ bool BridgedLocation::isAutoGenerated() const {
bool BridgedLocation::isEqualTo(BridgedLocation rhs) const {
return getLoc().isEqualTo(rhs.getLoc());
}
BridgedSourceLoc BridgedLocation::getSourceLocation() const {
swift::SILDebugLocation debugLoc = getLoc();
swift::SILLocation silLoc = debugLoc.getLocation();
swift::SourceLoc sourceLoc = silLoc.getSourceLoc();
return BridgedSourceLoc(sourceLoc.getOpaquePointerValue());
}
bool BridgedLocation::hasSameSourceLocation(BridgedLocation rhs) const {
return getLoc().hasSameSourceLocation(rhs.getLoc());
}
Expand Down Expand Up @@ -991,6 +1015,45 @@ SwiftInt BridgedInstruction::FullApplySite_numIndirectResultArguments() const {
return fas.getNumIndirectSILResults();
}

//===----------------------------------------------------------------------===//
// VarDeclInst and DebugVariableInst
//===----------------------------------------------------------------------===//

OptionalBridgedVarDecl BridgedInstruction::DebugValue_getDecl() const {
return {getAs<swift::DebugValueInst>()->getDecl()};
}

OptionalBridgedVarDecl BridgedInstruction::AllocStack_getDecl() const {
return {getAs<swift::AllocStackInst>()->getDecl()};
}

OptionalBridgedVarDecl BridgedInstruction::AllocBox_getDecl() const {
return {getAs<swift::AllocBoxInst>()->getDecl()};
}

OptionalBridgedVarDecl BridgedInstruction::GlobalAddr_getDecl() const {
return {getAs<swift::DebugValueInst>()->getDecl()};
}

OptionalBridgedVarDecl BridgedInstruction::RefElementAddr_getDecl() const {
return {getAs<swift::DebugValueInst>()->getDecl()};
}

OptionalBridgedSILDebugVariable
BridgedInstruction::DebugValue_getVarInfo() const {
return getAs<swift::DebugValueInst>()->getVarInfo();
}

OptionalBridgedSILDebugVariable
BridgedInstruction::AllocStack_getVarInfo() const {
return getAs<swift::AllocStackInst>()->getVarInfo();
}

OptionalBridgedSILDebugVariable
BridgedInstruction::AllocBox_getVarInfo() const {
return getAs<swift::AllocBoxInst>()->getVarInfo();
}

//===----------------------------------------------------------------------===//
// BridgedBasicBlock
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -1371,17 +1434,6 @@ BridgedInstruction BridgedBuilder::createEndCOWMutation(BridgedValue instance, b
return {get().createEndCOWMutation(regularLoc(), instance.getSILValue(), keepUnique)};
}

//===----------------------------------------------------------------------===//
// BridgedNominalTypeDecl
//===----------------------------------------------------------------------===//

BridgedStringRef BridgedNominalTypeDecl::getName() const {
return decl->getName().str();
}

bool BridgedNominalTypeDecl::isGlobalActor() const { return decl->isGlobalActor(); }


SWIFT_END_NULLABILITY_ANNOTATIONS

#endif
Loading