Skip to content

Commit

Permalink
Maintenance (#141)
Browse files Browse the repository at this point in the history
* Use detected python interp for tests
* Improve source loc handling
  • Loading branch information
ahueck committed Dec 23, 2023
1 parent bac2841 commit 223224d
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/passes/filter/IRSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct DefaultSearch {

if (isa<llvm::PHINode>(val)) {
// FIXME
// this mechanism tries to avoid endless recurison in loops, i.e.,
// this mechanism tries to avoid endless recursion in loops, i.e.,
// do we bounce around multiple phi nodes (visit counter >1), then
// we should likely skip search
// see amg with openmp `amg2013/parcsr_ls/par_lr_interp.c`
Expand Down
51 changes: 45 additions & 6 deletions lib/support/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#include "System.h"

#include <cstdio>
#include <dlfcn.h>
#include <filesystem>
#include <link.h> // For link_map, see SourceLocation::create
#include <memory>
#include <sstream>
#include <sys/resource.h>
Expand Down Expand Up @@ -125,14 +127,51 @@ class SourceLocHelper {
}
};

struct RemoveEnvInScope {
explicit RemoveEnvInScope(std::string_view var_name) : var_name_(var_name) {
old_val_ = [](std::string_view env_var_name) {
const auto* env_data = getenv(env_var_name.data());
if (env_data) {
return env_data;
}
return "";
}(var_name);

if (!old_val_.empty()) {
setenv(var_name.data(), "", true);
}
}

~RemoveEnvInScope() {
if (!old_val_.empty()) {
setenv(var_name_.data(), old_val_.data(), true);
}
}

private:
std::string_view var_name_;
std::string_view old_val_;
};

} // namespace system

std::optional<SourceLocation> SourceLocation::create(const void* addr) {
const auto pipe = [](const void* addr) -> std::optional<system::CommandPipe> {
using namespace system;
const auto& sloc_helper = SourceLocHelper::get();
std::optional<SourceLocation> SourceLocation::create(const void* addr, intptr_t offset_ptr) {
// Preload might cause infinite recursion, hence temp. remove this flag in this scope only:
system::RemoveEnvInScope rm_preload_var{"LD_PRELOAD"};

const auto pipe = [](const void* paddr, intptr_t offset_ptr) -> std::optional<system::CommandPipe> {
const auto& sloc_helper = system::SourceLocHelper::get();
const auto& proc = system::Process::get();

// FIXME: Inst Pointer points one past what we need with __built_in_return_addr(0), hacky way to fix:
const auto addr = [](const auto addr) { // reinterpret_cast<intptr_t>(paddr) - offset_ptr;
// Transform addr to VMA Addr:
Dl_info info;
link_map* link_map;
dladdr1((void*)addr, &info, (void**)&link_map, RTLD_DL_LINKMAP);
return addr - link_map->l_addr;
}(reinterpret_cast<intptr_t>(paddr) - offset_ptr);

if (sloc_helper.hasLLVMSymbolizer()) {
std::ostringstream command;
command << "llvm-symbolizer --demangle --output-style=GNU -f -e " << proc.exe() << " " << addr;
Expand All @@ -144,15 +183,15 @@ std::optional<SourceLocation> SourceLocation::create(const void* addr) {

if (sloc_helper.hasAddr2line()) {
std::ostringstream command;
command << "addr2line --demangle=auto -f -e " << proc.exe() << " " << addr;
command << "addr2line --demangle=auto -f -e " << proc.exe() << " " << std::hex << addr;
auto addr2line = system::CommandPipe::create(command.str());
if (addr2line) {
return addr2line;
}
}

return {};
}(addr);
}(addr, offset_ptr);

if (!pipe) {
return {};
Expand Down
2 changes: 1 addition & 1 deletion lib/support/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct SourceLocation {
std::string file;
std::string line;

static std::optional<SourceLocation> create(const void* addr);
static std::optional<SourceLocation> create(const void* addr, intptr_t offset_ptr = 1);
};

} // namespace typeart
Expand Down
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
typeart_find_llvm_progs(TYPEART_FILECHECK_EXEC "FileCheck-${LLVM_VERSION_MAJOR};FileCheck" ABORT_IF_MISSING)

find_package(Python3 COMPONENTS Interpreter)

if(LLVM_EXTERNAL_LIT)
cmake_path(GET LLVM_EXTERNAL_LIT PARENT_PATH LLVM_EXTERNAL_LIT_DIR)
endif()
Expand Down
2 changes: 2 additions & 0 deletions test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,5 @@ config.substitutions.append(('%wrapper-ir-compare', '{}/scripts/typeart_ir_compa
config.substitutions.append(('%mpicxx-compiler', '{}'.format(config.mpicxx)))
config.substitutions.append(('%mpicc-compiler', '{}'.format(config.mpicc)))
config.substitutions.append(('%mpi-exec', '{}'.format(config.mpiexec)))

config.substitutions.append(('%python-interp', config.python_interp))
2 changes: 2 additions & 0 deletions test/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ config.asan=@TYPEARTPASS_ASAN@
config.ubsan=@TYPEARTPASS_UBSAN@
config.coverage=@TYPEART_COVERAGE@

config.python_interp = "@Python3_EXECUTABLE@"

# Let the main config do the real work.
config.loaded_site_config = True
lit_config.load_config(config, "@TYPEARTPASS_PROJECT_DIR@/test/lit.cfg")
4 changes: 0 additions & 4 deletions test/pass/new_delete/09_inv_struct_delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ struct S1 {
// CHECK: invoke{{.*}} i8* @_Znwm(i64{{( noundef)?}} 16)
// CHECK: call void @__typeart_alloc(i8* [[POINTER:%[0-9a-z]+]], i32 {{2[0-9]+}}, i64 1)
// CHECK: bitcast i8* [[POINTER]] to %struct.S1*
// CHECK-NOT: call void @_ZdlPv(i8*{{( noundef)?}} [[POINTER2:%[0-9a-z]+]])
// CHECK-NOT: call void @__typeart_free(i8* {{.*}}[[POINTER2]])
void foo() {
S1* b{nullptr};
try {
Expand All @@ -28,8 +26,6 @@ void foo() {
// CHECK: invoke{{.*}} i8* @_Znwm(i64{{( noundef)?}} 16)
// CHECK: call void @__typeart_alloc(i8* [[POINTER:%[0-9a-z]+]], i32 {{2[0-9]+}}, i64 1)
// CHECK: bitcast i8* [[POINTER]] to %struct.S1*
// CHECK-NOT: call void @_ZdaPv(i8*{{( noundef)?}} [[POINTER2:%[0-9a-z]+]])
// CHECK-NOT: call void @__typeart_free(i8* {{.*}}[[POINTER2]])
int main() {
try {
S1* ss = new S1;
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/46_source_location.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int main(int argc, char** argv) {
// CHECK: Address check OK
// CHECK: Loc File:{{.*}}46_source_location.c
// CHECK: Loc Function: main
// CHECK: Loc Line: {{[0-9]+}}
// CHECK: Loc Line: 33
// CHECK: Address check OK
// CHECK: Address check OK
// CHECK: Address check OK
8 changes: 5 additions & 3 deletions test/script/12_ir_viewer.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# RUN: chmod +x %s
# RUN: %s %wrapper-ir-viewer %wrapper-cc %S | %filecheck %s
# RUN: %s %wrapper-ir-viewer %wrapper-cc %S %python-interp | %filecheck %s


function exists() {
Expand All @@ -14,6 +14,8 @@ function exists() {

cd "$3"

python_interp="$4"

# CHECK: 0
# CHECK: 0
# CHECK: 0
Expand All @@ -23,7 +25,7 @@ exists 12_ir_viewer_target_heap.ll
exists 12_ir_viewer_target_opt.ll
exists 12_ir_viewer_target_stack.ll

python $1 -s -w "$2" 12_ir_viewer_target.c -- -g
"$python_interp" $1 -s -w "$2" 12_ir_viewer_target.c -- -g
# CHECK: 1
# CHECK: 1
# CHECK: 1
Expand All @@ -35,7 +37,7 @@ exists 12_ir_viewer_target_opt.ll
exists 12_ir_viewer_target_stack.ll
exists 12_ir_viewer_target-types-ir-viewer.yaml

python $1 -c 12_ir_viewer_target.c
"$python_interp" $1 -c 12_ir_viewer_target.c
# CHECK: 0
# CHECK: 0
# CHECK: 0
Expand Down
10 changes: 6 additions & 4 deletions test/script/13_ir_compare.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# RUN: chmod +x %s
# RUN: %s %wrapper-ir-compare %wrapper-cc %wrapper-cc %S | %filecheck %s
# RUN: %s %wrapper-ir-compare %wrapper-cc %wrapper-cc %S %python-interp | %filecheck %s


function exists() {
Expand All @@ -14,12 +14,14 @@ function exists() {

cd "$4"

python_interp="$5"

# CHECK: 0
# CHECK: 0
exists 12_ir_viewer_target_heap.ll-a
exists 12_ir_viewer_target_heap.ll-b

python $1 -s -a "$2" -b "$3" 12_ir_viewer_target.c -- -g
"$python_interp" $1 -s -a "$2" -b "$3" 12_ir_viewer_target.c -- -g
# CHECK: 1
# CHECK: 1
exists 12_ir_viewer_target_heap.ll-a
Expand All @@ -32,15 +34,15 @@ rm 12_ir_viewer_target_heap.ll-a 12_ir_viewer_target_heap.ll-b
exists 12_ir_viewer_target_stack.ll-a
exists 12_ir_viewer_target_stack.ll-b

python $1 -s -m stack -a "$2" -b "$3" 12_ir_viewer_target.c -- -g
"$python_interp" $1 -s -m stack -a "$2" -b "$3" 12_ir_viewer_target.c -- -g
# CHECK: 1
# CHECK: 1
exists 12_ir_viewer_target_stack.ll-a
exists 12_ir_viewer_target_stack.ll-b

rm 12_ir_viewer_target_stack.ll-a 12_ir_viewer_target_stack.ll-b

python $1 -s -m base -a "$2" -b "$3" 12_ir_viewer_target.c -- -g
"$python_interp" $1 -s -m base -a "$2" -b "$3" 12_ir_viewer_target.c -- -g
# CHECK: 1
# CHECK: 1
exists 12_ir_viewer_target_base.ll-a
Expand Down

0 comments on commit 223224d

Please sign in to comment.