Fix NVPTX derivative codegen to use __nv_* device math functions instead of host libcalls#2777
Draft
Fix NVPTX derivative codegen to use __nv_* device math functions instead of host libcalls#2777
Conversation
…f plain libcalls Agent-Logs-Url: https://github.com/EnzymeAD/Enzyme/sessions/d74331c1-24b3-4d41-ad79-ac6fad2beec2 Co-authored-by: minansys <149007967+minansys@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix error: no libcall available for flog in CUDA code
Fix NVPTX derivative codegen to use __nv_* device math functions instead of host libcalls
Apr 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On Windows NVPTX (CUDA), differentiating functions like
pow,sin,cos,tanh,sinh,coshfails with errors likeno libcall available for flogbecause Enzyme generates derivative helper calls using host math names (cosh,llvm.log.f64, etc.) that don't exist as CUDA device functions.Root causes
SameTypesFuncpath (tanh→cosh,sinh→cosh,cosh→sinh):SameTypesFunc<"cosh">unconditionally inserts@cosh. When differentiating@__nv_tanhthe derivative must call@__nv_cosh.LLVM intrinsic path (
pow→log,sin→cos,cos→sin): Derivatives generatellvm.log.f64/llvm.cos.f64intrinsics. On NVPTX these must be replaced by__nv_log/__nv_cosviaReplaceFunctionImplementation, but on Windows the__nv_*functions are often absent from the module, so the substitution never fires and the NVPTX backend errors trying to lower the bare intrinsic.Changes
enzyme-tblgen.cpp—SameTypesFunccodegen: Generated code now checks whether the original call target carries theimplements2attribute (set by PreserveNVVM on all__nv_*functions). If present, it first searches the module for the corresponding implementing function (e.g.__nv_coshwhereimplements2="cosh"); if not found and the target triple containsnvptx, it falls back to declaring__nv_<funcname>directly.FunctionUtils.cpp—ReplaceFunctionImplementation: Before the main substitution loop, on NVPTX targets the function now scans for LLVM math intrinsics (llvm.log.f64,llvm.sin.f32, etc.) present in the module and auto-declares their__nv_*counterparts (withimplements/implements2/enzyme_mathattributes) when they are missing. This allows the subsequent loop to correctly replace intrinsic uses with the proper device function.PreserveNVVM.h: ExportsisTargetNVPTXfor use inFunctionUtils.cpp.test/Enzyme/ReverseMode/nvvm_tanh.ll: New lit test verifying that differentiating@__nv_tanhon an NVPTX module produces a call to@__nv_coshrather than@cosh.