Skip to content

Commit

Permalink
Add SALT_FORTRAN_VERBOSE env var
Browse files Browse the repository at this point in the history
Only outputs on error unless SALT_FORTRAN_VERBOSE is set to non-zero
value. Set SALT_FORTRAN_VERBOSE in tests so that check for output
succeeds.
  • Loading branch information
nchaimov committed Jan 16, 2025
1 parent 6630ef9 commit d0e1f94
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 31 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ if(MLIR_FOUND AND Flang_FOUND)
endif ()

set(SALT_FLANG_PLUGIN_HEADER_FILES
dprint.hpp
selectfile.hpp
flang_source_location.hpp
flang_instrumentation_constants.hpp
Expand All @@ -298,6 +299,7 @@ if(MLIR_FOUND AND Flang_FOUND)
list(TRANSFORM SALT_FLANG_PLUGIN_HEADER_FILES PREPEND "${CMAKE_SOURCE_DIR}/include/")

set(SALT_FLANG_PLUGIN_SRCS
dprint.cpp
selectfile.cpp
flang_source_location.cpp
flang_instrumentation_point.cpp
Expand Down Expand Up @@ -652,7 +654,7 @@ foreach(test_source IN LISTS FORTRAN_TESTS_SOURCES_LIST)
set_tests_properties(instrument_${test_source}
PROPERTIES
REQUIRED_FILES "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/fparse-llvm"
ENVIRONMENT "SALT_FORTRAN_CONFIG_FILE=${CMAKE_SOURCE_DIR}/config_files/tau_config.yaml"
ENVIRONMENT "SALT_FORTRAN_CONFIG_FILE=${CMAKE_SOURCE_DIR}/config_files/tau_config.yaml;SALT_FORTRAN_VERBOSE=1"
PASS_REGULAR_EXPRESSION "SALT Instrumentor Plugin finished"
)
endforeach()
Expand Down
12 changes: 12 additions & 0 deletions include/dprint.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#ifndef DPRINT_HPP
#define DPRINT_HPP

#include "llvm/Support/raw_ostream.h"

namespace salt {
void enableVerbose();

llvm::raw_ostream &verboseStream();
}

#ifdef DEBUG_NO_WAY
#define DPRINT(__fmt, ...) printf(__fmt, ##__VA_ARGS__)
#define DPRINT0(__fmt) printf(__fmt)
Expand All @@ -6,3 +17,4 @@
#define DPRINT0(__fmt)
#endif

#endif //DPRINT_HPP
3 changes: 3 additions & 0 deletions include/flang_instrumentation_constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ limitations under the License.
#ifndef FLANG_INSTRUMENTATION_CONSTANTS_HPP
#define FLANG_INSTRUMENTATION_CONSTANTS_HPP

// Verbose flag environment variable
#define SALT_FORTRAN_VERBOSE_VAR "SALT_FORTRAN_VERBOSE"

// Configuration file environment variable
#define SALT_FORTRAN_CONFIG_FILE_VAR "SALT_FORTRAN_CONFIG_FILE"
#define SALT_FORTRAN_CONFIG_DEFAULT_PATH "config_files/tau_config.yaml"
Expand Down
29 changes: 29 additions & 0 deletions src/dprint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Copyright (C) 2025, ParaTools, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "dprint.hpp"

static bool verboseEnabled{false};

void salt::enableVerbose() {
verboseEnabled = true;
}

llvm::raw_ostream & salt::verboseStream() {
if (verboseEnabled) {
return llvm::outs();
}
return llvm::nulls();
}
70 changes: 40 additions & 30 deletions src/flang_salt_instrument_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ limitations under the License.
#define RYML_SINGLE_HDR_DEFINE_NOW
#define RYML_SHARED

#include <dprint.hpp>
#include <ryml_all.hpp>

#include <clang/Basic/SourceLocation.h>
Expand All @@ -60,7 +61,6 @@ using namespace Fortran::frontend;
* Visits each node in the parse tree.
*/
namespace salt::fortran {

class SaltInstrumentAction final : public PluginParseTreeAction {
struct SaltInstrumentParseTreeVisitor {
explicit SaltInstrumentParseTreeVisitor(Fortran::parser::Parsing *parsing,
Expand Down Expand Up @@ -200,31 +200,31 @@ namespace salt::fortran {
}

void Post(const Fortran::parser::MainProgram &) {
llvm::outs() << "Exit main program: " << mainProgramName_ << "\n";
verboseStream() << "Exit main program: " << mainProgramName_ << "\n";
isInMainProgram_ = false;
}

void Post(const Fortran::parser::ProgramStmt &program) {
mainProgramName_ = program.v.ToString();
mainProgramLine_ = parsing->allCooked().GetSourcePositionRange(program.v.source)->first.line;
llvm::outs() << "Enter main program: " << mainProgramName_ << "\n";
verboseStream() << "Enter main program: " << mainProgramName_ << "\n";
}

bool Pre(const Fortran::parser::SubroutineStmt &subroutineStmt) {
const auto &name = std::get<Fortran::parser::Name>(subroutineStmt.t);
subprogramName_ = name.ToString();
subProgramLine_ = parsing->allCooked().GetSourcePositionRange(name.source)->first.line;
llvm::outs() << "Enter Subroutine: " << subprogramName_ << "\n";
verboseStream() << "Enter Subroutine: " << subprogramName_ << "\n";
if (!shouldInstrumentSubprogram(subprogramName_)) {
llvm::outs() << "Skipping instrumentation of " << subprogramName_ <<
verboseStream() << "Skipping instrumentation of " << subprogramName_ <<
" due to selective instrumentation\n";
skipInstrumentSubprogram_ = true;
}
return true;
}

void Post(const Fortran::parser::SubroutineSubprogram &) {
llvm::outs() << "Exit Subroutine: " << subprogramName_ << "\n";
verboseStream() << "Exit Subroutine: " << subprogramName_ << "\n";
skipInstrumentSubprogram_ = false;
subprogramName_.clear();
}
Expand All @@ -233,17 +233,17 @@ namespace salt::fortran {
const auto &name = std::get<Fortran::parser::Name>(functionStmt.t);
subprogramName_ = name.ToString();
subProgramLine_ = parsing->allCooked().GetSourcePositionRange(name.source)->first.line;
llvm::outs() << "Enter Function: " << subprogramName_ << "\n";
verboseStream() << "Enter Function: " << subprogramName_ << "\n";
if (!shouldInstrumentSubprogram(subprogramName_)) {
llvm::outs() << "Skipping instrumentation of " << subprogramName_ <<
verboseStream() << "Skipping instrumentation of " << subprogramName_ <<
" due to selective instrumentation\n";
skipInstrumentSubprogram_ = true;
}
return true;
}

void Post(const Fortran::parser::FunctionSubprogram &) {
llvm::outs() << "Exit Function: " << subprogramName_ << "\n";
verboseStream() << "Exit Function: " << subprogramName_ << "\n";
skipInstrumentSubprogram_ = false;
subprogramName_.clear();
subProgramLine_ = 0;
Expand All @@ -264,7 +264,7 @@ namespace salt::fortran {

void handleExecutionPart(const Fortran::parser::ExecutionPart &executionPart, bool pre) {
if (const Fortran::parser::Block &block = executionPart.v; block.empty()) {
llvm::outs() << "WARNING: Execution part empty.\n";
verboseStream() << "WARNING: Execution part empty.\n";
} else {
const std::optional startLocOpt{getLocation(parsing, block.front(), false)};
const std::optional endLocOpt{getLocation(parsing, block.back(), true)};
Expand Down Expand Up @@ -308,18 +308,18 @@ namespace salt::fortran {
const std::string splitTimerName{ss2.str()};

if (isInMainProgram_) {
llvm::outs() << "Program begin \"" << mainProgramName_ << "\" at " << startLoc.line << ", "
verboseStream() << "Program begin \"" << mainProgramName_ << "\" at " << startLoc.line << ", "
<<
startLoc.column << "\n";
addProgramBeginInstrumentation(startLoc.line, splitTimerName);
} else {
llvm::outs() << "Subprogram begin \"" << subprogramName_ << "\" at " << startLoc.line <<
verboseStream() << "Subprogram begin \"" << subprogramName_ << "\" at " << startLoc.line <<
", " <<
startLoc.column << "\n";
addProcedureBeginInstrumentation(startLoc.line, splitTimerName);
}
} else {
llvm::outs() << "End at " << endLoc.line << ", " << endLoc.column << "\n";
verboseStream() << "End at " << endLoc.line << ", " << endLoc.column << "\n";
addProcedureEndInstrumentation(endLoc.line);
}
}
Expand All @@ -334,7 +334,7 @@ namespace salt::fortran {
actionStmt->statement.u)) {
const std::optional returnPos{locationFromSource(parsing, actionStmt->source, false)};
const int returnLine{returnPos.value().line};
llvm::outs() << "Return statement at " << returnLine << "\n";
verboseStream() << "Return statement at " << returnLine << "\n";
addReturnStmtInstrumentation(returnLine);
}
}
Expand All @@ -359,7 +359,7 @@ namespace salt::fortran {
source,
true).value()
};
llvm::outs() << "If-return, conditional: (" << startPos.line << "," << startPos.column << ") - "
verboseStream() << "If-return, conditional: (" << startPos.line << "," << startPos.column << ") - "
<< "(" << endPos.line << "," << endPos.column << ")\n";
// TODO handle return <value> case
// TODO handle multi-line
Expand Down Expand Up @@ -416,7 +416,7 @@ namespace salt::fortran {
int lineNum{0};
const auto &instPts{visitor.getInstrumentationPoints()};

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

// Sanity check: are instrumentation points in the right order?
if (!std::is_sorted(instPts.cbegin(), instPts.cend(), [&](const auto &p1, const auto &p2) {
Expand Down Expand Up @@ -598,11 +598,29 @@ namespace salt::fortran {
return true;
}

static void dumpSelectiveRequests() {
const auto printStr = [&](const auto &a) { verboseStream() << a << "\n"; };
verboseStream() << "File include list:\n";
std::for_each(fileincludelist.cbegin(), fileincludelist.cend(), printStr);
verboseStream() << "File exclude list:\n";
std::for_each(fileexcludelist.cbegin(), fileexcludelist.cend(), printStr);
verboseStream() << "Include list:\n";
std::for_each(includelist.cbegin(), includelist.cend(), printStr);
verboseStream() << "Exclude list:\n";
std::for_each(excludelist.cbegin(), excludelist.cend(), printStr);
}

/**
* This is the entry point for the plugin.
*/
void executeAction() override {
llvm::outs() << "==== SALT Instrumentor Plugin starting ====\n";
if (const char *val = getenv(SALT_FORTRAN_VERBOSE_VAR)) {
if (const std::string verboseFlag{val}; !verboseFlag.empty() && verboseFlag != "0"s) {
enableVerbose();
}
}

verboseStream() << "==== SALT Instrumentor Plugin starting ====\n";

// This is the object through which we access the parse tree
// and the source
Expand All @@ -614,7 +632,8 @@ namespace salt::fortran {
llvm::errs() << "ERROR: Unable to find input file name!\n";
std::exit(-1);
}
llvm::outs() << "Have input file: " << *inputFilePathStr << "\n";

verboseStream() << "Have input file: " << *inputFilePathStr << "\n";

const std::filesystem::path inputFilePath{inputFilePathStr.value()};

Expand All @@ -625,15 +644,7 @@ namespace salt::fortran {

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);
dumpSelectiveRequests();
} else {
llvm::errs() << "ERROR: Unable to read selective instrumentation file at " << selectPath << "\n";
std::exit(-4);
Expand Down Expand Up @@ -663,7 +674,7 @@ namespace salt::fortran {
// so the file is output into the .inst file unchanged.
bool skipInstrument{false};
if (!shouldInstrumentFile(inputFilePath)) {
llvm::outs() << "Skipping instrumentation of " << inputFilePath
verboseStream() << "Skipping instrumentation of " << inputFilePath
<< " due to selective instrumentation.\n";
skipInstrument = true;
}
Expand All @@ -676,10 +687,9 @@ namespace salt::fortran {

outputFileStream->flush();

llvm::outs() << "==== SALT Instrumentor Plugin finished ====\n";
verboseStream() << "==== SALT Instrumentor Plugin finished ====\n";
}
};

}

[[maybe_unused]] static FrontendPluginRegistry::Add<salt::fortran::SaltInstrumentAction> X(
Expand Down

0 comments on commit d0e1f94

Please sign in to comment.