Skip to content
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

Add optimization option --fcombine-load-threshold=XXX #77

Merged
merged 5 commits into from
Apr 11, 2018
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/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ namespace vc4c
* Whether moving of constants to out side of loops
*/
bool moveConstants = true;
/*
* The threshold for combineLoadingLiterals
*/
int combineLoadingLiteralsThreshold = 6;
};

/*
Expand Down
43 changes: 41 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,41 @@ static void printInfo()
std::cout << vc4c::to_string<std::string>(infoString, "; ") << std::endl;
}

/*
* parse options with parameter like xxx=n
* if invalid parameter are passed, raise an exception
*/
Optional<int> parseIntOption(std::string name, std::string input)
{
std::stringstream ss(input);
std::string buffer;
std::getline(ss, buffer, '=');
if(buffer == name)
{
// XXX if input doesn't include '=', `ss` already reached the end.
// Then, the second `std::getline` do nothing.
// To detect that checking `name` == `buffer` is required.
std::getline(ss, buffer, '=');
if(name == buffer)
{
std::string err = "option parse error: parameter of integer expected in " + name;
throw CompilationError(CompilationStep::PRECOMPILATION, err);
}
try
{
auto s = std::stoi(buffer);
return Optional<int>(s);
}
catch(const std::invalid_argument& e)
{
std::string err = "option parse error: parameter of integer expected in " + name + ", but " + buffer;
throw CompilationError(CompilationStep::PRECOMPILATION, err);
}
}

return {};
}

/*
*
*/
Expand Down Expand Up @@ -178,9 +213,9 @@ int main(int argc, char** argv)
config.frontend = Frontend::LLVM_IR;
else if(strcmp("--disassemble", argv[i]) == 0)
runDisassembler = true;
else if(strcmp("--fmoveconstants", argv[i]) == 0)
else if(strcmp("--fmove-constants", argv[i]) == 0)
config.moveConstants = true;
else if(strcmp("--fnomoveconstants", argv[i]) == 0)
else if(strcmp("--fnomove-constants", argv[i]) == 0)
config.moveConstants = false;
else if(strcmp("-o", argv[i]) == 0)
{
Expand All @@ -189,6 +224,10 @@ int main(int argc, char** argv)
i += 2;
break;
}
else if(auto opt = parseIntOption("--fcombine-load-threshold", argv[i]))
{
config.combineLoadingLiteralsThreshold = opt.value();
}
else
options.append(argv[i]).append(" ");
}
Expand Down
9 changes: 5 additions & 4 deletions src/optimization/Combiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,10 @@ static Optional<Literal> getSourceLiteral(InstructionWalker it)
return {};
}

static bool canReplaceLiteralLoad(InstructionWalker it, const InstructionWalker start, const InstructionWalker match)
static bool canReplaceLiteralLoad(InstructionWalker it, const InstructionWalker start, const InstructionWalker match,
std::size_t stepsLeft = ACCUMULATOR_THRESHOLD_HINT)
{
std::size_t stepsLeft = ACCUMULATOR_THRESHOLD_HINT;
InstructionWalker pos = it;
InstructionWalker pos = it.copy();
// check whether the instruction last loading the same literal is at most ACCUMULATOR_THRESHOLD_HINT instructions
// before this one
while(stepsLeft > 0 && !pos.isStartOfBlock() && pos != start)
Expand Down Expand Up @@ -620,7 +620,8 @@ void optimizations::combineLoadingLiterals(const Module& module, Method& method,
if(literal)
{
if(lastLoadImmediate.find(literal->unsignedInt()) != lastLoadImmediate.end() &&
canReplaceLiteralLoad(it, block.begin(), lastLoadImmediate.at(literal->unsignedInt())))
canReplaceLiteralLoad(it, block.begin(), lastLoadImmediate.at(literal->unsignedInt()),
config.combineLoadingLiteralsThreshold))
{
Local* oldLocal = it->getOutput()->local;
Local* newLocal = lastLoadImmediate.at(literal->unsignedInt())->getOutput()->local;
Expand Down