Skip sanitizer IR passes when LTO is enabled

With ThinLTO, the linker runs sanitizer passes at link time via
-fsanitize= flags, where it has whole-program visibility. Running
them at bitcode emission too double-instruments every module,
producing hundreds of "Redundant instrumentation detected" warnings.
Per-function sanitize/no_sanitize attributes are preserved in the
bitcode and respected by the linker's pass.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Jesse Meyer
2026-02-02 13:34:36 -05:00
parent 43ad4a1d9f
commit 9eba12948a

View File

@@ -2454,14 +2454,21 @@ gb_internal WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) {
// tsan - Linux, Darwin
// ubsan - Linux, Darwin, Windows (NOT SUPPORTED WITH LLVM C-API)
if (build_context.sanitizer_flags & SanitizerFlag_Address) {
array_add(&passes, "asan");
}
if (build_context.sanitizer_flags & SanitizerFlag_Memory) {
array_add(&passes, "msan");
}
if (build_context.sanitizer_flags & SanitizerFlag_Thread) {
array_add(&passes, "tsan");
// With LTO, sanitizer passes run at link time (via -fsanitize= linker flags)
// where the linker has whole-program visibility. Running them here too would
// double-instrument every module, producing "Redundant instrumentation" warnings.
// Per-function sanitize attributes in the bitcode are preserved and respected
// by the linker's sanitizer pass.
if (build_context.lto_kind == LTO_None) {
if (build_context.sanitizer_flags & SanitizerFlag_Address) {
array_add(&passes, "asan");
}
if (build_context.sanitizer_flags & SanitizerFlag_Memory) {
array_add(&passes, "msan");
}
if (build_context.sanitizer_flags & SanitizerFlag_Thread) {
array_add(&passes, "tsan");
}
}
if (passes.count == 0) {