Skip to content

Handle llvm context error handler #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wsmoses opened this issue Jun 1, 2025 · 5 comments
Open

Handle llvm context error handler #514

wsmoses opened this issue Jun 1, 2025 · 5 comments

Comments

@wsmoses
Copy link
Contributor

wsmoses commented Jun 1, 2025

parsing an opaque pointer bitcode with non opaque poitner context exit's julia rrather than throwing an error to be caught.

cc @giordano @vchuravy

https://github.com/llvm/llvm-project/blob/ccc416312ed72e92a885425d9cb9c01f9afa58eb/llvm/lib/IR/LLVMContext.cpp#L251

error: Opaque pointers are only supported in -opaque-pointers mode (Producer: 'LLVM15.0.7' Reader: 'LLVM 15.0.7jl')

#0  __GI_exit (status=1) at ./stdlib/exit.c:137
#1  0x00007aea4f36e126 in llvm::LLVMContext::diagnose(llvm::DiagnosticInfo const&) () from /home/wmoses/.julia/juliaup/julia-1.10.9+0.x64.linux.gnu/bin/../lib/julia/libLLVM-15jl.so
#2  0x00007aea4f36e24f in llvm::LLVMContext::emitError(llvm::Twine const&) () from /home/wmoses/.julia/juliaup/julia-1.10.9+0.x64.linux.gnu/bin/../lib/julia/libLLVM-15jl.so
#3  0x00007aea4fddc319 in llvm::errorToErrorCodeAndEmitErrors(llvm::LLVMContext&, llvm::Error) () from /home/wmoses/.julia/juliaup/julia-1.10.9+0.x64.linux.gnu/bin/../lib/julia/libLLVM-15jl.so
#4  0x00007aea4fdd0c2b in LLVMParseBitcodeInContext2 () from /home/wmoses/.julia/juliaup/julia-1.10.9+0.x64.linux.gnu/bin/../lib/julia/libLLVM-15jl.so
#5  0x00007aea0e88f89b in julia_LLVMParseBitcodeInContext2_31821 () at /home/wmoses/.julia/packages/LLVM/2JPxT/lib/15/libLLVM.jl:228
#6  0x00007aea0e8aa87f in jfptr_LLVMParseBitcodeInContext2_31822 () from /home/wmoses/.julia/compiled/v1.10/Enzyme/G1p5n_EYKCz.so
#7  0x00007aea5484706e in _jl_invoke (world=<optimized out>, mfunc=0x7aea4b002220, nargs=3, args=0x7fff765bff68, F=0x7aea2f5b43b0 <jl_system_image_data+1663600>) at /cache/build/builder-amdci5-6/julialang/julia-release-1-dot-10/src/gf.c:2895
#8  ijl_apply_generic (F=<optimized out>, args=0x7fff765bff68, nargs=<optimized out>) at /cache/build/builder-amdci5-6/julialang/julia-release-1-dot-10/src/gf.c:3077
#9  0x00007aea24341dee in julia_parse_17843 () at /home/wmoses/.julia/packages/LLVM/2JPxT/src/core/module.jl:223
#10 0x00007aea2423c03f in macro expansion () at /home/wmoses/.julia/packages/LLVM/2JPxT/src/core/module.jl:236
#11 macro expansion () at /home/wmoses/.julia/packages/LLVM/2JPxT/src/base.jl:97
#12 julia_parse_17288 () at /home/wmoses/.julia/packages/LLVM/2JPxT/src/core/module.jl:235
#13 0x00007aea24313528 in jfptr_parse_17289 () from /home/wmoses/.julia/compiled/v1.10/GPUCompiler/yPwef_EYKCz.so
#14 0x00007aea5484706e in _jl_invoke (world=<optimized out>, mfunc=0x7aea4b786e00, nargs=2, args=0x7fff765c02d0, F=0x7aea3f452920 <jl_system_image_data+274912>) at /cache/build/builder-amdci5-6/julialang/julia-release-1-dot-10/src/gf.c:2895
#15 ijl_apply_generic (F=<optimized out>
@wsmoses
Copy link
Contributor Author

wsmoses commented Jun 1, 2025

#59 0x0000710865ccdc28 in julia_abstract_call_gf_by_type_34114 (interp=<error reading variable: Cannot access memory at address 0x7b26>, f=0x71087d2a7fd0 <jl_system_image_data+102928>, arginfo=..., 
--Type <RET> for more, q to quit, c to continue without paging--q
Quit
(rr) f 1
#1  0x0000710875f98100 in llvm::LLVMContext::diagnose (this=0x56fc0a31e720, DI=...) at /home/wmoses/git/julia10/deps/srccache/llvm-julia-15.0.7-10/llvm/lib/IR/LLVMContext.cpp:263
263	    exit(1);
(rr) l
258	  DiagnosticPrinterRawOStream DP(errs());
259	  errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
260	  DI.print(DP);
261	  errs() << "\n";
262	  if (DI.getSeverity() == DS_Error)
263	    exit(1);
264	}
265	
266	void LLVMContext::emitError(uint64_t LocCookie, const Twine &ErrorStr) {
267	  diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
(rr) p pImpl->DiagHandler
$1 = std::unique_ptr<llvm::DiagnosticHandler> = {get() = 0x56fc0a665ac0}
(rr) b LLVMContext::diagnose
Breakpoint 2 at 0x710875f97f60: file /home/wmoses/git/julia10/deps/srccache/llvm-julia-15.0.7-10/llvm/lib/IR/LLVMContext.cpp, line 243.
(rr) rc
Continuing.

Thread 1 hit Breakpoint 2, llvm::LLVMContext::diagnose (this=0x56fc0a31e720, DI=...) at /home/wmoses/git/julia10/deps/srccache/llvm-julia-15.0.7-10/llvm/lib/IR/LLVMContext.cpp:243
243	void LLVMContext::diagnose(const DiagnosticInfo &DI) {
(rr) n
245	    if (LLVMRemarkStreamer *RS = getLLVMRemarkStreamer())
(rr) 
244	  if (auto *OptDiagBase = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
(rr) 
250	      (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) &&
(rr) 
199	      pointer    _M_ptr() const noexcept { return std::get<0>(_M_t); }
(rr) 
258	  DiagnosticPrinterRawOStream DP(errs());
(rr) 
259	  errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
(rr) 
error: 260	  DI.print(DP);
(rr) 
Opaque pointers are only supported in -opaque-pointers mode (Producer: 'LLVM15.0.7' Reader: 'LLVM 15.0.7jl')261	  errs() << "\n";
(rr) p !pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)
$2 = true
(rr) b handleDiagnostics
Breakpoint 3 at 0x710875f23120: handleDiagnostics. (4 locations)
(rr) rc
Continuing.

Thread 1 hit Breakpoint 3.2, llvm::DiagnosticHandler::handleDiagnostics (DI=..., this=0x56fc0a665ac0) at /home/wmoses/git/julia10/deps/srccache/llvm-julia-15.0.7-10/llvm/include/llvm/IR/DiagnosticHandler.h:43
43	    if (DiagHandlerCallback) {
(rr) n
llvm::LLVMContext::diagnose (this=0x56fc0a31e720, DI=...) at /home/wmoses/git/julia10/deps/srccache/llvm-julia-15.0.7-10/llvm/lib/IR/LLVMContext.cpp:254
254	  if (!isDiagnosticEnabled(DI))
(rr) rc
Continuing.

Thread 1 hit Breakpoint 3.2, llvm::DiagnosticHandler::handleDiagnostics (DI=..., this=0x56fc0a665ac0) at /home/wmoses/git/julia10/deps/srccache/llvm-julia-15.0.7-10/llvm/include/llvm/IR/DiagnosticHandler.h:43
43	    if (DiagHandlerCallback) {
(rr) l
38	  /// Override handleDiagnostics to provide custom implementation.
39	  /// Return true if it handles diagnostics reporting properly otherwise
40	  /// return false to make LLVMContext::diagnose() to print the message
41	  /// with a prefix based on the severity.
42	  virtual bool handleDiagnostics(const DiagnosticInfo &DI) {
43	    if (DiagHandlerCallback) {
44	      DiagHandlerCallback(DI, DiagnosticContext);
45	      return true;
46	    }
47	    return false;
(rr) p DiagHandlerCallback
$3 = (llvm::DiagnosticHandler::DiagnosticHandlerTy) 0x0

yeah the problem is misisng the diaghandlercallback from https://github.com/JuliaLLVM/LLVM.jl/blob/326f833f770cbfa83968e75f59ec0e3a85b8fa4b/src/core/context.jl#L187C26-L187C43

@wsmoses
Copy link
Contributor Author

wsmoses commented Jun 1, 2025

somehow it isn't registered, doing this fixes it:

(LLVM.context(), LLVM.API.LLVMContextGetDiagnosticHandler(LLVM.context())) = (LLVM.Context(0x0000000000bc8e50, typed ptrs), Ptr{Nothing} @0x0000000000000000)
e2 = LLVM.LLVMException("Opaque pointers are only supported in -opaque-pointers mode (Producer: 'LLVM15.0.7' Reader: 'LLVM 16.0.6jl')")
ERROR: LoadError: 
No reverse pass found for ejlstr$square$./libsquare.dylib
 at context:   %3 = call double @"ejlstr$square$./libsquare.dylib"(double %0) #5, !dbg !14

Stacktrace:
 [1] csquare
   @ ~/git/Enzyme.jl/ba.jl:12


Stacktrace:
 [1] csquare
   @ ~/git/Enzyme.jl/ba.jl:12 [inlined]
 [2] diffejulia_csquare_368wrap
   @ ~/git/Enzyme.jl/ba.jl:0
 [3] macro expansion
   @ ~/git/Enzyme.jl/src/compiler.jl:5491 [inlined]
 [4] enzyme_call
   @ ~/git/Enzyme.jl/src/compiler.jl:5025 [inlined]
 [5] CombinedAdjointThunk
   @ ~/git/Enzyme.jl/src/compiler.jl:4900 [inlined]
 [6] autodiff
   @ ~/git/Enzyme.jl/src/Enzyme.jl:515 [inlined]
 [7] autodiff(mode::ReverseMode{false, false, false, FFIABI, false, false}, f::typeof(csquare), ::Type{Active}, args::Active{Float64})
   @ Enzyme ~/git/Enzyme.jl/src/Enzyme.jl:536
 [8] macro expansion
   @ show.jl:1232 [inlined]
 [9] top-level scope
   @ ~/git/Enzyme.jl/ba.jl:14
in expression starting at /home/wmoses/git/Enzyme.jl/ba.jl:14
(base) wmoses@hydra:~/git/Enzyme.jl$ git push -fgi^C
(base) wmoses@hydra:~/git/Enzyme.jl$ git diff HEAD~!
fatal: ambiguous argument 'HEAD~!': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
(base) wmoses@hydra:~/git/Enzyme.jl$ git diff HEAD~1
diff --git a/src/compiler/validation.jl b/src/compiler/validation.jl
index 6992069c..f5e7a1e0 100644
--- a/src/compiler/validation.jl
+++ b/src/compiler/validation.jl
@@ -519,10 +519,15 @@ function try_import_llvmbc(mod::LLVM.Module, flib::String, fname::String, import
         end
 
         if data !== nothing
+           @show LLVM.context(), LLVM.API.LLVMContextGetDiagnosticHandler(LLVM.context())
+           if LLVM.API.LLVMContextGetDiagnosticHandler(LLVM.context()) == C_NULL
+               LLVM._install_handlers(LLVM.context())
+           end
             try
                 inmod = parse(LLVM.Module, data)
                 found = haskey(functions(inmod), fname)
             catch e2
+               @show e2
             end
         end
     catch e

@maleadt
Copy link
Collaborator

maleadt commented Jun 2, 2025

We already install the handler for every context created by LLVM:

_install_handlers(ctx)

I guess this is with a non-LLVM.jl managed context?

@wsmoses
Copy link
Contributor Author

wsmoses commented Jun 2, 2025

i guess -- but as this is from enzyme the only contexts we get come from gpucompiler

@maleadt
Copy link
Collaborator

maleadt commented Jun 2, 2025

Ah, I guess we also need to apply this to ORC's TSCtx:

function ThreadSafeContext(; opaque_pointers=nothing)
ts_ctx = ThreadSafeContext(API.LLVMOrcCreateNewThreadSafeContext())
if opaque_pointers !== nothing
opaque_pointers!(context(ts_ctx), opaque_pointers)
end
activate(ts_ctx)
ts_ctx
end

Can you try adding a call to _install_handlers(context(ts_ctx)) or so to the ctor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants