Skip to content

Commit 7ae56aa

Browse files
committed
[sil] Add a new instruction ignored_use.
This is used for synthetic uses like _ = x that do not act as a true use but instead only suppress unused variable warnings. This patch just adds the instruction. Eventually, we can use it to move the unused variable warning from Sema to SIL slimmming the type checker down a little bit... but for now I am using it so that other diagnostic passes can have a SIL instruction (with SIL location) so that we can emit diagnostics on code like _ = x. Today we just do not emit anything at all for that case so a diagnostic SIL pass would not see any instruction that it could emit a diagnostic upon. In the next patch of this series, I am going to add SILGen support to do that.
1 parent bdfb609 commit 7ae56aa

File tree

20 files changed

+114
-3
lines changed

20 files changed

+114
-3
lines changed

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,3 +1736,6 @@ final public class ThunkInst : Instruction {
17361736

17371737
final public class MergeIsolationRegionInst : Instruction {
17381738
}
1739+
1740+
final public class IgnoredUseInst : Instruction {
1741+
}

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,5 @@ public func registerSILClasses() {
258258
register(CheckedCastAddrBranchInst.self)
259259
register(ThunkInst.self)
260260
register(MergeIsolationRegionInst.self)
261+
register(IgnoredUseInst.self)
261262
}

docs/SIL/Instructions.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5551,3 +5551,21 @@ sil-instruction ::= 'has_symbol' sil-decl-ref
55515551
Returns true if each of the underlying symbol addresses associated with
55525552
the given declaration are non-null. This can be used to determine
55535553
whether a weakly-imported declaration is available at runtime.
5554+
5555+
## Miscellaneous instructions
5556+
5557+
### ignored_use
5558+
5559+
```none
5560+
sil-instruction ::= 'ignored_use'
5561+
```
5562+
5563+
This instruction acts as a synthetic use instruction that suppresses unused
5564+
variable warnings. In Swift the equivalent operation is '_ = x'. This
5565+
importantly also provides a way to find the source location for '_ = x' when
5566+
emitting SIL diagnostics. It is only legal in Raw SIL and is removed as dead
5567+
code when we convert to Canonical SIL.
5568+
5569+
DISCUSSION: Before the introduction of this instruction, in certain cases,
5570+
SILGen would just not emit anything for '_ = x'... so one could not emit
5571+
diagnostics upon this case.

include/swift/SIL/SILBuilder.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3089,6 +3089,15 @@ class SILBuilder {
30893089
getModule(), getSILDebugLocation(Loc), Decl));
30903090
}
30913091

3092+
//===--------------------------------------------------------------------===//
3093+
// Misc Uses
3094+
//===--------------------------------------------------------------------===//
3095+
3096+
IgnoredUseInst *createIgnoredUse(SILLocation loc, SILValue value) {
3097+
return insert(new (getModule())
3098+
IgnoredUseInst(getSILDebugLocation(loc), value));
3099+
}
3100+
30923101
//===--------------------------------------------------------------------===//
30933102
// Private Helper Methods
30943103
//===--------------------------------------------------------------------===//

include/swift/SIL/SILCloner.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3796,6 +3796,14 @@ void SILCloner<ImplClass>::visitHasSymbolInst(HasSymbolInst *Inst) {
37963796
Inst->getDecl()));
37973797
}
37983798

3799+
template <typename ImplClass>
3800+
void SILCloner<ImplClass>::visitIgnoredUseInst(IgnoredUseInst *Inst) {
3801+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
3802+
recordClonedInstruction(
3803+
Inst, getBuilder().createIgnoredUse(getOpLocation(Inst->getLoc()),
3804+
getOpValue(Inst->getOperand())));
3805+
}
3806+
37993807
} // end namespace swift
38003808

38013809
#endif

include/swift/SIL/SILInstruction.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11725,6 +11725,17 @@ class MergeIsolationRegionInst final
1172511725
}
1172611726
};
1172711727

11728+
/// An instruction that represents a semantic-less use that is used to
11729+
/// suppresses unused value variable warnings. E.x.: _ = x.
11730+
class IgnoredUseInst final
11731+
: public UnaryInstructionBase<SILInstructionKind::IgnoredUseInst,
11732+
NonValueInstruction> {
11733+
friend SILBuilder;
11734+
11735+
IgnoredUseInst(SILDebugLocation loc, SILValue operand)
11736+
: UnaryInstructionBase(loc, operand) {}
11737+
};
11738+
1172811739
inline SILType *AllocRefInstBase::getTypeStorage() {
1172911740
// If the size of the subclasses are equal, then all of this compiles away.
1173011741
if (auto I = dyn_cast<AllocRefInst>(this))

include/swift/SIL/SILNodes.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,9 @@ NON_VALUE_INST(MarkUnresolvedMoveAddrInst, mark_unresolved_move_addr,
897897
NON_VALUE_INST(MergeIsolationRegionInst, merge_isolation_region,
898898
SILInstruction, None, DoesNotRelease)
899899

900+
NON_VALUE_INST(IgnoredUseInst, ignored_use,
901+
SILInstruction, None, DoesNotRelease)
902+
900903
NON_VALUE_INST(IncrementProfilerCounterInst, increment_profiler_counter,
901904
SILInstruction, MayReadWrite, DoesNotRelease)
902905

lib/IRGen/IRGenSIL.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,9 @@ class IRGenSILFunction :
12731273
visitMoveOnlyWrapperToCopyableBoxInst(MoveOnlyWrapperToCopyableBoxInst *i) {
12741274
llvm_unreachable("OSSA instruction");
12751275
}
1276+
1277+
void visitIgnoredUseInst(IgnoredUseInst *i) {}
1278+
12761279
void
12771280
visitMoveOnlyWrapperToCopyableAddrInst(MoveOnlyWrapperToCopyableAddrInst *i) {
12781281
auto e = getLoweredExplosion(i->getOperand());

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ OPERAND_OWNERSHIP(TrivialUse, GlobalAddr)
214214
// The dealloc_stack_ref operand needs to have NonUse ownership because
215215
// this use comes after the last consuming use (which is usually a dealloc_ref).
216216
OPERAND_OWNERSHIP(NonUse, DeallocStackRef)
217+
OPERAND_OWNERSHIP(InstantaneousUse, IgnoredUse)
217218

218219
// Use an owned or guaranteed value only for the duration of the operation.
219220
OPERAND_OWNERSHIP(InstantaneousUse, ExistentialMetatype)

lib/SIL/IR/SILPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,6 +2873,10 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
28732873
*this << GI->getFormalResumeType();
28742874
}
28752875

2876+
void visitIgnoredUseInst(IgnoredUseInst *i) {
2877+
*this << getIDAndType(i->getOperand());
2878+
}
2879+
28762880
void visitGetAsyncContinuationAddrInst(GetAsyncContinuationAddrInst *GI) {
28772881
if (GI->throws())
28782882
*this << "[throws] ";

0 commit comments

Comments
 (0)