Skip to content

Commit 625619e

Browse files
committed
SIL: add a bare attribute to global_value
The `bare` attribute indicates that the object header is not used throughout the lifetime of the value. This means, no reference counting operations are performed on the object and its metadata is not used. The header of bare objects doesn't need to be initialized.
1 parent b08710d commit 625619e

File tree

18 files changed

+75
-17
lines changed

18 files changed

+75
-17
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjectOutliner.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ private func replace(object allocRef: AllocRefInstBase,
310310

311311
// Replace the alloc_ref by global_value + strong_retain instructions.
312312
let builder = Builder(before: allocRef, context)
313-
let globalValue = builder.createGlobalValue(global: global)
313+
let globalValue = builder.createGlobalValue(global: global, isBare: false)
314314
builder.createStrongRetain(operand: globalValue)
315315

316316
endCOW.uses.replaceAll(with: endCOW.instance, context)

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,14 @@ extension AllocRefInst {
438438
}
439439
}
440440

441+
extension GlobalValueInst {
442+
func setIsBare(_ context: some MutatingContext) {
443+
context.notifyInstructionsChanged()
444+
bridged.GlobalValueInst_setIsBare()
445+
context.notifyInstructionChanged(self)
446+
}
447+
}
448+
441449
extension TermInst {
442450
func replaceBranchTarget(from fromBlock: BasicBlock, to toBlock: BasicBlock, _ context: some MutatingContext) {
443451
context.notifyBranchesChanged()

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ public struct Builder {
239239
return notifyNew(bridged.createGlobalAddr(global.bridged).getAs(GlobalAddrInst.self))
240240
}
241241

242-
public func createGlobalValue(global: GlobalVariable) -> GlobalValueInst {
243-
return notifyNew(bridged.createGlobalValue(global.bridged).getAs(GlobalValueInst.self))
242+
public func createGlobalValue(global: GlobalVariable, isBare: Bool) -> GlobalValueInst {
243+
return notifyNew(bridged.createGlobalValue(global.bridged, isBare).getAs(GlobalValueInst.self))
244244
}
245245

246246
public func createStruct(type: Type, elements: [Value]) -> StructInst {

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,9 @@ final public class PreviousDynamicFunctionRefInst : FunctionRefBaseInst {
523523

524524
final public class GlobalAddrInst : GlobalAccessInst {}
525525

526-
final public class GlobalValueInst : GlobalAccessInst {}
526+
final public class GlobalValueInst : GlobalAccessInst {
527+
public var isBare: Bool { bridged.GlobalValueInst_isBare() }
528+
}
527529

528530
final public class AllocGlobalInst : Instruction {
529531
public var global: GlobalVariable {

docs/SIL.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5490,7 +5490,7 @@ global_value
54905490
`````````````
54915491
::
54925492

5493-
sil-instruction ::= 'global_value' sil-global-name ':' sil-type
5493+
sil-instruction ::= 'global_value' ('[' 'bare' ']')? sil-global-name ':' sil-type
54945494

54955495
%1 = global_value @v : $T
54965496

@@ -5499,6 +5499,11 @@ by ``alloc_global``. It is undefined behavior to perform this operation on a
54995499
global variable which has not been initialized, except the global variable
55005500
has a static initializer.
55015501

5502+
The ``bare`` attribute indicates that the object header is not used throughout
5503+
the lifetime of the value. This means, no reference counting operations are
5504+
performed on the object and its metadata is not used. The header of bare
5505+
objects doesn't need to be initialized.
5506+
55025507
integer_literal
55035508
```````````````
55045509
::

include/swift/SIL/SILBridging.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,14 @@ struct BridgedInstruction {
794794
}, [](swift::SILDeclRef) {});
795795
}
796796

797+
bool GlobalValueInst_isBare() const {
798+
return getAs<swift::GlobalValueInst>()->isBare();
799+
}
800+
801+
void GlobalValueInst_setIsBare() const {
802+
getAs<swift::GlobalValueInst>()->setBare(true);
803+
}
804+
797805
SWIFT_IMPORT_UNSAFE
798806
inline BridgedBasicBlock CheckedCastBranch_getSuccessBlock() const;
799807

@@ -1250,8 +1258,8 @@ struct BridgedBuilder{
12501258
}
12511259

12521260
SWIFT_IMPORT_UNSAFE
1253-
BridgedInstruction createGlobalValue(BridgedGlobalVar global) const {
1254-
return {builder().createGlobalValue(regularLoc(), global.getGlobal())};
1261+
BridgedInstruction createGlobalValue(BridgedGlobalVar global, bool isBare) const {
1262+
return {builder().createGlobalValue(regularLoc(), global.getGlobal(), isBare)};
12551263
}
12561264

12571265
SWIFT_IMPORT_UNSAFE

include/swift/SIL/SILBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,9 @@ class SILBuilder {
682682
return insert(new (F->getModule())
683683
GlobalAddrInst(getSILDebugLocation(Loc), Ty));
684684
}
685-
GlobalValueInst *createGlobalValue(SILLocation Loc, SILGlobalVariable *g) {
685+
GlobalValueInst *createGlobalValue(SILLocation Loc, SILGlobalVariable *g, bool isBare) {
686686
return insert(new (getModule()) GlobalValueInst(getSILDebugLocation(Loc), g,
687-
getTypeExpansionContext()));
687+
getTypeExpansionContext(), isBare));
688688
}
689689
BaseAddrForOffsetInst *createBaseAddrForOffset(SILLocation Loc, SILType Ty) {
690690
return insert(new (F->getModule())

include/swift/SIL/SILCloner.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,8 @@ SILCloner<ImplClass>::visitGlobalValueInst(GlobalValueInst *Inst) {
11251125
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
11261126
recordClonedInstruction(
11271127
Inst, getBuilder().createGlobalValue(getOpLocation(Inst->getLoc()),
1128-
Inst->getReferencedGlobal()));
1128+
Inst->getReferencedGlobal(),
1129+
Inst->isBare()));
11291130
}
11301131

11311132
template<typename ImplClass>

include/swift/SIL/SILInstruction.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4119,9 +4119,18 @@ class GlobalValueInst
41194119
: public InstructionBase<SILInstructionKind::GlobalValueInst,
41204120
GlobalAccessInst> {
41214121
friend SILBuilder;
4122+
USE_SHARED_UINT8;
41224123

41234124
GlobalValueInst(SILDebugLocation DebugLoc, SILGlobalVariable *Global,
4124-
TypeExpansionContext context);
4125+
TypeExpansionContext context, bool isBare);
4126+
public:
4127+
bool isBare() const {
4128+
return sharedUInt8().GlobalValueInst.isBare;
4129+
}
4130+
4131+
void setBare(bool isBare = true) {
4132+
sharedUInt8().GlobalValueInst.isBare = isBare;
4133+
}
41254134
};
41264135

41274136
/// IntegerLiteralInst - Encapsulates an integer constant, as defined originally

include/swift/SIL/SILNode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class alignas(8) SILNode :
202202
SHARED_FIELD(EndCOWMutationInst, bool keepUnique);
203203
SHARED_FIELD(ConvertFunctionInst, bool withoutActuallyEscaping);
204204
SHARED_FIELD(BeginCOWMutationInst, bool native);
205+
SHARED_FIELD(GlobalValueInst, bool isBare);
205206

206207
SHARED_FIELD(SILArgument, uint8_t
207208
valueOwnershipKind : NumVOKindBits,

0 commit comments

Comments
 (0)