Skip to content

Commit

Permalink
Introduce way to abort the compilation after the first dumped resource (
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer authored Feb 18, 2024
1 parent 93904cc commit 88a99f8
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .run/spice.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="run -O3 -d -ir ../../media/test-project/test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="build ../../media/test-project/test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<envs>
<env name="LLVM_LIB_DIR" value="D:/LLVM/build-release/lib" />
<env name="LLVM_INCLUDE_DIR" value="D:/LLVM/llvm/include" />
Expand Down
1 change: 1 addition & 0 deletions docs/docs/cli/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ You can apply following options to the `build` subcommand:
| `-s`, `-asm` | `--dump-assembly` | Dump Assembly code |
| `-b`, `-obj` | `--dump-object-file` | Dump object files |
| - | `--dump-to-files` | Redirect all dumps to files instead of printing them to the screen |
| - | `--abort-after-dump` | Abort the compilation process after dumping the first requested resource |
| `-j <n>` | `--jobs <n>` | Set number of jobs to parallelize compilation (default is auto) |
| `-t` | `--target` | Target triple for the emitted executable (for cross-compiling). <br> Format: `<arch><sub>-<vendor>-<sys>-<abi>` |
| `-o` | `--output` | Set path for executable output. |
Expand Down
1 change: 0 additions & 1 deletion docs/docs/cli/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ You can apply following options to the `install` subcommand:
| `-ir` | `--dump-ir` | Dump LLVM-IR |
| `-s`, `-asm` | `--dump-assembly` | Dump Assembly code |
| `-b`, `-obj` | `--dump-object-file` | Dump object files |
| - | `--dump-to-files` | Redirect all dumps to files instead of printing them to the screen |
| `-d` | `--debug-output` | Print compiler output for debugging. |
| `-j <n>` | `--jobs <n>` | Set number of jobs to parallelize compilation (Default is auto) |
| `-o` | `--output` | Set path for executable output. |
Expand Down
1 change: 0 additions & 1 deletion docs/docs/cli/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ You can apply following options to the `run` subcommand:
| `-ir` | `--dump-ir` | Dump LLVM-IR |
| `-s`, `-asm` | `--dump-assembly` | Dump Assembly code |
| `-b`, `-obj` | `--dump-object-file` | Dump object files |
| - | `--dump-to-files` | Redirect all dumps to files instead of printing them to the screen |
| `-j <n>` | `--jobs <n>` | Set number of jobs to parallelize compilation (default is auto) |
| `-o` | `--output` | Set path for executable output. |
| `-O<x>` | - | Set optimization level. <br> Valid options: `-O0`, `-O1`, `-O2`, `-O3`, `-Os`, `-Oz` |
Expand Down
34 changes: 33 additions & 1 deletion src/SourceFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <objectemitter/ObjectEmitter.h>
#include <symboltablebuilder/SymbolTable.h>
#include <symboltablebuilder/SymbolTableBuilder.h>
#include <typechecker/MacroDefs.h>
#include <typechecker/TypeChecker.h>
#include <util/CompilerWarning.h>
#include <util/FileUtil.h>
Expand Down Expand Up @@ -487,17 +488,26 @@ void SourceFile::concludeCompilation() {

void SourceFile::runFrontEnd() { // NOLINT(misc-no-recursion)
runLexer();
CHECK_ABORT_FLAG_V()
runParser();
CHECK_ABORT_FLAG_V()
runCSTVisualizer();
CHECK_ABORT_FLAG_V()
runASTBuilder();
CHECK_ABORT_FLAG_V()
runASTVisualizer();
CHECK_ABORT_FLAG_V()
runImportCollector();
CHECK_ABORT_FLAG_V()
runSymbolTableBuilder();
CHECK_ABORT_FLAG_V()
}

void SourceFile::runMiddleEnd() {
runTypeCheckerPre();
CHECK_ABORT_FLAG_V()
runTypeCheckerPost();
CHECK_ABORT_FLAG_V()
}

void SourceFile::runBackEnd() { // NOLINT(misc-no-recursion)
Expand All @@ -508,14 +518,20 @@ void SourceFile::runBackEnd() { // NOLINT(misc-no-recursion)
// Submit source file compilation to the task queue
resourceManager.threadPool.detach_task([&]() {
runIRGenerator();
CHECK_ABORT_FLAG_V()
if (resourceManager.cliOptions.useLTO) {
runPreLinkIROptimizer();
CHECK_ABORT_FLAG_V()
runBitcodeLinker();
CHECK_ABORT_FLAG_V()
runPostLinkIROptimizer();
CHECK_ABORT_FLAG_V()
} else {
runDefaultIROptimizer();
CHECK_ABORT_FLAG_V()
}
runObjectEmitter();
CHECK_ABORT_FLAG_V()
concludeCompilation();
});

Expand All @@ -525,6 +541,7 @@ void SourceFile::runBackEnd() { // NOLINT(misc-no-recursion)
if (mainFile) {
resourceManager.totalTimer.stop();
if (resourceManager.cliOptions.printDebugOutput) {
CHECK_ABORT_FLAG_V()
std::cout << "\nSuccessfully compiled " << std::to_string(resourceManager.sourceFiles.size()) << " source file(s).\n";
std::cout << "Total compile time: " << std::to_string(resourceManager.totalTimer.getDurationMilliseconds()) << " ms\n";
}
Expand Down Expand Up @@ -677,6 +694,17 @@ void SourceFile::dumpOutput(const std::string &content, const std::string &capti
// Dump to console
tout.println("\n" + caption + ":\n" + content);
}

// If the abort after dump is requested, set the abort compilation flag
if (resourceManager.cliOptions.dumpSettings.abortAfterDump) {
// If this is an IR dump whilst having optimization enabled, we may not abort when dumping unoptimized IR,
// because we also have to dump the optimized IR
if (resourceManager.cliOptions.dumpSettings.dumpIR && fileSuffix == "ir-code.ll") {
resourceManager.abortCompilation = resourceManager.cliOptions.optLevel == OptLevel::O0;
} else {
resourceManager.abortCompilation = true;
}
}
}

void SourceFile::visualizerPreamble(std::stringstream &output) const {
Expand All @@ -689,7 +717,7 @@ void SourceFile::visualizerPreamble(std::stringstream &output) const {

void SourceFile::visualizerOutput(std::string outputName, const std::string &output) const {
if (resourceManager.cliOptions.dumpSettings.dumpToFiles) {
// Check if the dot command exists
// Check if graphviz is installed
// GCOV_EXCL_START
if (FileUtil::isCommandAvailable("dot"))
throw CompilerError(IO_ERROR, "Please check if you have installed 'Graphviz Dot' and added it to the PATH variable");
Expand All @@ -713,6 +741,10 @@ void SourceFile::visualizerOutput(std::string outputName, const std::string &out
// Dump to console
std::cout << "\nSerialized " << outputName << ":\n\n" << output << "\n";
}

// If the abort after dump is requested, set the abort compilation flag
if (resourceManager.cliOptions.dumpSettings.abortAfterDump)
resourceManager.abortCompilation = true;
}

void SourceFile::printStatusMessage(const char *stage, const CompileStageIOType &in, const CompileStageIOType &out,
Expand Down
7 changes: 5 additions & 2 deletions src/driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ void Driver::addBuildSubcommand() {
subCmd->add_flag<bool>("--no-entry", cliOptions.noEntryFct, "Do not generate main function");
// --static
subCmd->add_flag<bool>("--static", cliOptions.staticLinking, "Link statically");
// --dump-to-files
subCmd->add_flag<bool>("--dump-to-files", cliOptions.dumpSettings.dumpToFiles, "Redirect dumps to files instead of printing");
// --abort-after-dump
subCmd->add_flag<bool>("--abort-after-dump", cliOptions.dumpSettings.abortAfterDump,
"Abort the compilation process after dumping the first requested resource");
}

/**
Expand Down Expand Up @@ -343,8 +348,6 @@ void Driver::addCompileSubcommandOptions(CLI::App *subCmd) {
subCmd->add_flag<bool>("--dump-assembly,-asm,-s", cliOptions.dumpSettings.dumpAssembly, "Dump Assembly code");
// --dump-object-file
subCmd->add_flag<bool>("--dump-object-file,-obj", cliOptions.dumpSettings.dumpObjectFile, "Dump object file");
// --dump-to-files
subCmd->add_flag<bool>("--dump-to-files", cliOptions.dumpSettings.dumpToFiles, "Redirect dumps to files instead of printing");

// Source file
subCmd->add_option<std::filesystem::path>("<main-source-file>", cliOptions.mainSourceFile, "Main source file")
Expand Down
1 change: 1 addition & 0 deletions src/driver/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct CliOptions {
bool dumpAssembly = false;
bool dumpObjectFile = false;
bool dumpToFiles = false;
bool abortAfterDump = false;
} dumpSettings;
bool namesForIRValues = false;
OptLevel optLevel = OptLevel::O0; // Default optimization level for debug build mode is O0
Expand Down
6 changes: 4 additions & 2 deletions src/exception/CompilerError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

namespace spice::compiler {

CompilerError::CompilerError(const CompilerErrorType &type, const std::string &message) {
CompilerError::CompilerError(const CompilerErrorType &type, const std::string &message) : type(type) {
errorMessage = "[Error|Compiler]:\n";
errorMessage += getMessagePrefix(type) + ": " + message;
}

CompilerError::CompilerError(const CodeLoc &codeLoc, const CompilerErrorType &type, const std::string &message) {
CompilerError::CompilerError(const CodeLoc &codeLoc, const CompilerErrorType &type, const std::string &message) : type(type) {
errorMessage = "[Error|Compiler] " + codeLoc.toPrettyString() + ":\n";
errorMessage += getMessagePrefix(type) + ": " + message;
}
Expand Down Expand Up @@ -61,6 +61,8 @@ std::string CompilerError::getMessagePrefix(CompilerErrorType type) {
return "Printf has null type";
case OOM:
return "An out of memory error occurred";
case ABORTED_BY_DUMP:
return "Aborted by dump";
case INVALID_FUNCTION:
return "Invalid function";
case INVALID_MODULE:
Expand Down
2 changes: 2 additions & 0 deletions src/exception/CompilerError.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum CompilerErrorType : uint8_t {
REFERENCED_UNDEFINED_FUNCTION_IR,
PRINTF_NULL_TYPE,
OOM,
ABORTED_BY_DUMP,
INVALID_FUNCTION,
INVALID_MODULE
};
Expand All @@ -46,6 +47,7 @@ class CompilerError : public std::exception {
[[nodiscard]] static std::string getMessagePrefix(CompilerErrorType errorType);

// Public members
CompilerErrorType type;
std::string errorMessage;
};

Expand Down
1 change: 1 addition & 0 deletions src/global/GlobalResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class GlobalResourceManager {
BS::thread_pool threadPool = BS::thread_pool(cliOptions.compileJobCount);
BS::synced_stream tout;
ErrorManager errorManager;
bool abortCompilation = false;

private:
// Private members
Expand Down
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <exception/ParserError.h>
#include <exception/SemanticError.h>
#include <global/GlobalResourceManager.h>
#include <typechecker/MacroDefs.h>
#include <util/FileUtil.h>

using namespace spice::compiler;
Expand All @@ -27,8 +28,11 @@ bool compileProject(CliOptions &cliOptions) {

// Run compile pipeline for main source file. All dependent source files are triggered by their parents
mainSourceFile->runFrontEnd();
CHECK_ABORT_FLAG_B()
mainSourceFile->runMiddleEnd();
CHECK_ABORT_FLAG_B()
mainSourceFile->runBackEnd();
CHECK_ABORT_FLAG_B()

// Link the target executable (link object files to executable)
resourceManager.linker.prepare();
Expand Down
7 changes: 7 additions & 0 deletions src/typechecker/MacroDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@
return nullptr; \
}

#define CHECK_ABORT_FLAG_V() \
if (resourceManager.abortCompilation) \
return;

#define CHECK_ABORT_FLAG_B() \
if (resourceManager.abortCompilation) \
return true;
#pragma warning(default : 4129)
1 change: 1 addition & 0 deletions test/TestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void execTestCase(const TestCase &testCase) {
/* dumpAssembly= */ false,
/* dumpObjectFile= */ false,
/* dumpToFiles= */ false,
/* abortAfterDump */ false,
},
/* namesForIRValues= */ true,
/* optLevel= */ OptLevel::O0,
Expand Down

0 comments on commit 88a99f8

Please sign in to comment.