Skip to content

Commit d49108d

Browse files
committed
libswift: add the SIL Builder
And two example instruction building functions.
1 parent 81f5e2f commit d49108d

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

include/swift/SIL/SILBridging.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ BridgedMultiValueResult
166166
BridgedStringRef CondFailInst_getMessage(BridgedInstruction cfi);
167167
BridgedGlobalVar GlobalAccessInst_getGlobal(BridgedInstruction globalInst);
168168

169+
BridgedInstruction SILBuilder_createBuiltinBinaryFunction(
170+
BridgedInstruction insertionPoint,
171+
BridgedLocation loc, BridgedStringRef name,
172+
BridgedType operandType, BridgedType resultType, BridgedValueArray arguments);
173+
BridgedInstruction SILBuilder_createCondFail(BridgedInstruction insertionPoint,
174+
BridgedLocation loc, BridgedValue condition, BridgedStringRef messge);
175+
169176
#ifdef __cplusplus
170177
} // extern "C"
171178
#endif

include/swift/SIL/SILBuilder.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ class SILBuilder {
123123
setInsertionPoint(I);
124124
}
125125

126+
// Used by libswift bridging.
127+
explicit SILBuilder(SILInstruction *I, const SILDebugScope *debugScope)
128+
: TempContext(I->getFunction()->getModule()),
129+
C(TempContext), F(I->getFunction()), CurDebugScope(debugScope) {
130+
setInsertionPoint(I);
131+
}
132+
126133
explicit SILBuilder(SILBasicBlock::iterator I,
127134
SmallVectorImpl<SILInstruction *> *InsertedInstrs = 0)
128135
: SILBuilder(&*I, InsertedInstrs) {}

lib/SIL/Utils/SILBridging.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "swift/SIL/SILNode.h"
1414
#include "swift/SIL/SILBridgingUtils.h"
1515
#include "swift/SIL/SILGlobalVariable.h"
16+
#include "swift/SIL/SILBuilder.h"
1617

1718
using namespace swift;
1819

@@ -361,3 +362,26 @@ BridgedStringRef CondFailInst_getMessage(BridgedInstruction cfi) {
361362
BridgedGlobalVar GlobalAccessInst_getGlobal(BridgedInstruction globalInst) {
362363
return {castToInst<GlobalAccessInst>(globalInst)->getReferencedGlobal()};
363364
}
365+
366+
//===----------------------------------------------------------------------===//
367+
// SILBuilder
368+
//===----------------------------------------------------------------------===//
369+
370+
BridgedInstruction SILBuilder_createBuiltinBinaryFunction(
371+
BridgedInstruction insertionPoint,
372+
BridgedLocation loc, BridgedStringRef name,
373+
BridgedType operandType, BridgedType resultType,
374+
BridgedValueArray arguments) {
375+
SILBuilder builder(castToInst(insertionPoint), getSILDebugScope(loc));
376+
SmallVector<SILValue, 16> argValues;
377+
return {builder.createBuiltinBinaryFunction(getRegularLocation(loc),
378+
getStringRef(name), getSILType(operandType), getSILType(resultType),
379+
getSILValues(arguments, argValues))};
380+
}
381+
382+
BridgedInstruction SILBuilder_createCondFail(BridgedInstruction insertionPoint,
383+
BridgedLocation loc, BridgedValue condition, BridgedStringRef messge) {
384+
SILBuilder builder(castToInst(insertionPoint), getSILDebugScope(loc));
385+
return {builder.createCondFail(getRegularLocation(loc),
386+
castToSILValue(condition), getStringRef(messge))};
387+
}

libswift/Sources/Optimizer/PassManager/PassUtils.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,15 @@ struct FunctionPass {
4949
}
5050
}
5151

52+
extension Builder {
53+
init(at insPnt: Instruction, location: Location,
54+
_ context: FunctionPassContext) {
55+
self.init(insertionPoint: insPnt, location: location,
56+
passContext: context.passContext)
57+
}
58+
59+
init(at insPnt: Instruction, _ context: FunctionPassContext) {
60+
self.init(insertionPoint: insPnt, location: insPnt.location,
61+
passContext: context.passContext)
62+
}
63+
}

libswift/Sources/SIL/Builder.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===--- Builder.swift - Building and modifying SIL ----------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SILBridging
14+
15+
/// A utility to create new instructions at a given insertion point.
16+
public struct Builder {
17+
let insertionPoint: Instruction
18+
let location: Location
19+
private let passContext: BridgedPassContext
20+
21+
private var bridgedInsPoint: BridgedInstruction { insertionPoint.bridged }
22+
23+
private func notifyInstructionsChanged() {
24+
PassContext_notifyChanges(passContext, instructionsChanged)
25+
}
26+
27+
private func notifyCallsChanged() {
28+
PassContext_notifyChanges(passContext, callsChanged)
29+
}
30+
31+
private func notifyBranchesChanged() {
32+
PassContext_notifyChanges(passContext, branchesChanged)
33+
}
34+
35+
public init(insertionPoint: Instruction, location: Location,
36+
passContext: BridgedPassContext) {
37+
self.insertionPoint = insertionPoint
38+
self.location = location;
39+
self.passContext = passContext
40+
}
41+
42+
public func createBuiltinBinaryFunction(name: String,
43+
operandType: Type, resultType: Type, arguments: [Value]) -> BuiltinInst {
44+
notifyInstructionsChanged()
45+
return arguments.withBridgedValues { valuesRef in
46+
return name.withBridgedStringRef { nameStr in
47+
let bi = SILBuilder_createBuiltinBinaryFunction(
48+
bridgedInsPoint, location.bridgedLocation, nameStr,
49+
operandType.silType, resultType.silType, valuesRef)
50+
return bi.getAs(BuiltinInst.self)
51+
}
52+
}
53+
}
54+
55+
public func createCondFail(condition: Value, message: String) -> CondFailInst {
56+
notifyInstructionsChanged()
57+
return message.withBridgedStringRef { messageStr in
58+
let cf = SILBuilder_createCondFail(
59+
bridgedInsPoint, location.bridgedLocation, condition.bridged, messageStr)
60+
return cf.getAs(CondFailInst.self)
61+
}
62+
}
63+
}

libswift/Sources/SIL/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
add_libswift_module(SIL
1010
Argument.swift
1111
BasicBlock.swift
12+
Builder.swift
1213
Function.swift
1314
GlobalVariable.swift
1415
Instruction.swift

0 commit comments

Comments
 (0)