From 42b4a48bc96ce22562230cd1a266f93a013c76dd Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 21 Dec 2020 22:18:19 -0700 Subject: WIP start adding support for TSAN --- src/stage1/codegen.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/stage1/codegen.cpp') diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index c7f429ef49..3f14307d3c 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -472,6 +472,10 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) { ZigLLVMFunctionSetCallingConv(llvm_fn, get_llvm_cc(g, cc)); } + if (g->tsan_enabled) { + addLLVMFnAttr(llvm_fn, "sanitize_thread"); + } + bool want_cold = fn->is_cold; if (want_cold) { ZigLLVMAddFunctionAttrCold(llvm_fn); -- cgit v1.2.3 From 4d8c5dd4be7a84c5f01f8910c44c5a1e23740b77 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 22 Dec 2020 22:00:58 -0700 Subject: stage1: add tsan LLVM passes when appropriate --- src/stage1/codegen.cpp | 4 ++-- src/zig_llvm.cpp | 12 +++++++++++- src/zig_llvm.h | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) (limited to 'src/stage1/codegen.cpp') diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index 3f14307d3c..738c4b7b0a 100644 --- 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 index 9b1ab71e9a..51af5e06d1 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -50,6 +50,7 @@ #include #include #include +#include #include #include @@ -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 index 4e57af7933..9a31092265 100644 --- 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); -- cgit v1.2.3