Skip to content

Commit 20dd837

Browse files
Merge pull request #77291 from nate-chandler/bug/20241029/1
[SIL] BuiltinInsts have type dependent operands.
2 parents 65a6d03 + 0597ec5 commit 20dd837

File tree

5 files changed

+117
-31
lines changed

5 files changed

+117
-31
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: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "swift/Basic/OptionSet.h"
3232
#include "swift/Basic/ProfileCounter.h"
3333
#include "swift/Basic/Range.h"
34+
#include "swift/Basic/TaggedUnion.h"
3435
#include "swift/SIL/Consumption.h"
3536
#include "swift/SIL/SILAllocated.h"
3637
#include "swift/SIL/SILArgumentArrayRef.h"
@@ -4150,6 +4151,24 @@ class KeyPathInst final
41504151
~KeyPathInst();
41514152
};
41524153

4154+
struct SILInstructionContext {
4155+
using Storage = TaggedUnion<SILModule *, SILFunction *>;
4156+
Storage storage;
4157+
4158+
static SILInstructionContext forModule(SILModule &M) { return {Storage(&M)}; }
4159+
4160+
static SILInstructionContext forFunction(SILFunction &F) {
4161+
return {Storage(&F)};
4162+
}
4163+
4164+
static SILInstructionContext forFunctionInModule(SILFunction *F,
4165+
SILModule &M);
4166+
4167+
SILFunction *getFunction();
4168+
4169+
SILModule &getModule();
4170+
};
4171+
41534172
/// Represents an invocation of builtin functionality provided by the code
41544173
/// generator.
41554174
class BuiltinInst final
@@ -4164,13 +4183,16 @@ class BuiltinInst final
41644183
/// The substitutions.
41654184
SubstitutionMap Substitutions;
41664185

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

41704192
static BuiltinInst *create(SILDebugLocation DebugLoc, Identifier Name,
4171-
SILType ReturnType,
4172-
SubstitutionMap Substitutions,
4173-
ArrayRef<SILValue> Args, SILModule &M);
4193+
SILType ReturnType, SubstitutionMap Substitutions,
4194+
ArrayRef<SILValue> Args,
4195+
SILInstructionContext context);
41744196

41754197
public:
41764198
/// Return the name of the builtin operation.
@@ -4216,13 +4238,19 @@ class BuiltinInst final
42164238

42174239
/// The arguments to the builtin.
42184240
OperandValueArrayRef getArguments() const {
4219-
return OperandValueArrayRef(getAllOperands());
4241+
return OperandValueArrayRef(getArgumentOperands());
42204242
}
42214243
ArrayRef<Operand> getArgumentOperands() const {
4222-
return getAllOperands();
4244+
return getAllOperands().slice(0, numNormalOperands);
42234245
}
42244246
MutableArrayRef<Operand> getArgumentOperands() {
4225-
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);
42264254
}
42274255
};
42284256

lib/SIL/IR/SILInstruction.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,6 +1974,26 @@ UncheckedTakeEnumDataAddrInst::isDestructive(EnumDecl *forEnum, SILModule &M) {
19741974
return false;
19751975
}
19761976

1977+
SILInstructionContext SILInstructionContext::forFunctionInModule(SILFunction *F,
1978+
SILModule &M) {
1979+
if (F) {
1980+
assert(&F->getModule() == &M);
1981+
return forFunction(*F);
1982+
}
1983+
return forModule(M);
1984+
}
1985+
1986+
SILFunction *SILInstructionContext::getFunction() {
1987+
return *storage.dyn_cast<SILFunction *>();
1988+
}
1989+
1990+
SILModule &SILInstructionContext::getModule() {
1991+
if (auto *m = storage.dyn_cast<SILModule *>()) {
1992+
return **m;
1993+
}
1994+
return storage.get<SILFunction *>()->getModule();
1995+
}
1996+
19771997
#ifndef NDEBUG
19781998

19791999
//---

lib/SIL/IR/SILInstructions.cpp

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class TypeDependentOperandCollector {
7070
}
7171

7272
void addTo(SmallVectorImpl<SILValue> &typeDependentOperands,
73-
SILFunction &f);
73+
SILInstructionContext context);
7474
};
7575

7676
}
@@ -113,15 +113,18 @@ void TypeDependentOperandCollector::collect(SubstitutionMap subs) {
113113
/// Given that we've collected a set of type dependencies, add operands
114114
/// for those dependencies to the given vector.
115115
void TypeDependentOperandCollector::addTo(SmallVectorImpl<SILValue> &operands,
116-
SILFunction &F) {
116+
SILInstructionContext context) {
117117
for (GenericEnvironment *genericEnv : genericEnvs) {
118-
SILValue def = F.getModule().getLocalGenericEnvironmentDef(genericEnv, &F);
119-
assert(def->getFunction() == &F &&
118+
SILValue def = context.getModule().getLocalGenericEnvironmentDef(
119+
genericEnv, context.getFunction());
120+
assert(def->getFunction() == context.getFunction() &&
120121
"def of local environment is in wrong function");
121122
operands.push_back(def);
122123
}
123-
if (hasDynamicSelf)
124-
operands.push_back(F.getDynamicSelfMetadata());
124+
if (hasDynamicSelf) {
125+
assert(context.getFunction());
126+
operands.push_back(context.getFunction()->getDynamicSelfMetadata());
127+
}
125128
}
126129

127130
/// Collects all root local archetypes from a type and a substitution list, and
@@ -130,12 +133,22 @@ void TypeDependentOperandCollector::addTo(SmallVectorImpl<SILValue> &operands,
130133
/// of corresponding operands for the instruction being formed, because we need
131134
/// to reserve enough memory for these operands.
132135
template <class... Sources>
133-
static void collectTypeDependentOperands(
134-
SmallVectorImpl<SILValue> &typeDependentOperands,
135-
SILFunction &F, Sources &&... sources) {
136+
static void
137+
collectTypeDependentOperands(SmallVectorImpl<SILValue> &typeDependentOperands,
138+
SILInstructionContext context,
139+
Sources &&...sources) {
136140
TypeDependentOperandCollector collector;
137141
collector.collectAll(std::forward<Sources>(sources)...);
138-
collector.addTo(typeDependentOperands, F);
142+
collector.addTo(typeDependentOperands, context);
143+
}
144+
145+
template <class... Sources>
146+
static void
147+
collectTypeDependentOperands(SmallVectorImpl<SILValue> &typeDependentOperands,
148+
SILFunction &function, Sources &&...sources) {
149+
collectTypeDependentOperands(typeDependentOperands,
150+
SILInstructionContext::forFunction(function),
151+
std::forward<Sources>(sources)...);
139152
}
140153

141154
//===----------------------------------------------------------------------===//
@@ -516,19 +529,22 @@ BuiltinInst *BuiltinInst::create(SILDebugLocation Loc, Identifier Name,
516529
SILType ReturnType,
517530
SubstitutionMap Substitutions,
518531
ArrayRef<SILValue> Args,
519-
SILModule &M) {
520-
auto Size = totalSizeToAlloc<swift::Operand>(Args.size());
521-
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));
522538
return ::new (Buffer) BuiltinInst(Loc, Name, ReturnType, Substitutions,
523-
Args);
539+
allOperands, Args.size());
524540
}
525541

526542
BuiltinInst::BuiltinInst(SILDebugLocation Loc, Identifier Name,
527543
SILType ReturnType, SubstitutionMap Subs,
528-
ArrayRef<SILValue> Args)
529-
: InstructionBaseWithTrailingOperands(Args, Loc, ReturnType), Name(Name),
530-
Substitutions(Subs) {
531-
}
544+
ArrayRef<SILValue> allOperands,
545+
unsigned numNormalOperands)
546+
: InstructionBaseWithTrailingOperands(allOperands, Loc, ReturnType),
547+
Name(Name), Substitutions(Subs), numNormalOperands(numNormalOperands) {}
532548

533549
IncrementProfilerCounterInst *IncrementProfilerCounterInst::create(
534550
SILDebugLocation Loc, unsigned CounterIdx, StringRef PGOFuncName,
@@ -2493,7 +2509,7 @@ OpenPackElementInst *OpenPackElementInst::create(
24932509
PackType *packSubstitution) {
24942510
collector.collect(packSubstitution->getCanonicalType());
24952511
});
2496-
collector.addTo(typeDependentOperands, F);
2512+
collector.addTo(typeDependentOperands, SILInstructionContext::forFunction(F));
24972513

24982514
SILType type = SILType::getSILTokenType(F.getASTContext());
24992515

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)