Skip to content

[PlaygroundTransform] Add experimental feature PlaygroundExtendedCallbacks to pass more info in callbacks #67215

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 2 commits into from
Jul 18, 2023
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
4 changes: 4 additions & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ EXPERIMENTAL_FEATURE(StrictConcurrency, true)
/// Defer Sendable checking to SIL diagnostic phase.
EXPERIMENTAL_FEATURE(DeferredSendableChecking, false)

/// Enable extended callbacks (with additional parameters) to be used when the
/// "playground transform" is enabled.
EXPERIMENTAL_FEATURE(PlaygroundExtendedCallbacks, true)

#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
#undef EXPERIMENTAL_FEATURE
#undef UPCOMING_FEATURE
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3480,6 +3480,10 @@ static bool usesFeatureDeferredSendableChecking(Decl *decl) {
return false;
}

static bool usesFeaturePlaygroundExtendedCallbacks(Decl *decl) {
return false;
}

/// Suppress the printing of a particular feature.
static void suppressingFeature(PrintOptions &options, Feature feature,
llvm::function_ref<void()> action) {
Expand Down
40 changes: 32 additions & 8 deletions lib/Sema/PlaygroundTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -41,13 +41,18 @@ class Instrumenter : InstrumenterBase {
std::mt19937_64 &RNG;
unsigned &TmpNameIndex;
bool HighPerformance;
bool ExtendedCallbacks;

DeclNameRef DebugPrintName;
DeclNameRef PrintName;
DeclNameRef PostPrintName;
DeclNameRef PostPrintExtendedName;
DeclNameRef LogWithIDName;
DeclNameRef LogWithIDExtendedName;
DeclNameRef LogScopeExitName;
DeclNameRef LogScopeExitExtendedName;
DeclNameRef LogScopeEntryName;
DeclNameRef LogScopeEntryExtendedName;
DeclNameRef SendDataName;

struct BracePair {
Expand Down Expand Up @@ -126,12 +131,17 @@ class Instrumenter : InstrumenterBase {
unsigned &TmpNameIndex)
: InstrumenterBase(C, DC), RNG(RNG), TmpNameIndex(TmpNameIndex),
HighPerformance(HP),
ExtendedCallbacks(C.LangOpts.hasFeature(Feature::PlaygroundExtendedCallbacks)),
DebugPrintName(C.getIdentifier("__builtin_debugPrint")),
PrintName(C.getIdentifier("__builtin_print")),
PostPrintName(C.getIdentifier("__builtin_postPrint")),
PostPrintExtendedName(C.getIdentifier("__builtin_postPrint_extended")),
LogWithIDName(C.getIdentifier("__builtin_log_with_id")),
LogWithIDExtendedName(C.getIdentifier("__builtin_log_with_id_extended")),
LogScopeExitName(C.getIdentifier("__builtin_log_scope_exit")),
LogScopeExitExtendedName(C.getIdentifier("__builtin_log_scope_exit_extended")),
LogScopeEntryName(C.getIdentifier("__builtin_log_scope_entry")),
LogScopeEntryExtendedName(C.getIdentifier("__builtin_log_scope_entry_extended")),
SendDataName(C.getIdentifier("__builtin_send_data")) { }

Stmt *transformStmt(Stmt *S) {
Expand Down Expand Up @@ -736,7 +746,7 @@ class Instrumenter : InstrumenterBase {
}

Added<Stmt *> logPostPrint(SourceRange SR) {
return buildLoggerCallWithArgs(PostPrintName, {}, SR);
return buildLoggerCallWithArgs(ExtendedCallbacks ? PostPrintExtendedName : PostPrintName, {}, SR);
}

std::pair<PatternBindingDecl *, VarDecl *>
Expand Down Expand Up @@ -776,15 +786,15 @@ class Instrumenter : InstrumenterBase {
const unsigned id_num = Distribution(RNG);
Expr *IDExpr = IntegerLiteralExpr::createFromUnsigned(Context, id_num, SourceLoc());

return buildLoggerCallWithArgs(LogWithIDName, { *E, NameExpr, IDExpr }, SR);
return buildLoggerCallWithArgs(ExtendedCallbacks ? LogWithIDExtendedName : LogWithIDName, { *E, NameExpr, IDExpr }, SR);
}

Added<Stmt *> buildScopeEntry(SourceRange SR) {
return buildLoggerCallWithArgs(LogScopeEntryName, {}, SR);
return buildLoggerCallWithArgs(ExtendedCallbacks ? LogScopeEntryExtendedName : LogScopeEntryName, {}, SR);
}

Added<Stmt *> buildScopeExit(SourceRange SR) {
return buildLoggerCallWithArgs(LogScopeExitName, {}, SR);
return buildLoggerCallWithArgs(ExtendedCallbacks ? LogScopeExitExtendedName : LogScopeExitName, {}, SR);
}

Added<Stmt *> buildLoggerCallWithArgs(DeclNameRef LoggerName,
Expand Down Expand Up @@ -813,14 +823,28 @@ class Instrumenter : InstrumenterBase {

llvm::SmallVector<Expr *, 6> ArgsWithSourceRange(Args.begin(), Args.end());

ArgsWithSourceRange.append(
{StartLine, EndLine, StartColumn, EndColumn, ModuleExpr, FileExpr});

UnresolvedDeclRefExpr *LoggerRef = new (Context)
UnresolvedDeclRefExpr(LoggerName, DeclRefKind::Ordinary,
DeclNameLoc(SR.End));
LoggerRef->setImplicit(true);

if (ExtendedCallbacks) {
StringRef filePath = Context.SourceMgr.getDisplayNameForLoc(SR.Start);

Expr *FilePathExpr = new (Context) StringLiteralExpr(
Context.AllocateCopy(filePath), SourceRange(), /*implicit=*/true);

std::string moduleName = std::string(TypeCheckDC->getParentModule()->getName());
Expr *ModuleNameExpr = new (Context) StringLiteralExpr(
Context.AllocateCopy(moduleName), SourceRange(), /*implicit=*/true);

ArgsWithSourceRange.append(
{StartLine, EndLine, StartColumn, EndColumn, ModuleNameExpr, FilePathExpr});
} else {
ArgsWithSourceRange.append(
{StartLine, EndLine, StartColumn, EndColumn, ModuleExpr, FileExpr});
}

auto *ArgList =
ArgumentList::forImplicitUnlabeled(Context, ArgsWithSourceRange);
ApplyExpr *LoggerCall =
Expand Down
24 changes: 24 additions & 0 deletions test/PlaygroundTransform/Inputs/PlaygroundsRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ struct ModuleFileIdentifier {
class LogRecord {
let text : String

init(api : String, object : Any, name : String, id : Int, range : SourceRange, moduleName : String, filePath : String) {
var object_description : String = ""
print(object, terminator: "", to: &object_description)
text = range.text + " " + api + "[" + name + "='" + object_description + "'" + " module: " + moduleName + ". file: " + filePath + "]"
}
init(api : String, object : Any, name : String, id : Int, range : SourceRange, moduleFileId : ModuleFileIdentifier) {
var object_description : String = ""
print(object, terminator: "", to: &object_description)
Expand All @@ -38,6 +43,9 @@ class LogRecord {
print(object, terminator: "", to: &object_description)
text = moduleFileId.text + " " + range.text + " " + api + "['" + object_description + "']"
}
init(api : String, range : SourceRange, moduleName : String, filePath: String) {
text = range.text + " " + api + " " + " module: " + moduleName + ". file: " + filePath
}
init(api: String, range : SourceRange, moduleFileId : ModuleFileIdentifier) {
text = moduleFileId.text + " " + range.text + " " + api
}
Expand All @@ -53,21 +61,37 @@ public func __builtin_log_with_id<T>(_ object : T, _ name : String, _ id : Int,
return LogRecord(api:"__builtin_log", object:object, name:name, id:id, range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleFileId:moduleFileId)
}

public func __builtin_log_with_id_extended<T>(_ object: T, _ name: String, _ id: Int, _ sl: Int, _ el: Int, _ sc: Int, _ ec: Int, _ moduleName: String, _ filePath: String) -> AnyObject? {
return LogRecord(api:"__builtin_log_with_id_extended", object:object, name:name, id:id, range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleName:moduleName, filePath:filePath)
}

public func __builtin_log_scope_entry(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleId : Int, _ fileId : Int) -> AnyObject? {
let moduleFileId = ModuleFileIdentifier(moduleId:moduleId, fileId:fileId)
return LogRecord(api:"__builtin_log_scope_entry", range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleFileId:moduleFileId)
}

public func __builtin_log_scope_entry_extended(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleName: String, _ filePath: String) -> AnyObject? {
return LogRecord(api:"__builtin_log_scope_entry_extended", range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleName:moduleName, filePath:filePath)
}

public func __builtin_log_scope_exit(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleId : Int, _ fileId : Int) -> AnyObject? {
let moduleFileId = ModuleFileIdentifier(moduleId:moduleId, fileId:fileId)
return LogRecord(api:"__builtin_log_scope_exit", range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleFileId:moduleFileId)
}

public func __builtin_log_scope_exit_extended(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleName: String, _ filePath: String) -> AnyObject? {
return LogRecord(api:"__builtin_log_scope_exit_extended", range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleName:moduleName, filePath:filePath)
}

public func __builtin_postPrint(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleId : Int, _ fileId : Int) -> AnyObject? {
let moduleFileId = ModuleFileIdentifier(moduleId:moduleId, fileId:fileId)
return LogRecord(api:"__builtin_postPrint", range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleFileId:moduleFileId)
}

public func __builtin_postPrint_extended(_ sl : Int, _ el : Int, _ sc : Int, _ ec: Int, _ moduleName: String, _ filePath: String) -> AnyObject? {
return LogRecord(api:"__builtin_postPrint_extended", range:SourceRange(sl:sl, el:el, sc:sc, ec:ec), moduleName:moduleName, filePath:filePath)
}

public func __builtin_send_data(_ object:AnyObject?) {
print((object as! LogRecord).text)
}
Expand Down
46 changes: 46 additions & 0 deletions test/PlaygroundTransform/extended_callbacks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// RUN: %empty-directory(%t)
// RUN: cp %s %t/main.swift
// RUN: %target-build-swift -whole-module-optimization -module-name PlaygroundSupport -emit-module-path %t/PlaygroundSupport.swiftmodule -parse-as-library -c -o %t/PlaygroundSupport.o %S/Inputs/SilentPCMacroRuntime.swift %S/Inputs/PlaygroundsRuntime.swift
// RUN: %target-build-swift -Xfrontend -playground -Xfrontend -playground-high-performance -enable-experimental-feature PlaygroundExtendedCallbacks -o %t/main -I=%t %t/PlaygroundSupport.o %t/main.swift
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s
// RUN: %target-build-swift -Xfrontend -pc-macro -Xfrontend -playground -Xfrontend -playground-high-performance -enable-experimental-feature PlaygroundExtendedCallbacks -o %t/main2 -I=%t %t/PlaygroundSupport.o %t/main.swift
// RUN: %target-codesign %t/main2
// RUN: %target-run %t/main2 | %FileCheck %s
// REQUIRES: executable_test

import PlaygroundSupport

var a = true
if (a) {
5
} else {
7
}

for i in 0..<3 {
i
}
// CHECK: [{{.*}}] __builtin_log_with_id_extended[a='true' module: main{{.*}}. file: {{.*/main[\d]?.swift}}]
// CHECK-NEXT: [{{.*}}] __builtin_log_with_id_extended[='5' module: main{{.*}}. file: {{.*/main[\d]?.swift}}]
// CHECK-NEXT: [{{.*}}] __builtin_log_with_id_extended[='0' module: main{{.*}}. file: {{.*/main[\d]?.swift}}]
// CHECK-NEXT: [{{.*}}] __builtin_log_with_id_extended[='1' module: main{{.*}}. file: {{.*/main[\d]?.swift}}]
// CHECK-NEXT: [{{.*}}] __builtin_log_with_id_extended[='2' module: main{{.*}}. file: {{.*/main[\d]?.swift}}]

var b = true
for i in 0..<3 {
i
continue
}
// CHECK-NEXT: [{{.*}}] __builtin_log_with_id_extended[b='true' module: main{{.*}}. file: {{.*/main[\d]?.swift}}]
// CHECK-NEXT: [{{.*}}] __builtin_log_with_id_extended[='0' module: main{{.*}}. file: {{.*/main[\d]?.swift}}]
// CHECK-NEXT: [{{.*}}] __builtin_log_with_id_extended[='1' module: main{{.*}}. file: {{.*/main[\d]?.swift}}]
// CHECK-NEXT: [{{.*}}] __builtin_log_with_id_extended[='2' module: main{{.*}}. file: {{.*/main[\d]?.swift}}]

var c = true
for i in 0..<3 {
i
break
}
// CHECK-NEXT: [{{.*}}] __builtin_log_with_id_extended[c='true' module: main{{.*}}. file: {{.*/main[\d]?.swift}}]
// CHECK-NEXT: [{{.*}}] __builtin_log_with_id_extended[='0' module: main{{.*}}. file: {{.*/main[\d]?.swift}}]