Skip to content
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
2 changes: 1 addition & 1 deletion cmake/typeartToolchainOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ mark_as_advanced(TYPEART_TEST_CONFIGURE_IDE)
option(TYPEART_CONFIG_DIR_IS_SHARE "Install to \"share/cmake/\" instead of \"lib/cmake/\"" OFF)
mark_as_advanced(TYPEART_CONFIG_DIR_IS_SHARE)

option(TYPEART_USE_NEW_PASSMANAGER "Use the new LLVM PassManager" ON)
option(TYPEART_USE_LEGACY_WRAPPER "Use the old TypeART compiler wrapper" OFF)
# mark_as_advanced(TYPEART_USE_NEW_PASSMANAGER)

if(LLVM_VERSION_MAJOR VERSION_GREATER_EQUAL "18")
Expand Down
30 changes: 30 additions & 0 deletions lib/passes/TypeARTPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
#include "instrumentation/MemOpArgCollector.h"
#include "instrumentation/MemOpInstrumentation.h"
#include "instrumentation/TypeARTFunctions.h"
#include "support/ConfigurationBase.h"
#include "support/Logger.h"
#include "support/ModuleDumper.h"
#include "support/Table.h"
#include "support/Util.h"
#include "typegen/TypeGenerator.h"

#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringRef.h"
Expand Down Expand Up @@ -227,6 +230,12 @@ class TypeArtPass : public llvm::PassInfoMixin<TypeArtPass> {
}

void printStats(llvm::raw_ostream& out) {
const auto scope_exit_cleanup_counter = llvm::make_scope_exit([&]() {
NumInstrumentedAlloca = 0;
NumInstrumentedFrees = 0;
NumInstrumentedGlobal = 0;
NumInstrumentedMallocs = 0;
});
meminst_finder->printStats(out);

const auto get_ta_mode = [&]() {
Expand Down Expand Up @@ -270,7 +279,10 @@ class TypeArtPass : public llvm::PassInfoMixin<TypeArtPass> {
llvm::PreservedAnalyses run(llvm::Module& m, llvm::ModuleAnalysisManager&) {
bool changed{false};
changed |= doInitialization(m);
const bool heap = configuration()[config::ConfigStdArgs::heap]; // Must happen after doInit
dump_module(m, heap ? util::module::ModulePhase::kBase : util::module::ModulePhase::kOpt);
changed |= runOnModule(m);
dump_module(m, heap ? util::module::ModulePhase::kHeap : util::module::ModulePhase::kStack);
changed |= doFinalization();
return changed ? llvm::PreservedAnalyses::none() : llvm::PreservedAnalyses::all();
}
Expand Down Expand Up @@ -388,6 +400,24 @@ bool LegacyTypeArtPass::doFinalization(llvm::Module&) {
llvm::PassPluginLibraryInfo getTypeartPassPluginInfo() {
using namespace llvm;
return {LLVM_PLUGIN_API_VERSION, "TypeART", LLVM_VERSION_STRING, [](PassBuilder& pass_builder) {
pass_builder.registerPipelineStartEPCallback([](auto& MPM, OptimizationLevel) {
auto parameters = typeart::util::pass::parsePassParameters(typeart::config::pass::parse_typeart_config,
"typeart<heap;stats>", "typeart");
if (!parameters) {
LOG_FATAL("Error parsing heap params: " << parameters.takeError())
return;
}
MPM.addPass(typeart::pass::TypeArtPass(typeart::pass::TypeArtPass(parameters.get())));
});
pass_builder.registerOptimizerLastEPCallback([](auto& MPM, OptimizationLevel) {
auto parameters = typeart::util::pass::parsePassParameters(typeart::config::pass::parse_typeart_config,
"typeart<no-heap;stack;stats>", "typeart");
if (!parameters) {
LOG_FATAL("Error parsing stack params: " << parameters.takeError())
return;
}
MPM.addPass(typeart::pass::TypeArtPass(typeart::pass::TypeArtPass(parameters.get())));
});
pass_builder.registerPipelineParsingCallback(
[](StringRef name, ModulePassManager& module_pm, ArrayRef<PassBuilder::PipelineElement>) {
if (typeart::util::pass::checkParametrizedPassName(name, "typeart")) {
Expand Down
23 changes: 17 additions & 6 deletions lib/passes/analysis/MemInstFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

#include <algorithm>
#include <cstdlib>
#include <llvm/ADT/ScopeExit.h>
#include <sstream>
#include <string>
#include <utility>
Expand Down Expand Up @@ -164,7 +165,7 @@ class MemInstFinderPass : public MemInstFinder {
const GlobalDataList& getModuleGlobals() const override;
void printStats(llvm::raw_ostream&) const override;
// void configure(MemInstFinderConfig&) override;
~MemInstFinderPass() = default;
~MemInstFinderPass() override = default;

private:
bool runOnFunction(llvm::Function&);
Expand Down Expand Up @@ -389,11 +390,21 @@ bool MemInstFinderPass::runOnFunction(llvm::Function& function) {
} // namespace typeart

void MemInstFinderPass::printStats(llvm::raw_ostream& out) const {
auto all_stack = double(NumDetectedAllocs);
auto nonarray_stack = double(NumFilteredNonArrayAllocs);
auto malloc_alloc_stack = double(NumFilteredMallocAllocs);
auto call_filter_stack = double(NumCallFilteredAllocs);
auto filter_pointer_stack = double(NumFilteredPointerAllocs);
const auto scope_exit_cleanup_counter = llvm::make_scope_exit([&]() {
NumDetectedAllocs = 0;
NumFilteredNonArrayAllocs = 0;
NumFilteredMallocAllocs = 0;
NumCallFilteredAllocs = 0;
NumFilteredPointerAllocs = 0;
NumDetectedHeap = 0;
NumFilteredGlobals = 0;
NumDetectedGlobals = 0;
});
auto all_stack = double(NumDetectedAllocs);
auto nonarray_stack = double(NumFilteredNonArrayAllocs);
auto malloc_alloc_stack = double(NumFilteredMallocAllocs);
auto call_filter_stack = double(NumCallFilteredAllocs);
auto filter_pointer_stack = double(NumFilteredPointerAllocs);

const auto call_filter_stack_p =
(call_filter_stack /
Expand Down
74 changes: 74 additions & 0 deletions lib/passes/support/ModuleDumper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#ifndef C0CB906E_91B6_4B24_927B_ABFC733C6631
#define C0CB906E_91B6_4B24_927B_ABFC733C6631

#include "support/Logger.h"

#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"

#include <cstdlib>
#include <filesystem>
#include <string_view>

namespace typeart::util::module {

enum class ModulePhase { kBase, kHeap, kOpt, kStack };

namespace detail {

inline void dump_module_if(const llvm::Module& module, llvm::raw_ostream& out_s) {
LOG_FATAL("Dumping...")
module.print(out_s, nullptr);
}

inline std::string_view gets_source_file(const llvm::Module& module) {
return module.getSourceFileName();
}

inline std::string_view get_extension(ModulePhase phase) {
switch (phase) {
case ModulePhase::kBase:
return "_base.ll";
case ModulePhase::kHeap:
return "_heap.ll";
case ModulePhase::kOpt:
return "_opt.ll";
case ModulePhase::kStack:
return "_stack.ll";
}
return "_unknown.ll";
}

} // namespace detail

inline void dump_module(const llvm::Module& module, ModulePhase phase) {
if (std::getenv("TYPEART_PASS_INTERNAL_EMIT_IR") == nullptr) {
LOG_DEBUG("No dump required")
return;
}

const auto source = std::filesystem::path{detail::gets_source_file(module)};

if (!source.has_filename()) {
LOG_ERROR("No filename for module")
return;
}

const auto source_ll = source.parent_path() / (source.stem().string() + detail::get_extension(phase).data());
LOG_DEBUG("Dumping to file " << source_ll);

std::error_code error_code;
llvm::raw_fd_ostream file_out{source_ll.c_str(), error_code};
if (error_code) {
LOG_FATAL("Error while opening file " << error_code.message())
return;
}

detail::dump_module_if(module, file_out);
file_out.close();
}

} // namespace typeart::util::module

#endif /* C0CB906E_91B6_4B24_927B_ABFC733C6631 */
31 changes: 23 additions & 8 deletions scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ function(typeart_configure_script input output)

list(JOIN TYPEART_SAN_FLAGS " " TYPEART_SAN_FLAGS)

if(${LLVM_VERSION_MAJOR} GREATER_EQUAL 18)
# FIXME workaround, enabled by default? > not compat with dimeta
set(TYPEART_INITIAL_FLAGS "-Xclang -fexperimental-assignment-tracking=disabled")
endif()

if(ARG_WITH_FILTER)
set(TYPEART_FILTER_FLAG "export TYPEART_FILTER=\${TYPEART_FILTER:-1}")
endif()

typeart_target_generate_file(${input} ${output})
endfunction()

Expand Down Expand Up @@ -181,23 +190,29 @@ typeart_configure_script(typeart-tmpl.sh.in apply.sh

typeart_configure_script(reduce-tester.in reduce-tester.sh)

if(TYPEART_USE_LEGACY_WRAPPER)
set(TYPEART_WRAPPER_FILE typeart-wrapper.in)
else()
set(TYPEART_WRAPPER_FILE typeart-wrapperv2.in)
endif()

typeart_configure_script(
typeart-wrapper.in typeart-clang${CMAKE_DEBUG_POSTFIX}
${TYPEART_WRAPPER_FILE} typeart-clang${CMAKE_DEBUG_POSTFIX}
INSTALL_MODE ON
COMPILER ${TYPEART_CLANG_EXEC}
)
typeart_configure_script(
typeart-wrapper.in typeart-clang++${CMAKE_DEBUG_POSTFIX}
${TYPEART_WRAPPER_FILE} typeart-clang++${CMAKE_DEBUG_POSTFIX}
INSTALL_MODE ON
COMPILER ${TYPEART_CLANGCXX_EXEC}
)

typeart_configure_script(
typeart-wrapper.in typeart-clang-test
${TYPEART_WRAPPER_FILE} typeart-clang-test
COMPILER ${TYPEART_CLANG_EXEC}
)
typeart_configure_script(
typeart-wrapper.in typeart-clang++-test
${TYPEART_WRAPPER_FILE} typeart-clang++-test
COMPILER ${TYPEART_CLANGCXX_EXEC}
)

Expand All @@ -207,27 +222,27 @@ endif()

if(MPI_C_FOUND)
typeart_configure_script(
typeart-wrapper.in typeart-mpicc${CMAKE_DEBUG_POSTFIX}
${TYPEART_WRAPPER_FILE} typeart-mpicc${CMAKE_DEBUG_POSTFIX}
INSTALL_MODE ON
WITH_FILTER ON
COMPILER "${TYPEART_MPICC}"
)
typeart_configure_script(
typeart-wrapper.in typeart-mpicc-test
${TYPEART_WRAPPER_FILE} typeart-mpicc-test
WITH_FILTER ON
COMPILER "${TYPEART_MPICC}"
)
endif()

if(MPI_CXX_FOUND)
typeart_configure_script(
typeart-wrapper.in typeart-mpic++${CMAKE_DEBUG_POSTFIX}
${TYPEART_WRAPPER_FILE} typeart-mpic++${CMAKE_DEBUG_POSTFIX}
INSTALL_MODE ON
WITH_FILTER ON
COMPILER "${TYPEART_MPICXX}"
)
typeart_configure_script(
typeart-wrapper.in typeart-mpic++-test
${TYPEART_WRAPPER_FILE} typeart-mpic++-test
WITH_FILTER ON
COMPILER "${TYPEART_MPICXX}"
)
Expand Down
105 changes: 105 additions & 0 deletions scripts/typeart-wrapperv2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/bin/bash
#
# TypeART library
#
# Copyright (c) 2017-2025 TypeART Authors
# Distributed under the BSD 3-Clause license.
# (See accompanying file LICENSE.txt or copy at
# https://opensource.org/licenses/BSD-3-Clause)
#
# Project home: https://github.com/tudasc/TypeART
#
# SPDX-License-Identifier: BSD-3-Clause
#

function typeart_is_wrapper_disabled_fn() {
case "${TYPEART_WRAPPER}" in
off | OFF | 0 | false | FALSE)
return 1
;;
esac
return 0
}

function typeart_global_init_fn() {
local -r typeart_use_rel_path=@TYPEART_RELOCATABLE@
if [ "$typeart_use_rel_path" == 0 ]; then
local -r typeart_bin_dir="@TYPEART_BINARY_DIR@"
local -r typeart_lib_dir="@TYPEART_RT_DIR@"
local -r typeart_include_dir="@TYPEART_INCLUDE_DIRS@"
local -r typeart_pass="@TYPEART_PASS_DIR@/$<TARGET_FILE_NAME:typeart::TransformPass>"
else
# shellcheck disable=SC2155
local -r typeart_bin_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC2155
local -r typeart_install_dir="$(dirname "${typeart_bin_dir}")"
local -r typeart_lib_dir="${typeart_install_dir}/@CMAKE_INSTALL_LIBDIR@"
local -r typeart_include_dir="-I${typeart_install_dir}/@CMAKE_INSTALL_INCLUDEDIR@/@PROJECT_NAME@"
local -r typeart_pass="${typeart_lib_dir}/$<TARGET_FILE_NAME:typeart::TransformPass>"
fi

readonly typeart_compiler="@TYPEART_COMPILER@"

readonly typeart_includes="${typeart_include_dir}"
readonly typeart_ldflags="-L${typeart_lib_dir}/ \
-Wl,-rpath,${typeart_lib_dir}/ \
-l$<TARGET_FILE_BASE_NAME:typeart::Runtime>"
readonly typeart_san_flags="@TYPEART_SAN_FLAGS@"
readonly typeart_plugin="-fpass-plugin="${typeart_pass}""

typeart_more_flags="@TYPEART_INITIAL_FLAGS@"
@TYPEART_FILTER_FLAG@
case "${TYPEART_WRAPPER_EMIT_IR}" in
on | ON | 1 | true | TRUE)
export TYPEART_PASS_INTERNAL_EMIT_IR=1
typeart_more_flags+=" -fno-discard-value-names"
;;
esac
}

function typeart_is_linking_fn() {
local arg=""
for arg in "$@"; do
case "$arg" in
-c | -S | -E | -emit-llvm)
return 0
;;
esac
done
return 1
}

function typeart_parse_cmd_line_fn() {
local typeart_has_debug=0
for arg in "$@"; do
if [ "$arg" = "-g" ]; then
typeart_has_debug=1
break
fi
done
if [ $typeart_has_debug == 0 ]; then
typeart_more_flags+=" -g"
fi
}

function typeart_main_driver_fn() {
typeart_global_init_fn

typeart_is_wrapper_disabled_fn
if [ "$?" == 1 ]; then
# shellcheck disable=SC2068
$typeart_compiler $@
return 0
fi

typeart_parse_cmd_line_fn "$@"

typeart_is_linking_fn "$@"
if [ "$?" == 1 ]; then
typeart_more_flags+=" ${typeart_ldflags}"
fi

$typeart_compiler ${typeart_plugin} ${typeart_includes} ${typeart_more_flags} ${typeart_san_flags} $@
}

typeart_main_driver_fn "$@"
Loading