Skip to content

Commit b916906

Browse files
committed
Swift SIL: add some APIs
* `Options.assertConfiguration` * `Argument.isIndirectResult` * in `Function`: `selfArgument`, `isTransparent`, `performanceConstraints` and `inlineStrategy` * `BuiltinInst.substitutionMap` * `SubstitutionMap.replacementTypes` * `Type.canBeClass`
1 parent 82734b6 commit b916906

File tree

9 files changed

+143
-0
lines changed

9 files changed

+143
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/Options.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,18 @@ struct Options {
2727
func enableSimplification(for inst: Instruction) -> Bool {
2828
_bridged.enableSimplificationFor(inst.bridged)
2929
}
30+
31+
enum AssertConfiguration {
32+
case enabled
33+
case disabled
34+
case unknown
35+
}
36+
37+
var assertConfiguration: AssertConfiguration {
38+
switch _bridged.getAssertConfiguration() {
39+
case .Debug: return .enabled
40+
case .Release, .Unchecked: return .disabled
41+
default: return .unknown
42+
}
43+
}
3044
}

SwiftCompilerSources/Sources/SIL/Argument.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ final public class FunctionArgument : Argument {
4242
public var convention: ArgumentConvention {
4343
bridged.getConvention().convention
4444
}
45+
46+
public var isIndirectResult: Bool {
47+
return index < parentFunction.numIndirectResultArguments
48+
}
4549
}
4650

4751
final public class BlockArgument : Argument {

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
7878
assert(selfIdx >= 0)
7979
return selfIdx
8080
}
81+
82+
public var selfArgument: FunctionArgument { arguments[selfArgumentIndex] }
8183

8284
public var argumentTypes: ArgumentTypeArray { ArgumentTypeArray(function: self) }
8385
public var resultType: Type { bridged.getSILResultType().type }
@@ -122,6 +124,8 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
122124
/// This means that the function terminates the program.
123125
public var isProgramTerminationPoint: Bool { hasSemanticsAttribute("programtermination_point") }
124126

127+
public var isTransparent: Bool { bridged.isTransparent() }
128+
125129
/// True if this is a `[global_init]` function.
126130
///
127131
/// Such a function is typically a global addressor which calls the global's
@@ -187,6 +191,37 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
187191
}
188192
}
189193

194+
public enum PerformanceConstraints {
195+
case none
196+
case noAllocations
197+
case noLocks
198+
}
199+
200+
public var performanceConstraints: PerformanceConstraints {
201+
switch bridged.getPerformanceConstraints() {
202+
case .None: return .none
203+
case .NoAllocation: return .noAllocations
204+
case .NoLocks: return .noLocks
205+
default: fatalError("unknown performance constraint")
206+
}
207+
}
208+
209+
public enum InlineStrategy {
210+
case automatic
211+
case never
212+
case always
213+
}
214+
215+
public var inlineStrategy: InlineStrategy {
216+
switch bridged.getInlineStrategy() {
217+
case .InlineDefault: return .automatic
218+
case .NoInline: return .never
219+
case .AlwaysInline: return .always
220+
default:
221+
fatalError()
222+
}
223+
}
224+
190225
/// True, if the function runs with a swift 5.1 runtime.
191226
/// Note that this is function specific, because inlinable functions are de-serialized
192227
/// in a client module, which might be compiled with a different deployment target.

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ final public class BuiltinInst : SingleValueInstruction {
365365
public var id: ID {
366366
return bridged.BuiltinInst_getID()
367367
}
368+
369+
public var substitutionMap: SubstitutionMap {
370+
SubstitutionMap(bridged.BuiltinInst_getSubstitutionMap())
371+
}
368372
}
369373

370374
final public class UpcastInst : SingleValueInstruction, UnaryInstruction {

SwiftCompilerSources/Sources/SIL/SubstitutionMap.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ public struct SubstitutionMap {
2424
}
2525

2626
public var isEmpty: Bool { bridged.empty() }
27+
28+
public var replacementTypes: OptionalTypeArray {
29+
let types = BridgedTypeArray.fromReplacementTypes(bridged)
30+
return OptionalTypeArray(bridged: types)
31+
}
2732
}

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
4343
public var isMetatype: Bool { bridged.isMetatype() }
4444
public var isNoEscapeFunction: Bool { bridged.isNoEscapeFunction() }
4545

46+
public var canBeClass: swift.TypeTraitResult { bridged.canBeClass() }
47+
4648
/// Can only be used if the type is in fact a nominal type (`isNominal` is true).
4749
public var nominal: NominalTypeDecl {
4850
NominalTypeDecl(bridged: BridgedNominalTypeDecl(decl: bridged.getNominalOrBoundGenericNominal()))
@@ -82,6 +84,26 @@ extension Type: Equatable {
8284
}
8385
}
8486

87+
public struct OptionalTypeArray : RandomAccessCollection, CustomReflectable {
88+
private let bridged: BridgedTypeArray
89+
90+
public var startIndex: Int { return 0 }
91+
public var endIndex: Int { return bridged.getCount() }
92+
93+
public init(bridged: BridgedTypeArray) {
94+
self.bridged = bridged
95+
}
96+
97+
public subscript(_ index: Int) -> Type? {
98+
bridged.getAt(index).typeOrNil
99+
}
100+
101+
public var customMirror: Mirror {
102+
let c: [Mirror.Child] = map { (label: nil, value: $0 ?? "<invalid>") }
103+
return Mirror(self, children: c)
104+
}
105+
}
106+
85107
public struct NominalFieldsArray : RandomAccessCollection, FormattedLikeArray {
86108
fileprivate let type: Type
87109
fileprivate let function: Function
@@ -118,6 +140,7 @@ public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray {
118140

119141
extension swift.SILType {
120142
var type: Type { Type(bridged: self) }
143+
var typeOrNil: Type? { isNull() ? nil : type }
121144
}
122145

123146
// TODO: use an AST type for this once we have it

include/swift/SIL/SILBridging.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ struct BridgedFunction {
237237
return getFunction()->isAvailableExternally();
238238
}
239239

240+
bool isTransparent() const {
241+
return getFunction()->isTransparent() == swift::IsTransparent;
242+
}
243+
240244
bool isGlobalInitFunction() const {
241245
return getFunction()->isGlobalInit();
242246
}
@@ -253,6 +257,20 @@ struct BridgedFunction {
253257
return getFunction()->getEffectsKind();
254258
}
255259

260+
swift::PerformanceConstraints getPerformanceConstraints() const {
261+
return getFunction()->getPerfConstraints();
262+
}
263+
264+
enum class InlineStrategy {
265+
InlineDefault = swift::InlineDefault,
266+
NoInline = swift::NoInline,
267+
AlwaysInline = swift::AlwaysInline
268+
};
269+
270+
InlineStrategy getInlineStrategy() const {
271+
return (InlineStrategy)getFunction()->getInlineStrategy();
272+
}
273+
256274
bool needsStackProtection() const {
257275
return getFunction()->needsStackProtection();
258276
}
@@ -364,6 +382,25 @@ struct OptionalBridgedInstruction {
364382
}
365383
};
366384

385+
struct BridgedTypeArray {
386+
llvm::ArrayRef<swift::Type> typeArray;
387+
388+
SWIFT_IMPORT_UNSAFE
389+
static BridgedTypeArray fromReplacementTypes(swift::SubstitutionMap substMap) {
390+
return {substMap.getReplacementTypes()};
391+
}
392+
393+
SwiftInt getCount() const { return SwiftInt(typeArray.size()); }
394+
395+
SWIFT_IMPORT_UNSAFE
396+
swift::SILType getAt(SwiftInt index) const {
397+
auto ty = swift::CanType(typeArray[index]);
398+
if (ty->isLegalSILType())
399+
return swift::SILType::getPrimitiveObjectType(ty);
400+
return swift::SILType();
401+
}
402+
};
403+
367404
struct BridgedInstruction {
368405
SwiftObject obj;
369406

@@ -457,6 +494,12 @@ struct BridgedInstruction {
457494
return getAs<swift::BuiltinInst>()->getBuiltinInfo().ID;
458495
}
459496

497+
SWIFT_IMPORT_UNSAFE
498+
swift::SubstitutionMap BuiltinInst_getSubstitutionMap() const {
499+
return getAs<swift::BuiltinInst>()->getSubstitutions();
500+
}
501+
502+
460503
bool AddressToPointerInst_needsStackProtection() const {
461504
return getAs<swift::AddressToPointerInst>()->needsStackProtection();
462505
}

include/swift/SIL/SILType.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ class SILType {
414414
return getASTType()->hasOpenedExistential();
415415
}
416416

417+
TypeTraitResult canBeClass() const {
418+
return getASTType()->canBeClass();
419+
}
420+
417421
/// Returns true if the referenced type is expressed in terms of one
418422
/// or more local archetypes.
419423
bool hasLocalArchetype() const {

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,17 @@ struct BridgedPassContext {
431431
return mod->getOptions().EnableMoveInoutStackProtection;
432432
}
433433

434+
enum class AssertConfiguration {
435+
Debug = swift::SILOptions::Debug,
436+
Release = swift::SILOptions::Release,
437+
Unchecked = swift::SILOptions::Unchecked
438+
};
439+
440+
AssertConfiguration getAssertConfiguration() const {
441+
swift::SILModule *mod = invocation->getPassManager()->getModule();
442+
return (AssertConfiguration)mod->getOptions().AssertConfig;
443+
}
444+
434445
bool enableSimplificationFor(BridgedInstruction inst) const;
435446
};
436447

0 commit comments

Comments
 (0)