Skip to content

Commit

Permalink
Added debug info disable probability options (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrDet authored Nov 18, 2024
1 parent 1594a17 commit 62478d3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
20 changes: 20 additions & 0 deletions compiler/code-gen/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

#pragma once

#include <cstddef>
#include <functional>
#include <string>

#include "compiler/code-gen/code-generator.h"
#include "compiler/compiler-core.h"
#include "compiler/stage.h"

#define NL NewLine()
Expand All @@ -24,6 +27,23 @@ struct OpenFile {
subdir(subdir),
compile_with_debug_info_flag(compile_with_debug_info_flag),
compile_with_crc(compile_with_crc) {
double debug_info_disable_prob = G->settings().debug_info_force_disable_prob.get();
if (compile_with_debug_info_flag && debug_info_disable_prob > 0) {
if (flip_coin(debug_info_disable_prob)) {
this->compile_with_debug_info_flag = false;
}
}
}

bool flip_coin(double prob) const {
static constexpr size_t PROB_RANGE = 100000;
size_t hash = std::hash<std::string>{}(file_name);
size_t seed = 1;
if (size_t seed_from_options = G->settings().debug_info_force_disable_prob_seed.get()) {
seed = seed_from_options;
}

return hash * seed % PROB_RANGE < prob * PROB_RANGE;
}

void compile(CodeGenerator &W) const {
Expand Down
20 changes: 19 additions & 1 deletion compiler/compiler-settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ void KphpOption<uint64_t>::dump_option(std::ostream &out) const noexcept {
out << value_;
}

template<>
void KphpOption<double>::dump_option(std::ostream &out) const noexcept {
out << value_;
}

template<>
void KphpOption<bool>::dump_option(std::ostream &out) const noexcept {
out << (value_ ? "true" : "false");
Expand Down Expand Up @@ -86,6 +91,19 @@ void KphpOption<uint64_t>::parse_arg_value() {
}
}

template<>
void KphpOption<double>::parse_arg_value() {
if (raw_option_arg_.empty()) {
value_ = 0.0;
} else {
try {
value_ = std::stod(raw_option_arg_);
} catch (...) {
throw_param_exception(fmt_format("double is expected but {} found", raw_option_arg_));
}
}
}

template<>
void KphpOption<bool>::parse_arg_value() {
if (vk::none_of_equal(raw_option_arg_, "1", "0", "")) {
Expand Down Expand Up @@ -304,7 +322,7 @@ void CompilerSettings::init() {
if (!no_pch.get()) {
ss << " -Winvalid-pch -fpch-preprocess";
}
if (is_k2_mode || dynamic_incremental_linkage.get()) {
if (is_k2_mode || dynamic_incremental_linkage.get()) {
ss << " -fPIC";
}
if (vk::contains(cxx.get(), "clang")) {
Expand Down
3 changes: 3 additions & 0 deletions compiler/compiler-settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ class CompilerSettings : vk::not_copyable {
KphpOption<std::string> archive_creator;
KphpOption<bool> dynamic_incremental_linkage;

KphpOption<double> debug_info_force_disable_prob;
KphpOption<uint64_t> debug_info_force_disable_prob_seed;

KphpOption<uint64_t> profiler_level;
KphpOption<bool> enable_global_vars_memory_stats;
KphpOption<bool> enable_full_performance_analyze;
Expand Down
5 changes: 5 additions & 0 deletions compiler/kphp2cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ int main(int argc, char *argv[]) {
"extra-linker-flags", "KPHP_EXTRA_LDFLAGS", get_default_extra_ldflags());
parser.add("C++ compiler debug level for building the output binary", settings->extra_cxx_debug_level,
"debug-level", "KPHP_DEBUG_LEVEL");
parser.add("Probability of partially disabling debug info for some random part of files",
settings->debug_info_force_disable_prob, "debug-info-force-disable-probability", "KPHP_DEBUG_INFO_FORCE_DISABLE_PROBABILITY");
parser.add("Seed for random check from --debug-info-force-disable-probability option",
settings->debug_info_force_disable_prob_seed, "debug-info-force-disable-probability-seed", "KPHP_DEBUG_INFO_FORCE_DISABLE_PROBABILITY_SEED");

parser.add("Archive creator for building the output binary", settings->archive_creator,
"archive-creator", "KPHP_ARCHIVE_CREATOR", "ar");
parser.add("Use dynamic incremental linkage for building the output binary", settings->dynamic_incremental_linkage,
Expand Down

0 comments on commit 62478d3

Please sign in to comment.