authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-03-02 21:43:39-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-03-02 21:43:39-08:00
log90c1a2c41aafb1e35e075fb7d0bdcf04c00db913
tree1037ffb947dc331e68a1dba73e590771113dcec3
parent33de937fd91c64cd65894369cf7d92665a8e582e
parent282b398f6da967f413e223b452457d37e93a70b3
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #19152 from antlilja/llvm-broken-debug

LLVM: Fail to emit if LLVM encounters broken debug info

4 files changed, 38 insertions(+), 2 deletions(-)

src/codegen/llvm.zig+4-2
...@@ -1245,9 +1245,11 @@ pub const Object = struct {...@@ -1245,9 +1245,11 @@ pub const Object = struct {
1245 );1245 );
1246 defer bitcode_memory_buffer.dispose();1246 defer bitcode_memory_buffer.dispose();
12471247
1248 context.enableBrokenDebugInfoCheck();
1249
1248 var module: *llvm.Module = undefined;1250 var module: *llvm.Module = undefined;
1249 if (context.parseBitcodeInContext2(bitcode_memory_buffer, &module).toBool()) {1251 if (context.parseBitcodeInContext2(bitcode_memory_buffer, &module).toBool() or context.getBrokenDebugInfo()) {
1250 std.debug.print("Failed to parse bitcode\n", .{});1252 log.err("Failed to parse bitcode", .{});
1251 return error.FailedToEmit;1253 return error.FailedToEmit;
1252 }1254 }
1253 break :emit .{ context, module };1255 break :emit .{ context, module };
src/codegen/llvm/bindings.zig+6
...@@ -37,6 +37,12 @@ pub const Context = opaque {...@@ -37,6 +37,12 @@ pub const Context = opaque {
3737
38 pub const setOptBisectLimit = ZigLLVMSetOptBisectLimit;38 pub const setOptBisectLimit = ZigLLVMSetOptBisectLimit;
39 extern fn ZigLLVMSetOptBisectLimit(C: *Context, limit: c_int) void;39 extern fn ZigLLVMSetOptBisectLimit(C: *Context, limit: c_int) void;
40
41 pub const enableBrokenDebugInfoCheck = ZigLLVMEnableBrokenDebugInfoCheck;
42 extern fn ZigLLVMEnableBrokenDebugInfoCheck(C: *Context) void;
43
44 pub const getBrokenDebugInfo = ZigLLVMGetBrokenDebugInfo;
45 extern fn ZigLLVMGetBrokenDebugInfo(C: *Context) bool;
40};46};
4147
42pub const Module = opaque {48pub const Module = opaque {
src/zig_llvm.cpp+25
...@@ -380,6 +380,31 @@ void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit) {...@@ -380,6 +380,31 @@ void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit) {
380 unwrap(context_ref)->setOptPassGate(opt_bisect);380 unwrap(context_ref)->setOptPassGate(opt_bisect);
381}381}
382382
383struct ZigDiagnosticHandler : public DiagnosticHandler {
384 bool BrokenDebugInfo;
385 ZigDiagnosticHandler() : BrokenDebugInfo(false) {}
386 bool handleDiagnostics(const DiagnosticInfo &DI) override {
387 // This dyn_cast should be casting to DiagnosticInfoIgnoringInvalidDebugMetadata
388 // but DiagnosticInfoIgnoringInvalidDebugMetadata is treated as DiagnosticInfoDebugMetadataVersion
389 // because of a bug in LLVM (see https://github.com/ziglang/zig/issues/19161).
390 // After this is fixed add an additional check for DiagnosticInfoIgnoringInvalidDebugMetadata
391 // but don't remove the current one as both indicate that debug info is broken.
392 if (auto *Remark = dyn_cast<DiagnosticInfoDebugMetadataVersion>(&DI)) {
393 BrokenDebugInfo = true;
394 }
395 return false;
396 }
397};
398
399void ZigLLVMEnableBrokenDebugInfoCheck(LLVMContextRef context_ref) {
400 unwrap(context_ref)->setDiagnosticHandler(std::make_unique<ZigDiagnosticHandler>());
401}
402
403bool ZigLLVMGetBrokenDebugInfo(LLVMContextRef context_ref) {
404 return ((const ZigDiagnosticHandler*)
405 unwrap(context_ref)->getDiagHandlerPtr())->BrokenDebugInfo;
406}
407
383void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv) {408void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv) {
384 cl::ParseCommandLineOptions(argc, argv);409 cl::ParseCommandLineOptions(argc, argv);
385}410}
src/zig_llvm.h+3
...@@ -44,6 +44,9 @@ ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, co...@@ -44,6 +44,9 @@ ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, co
4444
45ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);45ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);
4646
47ZIG_EXTERN_C void ZigLLVMEnableBrokenDebugInfoCheck(LLVMContextRef context_ref);
48ZIG_EXTERN_C bool ZigLLVMGetBrokenDebugInfo(LLVMContextRef context_ref);
49
47enum ZigLLVMTailCallKind {50enum ZigLLVMTailCallKind {
48 ZigLLVMTailCallKindNone,51 ZigLLVMTailCallKindNone,
49 ZigLLVMTailCallKindTail,52 ZigLLVMTailCallKindTail,