Skip to content

Commit f3dc266

Browse files
committed
Rename KeepFramePointer to EnableBacktraces, and add UWTableKind to default in that case. Also enable that feature when Target::Feature::Debug is set. Cleanup some helper functions.
1 parent 1868183 commit f3dc266

File tree

5 files changed

+39
-32
lines changed

5 files changed

+39
-32
lines changed

src/CodeGen_Internal.cpp

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -588,17 +588,27 @@ std::optional<std::string> get_md_string(llvm::Metadata *value) {
588588
}
589589
return std::nullopt;
590590
}
591+
592+
bool get_modflag_bool(const llvm::Module &mod, const char *flag, bool or_default = false) {
593+
return get_md_bool(mod.getModuleFlag(flag)).value_or(or_default);
594+
}
595+
596+
int get_modflag_int(const llvm::Module &mod, const char *flag, int or_default = 0) {
597+
return get_md_int(mod.getModuleFlag(flag)).value_or(or_default);
598+
}
599+
600+
std::string get_modflag_string(const llvm::Module &mod, const char *flag, std::string or_default = {}) {
601+
return get_md_string(mod.getModuleFlag(flag)).value_or(or_default);
602+
}
603+
591604
} // namespace
592605

593606
void get_target_options(const llvm::Module &module, llvm::TargetOptions &options) {
594-
bool use_soft_float_abi =
595-
get_md_bool(module.getModuleFlag("halide_use_soft_float_abi")).value_or(false);
596-
std::string mabi =
597-
get_md_string(module.getModuleFlag("halide_mabi")).value_or(std::string{});
607+
bool use_soft_float_abi = get_modflag_bool(module, "halide_use_soft_float_abi");
608+
std::string mabi = get_modflag_string(module, "halide_mabi");
598609

599610
// FIXME: can this be migrated into `set_function_attributes_from_halide_target_options()`?
600-
bool per_instruction_fast_math_flags =
601-
get_md_bool(module.getModuleFlag("halide_per_instruction_fast_math_flags")).value_or(false);
611+
bool per_instruction_fast_math_flags = get_modflag_bool(module, "halide_per_instruction_fast_math_flags");
602612

603613
options = llvm::TargetOptions();
604614
options.AllowFPOpFusion = per_instruction_fast_math_flags ? llvm::FPOpFusion::Strict : llvm::FPOpFusion::Fast;
@@ -610,8 +620,7 @@ void get_target_options(const llvm::Module &module, llvm::TargetOptions &options
610620
options.GuaranteedTailCallOpt = false;
611621
options.FunctionSections = true;
612622
options.UseInitArray = true;
613-
options.FloatABIType =
614-
use_soft_float_abi ? llvm::FloatABI::Soft : llvm::FloatABI::Hard;
623+
options.FloatABIType = use_soft_float_abi ? llvm::FloatABI::Soft : llvm::FloatABI::Hard;
615624
#if LLVM_VERSION >= 190
616625
options.MCOptions.X86RelaxRelocations = false;
617626
#else
@@ -628,7 +637,7 @@ void clone_target_options(const llvm::Module &from, llvm::Module &to) {
628637
// Clone bool metadata
629638
for (const char *s : {"halide_use_soft_float_abi",
630639
"halide_use_pic",
631-
"halide_keep_frame_pointer"}) {
640+
"halide_enable_backtraces"}) {
632641
if (auto md = get_md_bool(from.getModuleFlag(s))) {
633642
to.addModuleFlag(llvm::Module::Warning, s, *md ? 1 : 0);
634643
}
@@ -659,17 +668,12 @@ std::unique_ptr<llvm::TargetMachine> make_target_machine(const llvm::Module &mod
659668
llvm::TargetOptions options;
660669
get_target_options(module, options);
661670

662-
bool use_pic =
663-
get_md_bool(module.getModuleFlag("halide_use_pic")).value_or(true);
664-
665-
bool use_large_code_model =
666-
get_md_bool(module.getModuleFlag("halide_use_large_code_model")).value_or(false);
671+
bool use_pic = get_modflag_bool(module, "halide_use_pic", true);
672+
bool use_large_code_model = get_modflag_bool(module, "halide_use_large_code_model");
667673

668674
// Get module mcpu_target and mattrs.
669-
std::string mcpu_target =
670-
get_md_string(module.getModuleFlag("halide_mcpu_target")).value_or(std::string{});
671-
std::string mattrs =
672-
get_md_string(module.getModuleFlag("halide_mattrs")).value_or(std::string{});
675+
std::string mcpu_target = get_modflag_string(module, "halide_mcpu_target");
676+
std::string mattrs = get_modflag_string(module, "halide_mattrs");
673677

674678
auto *tm = llvm_target->createTargetMachine(
675679
#if LLVM_VERSION >= 210
@@ -689,17 +693,18 @@ std::unique_ptr<llvm::TargetMachine> make_target_machine(const llvm::Module &mod
689693
void set_function_attributes_from_halide_target_options(llvm::Function &fn) {
690694
llvm::Module &module = *fn.getParent();
691695

692-
std::string mcpu_target = get_md_string(module.getModuleFlag("halide_mcpu_target")).value_or(std::string{});
693-
std::string mcpu_tune = get_md_string(module.getModuleFlag("halide_mcpu_tune")).value_or(std::string{});
694-
std::string mattrs = get_md_string(module.getModuleFlag("halide_mattrs")).value_or(std::string{});
695-
int64_t vscale_range = get_md_int(module.getModuleFlag("halide_effective_vscale")).value_or(0);
696-
bool keep_fp = get_md_int(module.getModuleFlag("halide_keep_frame_pointer")).value_or(0);
696+
std::string mcpu_target = get_modflag_string(module, "halide_mcpu_target");
697+
std::string mcpu_tune = get_modflag_string(module, "halide_mcpu_tune");
698+
std::string mattrs = get_modflag_string(module, "halide_mattrs");
699+
int64_t vscale_range = get_modflag_int(module, "halide_effective_vscale");
700+
bool enable_bt = get_modflag_int(module, "halide_enable_backtraces");
697701

698702
fn.addFnAttr("target-cpu", mcpu_target);
699703
fn.addFnAttr("tune-cpu", mcpu_tune);
700704
fn.addFnAttr("target-features", mattrs);
701-
if (keep_fp) {
705+
if (enable_bt) {
702706
fn.addFnAttr("frame-pointer", "all");
707+
fn.setUWTableKind(llvm::UWTableKind::Default);
703708
}
704709

705710
// Halide-generated IR is not exception-safe.

src/CodeGen_LLVM.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,9 @@ void CodeGen_LLVM::init_codegen(const std::string &name) {
401401
module->setModuleIdentifier(name);
402402

403403
// Add some target specific info to the module as metadata.
404-
module->addModuleFlag(llvm::Module::Warning, "halide_keep_frame_pointer", target.has_feature(Target::Feature::KeepFramePointer) ? 1 : 0);
404+
bool enable_bt = target.has_feature(Target::Feature::EnableBacktraces) ||
405+
target.has_feature(Target::Feature::Debug);
406+
module->addModuleFlag(llvm::Module::Warning, "halide_enable_backtraces", enable_bt ? 1 : 0);
405407
module->addModuleFlag(llvm::Module::Warning, "halide_use_soft_float_abi", use_soft_float_abi() ? 1 : 0);
406408
module->addModuleFlag(llvm::Module::Warning, "halide_mcpu_target", MDString::get(*context, mcpu_target()));
407409
module->addModuleFlag(llvm::Module::Warning, "halide_mcpu_tune", MDString::get(*context, mcpu_tune()));

src/Target.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ bool lookup_processor(const std::string &tok, Target::Processor &result) {
646646
const std::map<std::string, Target::Feature> feature_name_map = {
647647
{"jit", Target::JIT},
648648
{"debug", Target::Debug},
649-
{"keep_frame_pointer", Target::KeepFramePointer},
649+
{"enable_backtraces", Target::EnableBacktraces},
650650
{"no_asserts", Target::NoAsserts},
651651
{"no_bounds_query", Target::NoBoundsQuery},
652652
{"sse41", Target::SSE41},

src/Target.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct Target {
8484
enum Feature {
8585
JIT = halide_target_feature_jit,
8686
Debug = halide_target_feature_debug,
87-
KeepFramePointer = halide_target_feature_keep_frame_pointer,
87+
EnableBacktraces = halide_target_feature_enable_backtraces,
8888
NoAsserts = halide_target_feature_no_asserts,
8989
NoBoundsQuery = halide_target_feature_no_bounds_query,
9090
SSE41 = halide_target_feature_sse41,

src/runtime/HalideRuntime.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,11 +1355,11 @@ extern int halide_error_vscale_invalid(void *user_context, const char *func_name
13551355
* get_runtime_compatible_target in Target.cpp if you add a new feature.
13561356
*/
13571357
typedef enum halide_target_feature_t {
1358-
halide_target_feature_jit = 0, ///< Generate code that will run immediately inside the calling process.
1359-
halide_target_feature_debug, ///< Turn on debug info and output for runtime code.
1360-
halide_target_feature_keep_frame_pointer, ///< Keep the frame pointer in tact in functions produced by LLVM.
1361-
halide_target_feature_no_asserts, ///< Disable all runtime checks, for slightly tighter code.
1362-
halide_target_feature_no_bounds_query, ///< Disable the bounds querying functionality.
1358+
halide_target_feature_jit = 0, ///< Generate code that will run immediately inside the calling process.
1359+
halide_target_feature_debug, ///< Turn on debug info and output for runtime code.
1360+
halide_target_feature_enable_backtraces, ///< Keep the frame pointer in tact in functions produced by LLVM.
1361+
halide_target_feature_no_asserts, ///< Disable all runtime checks, for slightly tighter code.
1362+
halide_target_feature_no_bounds_query, ///< Disable the bounds querying functionality.
13631363

13641364
halide_target_feature_sse41, ///< Use SSE 4.1 and earlier instructions. Only relevant on x86.
13651365
halide_target_feature_avx, ///< Use AVX 1 instructions. Only relevant on x86.

0 commit comments

Comments
 (0)