Skip to content

Commit a38db64

Browse files
committed
SIL: add the vector_base_addr instruction
It derives the address of the first element of a vector, i.e. a `Builtin.FixedArray`, from the address of the vector itself. Addresses of other vector elements can then be derived with `index_addr`.
1 parent 17fbcb5 commit a38db64

File tree

27 files changed

+128
-1
lines changed

27 files changed

+128
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/Verifier.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ extension LoadBorrowInst : VerifiableInstruction {
148148
}
149149
}
150150

151+
extension VectorBaseAddrInst : VerifiableInstruction {
152+
func verify(_ context: FunctionPassContext) {
153+
require(vector.type.isBuiltinFixedArray,
154+
"vector operand of vector_element_addr must be a Builtin.FixedArray")
155+
require(type == vector.type.builtinFixedArrayElementType(in: parentFunction,
156+
maximallyAbstracted: true).addressType,
157+
"result of vector_element_addr has wrong type")
158+
}
159+
}
160+
151161
// Used to check if any instruction is mutating the memory location within the liverange of a `load_borrow`.
152162
// Note that it is not checking if an instruction _may_ mutate the memory, but it's checking if any instruction
153163
// _definitely_ will mutate the memory.

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,10 @@ public struct Builder {
487487
return notifyNew(vectorInst.getAs(VectorInst.self))
488488
}
489489

490+
public func createVectorBaseAddr(vector: Value) -> VectorBaseAddrInst {
491+
return notifyNew(bridged.createVectorBaseAddr(vector.bridged).getAs(VectorBaseAddrInst.self))
492+
}
493+
490494
public func createGlobalAddr(global: GlobalVariable, dependencyToken: Value?) -> GlobalAddrInst {
491495
return notifyNew(bridged.createGlobalAddr(global.bridged, dependencyToken.bridged).getAs(GlobalAddrInst.self))
492496
}

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,10 @@ final public class ObjectInst : SingleValueInstruction {
13501350
final public class VectorInst : SingleValueInstruction {
13511351
}
13521352

1353+
final public class VectorBaseAddrInst : SingleValueInstruction, UnaryInstruction {
1354+
public var vector: Value { operand.value }
1355+
}
1356+
13531357
final public class DifferentiableFunctionInst: SingleValueInstruction {}
13541358

13551359
final public class LinearFunctionInst: SingleValueInstruction {}

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public func registerSILClasses() {
118118
register(MoveOnlyWrapperToCopyableAddrInst.self)
119119
register(ObjectInst.self)
120120
register(VectorInst.self)
121+
register(VectorBaseAddrInst.self)
121122
register(TuplePackExtractInst.self)
122123
register(TuplePackElementAddrInst.self)
123124
register(PackElementGetInst.self)

docs/SIL/Instructions.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3467,6 +3467,20 @@ Constructs a statically initialized vector of elements. This instruction
34673467
can only appear as final instruction in a global variable static
34683468
initializer list.
34693469

3470+
### vector_base_addr
3471+
3472+
```
3473+
sil-instruction ::= 'vector_base_addr' sil-operand
3474+
3475+
%1 = vector_base_addr %0 : $*Builtin.FixedArray<N, Element>
3476+
// %0 must have type $*Builtin.FixedArray
3477+
// %1 will be of the element type of the Builtin.FixedArray
3478+
```
3479+
3480+
Derives the address of the first element of a vector, i.e. a `Builtin.FixedArray`,
3481+
from the address of the vector itself.
3482+
Addresses of other vector elements can then be derived with `index_addr`.
3483+
34703484
### ref_element_addr
34713485

34723486
```

include/swift/SIL/AddressWalker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ TransitiveAddressWalker<Impl>::walk(SILValue projectedAddress) {
249249
isa<BeginAccessInst>(user) || isa<TailAddrInst>(user) ||
250250
isa<IndexAddrInst>(user) || isa<StoreBorrowInst>(user) ||
251251
isa<UncheckedAddrCastInst>(user) ||
252+
isa<VectorBaseAddrInst>(user) ||
252253
isa<MarkUnresolvedNonCopyableValueInst>(user) ||
253254
isa<MarkUninitializedInst>(user) || isa<DropDeinitInst>(user) ||
254255
isa<ProjectBlockStorageInst>(user) || isa<UpcastInst>(user) ||

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,7 @@ struct BridgedBuilder{
12581258
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createObject(BridgedType type, BridgedValueArray arguments,
12591259
SwiftInt numBaseElements) const;
12601260
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createVector(BridgedValueArray arguments) const;
1261+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createVectorBaseAddr(BridgedValue vector) const;
12611262
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createGlobalAddr(BridgedGlobalVar global,
12621263
OptionalBridgedValue dependencyToken) const;
12631264
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createGlobalValue(BridgedGlobalVar global,

include/swift/SIL/SILBridgingImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,6 +2453,10 @@ BridgedInstruction BridgedBuilder::createVector(BridgedValueArray arguments) con
24532453
return {unbridged().createVector(swift::ArtificialUnreachableLocation(), arguments.getValues(argValues))};
24542454
}
24552455

2456+
BridgedInstruction BridgedBuilder::createVectorBaseAddr(BridgedValue vector) const {
2457+
return {unbridged().createVectorBaseAddr(regularLoc(), vector.getSILValue())};
2458+
}
2459+
24562460
BridgedInstruction BridgedBuilder::createGlobalAddr(BridgedGlobalVar global,
24572461
OptionalBridgedValue dependencyToken) const {
24582462
return {unbridged().createGlobalAddr(regularLoc(), global.getGlobal(), dependencyToken.getSILValue())};

include/swift/SIL/SILBuilder.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/SIL/SILInstruction.h"
2323
#include "swift/SIL/SILModule.h"
2424
#include "swift/SIL/SILUndef.h"
25+
#include "swift/SIL/AbstractionPattern.h"
2526
#include "llvm/ADT/PointerUnion.h"
2627
#include "llvm/ADT/StringExtras.h"
2728
#include <type_traits>
@@ -1923,6 +1924,15 @@ class SILBuilder {
19231924
return createStructElementAddr(Loc, Operand, Field, ResultTy);
19241925
}
19251926

1927+
VectorBaseAddrInst *
1928+
createVectorBaseAddr(SILLocation loc, SILValue vector) {
1929+
auto arrayTy = vector->getType().getAs<BuiltinFixedArrayType>();
1930+
ASSERT(arrayTy && "operand of vector_extract must be a builtin array type");
1931+
auto elemtTy = getFunction().getLoweredType(Lowering::AbstractionPattern::getOpaque(), arrayTy->getElementType());
1932+
return insert(new (getModule()) VectorBaseAddrInst(
1933+
getSILDebugLocation(loc), vector, elemtTy.getAddressType()));
1934+
}
1935+
19261936
RefElementAddrInst *createRefElementAddr(SILLocation Loc, SILValue Operand,
19271937
VarDecl *Field, SILType ResultTy,
19281938
bool IsImmutable = false) {

include/swift/SIL/SILCloner.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,6 +2542,16 @@ SILCloner<ImplClass>::visitStructElementAddrInst(StructElementAddrInst *Inst) {
25422542
Inst->getField(), getOpType(Inst->getType())));
25432543
}
25442544

2545+
template<typename ImplClass>
2546+
void
2547+
SILCloner<ImplClass>::visitVectorBaseAddrInst(VectorBaseAddrInst *Inst) {
2548+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
2549+
recordClonedInstruction(
2550+
Inst, getBuilder().createVectorBaseAddr(
2551+
getOpLocation(Inst->getLoc()),
2552+
getOpValue(Inst->getVector())));
2553+
}
2554+
25452555
template<typename ImplClass>
25462556
void
25472557
SILCloner<ImplClass>::visitRefElementAddrInst(RefElementAddrInst *Inst) {

0 commit comments

Comments
 (0)