Builder tool - Load curves and configure optimizer

This commit is contained in:
Emmanuel BENOîT 2017-12-03 10:22:36 +01:00
parent 68ca08ceba
commit dcd149eee5
2 changed files with 96 additions and 53 deletions

View file

@ -100,8 +100,18 @@ T_SRDParserConfig BCLInitDefinitions_( ) noexcept
<< ( Alt( ) << "chooser"
<< ( AtLeast( 1 ) << ( List( ) << Int32( ) << Int32( ) ) ) )
<< BCLResolutions_ )
<< ( Rule( ) << "optimizer" << "off" )
<< ( Rule( ) << "optimizer" << "on" << EnterContext( "optim" ) )
<< ( Rule( ) << "optimizer" << "off"
<< []( T_SRDParserData const& d ) -> bool {
auto& bCfg{ d.currentData->value< T_BuildConfiguration >( ) };
bCfg.optimize = false;
return true;
} )
<< ( Rule( ) << "optimizer" << "on" << EnterContext( "optim" )
<< []( T_SRDParserData const& d ) -> bool {
auto& bCfg{ d.currentData->value< T_BuildConfiguration >( ) };
bCfg.optimize = true;
return true;
} )
;
defs.context( "optim" )
@ -128,14 +138,16 @@ T_SRDParserConfig BCLInitDefinitions_( ) noexcept
defs.context( "opt-cf" )
<< ( Rule( ) << "fixed-resolution" << Enum( "on-off" )
<< []( T_SRDParserData const& d ) -> bool {
auto const& in{ *d.input };
auto& bCfg{ d.currentData->value< T_BuildConfiguration >( ) };
bCfg.cfFixedSize = true;
bCfg.cfFixedSize = ( in[ 1 ].stringValue( ) == "on" );
return true;
} )
<< ( Rule( ) << "inputs" << Enum( "on-off" )
<< []( T_SRDParserData const& d ) -> bool {
auto const& in{ *d.input };
auto& bCfg{ d.currentData->value< T_BuildConfiguration >( ) };
bCfg.cfInputs = true;
bCfg.cfInputs = ( in[ 1 ].stringValue( ) == "on" );
return true;
} )
;

View file

@ -29,7 +29,7 @@ const struct option CmdLineOpts_[] = {
void PrintStreamError(
char const* const prefix ,
T_String const& name ,
X_StreamError const& error )
X_StreamError const& error ) noexcept
{
T_StringBuilder sb;
sb << prefix << " '" << name << "': " << error.what( );
@ -42,7 +42,7 @@ void PrintStreamError(
void WriteSRDError(
T_StringBuilder& sb ,
T_SRDError const& error )
T_SRDError const& error ) noexcept
{
switch ( error.type( ) ) {
case E_SRDErrorType::ERROR:
@ -58,6 +58,35 @@ void WriteSRDError(
sb << '(' << error.location( ) << ")] " << error.error( ) << '\n';
}
void WriteSRDErrors(
char const* const title ,
T_SRDErrors const& errors ) noexcept
{
T_StringBuilder sb;
sb << "===== " << title << '\n';
const auto nErrors( errors.size( ) );
for ( auto i = 0u ; i < nErrors ; i ++ ) {
WriteSRDError( sb , errors[ i ] );
}
sb << '\0';
fprintf( stderr , "%s" , sb.data( ) );
}
T_String FilePath(
T_String const& path ,
char const* const name ) noexcept
{
T_StringBuilder sb;
if ( path ) {
sb << path;
if ( !path.endsWith( "/" ) ) {
sb << '/';
}
}
sb << name;
return T_String{ std::move( sb ) };
}
/*============================================================================*/
@ -108,17 +137,8 @@ int main( int argc , char** argv )
} };
// Build configurations
const T_String bcfgFile( [&]() {
T_StringBuilder sb;
if ( oSrcPath ) {
sb << oSrcPath;
if ( !oSrcPath.endsWith( "/" ) ) {
sb << '/';
}
}
sb << "build.srd";
return T_String{ std::move( sb ) };
}( ) );
logger( [](){ return T_StringBuilder{ "Loading build configurations" }; } , 1 );
const T_String bcfgFile{ FilePath( oSrcPath , "build.srd" ) };
const T_BuildConfiguration cfg{ [&]() {
try {
T_BuildConfigurationLoader bcfgLoader;
@ -142,30 +162,13 @@ int main( int argc , char** argv )
exit( EXIT_FAILURE );
} catch ( X_SRDErrors const& e ) {
T_StringBuilder sb;
sb << "===== BUILD CONFIGURATIONS\n";
const auto nErrors( e.errors.size( ) );
for ( auto i = 0u ; i < nErrors ; i ++ ) {
WriteSRDError( sb , e.errors[ i ] );
}
sb << '\0';
fprintf( stderr , "%s" , sb.data( ) );
WriteSRDErrors( "BUILD CONFIGURATIONS" , e.errors );
exit( EXIT_FAILURE );
}
} () };
// Open file
const T_String inputName( [&]() {
T_StringBuilder sb;
if ( oSrcPath ) {
sb << oSrcPath;
if ( !oSrcPath.endsWith( "/" ) ) {
sb << '/';
}
}
sb << "demo.srd";
return T_String{ std::move( sb ) };
}( ) );
const T_String inputName{ FilePath( oSrcPath , "demo.srd" ) };
T_File input( inputName , E_FileMode::READ_ONLY );
try {
input.open( );
@ -174,7 +177,23 @@ int main( int argc , char** argv )
return 1;
}
// Load SRD data
// Load curves
logger( [](){ return T_StringBuilder{ "Loading curves data" }; } , 1 );
const T_String curvesFile{ FilePath( oSrcPath , "curves.srd" ) };
T_SyncCurvesIO::T_Data p;
try {
p = T_SyncCurvesIO{}.load( curvesFile );
} catch ( ebcl::X_StreamError const& e ) {
fprintf( stderr , "===== CURVES DATA\n" );
PrintStreamError( "Could not open" , curvesFile , e );
exit( EXIT_FAILURE );
} catch ( ebcl::X_SRDErrors const& e ) {
WriteSRDErrors( "CURVES DATA" , e.errors );
exit( EXIT_FAILURE );
}
// Load script
T_SRDMemoryTarget srdOut;
srdOut.clearComments( true ).clearFlushToken( true );
try {
@ -182,17 +201,12 @@ int main( int argc , char** argv )
T_FileInputStream fis{ input };
srdReader.read( inputName , fis );
} catch ( X_StreamError const& e ) {
fprintf( stderr , "===== SCRIPT\n" );
PrintStreamError( "Could not open" , inputName , e );
return 1;
} catch ( X_SRDErrors const& e ) {
T_StringBuilder sb;
const auto nErrors( e.errors.size( ) );
for ( auto i = 0u ; i < nErrors ; i ++ ) {
WriteSRDError( sb , e.errors[ i ] );
}
sb << "No parsing happened due to format errors\n" << '\0';
fprintf( stderr , "%s" , sb.data( ) );
return 2;
WriteSRDErrors( "SCRIPT" , e.errors );
exit( EXIT_FAILURE );
}
// Parse
@ -210,14 +224,31 @@ int main( int argc , char** argv )
auto parsed{ parser.result( ) };
// Optimize
opopt::T_OptData od;
od.logger = logger;
//od.fixedSize = std::make_pair( 1280u , 720u );
bool doneStuff{ false };
do {
doneStuff = opopt::FoldConstants( *parsed , od );
doneStuff = opopt::RemoveDeadCode( *parsed , od ) || doneStuff;
} while ( doneStuff );
if ( cfg.optimize ) {
opopt::T_OptData od;
od.logger = logger;
if ( !cfg.resolutionChooser && cfg.cfFixedSize ) {
od.fixedSize = cfg.resolutions[ 0 ]; // FIXME
}
if ( cfg.cfInputs ) {
od.curves = &p.curves;
}
bool doneStuff;
do {
doneStuff = false;
if ( cfg.constantFolding ) {
while ( opopt::FoldConstants( *parsed , od ) ) {
doneStuff = true;
}
}
if ( cfg.deadCodeElimination ) {
while ( opopt::RemoveDeadCode( *parsed , od ) ) {
doneStuff = true;
}
}
} while ( doneStuff );
}
// Compile
T_OpsCompiler compiler;