Skip to content

Commit a240aa7

Browse files
authored
Introduce CLI option to purebench to print compiled and optimized AST (#6693)
1 parent a890c83 commit a240aa7

File tree

7 files changed

+54
-3
lines changed

7 files changed

+54
-3
lines changed

ydb/library/yql/public/purecalc/common/interface.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ TProgramFactoryOptions::TProgramFactoryOptions()
2828
, UserData_()
2929
, LLVMSettings("OFF")
3030
, BlockEngineSettings("disable")
31+
, ExprOutputStream(nullptr)
3132
, CountersProvider(nullptr)
3233
, NativeYtTypeFlags(0)
3334
, UseSystemColumns(false)
@@ -83,6 +84,11 @@ TProgramFactoryOptions& TProgramFactoryOptions::SetBlockEngineSettings(TStringBu
8384
return *this;
8485
}
8586

87+
TProgramFactoryOptions& TProgramFactoryOptions::SetExprOutputStream(IOutputStream* exprOutputStream) {
88+
ExprOutputStream = exprOutputStream;
89+
return *this;
90+
}
91+
8692
TProgramFactoryOptions& TProgramFactoryOptions::SetCountersProvider(NKikimr::NUdf::ICountersProvider* countersProvider) {
8793
CountersProvider = countersProvider;
8894
return *this;

ydb/library/yql/public/purecalc/common/interface.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ namespace NYql {
250250
/// decision to the platform heuristics.
251251
TString BlockEngineSettings;
252252

253+
/// Output stream to dump the compiled and optimized expressions.
254+
IOutputStream* ExprOutputStream;
255+
253256
/// Provider for generic counters which can be used to export statistics from UDFs.
254257
NKikimr::NUdf::ICountersProvider* CountersProvider;
255258

@@ -323,6 +326,13 @@ namespace NYql {
323326
*/
324327
TProgramFactoryOptions& SetBlockEngineSettings(TStringBuf blockEngineSettings);
325328

329+
/**
330+
* Set the stream to dump the compiled and optimized expressions.
331+
*
332+
* @return reference to self, to allow method chaining.
333+
*/
334+
TProgramFactoryOptions& SetExprOutputStream(IOutputStream* exprOutputStream);
335+
326336
/**
327337
* Set new counters provider. Passed pointer should stay alive for as long as the processor factory
328338
* stays alive.

ydb/library/yql/public/purecalc/common/program_factory.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ using namespace NYql::NPureCalc;
1010

1111
TProgramFactory::TProgramFactory(const TProgramFactoryOptions& options)
1212
: Options_(options)
13+
, ExprOutputStream_(Options_.ExprOutputStream)
1314
, CountersProvider_(nullptr)
1415
{
1516
EnsureLoggingInitialized();
@@ -83,6 +84,7 @@ IPullStreamWorkerFactoryPtr TProgramFactory::MakePullStreamWorkerFactory(
8384
Modules_,
8485
Options_.LLVMSettings,
8586
BlockEngineMode_,
87+
ExprOutputStream_,
8688
CountersProvider_,
8789
mode,
8890
syntaxVersion,
@@ -111,6 +113,7 @@ IPullListWorkerFactoryPtr TProgramFactory::MakePullListWorkerFactory(
111113
Modules_,
112114
Options_.LLVMSettings,
113115
BlockEngineMode_,
116+
ExprOutputStream_,
114117
CountersProvider_,
115118
mode,
116119
syntaxVersion,
@@ -143,6 +146,7 @@ IPushStreamWorkerFactoryPtr TProgramFactory::MakePushStreamWorkerFactory(
143146
Modules_,
144147
Options_.LLVMSettings,
145148
BlockEngineMode_,
149+
ExprOutputStream_,
146150
CountersProvider_,
147151
mode,
148152
syntaxVersion,

ydb/library/yql/public/purecalc/common/program_factory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace NYql {
2424
IModuleResolver::TPtr ModuleResolver_;
2525
TUserDataTable UserData_;
2626
EBlockEngineMode BlockEngineMode_;
27+
IOutputStream* ExprOutputStream_;
2728
THashMap<TString, TString> Modules_;
2829
NKikimr::NUdf::ICountersProvider* CountersProvider_;
2930

ydb/library/yql/public/purecalc/common/worker_factory.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ TWorkerFactory<TBase>::TWorkerFactory(TWorkerFactoryOptions options, EProcessorM
3939
, UserData_(std::move(options.UserData))
4040
, LLVMSettings_(std::move(options.LLVMSettings))
4141
, BlockEngineMode_(options.BlockEngineMode)
42+
, ExprOutputStream_(options.ExprOutputStream)
4243
, CountersProvider_(options.CountersProvider_)
4344
, NativeYtTypeFlags_(options.NativeYtTypeFlags_)
4445
, DeterministicTimeProviderSeed_(options.DeterministicTimeProviderSeed_)
@@ -304,9 +305,19 @@ TExprNode::TPtr TWorkerFactory<TBase>::Compile(
304305
ythrow TCompileError("", ExprContext_.IssueManager.GetIssues().ToString()) << "Failed to optimize";
305306
}
306307

307-
if (ETraceLevel::TRACE_DETAIL <= StdDbgLevel()) {
308-
Cdbg << "After optimization:" << Endl;
309-
ConvertToAst(*exprRoot, ExprContext_, 0, true).Root->PrettyPrintTo(Cdbg, TAstPrintFlags::PerLine | TAstPrintFlags::ShortQuote | TAstPrintFlags::AdaptArbitraryContent);
308+
IOutputStream* exprOut = nullptr;
309+
if (ExprOutputStream_) {
310+
exprOut = ExprOutputStream_;
311+
} else if (ETraceLevel::TRACE_DETAIL <= StdDbgLevel()) {
312+
exprOut = &Cdbg;
313+
}
314+
315+
if (exprOut) {
316+
*exprOut << "After optimization:" << Endl;
317+
ConvertToAst(*exprRoot, ExprContext_, 0, true).Root
318+
->PrettyPrintTo(*exprOut, TAstPrintFlags::PerLine
319+
| TAstPrintFlags::ShortQuote
320+
| TAstPrintFlags::AdaptArbitraryContent);
310321
}
311322
return exprRoot;
312323
}

ydb/library/yql/public/purecalc/common/worker_factory.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace NYql {
2424
const THashMap<TString, TString>& Modules;
2525
TString LLVMSettings;
2626
EBlockEngineMode BlockEngineMode;
27+
IOutputStream* ExprOutputStream;
2728
NKikimr::NUdf::ICountersProvider* CountersProvider_;
2829
ETranslationMode TranslationMode_;
2930
ui16 SyntaxVersion_;
@@ -43,6 +44,7 @@ namespace NYql {
4344
const THashMap<TString, TString>& Modules,
4445
TString LLVMSettings,
4546
EBlockEngineMode BlockEngineMode,
47+
IOutputStream* ExprOutputStream,
4648
NKikimr::NUdf::ICountersProvider* CountersProvider,
4749
ETranslationMode translationMode,
4850
ui16 syntaxVersion,
@@ -61,6 +63,7 @@ namespace NYql {
6163
, Modules(Modules)
6264
, LLVMSettings(std::move(LLVMSettings))
6365
, BlockEngineMode(BlockEngineMode)
66+
, ExprOutputStream(ExprOutputStream)
6467
, CountersProvider_(CountersProvider)
6568
, TranslationMode_(translationMode)
6669
, SyntaxVersion_(syntaxVersion)
@@ -90,6 +93,7 @@ namespace NYql {
9093
TVector<THashSet<TString>> UsedColumns_;
9194
TString LLVMSettings_;
9295
EBlockEngineMode BlockEngineMode_;
96+
IOutputStream* ExprOutputStream_;
9397
NKikimr::NUdf::ICountersProvider* CountersProvider_;
9498
ui64 NativeYtTypeFlags_;
9599
TMaybe<ui64> DeterministicTimeProviderSeed_;

ydb/library/yql/tools/purebench/purebench.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <library/cpp/yson/writer.h>
1414

1515
#include <util/datetime/cputimer.h>
16+
#include <util/stream/file.h>
1617
#include <util/stream/format.h>
1718
#include <util/stream/null.h>
1819

@@ -33,6 +34,7 @@ int Main(int argc, const char *argv[])
3334
TString udfsDir;
3435
TString LLVMSettings;
3536
TString blockEngineSettings;
37+
TString exprFile;
3638
opts.AddHelpOption();
3739
opts.AddLongOption("ndebug", "should be at first argument, do not show debug info in error output").NoArgument();
3840
opts.AddLongOption('b', "blocks-engine", "Block engine settings").StoreResult(&blockEngineSettings).DefaultValue("disable");
@@ -45,13 +47,26 @@ int Main(int argc, const char *argv[])
4547
opts.AddLongOption("pt", "use PG syntax for test query").NoArgument();
4648
opts.AddLongOption("udfs-dir", "directory with UDFs").StoreResult(&udfsDir).DefaultValue("");
4749
opts.AddLongOption("llvm-settings", "LLVM settings").StoreResult(&LLVMSettings).DefaultValue("");
50+
opts.AddLongOption("print-expr", "print rebuild AST before execution").NoArgument();
51+
opts.AddLongOption("expr-file", "print AST to that file instead of stdout").StoreResult(&exprFile);
4852
opts.SetFreeArgsMax(0);
4953
TOptsParseResult res(&opts, argc, argv);
5054

5155
auto factoryOptions = TProgramFactoryOptions();
5256
factoryOptions.SetUDFsDir(udfsDir);
5357
factoryOptions.SetLLVMSettings(LLVMSettings);
5458
factoryOptions.SetBlockEngineSettings(blockEngineSettings);
59+
60+
IOutputStream* exprOut = nullptr;
61+
THolder<TFixedBufferFileOutput> exprFileHolder;
62+
if (res.Has("print-expr")) {
63+
exprOut = &Cout;
64+
} else if (!exprFile.empty()) {
65+
exprFileHolder.Reset(new TFixedBufferFileOutput(exprFile));
66+
exprOut = exprFileHolder.Get();
67+
}
68+
factoryOptions.SetExprOutputStream(exprOut);
69+
5570
auto factory = MakeProgramFactory(factoryOptions);
5671

5772
NYT::TNode members{NYT::TNode::CreateList()};

0 commit comments

Comments
 (0)