Skip to content

Commit

Permalink
Add debug function to print instrumentation points
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaimov committed Dec 19, 2024
1 parent 37fee15 commit 8ae8087
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/salt_instrument_flang_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ limitations under the License.
#include <optional>
#include <tuple>
#include <regex>
#include <algorithm>


#define RYML_SINGLE_HDR_DEFINE_NOW
Expand Down Expand Up @@ -73,7 +74,7 @@ class SaltInstrumentAction final : public PluginParseTreeAction {
RETURN_STMT // Stop timer on the line before
};

typedef std::map<SaltInstrumentationPointType, const std::string> InstrumentationMap;
using InstrumentationMap = std::map<SaltInstrumentationPointType, const std::string>;

struct SaltInstrumentationPoint {
SaltInstrumentationPoint(const SaltInstrumentationPointType instrumentation_point_type,
Expand All @@ -92,11 +93,38 @@ class SaltInstrumentAction final : public PluginParseTreeAction {

bool operator<(const SaltInstrumentationPoint &other) const {
if (startLine == other.startLine) {
return instrumentBefore();
if (instrumentBefore() && !other.instrumentBefore()) {
return true;
}
return false;
}
return startLine < other.startLine;
}

[[nodiscard]] std::string typeString() const {
switch (instrumentationPointType) {
case SaltInstrumentationPointType::PROGRAM_BEGIN:
return "PROGRAM_BEGIN"s;
case SaltInstrumentationPointType::PROCEDURE_BEGIN:
return "PROCEDURE_BEGIN"s;
case SaltInstrumentationPointType::PROCEDURE_END:
return "PROCEDURE_END"s;
case SaltInstrumentationPointType::RETURN_STMT:
return "RETURN_STMT"s;
default:
CRASH_NO_CASE;
}
}

[[nodiscard]] std::string toString() const {
std::stringstream ss;
ss << startLine << "\t";
ss << (instrumentBefore() ? "before" : "after") << "\t";
ss << typeString() << "\t";
ss << "\"" << timerName.value_or("<no name>") << "\"";
return ss.str();
}

SaltInstrumentationPointType instrumentationPointType;
int startLine;
std::optional<std::string> timerName;
Expand Down Expand Up @@ -125,6 +153,14 @@ class SaltInstrumentAction final : public PluginParseTreeAction {
return instrumentationPoints_;
}

[[nodiscard]] std::string dumpInstrumentationPoints() const {
std::stringstream ss;
for (const auto & instPt : getInstrumentationPoints()) {
ss << instPt.toString() << "\n";
}
return ss.str();
}

/**
* From a CharBlock object (generally held in the `source` field of a parse tree node,
* get the source position (file, line, column).
Expand Down Expand Up @@ -558,7 +594,7 @@ class SaltInstrumentAction final : public PluginParseTreeAction {

const std::string timerName{ss.str()};

// Split the timername string so that it will fit between Fortran 77's 72 character limit,
// Split the timerName string so that it will fit between Fortran 77's 72-character limit,
// and use character string line continuation syntax compatible with Fortran 77 and modern
// Fortran.
std::stringstream ss2;
Expand Down Expand Up @@ -659,6 +695,8 @@ class SaltInstrumentAction final : public PluginParseTreeAction {
int lineNum{0};
const auto &instPts{visitor.getInstrumentationPoints()};

llvm::outs() << "Will perform instrumentation:\n" << visitor.dumpInstrumentationPoints();

// Sanity check: are instrumentation points in the right order?
if (!std::is_sorted(instPts.cbegin(), instPts.cend())) {
DIE("ERROR: Instrumentation points not sorted by line number!\n");
Expand Down

0 comments on commit 8ae8087

Please sign in to comment.