Skip to content

constant_vprintf: Add a mode where prints are silenced when -DPRINT_DISABLED is present on the client side #41177

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
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
2 changes: 2 additions & 0 deletions stdlib/public/core/StaticPrint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -849,10 +849,12 @@ internal func constant_vprintf_backend(
@_semantics("oslog.requires_constant_arguments")
@inlinable
@_transparent
@_alwaysEmitIntoClient
@_optimize(none)
public func constant_vprintf(_ message: ConstantVPrintFMessage) {
let formatString = message.interpolation.formatString
let argumentClosures = message.interpolation.arguments.argumentClosures
if Bool(_builtinBooleanLiteral: Builtin.ifdef_PRINT_DISABLED()) { return }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let me just explain why this isn't the very 1st line in the function: If I put it into the 1st line, the copy-propagation + constant-folding + dead-code-elimination doesn't like that, because it for some reason isn't able to fully eliminate the ConstantVPrintFMessage input. I haven't fully investigated why, but placing the check as the 3rd line (after all accesses to "message") seems to work as a simple workaround.

let formatStringPointer = _getGlobalStringTablePointer(formatString)
constant_vprintf_backend(
fmt: formatStringPointer,
Expand Down
13 changes: 13 additions & 0 deletions test/stdlib/StaticPrintDisabled.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -I %t -o %t/a1.out -O && %target-run %t/a1.out | %FileCheck %s
// RUN: %target-build-swift %s -D PRINT_DISABLED -I %t -o %t/a2.out -O && %target-run %t/a2.out | %FileCheck %s --check-prefix CHECK-PRINT-DISABLED --allow-empty

// REQUIRES: executable_test
// REQUIRES: stdlib_static_print

let x = 42
let s = "ABCDE"
constant_vprintf("Hello World \(5) \(x) \(s)")

// CHECK: Hello World 5 42 ABCDE
// CHECK-PRINT-DISABLED-NOT: Hello World