Skip to content

Commit 0597ec5

Browse files
committed
[SIL] BuiltinInsts have type dependent operands.
Collect all types in the substitution map which constitute type-dependent operands and record them in the instruction's operand list. Fixes a bug where open_existential_metatype (e.g.) is deleted as dead because it has no users even when the type it defines is used in a substitution map of a builtin.
1 parent 38e6420 commit 0597ec5

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ class SILBuilder {
215215

216216
SILBuilderContext &getBuilderContext() const { return C; }
217217
SILModule &getModule() const { return C.Module; }
218+
SILInstructionContext getInstructionContext() const {
219+
if (!F)
220+
return SILInstructionContext::forModule(getModule());
221+
return SILInstructionContext::forFunction(*F);
222+
}
218223
ASTContext &getASTContext() const { return getModule().getASTContext(); }
219224
const Lowering::TypeLowering &getTypeLowering(SILType T) const {
220225

@@ -607,8 +612,8 @@ class SILBuilder {
607612
BuiltinInst *createBuiltin(SILLocation Loc, Identifier Name, SILType ResultTy,
608613
SubstitutionMap Subs,
609614
ArrayRef<SILValue> Args) {
610-
return insert(BuiltinInst::create(getSILDebugLocation(Loc), Name,
611-
ResultTy, Subs, Args, getModule()));
615+
return insert(BuiltinInst::create(getSILDebugLocation(Loc), Name, ResultTy,
616+
Subs, Args, getInstructionContext()));
612617
}
613618

614619
/// Create a binary function with the signature: OpdTy, OpdTy -> ResultTy.
@@ -621,7 +626,7 @@ class SILBuilder {
621626
appendOperandTypeName(OpdTy, NameStr);
622627
auto Ident = C.getIdentifier(NameStr);
623628
return insert(BuiltinInst::create(getSILDebugLocation(Loc), Ident, ResultTy,
624-
{}, Args, getModule()));
629+
{}, Args, getInstructionContext()));
625630
}
626631

627632
// Create a binary function with the signature: OpdTy1, OpdTy2 -> ResultTy.
@@ -634,8 +639,8 @@ class SILBuilder {
634639
appendOperandTypeName(OpdTy1, NameStr);
635640
appendOperandTypeName(OpdTy2, NameStr);
636641
auto Ident = C.getIdentifier(NameStr);
637-
return insert(BuiltinInst::create(getSILDebugLocation(Loc), Ident,
638-
ResultTy, {}, Args, getModule()));
642+
return insert(BuiltinInst::create(getSILDebugLocation(Loc), Ident, ResultTy,
643+
{}, Args, getInstructionContext()));
639644
}
640645

641646
/// Create a binary function with the signature:

include/swift/SIL/SILInstruction.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4183,13 +4183,16 @@ class BuiltinInst final
41834183
/// The substitutions.
41844184
SubstitutionMap Substitutions;
41854185

4186+
unsigned numNormalOperands;
4187+
41864188
BuiltinInst(SILDebugLocation DebugLoc, Identifier Name, SILType ReturnType,
4187-
SubstitutionMap Substitutions, ArrayRef<SILValue> Args);
4189+
SubstitutionMap Substitutions, ArrayRef<SILValue> Args,
4190+
unsigned numNormalOperands);
41884191

41894192
static BuiltinInst *create(SILDebugLocation DebugLoc, Identifier Name,
4190-
SILType ReturnType,
4191-
SubstitutionMap Substitutions,
4192-
ArrayRef<SILValue> Args, SILModule &M);
4193+
SILType ReturnType, SubstitutionMap Substitutions,
4194+
ArrayRef<SILValue> Args,
4195+
SILInstructionContext context);
41934196

41944197
public:
41954198
/// Return the name of the builtin operation.
@@ -4235,13 +4238,19 @@ class BuiltinInst final
42354238

42364239
/// The arguments to the builtin.
42374240
OperandValueArrayRef getArguments() const {
4238-
return OperandValueArrayRef(getAllOperands());
4241+
return OperandValueArrayRef(getArgumentOperands());
42394242
}
42404243
ArrayRef<Operand> getArgumentOperands() const {
4241-
return getAllOperands();
4244+
return getAllOperands().slice(0, numNormalOperands);
42424245
}
42434246
MutableArrayRef<Operand> getArgumentOperands() {
4244-
return getAllOperands();
4247+
return getAllOperands().slice(0, numNormalOperands);
4248+
}
4249+
ArrayRef<Operand> getTypeDependentOperands() const {
4250+
return getAllOperands().drop_front(numNormalOperands);
4251+
}
4252+
MutableArrayRef<Operand> getTypeDependentOperands() {
4253+
return getAllOperands().drop_front(numNormalOperands);
42454254
}
42464255
};
42474256

lib/SIL/IR/SILInstructions.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -529,19 +529,22 @@ BuiltinInst *BuiltinInst::create(SILDebugLocation Loc, Identifier Name,
529529
SILType ReturnType,
530530
SubstitutionMap Substitutions,
531531
ArrayRef<SILValue> Args,
532-
SILModule &M) {
533-
auto Size = totalSizeToAlloc<swift::Operand>(Args.size());
534-
auto Buffer = M.allocateInst(Size, alignof(BuiltinInst));
532+
SILInstructionContext context) {
533+
SmallVector<SILValue, 32> allOperands;
534+
copy(Args, std::back_inserter(allOperands));
535+
collectTypeDependentOperands(allOperands, context, Substitutions);
536+
auto Size = totalSizeToAlloc<swift::Operand>(allOperands.size());
537+
auto Buffer = context.getModule().allocateInst(Size, alignof(BuiltinInst));
535538
return ::new (Buffer) BuiltinInst(Loc, Name, ReturnType, Substitutions,
536-
Args);
539+
allOperands, Args.size());
537540
}
538541

539542
BuiltinInst::BuiltinInst(SILDebugLocation Loc, Identifier Name,
540543
SILType ReturnType, SubstitutionMap Subs,
541-
ArrayRef<SILValue> Args)
542-
: InstructionBaseWithTrailingOperands(Args, Loc, ReturnType), Name(Name),
543-
Substitutions(Subs) {
544-
}
544+
ArrayRef<SILValue> allOperands,
545+
unsigned numNormalOperands)
546+
: InstructionBaseWithTrailingOperands(allOperands, Loc, ReturnType),
547+
Name(Name), Substitutions(Subs), numNormalOperands(numNormalOperands) {}
545548

546549
IncrementProfilerCounterInst *IncrementProfilerCounterInst::create(
547550
SILDebugLocation Loc, unsigned CounterIdx, StringRef PGOFuncName,

test/SIL/cloning.sil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,20 @@ entry:
289289
%res = apply %callee() : $@convention(thin) () -> ()
290290
return %res : $()
291291
}
292+
293+
sil [transparent] [ossa] @getSize : $@convention(thin) <Type> (@thick Type.Type) -> Builtin.Word {
294+
bb0(%ty : $@thick Type.Type):
295+
%size = builtin "sizeof"<Type>() : $Builtin.Word
296+
return %size : $Builtin.Word
297+
}
298+
299+
// CHECK-LABEL: sil [ossa] @callee_builtin_type_dep_operand : {{.*}} {
300+
// CHECK: open_existential_metatype
301+
// CHECK-LABEL: } // end sil function 'callee_builtin_type_dep_operand'
302+
sil [ossa] @callee_builtin_type_dep_operand : $@convention(thin) (@thick any Any.Type) -> Builtin.Word {
303+
bb0(%0 : $@thick any Any.Type):
304+
%ty = open_existential_metatype %0 : $@thick any Any.Type to $@thick (@opened("00000000-0000-0000-0000-000000000000", Any) Self).Type
305+
%getSize = function_ref @getSize : $@convention(thin) <τ_0_0> (@thick τ_0_0.Type) -> Builtin.Word
306+
%retval = apply %getSize<@opened("00000000-0000-0000-0000-000000000000", Any) Self>(%ty) : $@convention(thin) <τ_0_0> (@thick τ_0_0.Type) -> Builtin.Word
307+
return %retval : $Builtin.Word
308+
}

0 commit comments

Comments
 (0)