Skip to content

Commit a60eafe

Browse files
committed
Require Builtin.valueToBridgeObject to have a builtin integer as argument
Previously it was used with an UInt struct argument. But this is bad because UInt is a type defined in the stdlib.
1 parent e34a6a1 commit a60eafe

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

lib/IRGen/GenConstant.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ static llvm::Constant *emitConstantValue(IRGenModule &IGM, SILValue operand) {
105105
llvm_unreachable("unsupported builtin for constant expression");
106106
}
107107
} else if (auto *VTBI = dyn_cast<ValueToBridgeObjectInst>(operand)) {
108-
auto *SI = cast<StructInst>(VTBI->getOperand());
109-
assert(SI->getElements().size() == 1);
110-
auto *val = emitConstantValue(IGM, SI->getElements()[0]);
108+
auto *val = emitConstantValue(IGM, VTBI->getOperand());
111109
auto *sTy = IGM.getTypeInfo(VTBI->getType()).getStorageType();
112110
return llvm::ConstantExpr::getIntToPtr(val, sTy);
113111
} else {

lib/SILGen/SILGenBuiltin.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -867,9 +867,15 @@ static ManagedValue emitBuiltinValueToBridgeObject(SILGenFunction &SGF,
867867
assert(args.size() == 1 && "ValueToBridgeObject should have one argument");
868868
assert(subs.getReplacementTypes().size() == 1 &&
869869
"ValueToBridgeObject should have one sub");
870-
auto &fromTL = SGF.getTypeLowering(subs.getReplacementTypes()[0]);
871-
assert(fromTL.isTrivial() && "Expected a trivial type");
872-
(void)fromTL;
870+
871+
Type argTy = subs.getReplacementTypes()[0];
872+
if (!argTy->is<BuiltinIntegerType>()) {
873+
SGF.SGM.diagnose(loc, diag::invalid_sil_builtin,
874+
"argument to builtin should be a builtin integer");
875+
SILType objPointerType = SILType::getBridgeObjectType(SGF.F.getASTContext());
876+
SILValue undef = SILUndef::get(objPointerType, SGF.SGM.M);
877+
return ManagedValue::forUnmanaged(undef);
878+
}
873879

874880
SILValue result = SGF.B.createValueToBridgeObject(loc, args[0].getValue());
875881
return SGF.emitManagedRetain(loc, result);

stdlib/public/core/Builtin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ public func _bridgeObject(
458458
@inlinable
459459
public func _bridgeObject(fromTagged x: UInt) -> Builtin.BridgeObject {
460460
_sanityCheck(x & _bridgeObjectTaggedPointerBits != 0)
461-
let object: Builtin.BridgeObject = Builtin.valueToBridgeObject(x)
461+
let object: Builtin.BridgeObject = Builtin.valueToBridgeObject(x._value)
462462
_sanityCheck(_isTaggedObject(object))
463463
return object
464464
}

test/SILGen/builtins.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,12 +809,13 @@ func once(control: Builtin.RawPointer) {
809809

810810
// CHECK-LABEL: sil hidden @$s8builtins19valueToBridgeObjectyBbSuF : $@convention(thin) (UInt) -> @owned Builtin.BridgeObject {
811811
// CHECK: bb0([[UINT:%.*]] : @trivial $UInt):
812-
// CHECK: [[CAST:%.*]] = value_to_bridge_object [[UINT]] : $UInt
812+
// CHECK: [[BI:%.*]] = struct_extract [[UINT]] : $UInt, #UInt._value
813+
// CHECK: [[CAST:%.*]] = value_to_bridge_object [[BI]]
813814
// CHECK: [[RET:%.*]] = copy_value [[CAST]] : $Builtin.BridgeObject
814815
// CHECK: return [[RET]] : $Builtin.BridgeObject
815816
// CHECK: } // end sil function '$s8builtins19valueToBridgeObjectyBbSuF'
816817
func valueToBridgeObject(_ x: UInt) -> Builtin.BridgeObject {
817-
return Builtin.valueToBridgeObject(x)
818+
return Builtin.valueToBridgeObject(x._value)
818819
}
819820

820821
// CHECK-LABEL: sil hidden @$s8builtins10assumeTrueyyBi1_F

0 commit comments

Comments
 (0)