Skip to content

[NFC] Add a flag to print a function's ast before SILGen #82548

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 1 commit into from
Jun 27, 2025
Merged
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
25 changes: 25 additions & 0 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
using namespace swift;
using namespace Lowering;

llvm::cl::list<std::string> PrintFunctionAST(
"print-function-ast", llvm::cl::CommaSeparated,
llvm::cl::desc("Only print out the ast for this function"));

//===----------------------------------------------------------------------===//
// SILGenModule Class implementation
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -879,6 +883,22 @@ void SILGenModule::visit(Decl *D) {
ASTVisitor::visit(D);
}

static bool isInPrintFunctionList(AbstractFunctionDecl *fd) {
if (PrintFunctionAST.empty()) {
return false;
}
auto fnName = SILDeclRef(fd).mangle();
for (const std::string &printFnName : PrintFunctionAST) {
if (printFnName == fnName)
return true;
if (!printFnName.empty() && printFnName[0] != '$' && !fnName.empty() &&
fnName[0] == '$' && printFnName == fnName.substr(1)) {
return true;
}
}
return false;
}

void SILGenModule::visitFuncDecl(FuncDecl *fd) { emitFunction(fd); }

void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
Expand Down Expand Up @@ -1477,6 +1497,11 @@ void SILGenModule::emitDifferentiabilityWitness(
}

void SILGenModule::emitAbstractFuncDecl(AbstractFunctionDecl *AFD) {
if (isInPrintFunctionList(AFD)) {
auto &out = llvm::errs();
AFD->dump(out);
}

// Emit default arguments and property wrapper initializers.
emitArgumentGenerators(AFD, AFD->getParameters());

Expand Down