Skip to content

[SIL] BuiltinInsts have type dependent operands. #77291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions include/swift/SIL/SILBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ class SILBuilder {

SILBuilderContext &getBuilderContext() const { return C; }
SILModule &getModule() const { return C.Module; }
SILInstructionContext getInstructionContext() const {
if (!F)
return SILInstructionContext::forModule(getModule());
return SILInstructionContext::forFunction(*F);
}
ASTContext &getASTContext() const { return getModule().getASTContext(); }
const Lowering::TypeLowering &getTypeLowering(SILType T) const {

Expand Down Expand Up @@ -607,8 +612,8 @@ class SILBuilder {
BuiltinInst *createBuiltin(SILLocation Loc, Identifier Name, SILType ResultTy,
SubstitutionMap Subs,
ArrayRef<SILValue> Args) {
return insert(BuiltinInst::create(getSILDebugLocation(Loc), Name,
ResultTy, Subs, Args, getModule()));
return insert(BuiltinInst::create(getSILDebugLocation(Loc), Name, ResultTy,
Subs, Args, getInstructionContext()));
}

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

// Create a binary function with the signature: OpdTy1, OpdTy2 -> ResultTy.
Expand All @@ -634,8 +639,8 @@ class SILBuilder {
appendOperandTypeName(OpdTy1, NameStr);
appendOperandTypeName(OpdTy2, NameStr);
auto Ident = C.getIdentifier(NameStr);
return insert(BuiltinInst::create(getSILDebugLocation(Loc), Ident,
ResultTy, {}, Args, getModule()));
return insert(BuiltinInst::create(getSILDebugLocation(Loc), Ident, ResultTy,
{}, Args, getInstructionContext()));
}

/// Create a binary function with the signature:
Expand Down
42 changes: 35 additions & 7 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "swift/Basic/OptionSet.h"
#include "swift/Basic/ProfileCounter.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/TaggedUnion.h"
#include "swift/SIL/Consumption.h"
#include "swift/SIL/SILAllocated.h"
#include "swift/SIL/SILArgumentArrayRef.h"
Expand Down Expand Up @@ -4150,6 +4151,24 @@ class KeyPathInst final
~KeyPathInst();
};

struct SILInstructionContext {
using Storage = TaggedUnion<SILModule *, SILFunction *>;
Storage storage;

static SILInstructionContext forModule(SILModule &M) { return {Storage(&M)}; }

static SILInstructionContext forFunction(SILFunction &F) {
return {Storage(&F)};
}

static SILInstructionContext forFunctionInModule(SILFunction *F,
SILModule &M);

SILFunction *getFunction();

SILModule &getModule();
};

/// Represents an invocation of builtin functionality provided by the code
/// generator.
class BuiltinInst final
Expand All @@ -4164,13 +4183,16 @@ class BuiltinInst final
/// The substitutions.
SubstitutionMap Substitutions;

unsigned numNormalOperands;

BuiltinInst(SILDebugLocation DebugLoc, Identifier Name, SILType ReturnType,
SubstitutionMap Substitutions, ArrayRef<SILValue> Args);
SubstitutionMap Substitutions, ArrayRef<SILValue> Args,
unsigned numNormalOperands);

static BuiltinInst *create(SILDebugLocation DebugLoc, Identifier Name,
SILType ReturnType,
SubstitutionMap Substitutions,
ArrayRef<SILValue> Args, SILModule &M);
SILType ReturnType, SubstitutionMap Substitutions,
ArrayRef<SILValue> Args,
SILInstructionContext context);

public:
/// Return the name of the builtin operation.
Expand Down Expand Up @@ -4216,13 +4238,19 @@ class BuiltinInst final

/// The arguments to the builtin.
OperandValueArrayRef getArguments() const {
return OperandValueArrayRef(getAllOperands());
return OperandValueArrayRef(getArgumentOperands());
}
ArrayRef<Operand> getArgumentOperands() const {
return getAllOperands();
return getAllOperands().slice(0, numNormalOperands);
}
MutableArrayRef<Operand> getArgumentOperands() {
return getAllOperands();
return getAllOperands().slice(0, numNormalOperands);
}
ArrayRef<Operand> getTypeDependentOperands() const {
return getAllOperands().drop_front(numNormalOperands);
}
MutableArrayRef<Operand> getTypeDependentOperands() {
return getAllOperands().drop_front(numNormalOperands);
}
};

Expand Down
20 changes: 20 additions & 0 deletions lib/SIL/IR/SILInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1974,6 +1974,26 @@ UncheckedTakeEnumDataAddrInst::isDestructive(EnumDecl *forEnum, SILModule &M) {
return false;
}

SILInstructionContext SILInstructionContext::forFunctionInModule(SILFunction *F,
SILModule &M) {
if (F) {
assert(&F->getModule() == &M);
return forFunction(*F);
}
return forModule(M);
}

SILFunction *SILInstructionContext::getFunction() {
return *storage.dyn_cast<SILFunction *>();
}

SILModule &SILInstructionContext::getModule() {
if (auto *m = storage.dyn_cast<SILModule *>()) {
return **m;
}
return storage.get<SILFunction *>()->getModule();
}

#ifndef NDEBUG

//---
Expand Down
54 changes: 35 additions & 19 deletions lib/SIL/IR/SILInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class TypeDependentOperandCollector {
}

void addTo(SmallVectorImpl<SILValue> &typeDependentOperands,
SILFunction &f);
SILInstructionContext context);
};

}
Expand Down Expand Up @@ -113,15 +113,18 @@ void TypeDependentOperandCollector::collect(SubstitutionMap subs) {
/// Given that we've collected a set of type dependencies, add operands
/// for those dependencies to the given vector.
void TypeDependentOperandCollector::addTo(SmallVectorImpl<SILValue> &operands,
SILFunction &F) {
SILInstructionContext context) {
for (GenericEnvironment *genericEnv : genericEnvs) {
SILValue def = F.getModule().getLocalGenericEnvironmentDef(genericEnv, &F);
assert(def->getFunction() == &F &&
SILValue def = context.getModule().getLocalGenericEnvironmentDef(
genericEnv, context.getFunction());
assert(def->getFunction() == context.getFunction() &&
"def of local environment is in wrong function");
operands.push_back(def);
}
if (hasDynamicSelf)
operands.push_back(F.getDynamicSelfMetadata());
if (hasDynamicSelf) {
assert(context.getFunction());
operands.push_back(context.getFunction()->getDynamicSelfMetadata());
}
}

/// Collects all root local archetypes from a type and a substitution list, and
Expand All @@ -130,12 +133,22 @@ void TypeDependentOperandCollector::addTo(SmallVectorImpl<SILValue> &operands,
/// of corresponding operands for the instruction being formed, because we need
/// to reserve enough memory for these operands.
template <class... Sources>
static void collectTypeDependentOperands(
SmallVectorImpl<SILValue> &typeDependentOperands,
SILFunction &F, Sources &&... sources) {
static void
collectTypeDependentOperands(SmallVectorImpl<SILValue> &typeDependentOperands,
SILInstructionContext context,
Sources &&...sources) {
TypeDependentOperandCollector collector;
collector.collectAll(std::forward<Sources>(sources)...);
collector.addTo(typeDependentOperands, F);
collector.addTo(typeDependentOperands, context);
}

template <class... Sources>
static void
collectTypeDependentOperands(SmallVectorImpl<SILValue> &typeDependentOperands,
SILFunction &function, Sources &&...sources) {
collectTypeDependentOperands(typeDependentOperands,
SILInstructionContext::forFunction(function),
std::forward<Sources>(sources)...);
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -516,19 +529,22 @@ BuiltinInst *BuiltinInst::create(SILDebugLocation Loc, Identifier Name,
SILType ReturnType,
SubstitutionMap Substitutions,
ArrayRef<SILValue> Args,
SILModule &M) {
auto Size = totalSizeToAlloc<swift::Operand>(Args.size());
auto Buffer = M.allocateInst(Size, alignof(BuiltinInst));
SILInstructionContext context) {
SmallVector<SILValue, 32> allOperands;
copy(Args, std::back_inserter(allOperands));
collectTypeDependentOperands(allOperands, context, Substitutions);
auto Size = totalSizeToAlloc<swift::Operand>(allOperands.size());
auto Buffer = context.getModule().allocateInst(Size, alignof(BuiltinInst));
return ::new (Buffer) BuiltinInst(Loc, Name, ReturnType, Substitutions,
Args);
allOperands, Args.size());
}

BuiltinInst::BuiltinInst(SILDebugLocation Loc, Identifier Name,
SILType ReturnType, SubstitutionMap Subs,
ArrayRef<SILValue> Args)
: InstructionBaseWithTrailingOperands(Args, Loc, ReturnType), Name(Name),
Substitutions(Subs) {
}
ArrayRef<SILValue> allOperands,
unsigned numNormalOperands)
: InstructionBaseWithTrailingOperands(allOperands, Loc, ReturnType),
Name(Name), Substitutions(Subs), numNormalOperands(numNormalOperands) {}

IncrementProfilerCounterInst *IncrementProfilerCounterInst::create(
SILDebugLocation Loc, unsigned CounterIdx, StringRef PGOFuncName,
Expand Down Expand Up @@ -2493,7 +2509,7 @@ OpenPackElementInst *OpenPackElementInst::create(
PackType *packSubstitution) {
collector.collect(packSubstitution->getCanonicalType());
});
collector.addTo(typeDependentOperands, F);
collector.addTo(typeDependentOperands, SILInstructionContext::forFunction(F));

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

Expand Down
17 changes: 17 additions & 0 deletions test/SIL/cloning.sil
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,20 @@ entry:
%res = apply %callee() : $@convention(thin) () -> ()
return %res : $()
}

sil [transparent] [ossa] @getSize : $@convention(thin) <Type> (@thick Type.Type) -> Builtin.Word {
bb0(%ty : $@thick Type.Type):
%size = builtin "sizeof"<Type>() : $Builtin.Word
return %size : $Builtin.Word
}

// CHECK-LABEL: sil [ossa] @callee_builtin_type_dep_operand : {{.*}} {
// CHECK: open_existential_metatype
// CHECK-LABEL: } // end sil function 'callee_builtin_type_dep_operand'
sil [ossa] @callee_builtin_type_dep_operand : $@convention(thin) (@thick any Any.Type) -> Builtin.Word {
bb0(%0 : $@thick any Any.Type):
%ty = open_existential_metatype %0 : $@thick any Any.Type to $@thick (@opened("00000000-0000-0000-0000-000000000000", Any) Self).Type
%getSize = function_ref @getSize : $@convention(thin) <τ_0_0> (@thick τ_0_0.Type) -> Builtin.Word
%retval = apply %getSize<@opened("00000000-0000-0000-0000-000000000000", Any) Self>(%ty) : $@convention(thin) <τ_0_0> (@thick τ_0_0.Type) -> Builtin.Word
return %retval : $Builtin.Word
}