Skip to content
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

Error when trying to show an error #792

Open
glwagner opened this issue Feb 22, 2025 · 5 comments
Open

Error when trying to show an error #792

glwagner opened this issue Feb 22, 2025 · 5 comments

Comments

@glwagner
Copy link
Collaborator

glwagner commented Feb 22, 2025

I found this error

SYSTEM (REPL): showing an error caused an error
ERROR: TypeError: in typeassert, expected Symbol, got a value of type GlobalRef

from the MWE below. The MWE contains an error (undefined reference), but the error message cannot be printed.

using Reactant

mutable struct TestClock{I}
    iteration :: I
end

mutable struct TestSimulation{C, I}
    clock :: C
    stop_iteration :: I
    running :: Bool
end

function test_run!(sim)
    @info "Running..."
    while sim.running
        @trace if sim.clock.iteration >= sim.stop_iteration
            @info "Stopping simulation."
            sim.running = false
        else
            # Error:
            clock.iteration += 1 # time step

            # Correct:
            # sim.clock.iteration += 1 # time step
        end
    end
    return nothing
end

clock = TestClock(ConcreteRNumber(0))
simulation = TestSimulation(clock, 3, true)

r_run! = @compile sync=true test_run!(simulation)
r_run!(simulation)
@glwagner
Copy link
Collaborator Author

Hmm, I think the issue is associated with @info

            @info "Stopping simulation."

@wsmoses
Copy link
Member

wsmoses commented Feb 23, 2025

@jumerckx can you take a look at this?

@jumerckx
Copy link
Collaborator

jumerckx commented Feb 23, 2025

With #794 this gives a different error

Stacktrace
ERROR: UndefVarError: `�J�]���Ѝ�` not defined in local scope
Suggestion: check for an assignment to a local variable that shadows a global of the same name.
Stacktrace:
  [1] macro expansion
    @ ~/Reactant.jl/lib/ReactantCore/src/ReactantCore.jl:375 [inlined]
  [2] step!
    @ ~/Reactant.jl/scratch/ocean.jl:16 [inlined]
  [3] step!(none::TestSimulation{TestClock{…}, Reactant.TracedRNumber{…}, Reactant.TracedRNumber{…}})
    @ Reactant ./<missing>:0
  [4] getproperty
    @ ./Base.jl:49 [inlined]
  [5] step!
    @ ~/Reactant.jl/scratch/ocean.jl:15 [inlined]
  [6] call_with_reactant(::typeof(step!), ::TestSimulation{…})
    @ Reactant ~/Reactant.jl/src/utils.jl:0
  [7] make_mlir_fn(f::Function, args::Vector{…}, kwargs::Tuple{}, name::String, concretein::Bool; toscalar::Bool, return_dialect::Symbol, args_in_result::Symbol, construct_function_without_args::Bool, do_transpose::Bool)
    @ Reactant.TracedUtils ~/Reactant.jl/src/TracedUtils.jl:261
  [8] make_mlir_fn
    @ ~/Reactant.jl/src/TracedUtils.jl:153 [inlined]
  [9] while_loop(cond_fn::var"#31#32", body_fn::typeof(step!), args::TestSimulation{…})
    @ Reactant.Ops ~/Reactant.jl/src/Ops.jl:1700
 [10] traced_while
    @ ~/Reactant.jl/src/ControlFlow.jl:12 [inlined]
 [11] traced_while(none::var"#31#32", none::typeof(step!), none::Tuple{TestSimulation{…}})
    @ Reactant ./<missing>:0
 [12] traced_while
    @ ~/Reactant.jl/src/ControlFlow.jl:12 [inlined]
 [13] call_with_reactant(::Reactant.MustThrowError, ::typeof(ReactantCore.traced_while), ::var"#31#32", ::typeof(step!), ::Tuple{…})
    @ Reactant ~/Reactant.jl/src/utils.jl:0
 [14] test_run!
    @ ~/Reactant.jl/scratch/ocean.jl:26 [inlined]
 [15] test_run!(none::TestSimulation{TestClock{…}, Reactant.TracedRNumber{…}, Reactant.TracedRNumber{…}})
    @ Reactant ./<missing>:0
 [16] test_run!
    @ ~/Reactant.jl/scratch/ocean.jl:26 [inlined]
 [17] call_with_reactant(::typeof(test_run!), ::TestSimulation{…})
    @ Reactant ~/Reactant.jl/src/utils.jl:0
 [18] make_mlir_fn(f::Function, args::Tuple{…}, kwargs::Tuple{}, name::String, concretein::Bool; toscalar::Bool, return_dialect::Symbol, args_in_result::Symbol, construct_function_without_args::Bool, do_transpose::Bool)
    @ Reactant.TracedUtils ~/Reactant.jl/src/TracedUtils.jl:261
 [19] make_mlir_fn
    @ ~/Reactant.jl/src/TracedUtils.jl:153 [inlined]
 [20] compile_mlir!(mod::Reactant.MLIR.IR.Module, f::Function, args::Tuple{…}, callcache::Dict{…}; optimize::Bool, no_nan::Bool, backend::String)
    @ Reactant.Compiler ~/Reactant.jl/src/Compiler.jl:600
 [21] compile_mlir!
    @ ~/Reactant.jl/src/Compiler.jl:575 [inlined]
 [22] (::Reactant.Compiler.var"#7#8"{@Kwargs{no_nan::Bool, optimize::Bool}, typeof(test_run!), Tuple{TestSimulation{…}}})()
    @ Reactant.Compiler ~/Reactant.jl/src/Compiler.jl:494
 [23] context!(f::Reactant.Compiler.var"#7#8"{@Kwargs{…}, typeof(test_run!), Tuple{…}}, ctx::Reactant.MLIR.IR.Context)
    @ Reactant.MLIR.IR ~/Reactant.jl/src/mlir/IR/Context.jl:76
 [24] compile_mlir(f::Function, args::Tuple{…}; client::Nothing, kwargs::@Kwargs{…})
    @ Reactant.Compiler ~/Reactant.jl/src/Compiler.jl:491
 [25] top-level scope
    @ ~/Reactant.jl/src/Compiler.jl:1013
Some type information was truncated. Use `show(err)` to see complete types.

This is not too surprising, @info et. al. expand into a lot of code that is difficult to trace through, especially within traced_while/if. I haven't dug deep into what exactly is going wrong, though.
Even still, printing would only occur at compile time, as the code is being traced, so it's purely for debugging.
For debug printing in functions that are being traced, I would just resort to println (or even Core.println as a last resort), those should work.

@glwagner
Copy link
Collaborator Author

glwagner commented Feb 24, 2025

You could print an error that says something like "@info was found on line X, but @info is not allowed in traced functions"

@avik-pal
Copy link
Collaborator

You could print an error that says something like "@info was found on line X, but @info is not allowed in traced functions"

We won't be able to detect @info/@warn, etc. Those get expanded into Base.CoreLogging, but there isn't a nice way to tell if it originated from those macros.

In this case, we might be able to call macroexpand inside our trace macro to fix it.

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

4 participants