Skip to content

Commit 713e403

Browse files
committed
[opt-remark] Teach opt-remark how to emit remarks for retain/release of @_emptyArrayStorage.
This just involved teaching opt-remark-gen how to pattern match the following SIL: ``` %0 = global_addr @_swiftEmptyArrayStorage : $*_SwiftEmptyArrayStorage %1 = address_to_pointer %0 : $*_SwiftEmptyArrayStorage to $Builtin.RawPointer %2 = raw_pointer_to_ref %1 : $Builtin.RawPointer to $__EmptyArrayStorage ``` Now we produce the following remark: ``` ./swift/test/SILOptimizer/opt_remark/opt_remark_generator.sil:105:3: remark: retain of type 'EmptyArrayStorage' strong_retain %2 : $EmptyArrayStorage // expected-remark {{retain of type 'EmptyArrayStorage'}} ^ ./swift/test/SILOptimizer/opt_remark/opt_remark_generator.sil:42:5: note: of 'swiftEmptyArrayStorage' var swiftEmptyArrayStorage: _SwiftEmptyArrayStorage ^ ```
1 parent 3602f1a commit 713e403

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/SILOptimizer/Transforms/OptRemarkGenerator.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "swift/SIL/Projection.h"
2929
#include "swift/SIL/SILFunction.h"
3030
#include "swift/SIL/SILInstruction.h"
31+
#include "swift/SIL/PatternMatch.h"
3132
#include "swift/SIL/SILModule.h"
3233
#include "swift/SIL/SILVisitor.h"
3334
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
@@ -37,6 +38,7 @@
3738
#include "llvm/Support/raw_ostream.h"
3839

3940
using namespace swift;
41+
using namespace swift::PatternMatch;
4042

4143
static llvm::cl::opt<bool> ForceVisitImplicitAutogeneratedFunctions(
4244
"optremarkgen-visit-implicit-autogen-funcs", llvm::cl::Hidden,
@@ -374,6 +376,30 @@ bool ValueToDeclInferrer::infer(
374376
}
375377
}
376378

379+
// A pattern that we see around empty array storage is:
380+
//
381+
// %0 = global_addr @_swiftEmptyArrayStorage : $*_SwiftEmptyArrayStorage
382+
// %1 = address_to_pointer %0 : $*_SwiftEmptyArrayStorage to $Builtin.RawPointer
383+
// %2 = raw_pointer_to_ref %1 : $Builtin.RawPointer to $__EmptyArrayStorage
384+
//
385+
// Recognize this case.
386+
{
387+
GlobalAddrInst *gai;
388+
if (match(value, m_RawPointerToRefInst(
389+
m_AddressToPointerInst(m_GlobalAddrInst(gai))))) {
390+
if (auto *decl = gai->getReferencedGlobal()->getDecl()) {
391+
std::string msg;
392+
{
393+
llvm::raw_string_ostream stream(msg);
394+
printNote(stream, decl);
395+
}
396+
resultingInferredDecls.push_back(
397+
Argument({keyKind, "InferredValue"}, std::move(msg), decl));
398+
return true;
399+
}
400+
}
401+
}
402+
377403
// We prefer decls not from uses since these are inherently noisier. Still,
378404
// it is better than nothing.
379405
bool foundDeclFromUse = false;

test/SILOptimizer/opt_remark/opt_remark_generator.sil

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ struct StructWithOwnerAndState {
3131
var state: TrivialState
3232
}
3333

34+
struct _SwiftEmptyArrayStorage {
35+
var nonTrivialField: Klass = Klass()
36+
}
37+
38+
class EmptyArrayStorage {
39+
var state: TrivialState = .first
40+
}
41+
3442
///////////
3543
// Tests //
3644
///////////
@@ -84,3 +92,17 @@ bb0(%0 : $StructWithOwner, %1 : $TrivialState):
8492
// expected-note @-3 {{of 'x'}}
8593
return %3 : $Klass
8694
}
95+
96+
// Please keep these next to this test to make the note on the decl not far from its check.
97+
var swiftEmptyArrayStorage: _SwiftEmptyArrayStorage
98+
sil_global hidden @$s20opt_remark_generator22swiftEmptyArrayStorageAA06_SwiftefG0Vvp : $_SwiftEmptyArrayStorage
99+
100+
sil @retainOnEmptyArrayStorage : $@convention(thin) () -> @owned EmptyArrayStorage {
101+
bb0:
102+
%0 = global_addr @$s20opt_remark_generator22swiftEmptyArrayStorageAA06_SwiftefG0Vvp : $*_SwiftEmptyArrayStorage
103+
%1 = address_to_pointer %0 : $*_SwiftEmptyArrayStorage to $Builtin.RawPointer
104+
%2 = raw_pointer_to_ref %1 : $Builtin.RawPointer to $EmptyArrayStorage
105+
strong_retain %2 : $EmptyArrayStorage // expected-remark {{retain of type 'EmptyArrayStorage'}}
106+
// expected-note @-9 {{of 'swiftEmptyArrayStorage'}}
107+
return %2 : $EmptyArrayStorage
108+
}

0 commit comments

Comments
 (0)