Skip to content

Commit

Permalink
Merge branch 'salt-fm' into install-structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zbeekman committed Jan 6, 2025
2 parents afd4130 + 84a4158 commit 9812369
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ if(MLIR_FOUND AND Flang_FOUND)
list(TRANSFORM SALT_FLANG_PLUGIN_HEADER_FILES PREPEND "${CMAKE_SOURCE_DIR}/include/")

set(SALT_FLANG_PLUGIN_SRCS
selectfile.cpp
salt_instrument_flang_plugin.cpp
)
list(TRANSFORM SALT_FLANG_PLUGIN_SRCS PREPEND "${CMAKE_SOURCE_DIR}/src/")
Expand Down
2 changes: 1 addition & 1 deletion include/selectfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ extern std::list<std::string> fileincludelist;
extern std::list<std::string> fileexcludelist;

// void parseInstrumentationCommand(char *line, int lineno);
void processInstrumentationRequests(const char *fname);
bool processInstrumentationRequests(const char *fname);

#endif
73 changes: 53 additions & 20 deletions src/salt_instrument_flang_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ limitations under the License.
#include "flang/Parser/source.h"
#include "flang/Common/indirection.h"

#include "selectfile.hpp"

// TODO Split declarations into a separate header file.
// TODO Put debug output behind verbose flag

#define SALT_FORTRAN_CONFIG_FILE_VAR "SALT_FORTRAN_CONFIG_FILE"
#define SALT_FORTRAN_CONFIG_DEFAULT_PATH "config_files/tau_config.yaml"

#define SALT_FORTRAN_SELECT_FILE_VAR "SALT_FORTRAN_SELECT_FILE"

#define SALT_FORTRAN_KEY "Fortran"
#define SALT_FORTRAN_PROGRAM_BEGIN_KEY "program_insert"
#define SALT_FORTRAN_PROCEDURE_BEGIN_KEY "procedure_begin_insert"
Expand All @@ -68,11 +72,11 @@ using namespace Fortran::frontend;
*/
class SaltInstrumentAction final : public PluginParseTreeAction {
enum class SaltInstrumentationPointType {
PROGRAM_BEGIN, // Declare profiler, initialize TAU, set node, start timer
PROCEDURE_BEGIN, // Declare profiler, start timer
PROCEDURE_END, // Stop timer on the line after
RETURN_STMT, // Stop timer on the line before
IF_RETURN // Transform if to if-then-endif, stop timer before return
PROGRAM_BEGIN, // Declare profiler, initialize TAU, set node, start timer
PROCEDURE_BEGIN, // Declare profiler, start timer
PROCEDURE_END, // Stop timer on the line after
RETURN_STMT, // Stop timer on the line before
IF_RETURN // Transform if to if-then-endif, stop timer before return
};

using InstrumentationMap = std::map<SaltInstrumentationPointType, const std::string>;
Expand Down Expand Up @@ -219,7 +223,7 @@ class SaltInstrumentAction final : public PluginParseTreeAction {
// for examples of getting source position for a parse tree node

// Never descend into InterfaceSpecification nodes, they can't contain executable statements.
bool Pre(const Fortran::parser::InterfaceSpecification &) { return false; }
static bool Pre(const Fortran::parser::InterfaceSpecification &) { return false; }

bool Pre(const Fortran::parser::MainProgram &) {
isInMainProgram_ = true;
Expand Down Expand Up @@ -572,18 +576,21 @@ class SaltInstrumentAction final : public PluginParseTreeAction {
std::holds_alternative<Fortran::common::Indirection<
Fortran::parser::ReturnStmt> >(ifAction.statement.u)) {
const auto startPos{
locationFromSource(std::get<Fortran::parser::ScalarLogicalExpr>(ifStmt.t).thing.thing.value().source,
false).value()
locationFromSource(
std::get<Fortran::parser::ScalarLogicalExpr>(ifStmt.t).thing.thing.value().source,
false).value()
};
const auto endPos{
locationFromSource(std::get<Fortran::parser::ScalarLogicalExpr>(ifStmt.t).thing.thing.value().source,
true).value()
locationFromSource(
std::get<Fortran::parser::ScalarLogicalExpr>(ifStmt.t).thing.thing.value().source,
true).value()
};
llvm::outs() << "If-return, conditional: (" << startPos.line << "," << startPos.column << ") - "
<< "(" << endPos.line << "," << endPos.column << ")\n";
<< "(" << endPos.line << "," << endPos.column << ")\n";
// TODO this assumes that the conditional fits on one list
// make more robust, test with more cases
addInstrumentationPoint(SaltInstrumentationPointType::IF_RETURN, startPos.line, std::nullopt, endPos.column);
addInstrumentationPoint(SaltInstrumentationPointType::IF_RETURN, startPos.line, std::nullopt,
endPos.column);
}
return true;
}
Expand Down Expand Up @@ -744,7 +751,7 @@ class SaltInstrumentAction final : public PluginParseTreeAction {
}

auto instIter{instPts.cbegin()};
bool shouldOutputLine{true};
bool shouldOutputLine{};
while (std::getline(inputStream, line)) {
++lineNum;
shouldOutputLine = true;
Expand All @@ -756,13 +763,13 @@ class SaltInstrumentAction final : public PluginParseTreeAction {
// TODO handle multi-line
// TODO handle line continuation if too long
if (instIter->instrumentationPointType == SaltInstrumentationPointType::IF_RETURN) {
shouldOutputLine = false;
line.erase(instIter->conditionalColumn);
line.insert(instIter->conditionalColumn, " then");
outputStream << line << "\n";
outputStream << getInstrumentationPointString(*instIter, instMap) << "\n";
outputStream << " return\n";
outputStream << " endif\n";
shouldOutputLine = false;
line.erase(instIter->conditionalColumn);
line.insert(instIter->conditionalColumn, " then");
outputStream << line << "\n";
outputStream << getInstrumentationPointString(*instIter, instMap) << "\n";
outputStream << " return\n";
outputStream << " endif\n";
} else {
outputStream << getInstrumentationPointString(*instIter, instMap) << "\n";
}
Expand All @@ -789,6 +796,15 @@ class SaltInstrumentAction final : public PluginParseTreeAction {
return SALT_FORTRAN_CONFIG_DEFAULT_PATH;
}

[[nodiscard]] static std::optional<std::string> getSelectFilePath() {
if (const char *val = getenv(SALT_FORTRAN_SELECT_FILE_VAR)) {
if (std::string selectFile{val}; !selectFile.empty()) {
return selectFile;
}
}
return std::nullopt;
}

[[nodiscard]] static ryml::Tree getConfigYamlTree(const std::string &configPath) {
std::ifstream inputStream{configPath};
if (!inputStream) {
Expand Down Expand Up @@ -882,6 +898,23 @@ class SaltInstrumentAction final : public PluginParseTreeAction {
const ryml::Tree yamlTree = getConfigYamlTree(configPath);
const InstrumentationMap instMap = getInstrumentationMap(yamlTree);

if (const auto selectPath{getSelectFilePath()}; selectPath.has_value()) {
if (processInstrumentationRequests(selectPath->c_str())) {
const auto printStr = [&](const auto &a) { llvm::outs() << a << "\n"; };
llvm::outs() << "File include list:\n";
std::for_each(fileincludelist.cbegin(), fileincludelist.cend(), printStr);
llvm::outs() << "File exclude list:\n";
std::for_each(fileexcludelist.cbegin(), fileexcludelist.cend(), printStr);
llvm::outs() << "Include list:\n";
std::for_each(includelist.cbegin(), includelist.cend(), printStr);
llvm::outs() << "Exclude list:\n";
std::for_each(excludelist.cbegin(), excludelist.cend(), printStr);
} else {
llvm::errs() << "ERROR: Unable to read selective instrumentation file at " << selectPath << "\n";
std::exit(-4);
}
}

// Get the extension of the input file
// For input file 'filename.ext' we will output to 'filename.inst.Ext'
// Since we are adding preprocessor directives in the emitted code,
Expand Down
11 changes: 9 additions & 2 deletions src/selectfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include "selectfile.hpp"
#include "dprint.hpp"

// TODO support all selective instrumentation types
// TODO refactor this to class instead of using global variables
// TODO modernize C++

std::list<std::string> excludelist;
std::list<std::string> includelist;
std::list<std::string> fileincludelist;
Expand Down Expand Up @@ -684,7 +688,7 @@ void parseError(const char *message, char *line, int lineno, int column)

#define SALT_UNUSED(expr) do { (void)(expr); } while (0)

void processInstrumentationRequests(const char *fname)
bool processInstrumentationRequests(const char *fname)
{

std::ifstream input(fname);
Expand All @@ -695,7 +699,8 @@ void processInstrumentationRequests(const char *fname)


if (!input) {
std::cerr << "ERROR: Cannot open file: " << fname << std::endl;
std::cerr << "ERROR: Cannot open selective instrumentation file: " << fname << std::endl;
return false;
}


Expand Down Expand Up @@ -883,4 +888,6 @@ void processInstrumentationRequests(const char *fname)

DPRINT0("fileexcludelist\n");
dump_list(fileexcludelist);

return true;
}

0 comments on commit 9812369

Please sign in to comment.