Skip to content

fix primal return type methoderror#2768

Closed
oscardssmith wants to merge 1 commit intoEnzymeAD:mainfrom
oscardssmith:os/fix-primal_return_type_methoderror
Closed

fix primal return type methoderror#2768
oscardssmith wants to merge 1 commit intoEnzymeAD:mainfrom
oscardssmith:os/fix-primal_return_type_methoderror

Conversation

@oscardssmith
Copy link
Collaborator

mostly fixes #2733 (but hits the following error because I'm metaprogramming badly somehow)

ERROR: UndefVarError: `ft` not defined in `Enzyme.Compiler`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
 [1] primal_return_type(mode::ForwardMode{false, FFIABI, false, false, false}, ft::Type{typeof(identity)}, tt::Type{Tuple{}})
   @ Enzyme.Compiler ~/.julia/dev/Enzyme/src/typeutils/inference.jl:139
 [2] autodiff(mode::CMode, f::FA, args::Vararg{Annotation, Nargs}) where {FA<:Annotation, CMode<:Mode, Nargs}
   @ Enzyme ~/.julia/dev/Enzyme/src/Enzyme.jl:556
 [3] autodiff(mode::CMode, f::F, args::Vararg{Annotation, Nargs}) where {F, CMode<:Mode, Nargs}
   @ Enzyme ~/.julia/dev/Enzyme/src/Enzyme.jl:534
 [4] top-level scope
   @ REPL[3]:1

@codecov
Copy link

codecov bot commented Nov 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 50.87%. Comparing base (986bbf4) to head (2e0e7e2).

❗ There is a different number of reports uploaded between BASE (986bbf4) and HEAD (2e0e7e2). Click for more details.

HEAD has 13 uploads less than BASE
Flag BASE (986bbf4) HEAD (2e0e7e2)
18 5
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #2768       +/-   ##
===========================================
- Coverage   68.91%   50.87%   -18.05%     
===========================================
  Files          58       13       -45     
  Lines       19861     1256    -18605     
===========================================
- Hits        13688      639    -13049     
+ Misses       6173      617     -5556     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vchuravy
Copy link
Member

This seems wrong to me, do see #2663 for the change that likely broke the behavior here.

In particular: https://github.com/EnzymeAD/Enzyme.jl/pull/2663/files#diff-a5ddd0c06b3228d600577533c56e26161c6952c6b5c89b4befb5fc6b66ffce45L104-L117

Comment on lines +112 to +118
if mi === nothing
result = Union{}
code = Any[Expr(:call, Base.throw, :(MethodError(ft, tt))),
Core.Compiler.ReturnNode(result)]
# create an empty CodeInfo to return the result
slotnames = Core.svec(Symbol("#self#"), :ft, :tt)
ci = create_fresh_codeinfo(primal_return_type, source, world, slotnames, code)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No we used stub successfully here before.

@github-actions
Copy link
Contributor

Your PR requires formatting changes to meet the project's style guidelines.
Please consider running Runic (git runic main) to apply these changes.

Click here to view the suggested changes.
diff --git a/src/typeutils/inference.jl b/src/typeutils/inference.jl
index d660fd13..c7259a2f 100644
--- a/src/typeutils/inference.jl
+++ b/src/typeutils/inference.jl
@@ -111,8 +111,10 @@ function primal_return_type_generator(world::UInt, source, self, @nospecialize(m
 
     if mi === nothing
         result = Union{}
-        code = Any[Expr(:call, Base.throw, :(MethodError(ft, tt))),
-        Core.Compiler.ReturnNode(result)]
+        code = Any[
+            Expr(:call, Base.throw, :(MethodError(ft, tt))),
+            Core.Compiler.ReturnNode(result),
+        ]
         # create an empty CodeInfo to return the result
         slotnames = Core.svec(Symbol("#self#"), :ft, :tt)
         ci = create_fresh_codeinfo(primal_return_type, source, world, slotnames, code)

Copy link
Member

@vchuravy vchuravy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should still use GeneratedFunctionStub

@oscardssmith
Copy link
Collaborator Author

The thing that doesn't make sense to me here with the old code is that you were stubbing primal_return_type when you want to just throw a MethodError? Also, why is GeneratedFunctionStub a thing we want to be using here at all?

@vchuravy
Copy link
Member

We are writing a generator for a generated function, so we can't throw a MethodError right here and there. We want to throw a method error from the generated function.

Also, why is GeneratedFunctionStub a thing we want to be using here at all?

Because that's the supported interface?

@vchuravy
Copy link
Member

Correct fix in #2771

@vchuravy vchuravy closed this Nov 11, 2025
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

Successfully merging this pull request may close these issues.

Problem generating thunk for function

2 participants