Skip to content

Commit 6b47a45

Browse files
committed
[Coverage] Avoid profiling functions with body macros
These replace the body of the function, and as such shouldn't be profiled.
1 parent c851753 commit 6b47a45

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

lib/SIL/IR/SILProfiler.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,23 @@ static bool shouldProfile(SILDeclRef Constant) {
114114
}
115115
}
116116

117-
// Do not profile AST nodes in unavailable contexts.
118117
if (auto *D = DC->getInnermostDeclarationDeclContext()) {
118+
// Do not profile AST nodes in unavailable contexts.
119119
if (D->getSemanticUnavailableAttr()) {
120120
LLVM_DEBUG(llvm::dbgs() << "Skipping ASTNode: unavailable context\n");
121121
return false;
122122
}
123+
124+
// Do not profile functions that have had their bodies replaced (e.g
125+
// function body macros).
126+
// TODO: If/when preamble macros become an official feature, we'll
127+
// need to be more nuanced here.
128+
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
129+
if (AFD->getOriginalBodySourceRange() != AFD->getBodySourceRange()) {
130+
LLVM_DEBUG(llvm::dbgs() << "Skipping function: body replaced\n");
131+
return false;
132+
}
133+
}
123134
}
124135

125136
// Do not profile code that hasn't been written by the user.

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,6 +2350,20 @@ public struct RemoteBodyMacro: BodyMacro {
23502350
}
23512351
}
23522352

2353+
@_spi(ExperimentalLanguageFeature)
2354+
public struct BodyMacroWithControlFlow: BodyMacro {
2355+
public static func expansion(
2356+
of node: AttributeSyntax,
2357+
providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
2358+
in context: some MacroExpansionContext
2359+
) throws -> [CodeBlockItemSyntax] {
2360+
[
2361+
"guard .random() else { return }",
2362+
"_ = try throwingFn()"
2363+
]
2364+
}
2365+
}
2366+
23532367
@_spi(ExperimentalLanguageFeature)
23542368
public struct TracedPreambleMacro: PreambleMacro {
23552369
public static func expansion(

test/Profiler/coverage_macros.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ macro fnCall<T>(_: () throws -> T, _: () throws -> T) -> (T, T) = #externalMacro
2828
@freestanding(expression)
2929
macro id<T>(_: T) -> T = #externalMacro(module: "MacroDefinition", type: "TupleMacro")
3030

31+
@attached(body)
32+
macro BodyMacroWithControlFlow() = #externalMacro(module: "MacroDefinition", type: "BodyMacroWithControlFlow")
33+
3134
// This needs to be matched up here due to the sorting of the SIL; just make
3235
// sure we emit the counter increments for the error branches.
3336
// CHECK-LABEL: sil hidden @$s15coverage_macros5test2Si_SityKF
@@ -104,3 +107,15 @@ func test5() throws -> (Int, Int, Int) { // CHECK-NEXT: [[@LINE]]:40 -> [
104107
: try throwingFn()) // CHECK-NEXT: [[@LINE]]:44 -> [[@LINE+1]]:19 : ((((0 - 2) - 4) - 6) - 7)
105108
return (x, y, z) // CHECK-NEXT: }
106109
}
110+
111+
// Not profiled.
112+
@BodyMacroWithControlFlow
113+
func test6() throws
114+
115+
116+
// Not profiled.
117+
@BodyMacroWithControlFlow
118+
func test7() throws {
119+
guard .random() else { return }
120+
print("hello")
121+
}

0 commit comments

Comments
 (0)