Skip to content

Commit f732ea6

Browse files
author
Joe Shajrawi
committed
Builtins: add ValueToBridgeObject instruction
1 parent 09f77ff commit f732ea6

22 files changed

+115
-1
lines changed

include/swift/AST/Builtins.def

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ BUILTIN_SIL_OPERATION(CastFromNativeObject, "castFromNativeObject", Special)
225225
/// or else undefined behavior will ensue.
226226
BUILTIN_SIL_OPERATION(CastToBridgeObject, "castToBridgeObject", Special)
227227

228+
/// ValueToBridgeObject has type (T) -> Builtin.BridgeObject.
229+
/// It sets the BridgeObject to a tagged pointer representation holding its
230+
// operands by tagging and shifting the operand if needed.
231+
///
232+
/// valueToBridgeObject(x) === (x << _swift_abi_ObjCReservedLowBits) |
233+
/// _swift_BridgeObject_TaggedPointerBits
234+
///
235+
/// x thus must not be using any high bits shifted away (via _swift_abi_ObjCReservedLowBits)
236+
/// or the tag bits post-shift.
237+
/// ARC operations on such tagged values are NOPs.
238+
BUILTIN_SIL_OPERATION(ValueToBridgeObject, "valueToBridgeObject", Special)
239+
228240
/// CastReferenceFromBridgeObject has type (Builtin.BridgeObject) -> T.
229241
/// It recovers the heap object reference by masking spare bits from the
230242
/// BridgeObject.

include/swift/SIL/PatternMatch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ UNARY_OP_MATCH_WITH_ARG_MATCHER(StrongRetainUnownedInst)
384384
UNARY_OP_MATCH_WITH_ARG_MATCHER(UnownedRetainInst)
385385
UNARY_OP_MATCH_WITH_ARG_MATCHER(UnownedReleaseInst)
386386
UNARY_OP_MATCH_WITH_ARG_MATCHER(ClassifyBridgeObjectInst)
387+
UNARY_OP_MATCH_WITH_ARG_MATCHER(ValueToBridgeObjectInst)
387388
UNARY_OP_MATCH_WITH_ARG_MATCHER(FixLifetimeInst)
388389
UNARY_OP_MATCH_WITH_ARG_MATCHER(CopyBlockInst)
389390
UNARY_OP_MATCH_WITH_ARG_MATCHER(DeallocStackInst)

include/swift/SIL/SILBuilder.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,13 @@ class SILBuilder {
864864
getSILDebugLocation(Loc), Op, Ty));
865865
}
866866

867+
ValueToBridgeObjectInst *createValueToBridgeObject(SILLocation Loc,
868+
SILValue value) {
869+
auto Ty = SILType::getBridgeObjectType(getASTContext());
870+
return insert(new (getModule()) ValueToBridgeObjectInst(
871+
getSILDebugLocation(Loc), value, Ty));
872+
}
873+
867874
BridgeObjectToWordInst *createBridgeObjectToWord(SILLocation Loc,
868875
SILValue Op) {
869876
auto Ty = SILType::getBuiltinWordType(getASTContext());

include/swift/SIL/SILCloner.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,15 @@ SILCloner<ImplClass>::visitRefToRawPointerInst(RefToRawPointerInst *Inst) {
11341134
getOpType(Inst->getType())));
11351135
}
11361136

1137+
template <typename ImplClass>
1138+
void SILCloner<ImplClass>::visitValueToBridgeObjectInst(
1139+
ValueToBridgeObjectInst *Inst) {
1140+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1141+
doPostProcess(
1142+
Inst, getBuilder().createValueToBridgeObject(
1143+
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand())));
1144+
}
1145+
11371146
template<typename ImplClass>
11381147
void
11391148
SILCloner<ImplClass>::visitRawPointerToRefInst(RawPointerToRefInst *Inst) {

include/swift/SIL/SILInstruction.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4084,6 +4084,18 @@ class BridgeObjectToRefInst
40844084
: UnaryInstructionBase(DebugLoc, Operand, Ty) {}
40854085
};
40864086

4087+
/// Sets the BridgeObject to a tagged pointer representation holding its
4088+
/// operands
4089+
class ValueToBridgeObjectInst
4090+
: public UnaryInstructionBase<SILInstructionKind::ValueToBridgeObjectInst,
4091+
ConversionInst> {
4092+
friend SILBuilder;
4093+
4094+
ValueToBridgeObjectInst(SILDebugLocation DebugLoc, SILValue Operand,
4095+
SILType BridgeObjectTy)
4096+
: UnaryInstructionBase(DebugLoc, Operand, BridgeObjectTy) {}
4097+
};
4098+
40874099
/// Retrieve the bit pattern of a BridgeObject.
40884100
class BridgeObjectToWordInst
40894101
: public UnaryInstructionBase<SILInstructionKind::BridgeObjectToWordInst,

include/swift/SIL/SILNodes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
353353

354354
SINGLE_VALUE_INST(ClassifyBridgeObjectInst, classify_bridge_object,
355355
SingleValueInstruction, None, DoesNotRelease)
356+
SINGLE_VALUE_INST(ValueToBridgeObjectInst, value_to_bridge_object,
357+
SingleValueInstruction, None, DoesNotRelease)
356358
SINGLE_VALUE_INST(MarkDependenceInst, mark_dependence,
357359
SingleValueInstruction, None, DoesNotRelease)
358360
SINGLE_VALUE_INST(CopyBlockInst, copy_block,

lib/AST/Builtins.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,13 @@ static ValueDecl *getClassifyBridgeObject(ASTContext &C, Identifier Id) {
882882
return getBuiltinFunction(Id, { C.TheBridgeObjectType }, resultTy);
883883
}
884884

885+
static ValueDecl *getValueToBridgeObject(ASTContext &C, Identifier Id) {
886+
BuiltinGenericSignatureBuilder builder(C);
887+
builder.addParameter(makeGenericParam(0));
888+
builder.setResult(makeConcrete(C.TheBridgeObjectType));
889+
return builder.build(Id);
890+
}
891+
885892
static ValueDecl *getUnsafeGuaranteed(ASTContext &C, Identifier Id) {
886893
// <T : AnyObject> T -> (T, Int8Ty)
887894
//
@@ -1820,6 +1827,10 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
18201827
case BuiltinValueKind::ClassifyBridgeObject:
18211828
if (!Types.empty()) return nullptr;
18221829
return getClassifyBridgeObject(Context, Id);
1830+
case BuiltinValueKind::ValueToBridgeObject:
1831+
if (!Types.empty())
1832+
return nullptr;
1833+
return getValueToBridgeObject(Context, Id);
18231834
case BuiltinValueKind::UnsafeGuaranteed:
18241835
return getUnsafeGuaranteed(Context, Id);
18251836

lib/IRGen/IRGenSIL.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,7 @@ class IRGenSILFunction :
10551055
void visitClassifyBridgeObjectInst(ClassifyBridgeObjectInst *i);
10561056
void visitBridgeObjectToRefInst(BridgeObjectToRefInst *i);
10571057
void visitBridgeObjectToWordInst(BridgeObjectToWordInst *i);
1058+
void visitValueToBridgeObjectInst(ValueToBridgeObjectInst *i);
10581059

10591060
void visitIndexAddrInst(IndexAddrInst *i);
10601061
void visitTailAddrInst(TailAddrInst *i);
@@ -4692,6 +4693,18 @@ visitClassifyBridgeObjectInst(ClassifyBridgeObjectInst *i) {
46924693
setLoweredExplosion(i, wordEx);
46934694
}
46944695

4696+
void IRGenSILFunction::visitValueToBridgeObjectInst(
4697+
ValueToBridgeObjectInst *i) {
4698+
Explosion in = getLoweredExplosion(i->getOperand());
4699+
Explosion out;
4700+
4701+
emitValueBitwiseCast(
4702+
*this, i->getLoc().getSourceLoc(), in,
4703+
cast<LoadableTypeInfo>(getTypeInfo(i->getOperand()->getType())), out,
4704+
cast<LoadableTypeInfo>(getTypeInfo(i->getType())));
4705+
4706+
setLoweredExplosion(i, out);
4707+
}
46954708

46964709
void IRGenSILFunction::visitBridgeObjectToRefInst(
46974710
swift::BridgeObjectToRefInst *i) {

lib/ParseSIL/ParseSIL.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,7 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
23422342
} break;
23432343

23442344
UNARY_INSTRUCTION(ClassifyBridgeObject)
2345+
UNARY_INSTRUCTION(ValueToBridgeObject)
23452346
UNARY_INSTRUCTION(FixLifetime)
23462347
UNARY_INSTRUCTION(EndLifetime)
23472348
UNARY_INSTRUCTION(CopyBlock)

lib/SIL/SILInstruction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,10 @@ namespace {
758758
return true;
759759
}
760760

761+
bool visitValueToBridgeObjectInst(ValueToBridgeObjectInst *i) {
762+
return true;
763+
}
764+
761765
bool visitBridgeObjectToWordInst(BridgeObjectToWordInst *X) {
762766
return true;
763767
}

0 commit comments

Comments
 (0)