motiejus/zig

fork of https://codeberg.org/ziglang/zig
git clone https://git.jakstys.lt/motiejus/zig.git
Log | Tree | Refs | README | LICENSE

commit 4d8c5dd4be7a84c5f01f8910c44c5a1e23740b77 (tree)
parent a4b134746f61a2f07ea5450c0504f51cba8f6be3
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Tue, 22 Dec 2020 22:00:58 -0700

stage1: add tsan LLVM passes when appropriate

Diffstat:
Msrc/stage1/codegen.cpp | 4++--
Msrc/zig_llvm.cpp | 12+++++++++++-
Msrc/zig_llvm.h | 2+-
3 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp @@ -8436,7 +8436,7 @@ static void zig_llvm_emit_output(CodeGen *g) { // pipeline multiple times if this is requested. if (asm_filename != nullptr && bin_filename != nullptr) { if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, &err_msg, g->build_mode == BuildModeDebug, - is_small, g->enable_time_report, nullptr, bin_filename, llvm_ir_filename)) + is_small, g->enable_time_report, g->tsan_enabled, nullptr, bin_filename, llvm_ir_filename)) { fprintf(stderr, "LLVM failed to emit file: %s\n", err_msg); exit(1); @@ -8446,7 +8446,7 @@ static void zig_llvm_emit_output(CodeGen *g) { } if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, &err_msg, g->build_mode == BuildModeDebug, - is_small, g->enable_time_report, asm_filename, bin_filename, llvm_ir_filename)) + is_small, g->enable_time_report, g->tsan_enabled, asm_filename, bin_filename, llvm_ir_filename)) { fprintf(stderr, "LLVM failed to emit file: %s\n", err_msg); exit(1); diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp @@ -50,6 +50,7 @@ #include <llvm/Transforms/IPO.h> #include <llvm/Transforms/IPO/AlwaysInliner.h> #include <llvm/Transforms/IPO/PassManagerBuilder.h> +#include <llvm/Transforms/Instrumentation/ThreadSanitizer.h> #include <llvm/Transforms/Scalar.h> #include <llvm/Transforms/Utils.h> @@ -93,6 +94,10 @@ static void addDiscriminatorsPass(const PassManagerBuilder &Builder, legacy::Pas PM.add(createAddDiscriminatorsPass()); } +static void addThreadSanitizerPass(const PassManagerBuilder &Builder, legacy::PassManagerBase &PM) { + PM.add(createThreadSanitizerLegacyPassPass()); +} + #ifndef NDEBUG static const bool assertions_on = true; #else @@ -179,7 +184,7 @@ unsigned ZigLLVMDataLayoutGetProgramAddressSpace(LLVMTargetDataRef TD) { bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref, char **error_message, bool is_debug, - bool is_small, bool time_report, + bool is_small, bool time_report, bool tsan, const char *asm_filename, const char *bin_filename, const char *llvm_ir_filename) { TimePassesIsEnabled = time_report; @@ -245,6 +250,11 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM PMBuilder->Inliner = createFunctionInliningPass(PMBuilder->OptLevel, PMBuilder->SizeLevel, false); } + if (tsan) { + PMBuilder->addExtension(PassManagerBuilder::EP_OptimizerLast, addThreadSanitizerPass); + PMBuilder->addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, addThreadSanitizerPass); + } + // Set up the per-function pass manager. legacy::FunctionPassManager FPM = legacy::FunctionPassManager(module); auto tliwp = new(std::nothrow) TargetLibraryInfoWrapperPass(tlii); diff --git a/src/zig_llvm.h b/src/zig_llvm.h @@ -48,7 +48,7 @@ ZIG_EXTERN_C char *ZigLLVMGetNativeFeatures(void); ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref, char **error_message, bool is_debug, - bool is_small, bool time_report, + bool is_small, bool time_report, bool tsan, const char *asm_filename, const char *bin_filename, const char *llvm_ir_filename);