Skip to content

Commit c64d56c

Browse files
committed
[NFC] SIL: Added SILInstructionContext.
It has a module or maybe a function. In the future we may want to change this to be either a SILFunction or a SILGlobalVariable.
1 parent 560fe75 commit c64d56c

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 19 additions & 0 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

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
//---

0 commit comments

Comments
 (0)